bgammon-cli/main.go

94 lines
1.7 KiB
Go
Raw Normal View History

2021-06-19 06:40:19 +00:00
package main
import (
"flag"
"fmt"
"log"
2021-08-05 22:17:29 +00:00
"net/http"
_ "net/http/pprof"
2021-06-19 06:40:19 +00:00
"time"
2021-06-24 02:23:35 +00:00
"code.rocketnine.space/tslocum/cview"
)
2021-06-19 06:40:19 +00:00
2023-09-09 06:59:05 +00:00
var (
serverAddress string
gameLogged bool
statusLogged bool
debug int
)
2021-06-26 02:53:49 +00:00
2021-06-19 06:40:19 +00:00
func l(s string) {
2021-06-24 02:23:35 +00:00
m := time.Now().Format("15:04") + string(cview.BoxDrawingsLightVertical) + " " + s
2021-07-05 09:01:13 +00:00
if statusWriter != nil {
if statusLogged {
2021-06-26 02:53:49 +00:00
statusWriter.Write([]byte("\n" + m))
return
2021-06-19 06:40:19 +00:00
}
statusWriter.Write([]byte(m))
statusLogged = true
2021-06-19 06:40:19 +00:00
return
}
log.Print(m)
}
func lf(format string, a ...interface{}) {
l(fmt.Sprintf(format, a...))
}
2021-07-05 09:01:13 +00:00
func lg(s string) {
m := time.Now().Format("15:04") + string(cview.BoxDrawingsLightVertical) + " " + s
if gameWriter != nil {
if gameLogged {
2021-07-05 09:01:13 +00:00
gameWriter.Write([]byte("\n" + m))
return
}
gameWriter.Write([]byte(m))
gameLogged = true
2021-07-05 09:01:13 +00:00
return
}
log.Print(m)
}
2023-09-10 21:59:01 +00:00
func handleAutoRefresh() {
t := time.NewTicker(10 * time.Second) // TODO configurable
for range t.C {
if !autoRefresh || gameInProgress {
continue
}
2023-10-28 07:59:42 +00:00
board.client.Out <- []byte("ls")
2023-09-10 21:59:01 +00:00
}
}
2021-06-19 06:40:19 +00:00
func main() {
var (
username string
password string
)
flag.StringVar(&username, "username", "", "username")
flag.StringVar(&password, "password", "", "password")
2023-09-08 02:59:08 +00:00
flag.StringVar(&serverAddress, "server", "bgammon.org:1337", "server address")
2021-08-05 22:17:29 +00:00
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
2021-06-19 06:40:19 +00:00
flag.Parse()
2021-08-05 22:17:29 +00:00
if debug > 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
2023-08-27 05:45:28 +00:00
app = cview.NewApplication()
2021-06-26 02:53:49 +00:00
2023-08-27 05:45:28 +00:00
c := NewClient(serverAddress, username, password)
2021-06-24 02:23:35 +00:00
board = NewGameBoard(c)
2021-06-19 06:40:19 +00:00
2023-09-10 21:59:01 +00:00
go handleAutoRefresh()
2021-09-08 02:56:31 +00:00
err := RunApp(c, board)
2021-06-19 06:40:19 +00:00
if err != nil {
log.Fatalf("%+v", err)
2021-06-19 06:40:19 +00:00
}
}