Fix logging rolls

This commit is contained in:
Trevor Slocum 2024-02-23 09:43:08 -08:00
parent e365763b27
commit 845da8db8e
3 changed files with 26 additions and 29 deletions

View file

@ -22,10 +22,6 @@ import (
var Debug int
var Game = &bgammon.GameState{
Game: bgammon.NewGame(bgammon.VariantBackgammon),
}
var nonAlpha = regexp.MustCompile(`[^a-zA-Z]+`)
type Client struct {
@ -303,9 +299,9 @@ func (c *Client) handleTimeout() {
continue
}
if len(Game.Player2.Name) == 0 {
if len(c.game.Player2.Name) == 0 {
timeout := 2 * time.Minute
if Game.Winner != 0 {
if c.game.Winner != 0 {
timeout = 20 * time.Second
}
if time.Since(c.lastActivity) >= timeout {
@ -315,7 +311,7 @@ func (c *Client) handleTimeout() {
}
limit := 5 * time.Minute
if Game.Player1.Rating != 0 && Game.Player2.Rating != 0 {
if c.game.Player1.Rating != 0 && c.game.Player2.Rating != 0 {
limit = 12 * time.Minute
}
@ -376,10 +372,10 @@ func (c *Client) HandleEvents() {
case *bgammon.EventList:
case *bgammon.EventJoined:
if ev.PlayerNumber == 1 {
Game.Player1.Name = ev.Player
c.game.Player1.Name = ev.Player
} else if ev.PlayerNumber == 2 {
c.lastActivity = time.Now()
Game.Player2.Name = ev.Player
c.game.Player2.Name = ev.Player
}
if ev.Player != c.Username {
log.Printf("%s joined the match.", ev.Player)
@ -395,10 +391,10 @@ func (c *Client) HandleEvents() {
c.createMatch()
case *bgammon.EventLeft:
if Game.Player1.Name == ev.Player {
Game.Player1.Name = ""
} else if Game.Player2.Name == ev.Player {
Game.Player2.Name = ""
if c.game.Player1.Name == ev.Player {
c.game.Player1.Name = ""
} else if c.game.Player2.Name == ev.Player {
c.game.Player2.Name = ""
}
if ev.Player == c.Username {
c.createMatch()
@ -521,22 +517,23 @@ func (c *Client) HandleEvents() {
log.Fatalf("unexpected engine reply: %+v", ev)
}
case *bgammon.EventRolled:
Game.Roll1 = ev.Roll1
Game.Roll2 = ev.Roll2
c.game.Roll1 = ev.Roll1
c.game.Roll2 = ev.Roll2
c.game.Roll3 = ev.Roll3
var diceFormatted string
if Game.Turn == 0 {
if ev.Player == Game.Player1.Name {
diceFormatted = fmt.Sprintf("%d", Game.Roll1)
if c.game.Turn == 0 {
if ev.Player == c.game.Player1.Name {
diceFormatted = fmt.Sprintf("%d", c.game.Roll1)
} else {
diceFormatted = fmt.Sprintf("%d", Game.Roll2)
diceFormatted = fmt.Sprintf("%d", c.game.Roll2)
}
} else {
diceFormatted = fmt.Sprintf("%d-%d", Game.Roll1, Game.Roll2)
if Game.Roll3 != 0 {
diceFormatted += fmt.Sprintf("-%d", Game.Roll3)
diceFormatted = fmt.Sprintf("%d-%d", c.game.Roll1, c.game.Roll2)
if c.game.Roll3 != 0 {
diceFormatted += fmt.Sprintf("-%d", c.game.Roll3)
}
}
if ev.Player == Game.Player2.Name {
if ev.Player == c.game.Player2.Name {
c.lastActivity = time.Now()
}
c.rolled = true
@ -544,7 +541,7 @@ func (c *Client) HandleEvents() {
case *bgammon.EventFailedRoll:
log.Printf("*** Failed to roll: %s", ev.Reason)
case *bgammon.EventMoved:
if ev.Player == Game.Player2.Name {
if ev.Player == c.game.Player2.Name {
c.lastActivity = time.Now()
}
log.Printf("%s moved %s.", ev.Player, bgammon.FormatMoves(ev.Moves))
@ -554,12 +551,12 @@ func (c *Client) HandleEvents() {
extra = fmt.Sprintf(" from %s to %s", bgammon.FormatSpace(ev.From), bgammon.FormatSpace(ev.To))
}
log.Printf("*** Failed to move checker%s: %s", extra, ev.Reason)
log.Printf("*** Legal moves: %s", bgammon.FormatMoves(Game.Available))
log.Printf("*** Legal moves: %s", bgammon.FormatMoves(c.game.Available))
case *bgammon.EventFailedOk:
log.Printf("*** Failed to submit moves: %s", ev.Reason)
case *bgammon.EventWin:
log.Printf("%s wins!", ev.Player)
if ev.Player != Game.Player1.Name {
if ev.Player != c.game.Player1.Name {
c.Out <- []byte(fmt.Sprintf("say %s", phrases[randInt(len(phrases))]))
}
c.Out <- []byte("say Good game. Play again?")

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.17
require (
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240220213331-09878764aaa3
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb
nhooyr.io/websocket v1.8.10
)

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-20240220213331-09878764aaa3 h1:vFF5e61UnjJ0jqdeAh9iOduEQNxya1JG4PBnkLlxatc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240220213331-09878764aaa3/go.mod h1:cmhRMMrLrsMlG3rhcGbfsPdk0VKSEZMfkFtpRMxm/h8=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb h1:s6SHuRzqZzqoB9wK0+Ai9wY5X59zkifJIviaWwmhlO4=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb/go.mod h1:NGFAKlapNeIriQfrrayRBAv2jOuxHhhJH3zokKBJ/Gg=
code.rocket9labs.com/tslocum/tabula v0.0.0-20240207195947-691c7a5d5265 h1:B9R/SFVUAOut2o8rD7bc1fsa92eQdDKzxBFj7oxhDEc=
code.rocket9labs.com/tslocum/tabula v0.0.0-20240207195947-691c7a5d5265/go.mod h1:WEJXESKXqrMFLAArikQ79lpRibNeeE1C0VruxXYMF5M=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=