Terminal-based online backgammon client (FIBS)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.8 KiB

4 months ago
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
4 months ago
"time"
"code.rocketnine.space/tslocum/fibs"
"code.rocketnine.space/tslocum/cview"
)
4 months ago
var gameLogged bool
var statusLogged bool
var notify bool
var debug int
4 months ago
var serverAddress string
4 months ago
func l(s string) {
m := time.Now().Format("15:04") + string(cview.BoxDrawingsLightVertical) + " " + s
if statusWriter != nil {
if statusLogged {
statusWriter.Write([]byte("\n" + m))
return
4 months ago
}
statusWriter.Write([]byte(m))
statusLogged = true
4 months ago
return
}
log.Print(m)
}
func lf(format string, a ...interface{}) {
l(fmt.Sprintf(format, a...))
}
func lg(s string) {
m := time.Now().Format("15:04") + string(cview.BoxDrawingsLightVertical) + " " + s
if gameWriter != nil {
if gameLogged {
gameWriter.Write([]byte("\n" + m))
return
}
gameWriter.Write([]byte(m))
gameLogged = true
return
}
log.Print(m)
}
4 months ago
func main() {
var (
username string
password string
watchRandom bool
4 months ago
)
flag.StringVar(&username, "username", "", "username")
flag.StringVar(&password, "password", "", "password")
flag.StringVar(&serverAddress, "server", "fibs.com:4321", "server address")
flag.BoolVar(&notify, "notify", true, "show notification when another player is using bgammon")
flag.BoolVar(&watchRandom, "watch", false, "watch random game")
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
4 months ago
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
if debug > 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
// TODO
/*
r := func(b []byte) []byte {
b = bytes.Replace(b, []byte("-"), []byte(string(cview.BoxDrawingsLightHorizontal)), -1)
return b
}
ct := func(b []byte) []byte {
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightDownAndRight)), 1)
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightDownAndHorizontal)), 2)
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightDownAndLeft)), 1)
return b
}
cb := func(b []byte) []byte {
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightUpAndRight)), 1)
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightUpAndHorizontal)), 2)
b = bytes.Replace(b, []byte("+"), []byte(string(cview.BoxDrawingsLightUpAndLeft)), 1)
return b
}
boardTopBlack = ct(r(boardTopBlack))
boardTopWhite = ct(r(boardTopWhite))
4 months ago
boardBottomBlack = cb(r(boardBottomBlack))
boardBottomWhite = cb(r(boardBottomWhite))
*/
c := fibs.NewClient(serverAddress, username, password)
board = NewGameBoard(c)
4 months ago
if watchRandom {
go func() {
time.Sleep(1 * time.Second)
c.WatchRandomGame()
}()
}
err := RunApp(c, board)
4 months ago
if err != nil {
log.Fatalf("%+v", err)
4 months ago
}
}