Add quiet option

This commit is contained in:
Trevor Slocum 2023-12-23 00:47:24 -08:00
parent 452c4e83bf
commit 0e2ccb3d06
5 changed files with 38 additions and 43 deletions

7
bei.go
View file

@ -9,6 +9,7 @@ import (
"net/url"
"strconv"
"strings"
"time"
"code.rocket9labs.com/tslocum/bei"
"code.rocket9labs.com/tslocum/bgammon"
@ -22,14 +23,18 @@ type BEIClient struct {
count int
replayDir string
primary bool
quiet bool
started time.Time
}
func NewBEIClient(address string, count int, replayDir string, primary bool) *BEIClient {
func NewBEIClient(address string, count int, replayDir string, primary bool, quiet bool) *BEIClient {
c := &BEIClient{
address: address,
count: count,
replayDir: replayDir,
primary: primary,
quiet: quiet,
started: time.Now(),
}
return c
}

View file

@ -331,21 +331,6 @@ func (c *Client) HandleEvents() {
switch ev := e.(type) {
case *bgammon.EventWelcome:
c.Username = ev.PlayerName
areIs := "are"
if ev.Clients == 1 {
areIs = "is"
}
clientsPlural := "s"
if ev.Clients == 1 {
clientsPlural = ""
}
matchesPlural := "es"
if ev.Games == 1 {
matchesPlural = ""
}
log.Printf("*** Welcome, %s. There %s %d client%s playing %d match%s.", ev.PlayerName, areIs, ev.Clients, clientsPlural, ev.Games, matchesPlural)
if c.beiClient == nil {
c.createMatch()
}
@ -366,9 +351,6 @@ func (c *Client) HandleEvents() {
c.lastActivity = time.Now()
c.game.Player2.Name = ev.Player
}
if ev.Player != c.Username {
log.Printf("%s joined the match.", ev.Player)
}
case *bgammon.EventFailedJoin:
log.Printf("*** Failed to join match: %s", ev.Reason)
case *bgammon.EventFailedLeave:
@ -456,7 +438,10 @@ func (c *Client) HandleEvents() {
case *bei.EventFailChoose:
log.Fatalf("failed to retrieve roll choice from engine: %s", ev.Reason)
case *bei.EventOkChoose:
c.Out <- []byte("ok %d")
if len(ev.Rolls) == 0 {
log.Fatalf("failed to retrieve roll choice from engine: no rolls were returned: %+v", ev)
}
c.Out <- []byte(fmt.Sprintf("ok %d", ev.Rolls[0].Roll))
c.lastActivity = time.Now()
continue
default:
@ -490,6 +475,8 @@ func (c *Client) HandleEvents() {
}
for _, play := range ev.Moves[0].Play {
if play.From == 0 || play.From == 25 {
c.Out <- []byte(fmt.Sprintf("mv off/%d", play.To))
} else if play.From == 26 || play.From == 27 {
c.Out <- []byte(fmt.Sprintf("mv bar/%d", play.To))
} else {
c.Out <- []byte(fmt.Sprintf("mv %d/%d", play.From, play.To))
@ -558,12 +545,9 @@ func (c *Client) HandleEvents() {
c.lastActivity = time.Now()
}
c.rolled = true
if c.beiClient == nil || c.beiClient.primary {
if c.beiClient == nil || (c.beiClient.primary && !c.beiClient.quiet) {
log.Printf("%s rolled %s.", ev.Player, diceFormatted)
}
if c.game.Turn == 0 && c.game.Roll1 == c.game.Roll2 {
c.Out <- []byte("r")
}
case *bgammon.EventFailedRoll:
log.Printf("*** Failed to roll: %s", ev.Reason)
case *bgammon.EventMoved:
@ -582,7 +566,7 @@ func (c *Client) HandleEvents() {
}
c.game.AddLocalMove(m)
}
if c.beiClient == nil || c.beiClient.primary {
if c.beiClient == nil || (c.beiClient.primary && !c.beiClient.quiet) {
log.Printf("%s moved %s.", ev.Player, bgammon.FormatMoves(ev.Moves))
}
case *bgammon.EventFailedMove:
@ -596,24 +580,23 @@ func (c *Client) HandleEvents() {
c.Out <- []byte("board") // Refresh game state.
log.Printf("*** Failed to submit moves: %s", ev.Reason)
case *bgammon.EventWin:
if c.beiClient == nil || c.beiClient.primary {
if c.beiClient == nil || (c.beiClient.primary && !c.beiClient.quiet) {
log.Printf("%s wins!", ev.Player)
}
var pips int
if ev.Player == c.Username {
pips = c.game.Pips(1)
pips = c.game.Pips(2)
c.losses++
c.pipsLosses += pips
} else {
pips = c.game.Pips(2)
pips = c.game.Pips(1)
c.wins++
c.pipsWins += pips
}
total := c.wins + c.losses
pct := 0.0
pct := float64(c.losses) / float64(total) * 100
pw, pl := 0, 0
if c.wins != 0 {
pct = (float64(c.losses) / float64(total)) * 100
pw = c.pipsWins / c.wins
}
if c.losses != 0 {
@ -640,15 +623,20 @@ func (c *Client) HandleEvents() {
c.Out <- []byte("replay")
}
summary := fmt.Sprintf("W %02d / L %02d (%02d, %02.1f%%) Pips %02d / %02d (%03d) Bars %.2f / %.2f (%.0f%%) Bar pips %.2f / %.2f (%.0f%%)", c.losses, c.wins, total, pct, pl, pw, pips, float64(c.barsPlayer)/float64(total), float64(c.barsOpponent)/float64(total), pctBar, barsPlayerPips, barsOpponentPips, pctBarsPips)
summary := fmt.Sprintf("W %02d / L %02d (%02d, %03.1f%%) Pips %02d / %02d (%03d) Bars %.2f / %.2f (%03.1f%%) Bar pips %.2f / %.2f (%03.1f%%)", c.losses, c.wins, total, pct, pl, pw, pips, float64(c.barsPlayer)/float64(total), float64(c.barsOpponent)/float64(total), pctBar, barsPlayerPips, barsOpponentPips, pctBarsPips)
if c.beiClient != nil && c.beiClient.primary {
log.Printf("<%s> %s", c.Username, summary)
}
c.Out <- []byte("say " + summary)
if c.beiClient != nil && c.beiClient.count != 0 && total == c.beiClient.count {
c.Out <- []byte(fmt.Sprintf("say Played %d matches.", c.beiClient.count))
continue
delta := time.Since(c.beiClient.started)
gamesPerSecond := 0.0
if delta.Seconds() != 0 {
gamesPerSecond = float64(c.beiClient.count) / delta.Seconds()
}
log.Printf("<%s> Played %d matches in %s. (%.1f games/sec)", c.Username, c.beiClient.count, delta.Truncate(time.Second), gamesPerSecond)
os.Exit(0)
}
c.Out <- []byte("rematch")
case *bgammon.EventReplay:

4
go.mod
View file

@ -3,8 +3,8 @@ module code.rocket9labs.com/tslocum/bgammon-benchmark
go 1.21.4
require (
code.rocket9labs.com/tslocum/bei v0.0.0-20231222203708-dfd75da2f16a
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231221205256-cb3ead6cd00c
code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231223081447-262d006c714c
nhooyr.io/websocket v1.8.10
)

8
go.sum
View file

@ -1,7 +1,7 @@
code.rocket9labs.com/tslocum/bei v0.0.0-20231222203708-dfd75da2f16a h1:f6894UI+9njN26dLulz4KBPJx06gnilc4aT0kpiK1Is=
code.rocket9labs.com/tslocum/bei v0.0.0-20231222203708-dfd75da2f16a/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231221205256-cb3ead6cd00c h1:aMzzQtRRAGsh+6WVNv4vve/hFqTucE3Yq9zhaXhyibk=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231221205256-cb3ead6cd00c/go.mod h1:pM1hNhG/vKgk4Ktpszv5duQ3gJMrawO6gwotpJjHxyE=
code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773 h1:cu3FKYfk5WQcuxMXrqghHAmriyFiRCZ/8aUqIKI9yLU=
code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231223081447-262d006c714c h1:ri5THzKvxE5WqAH/d/U6gXunHJx2cwYUWrer8YQ0QEk=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231223081447-262d006c714c/go.mod h1:pM1hNhG/vKgk4Ktpszv5duQ3gJMrawO6gwotpJjHxyE=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=

10
main.go
View file

@ -57,8 +57,9 @@ func main() {
points int
outDir string
playAcey bool
debug int
quiet bool
verbose bool
debug int
)
flag.StringVar(&serverAddress, "tcp", "localhost:1337", "Server listen address (TCP)")
flag.StringVar(&engineAddressA, "engine-a", "", "Engine address (BEI protocol)")
@ -69,8 +70,9 @@ func main() {
flag.StringVar(&wildbgAddress, "wildbg", "", "wildbg service address (REST)")
flag.IntVar(&matchDelay, "delay", 0, "Delay (in seconds) to wait before starting match")
flag.BoolVar(&playAcey, "acey", false, "Play acey-deucey")
flag.IntVar(&debug, "debug", 0, "Port to serve debug information on")
flag.BoolVar(&quiet, "quiet", false, "Do not print rolls or moves")
flag.BoolVar(&verbose, "verbose", false, "Print all client messages")
flag.IntVar(&debug, "debug", 0, "Port to serve debug information on")
flag.Parse()
if debug > 0 {
@ -97,7 +99,7 @@ func main() {
if engineAddressA != "" && engineAddressB != "" {
{
beiClient := NewBEIClient(engineAddressA, count, outDir, true)
beiClient := NewBEIClient(engineAddressA, count, outDir, true, quiet)
err := beiClient.Connect()
if err != nil {
log.Fatalf("failed to connect to engine at %s: %s", engineAddressA, err)
@ -117,7 +119,7 @@ func main() {
}
time.Sleep(500 * time.Millisecond)
{
beiClient := NewBEIClient(engineAddressB, 0, "", false)
beiClient := NewBEIClient(engineAddressB, 0, "", false, quiet)
err := beiClient.Connect()
if err != nil {
log.Fatalf("failed to connect to engine at %s: %s", engineAddressB, err)