Detect when gnubg offers to resign

This commit is contained in:
Trevor Slocum 2024-11-17 10:23:41 -08:00
parent a9b97a1c25
commit 4be1dd8bc4
4 changed files with 13 additions and 7 deletions

View file

@ -405,9 +405,10 @@ func (c *Client) handleEvents() {
c.Out <- []byte("say failed to communicate with gnubg service")
time.Sleep(100 * time.Millisecond)
log.Fatalf("failed to communicate with gnubg service: %s", err)
} else if len(moves) == 0 {
log.Printf("AVAILABLE: %+v", Game.Available)
log.Fatalf("NO MOVES RETURNED")
} else if moves[0][0] == -1 && moves[0][1] == -1 {
c.Out <- []byte("resign")
c.lastActivity = time.Now()
continue
}
for i := 0; i < 4; i++ {

View file

@ -214,7 +214,7 @@ func analyze(g *bgammon.Game) ([4][2]int8, error) {
buf := scanner.Bytes()
if bytes.Contains(buf, []byte("The dice have been set")) {
setDice = true
} else if setDice && bytes.Contains(buf, []byte("gnubg moves")) {
} else if setDice && (bytes.Contains(buf, []byte("gnubg moves")) || bytes.Contains(buf, []byte("gnubg offers to resign"))) {
moves = buf
}
log.Println(string(buf))
@ -244,6 +244,11 @@ func analyze(g *bgammon.Game) ([4][2]int8, error) {
return [4][2]int8{}, err
}
resign := bytes.Contains(moves, []byte("gnubg offers to resign"))
if resign {
return [4][2]int8{{-1, -1}}, nil
}
movesWord := bytes.Index(moves, []byte("moves"))
if movesWord == -1 {
return [4][2]int8{}, fmt.Errorf("failed to parse gnubg moves: %s", moves)

2
go.mod
View file

@ -5,7 +5,7 @@ go 1.23
toolchain go1.23.2
require (
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117045316-3a28f785731f
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117073713-a318a6a9010e
nhooyr.io/websocket v1.8.17
)

4
go.sum
View file

@ -1,7 +1,7 @@
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b h1:Y0a14Kf/hSYepSmp4ZfDeE4CZZGBGBS97CNjCbKJm0c=
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117045316-3a28f785731f h1:vEOhwKSgoDzub0tpQ87spfS515lIEJarlem/7RThIGw=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117045316-3a28f785731f/go.mod h1:FzdCJoZoG/6nkxLl6OufdfFTXggr5ZArtl7eJ/CCcj0=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117073713-a318a6a9010e h1:PhN0FyXnuCrhzJaa+jS08ckVjf4Y17ECm+C1leBCJcg=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20241117073713-a318a6a9010e/go.mod h1:FzdCJoZoG/6nkxLl6OufdfFTXggr5ZArtl7eJ/CCcj0=
code.rocket9labs.com/tslocum/tabula v0.0.0-20241024013344-d112a9463c51 h1:JkjRKoSSmtHFSVIOAZV6e+Z+7r0mIxClsDpJxC/ZlOs=
code.rocket9labs.com/tslocum/tabula v0.0.0-20241024013344-d112a9463c51/go.mod h1:WEJXESKXqrMFLAArikQ79lpRibNeeE1C0VruxXYMF5M=
nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y=