From 7484d56da1d453b3326ad2902c5d74d0bbfd09a6 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 5 Dec 2023 00:31:50 -0800 Subject: [PATCH] Add quiet flag --- bot/client.go | 59 ++++++++++++++++++++++++++++----------------------- go.mod | 2 +- go.sum | 4 ++-- main.go | 4 +++- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/bot/client.go b/bot/client.go index d6a426b..87372d3 100644 --- a/bot/client.go +++ b/bot/client.go @@ -36,10 +36,11 @@ type Client struct { sentGreeting bool rolled bool lastActivity time.Time + quiet bool thinkTime time.Duration } -func NewClient(address string, username string, password string, points int, thinkTime time.Duration) *Client { +func NewClient(address string, username string, password string, points int, quiet bool, thinkTime time.Duration) *Client { const bufferSize = 10 c := &Client{ Address: address, @@ -48,14 +49,15 @@ func NewClient(address string, username string, password string, points int, thi Events: make(chan interface{}, bufferSize), Out: make(chan []byte, bufferSize), points: points, + quiet: quiet, thinkTime: thinkTime, } go c.handleTimeout() return c } -func NewLocalClient(conn net.Conn, address string, username string, password string, points int, thinkTime time.Duration) { - c := NewClient(address, username, password, points, thinkTime) +func NewLocalClient(conn net.Conn, address string, username string, password string, points int, quiet bool, thinkTime time.Duration) { + c := NewClient(address, username, password, points, quiet, thinkTime) c.connecting = true go c.connectTCP(conn) c.HandleEvents() @@ -405,13 +407,6 @@ func (c *Client) HandleEvents() { continue } - var t time.Time - if c.thinkTime != 0 { - t = time.Now() - } - log.Println("==========") - log.Println("Legal moves:", Game.Available) - b := Game.Board var roll1, roll2, roll3, roll4 int8 roll1, roll2 = int8(Game.Roll1), int8(Game.Roll2) @@ -420,31 +415,42 @@ func (c *Client) HandleEvents() { } boardState := tabula.Board{int8(b[0]), int8(b[1]), int8(b[2]), int8(b[3]), int8(b[4]), int8(b[5]), int8(b[6]), int8(b[7]), int8(b[8]), int8(b[9]), int8(b[10]), int8(b[11]), int8(b[12]), int8(b[13]), int8(b[14]), int8(b[15]), int8(b[16]), int8(b[17]), int8(b[18]), int8(b[19]), int8(b[20]), int8(b[21]), int8(b[22]), int8(b[23]), int8(b[24]), int8(b[25]), int8(b[26]), int8(b[27]), roll1, roll2, roll3, roll4} + var t time.Time + if c.thinkTime != 0 { + t = time.Now() + } + result := boardState.Analyze(1, Game.Available) if len(result) == 0 { log.Printf("Legal moves: %+v", Game.Available) log.Fatalf("NO PLAYABLE MOVES RETURNED: %+v", result) } - log.Println("Past:", result[0].Past) - builder := &strings.Builder{} - builder.Write([]byte("Board: ")) - for i := range boardState { - if i != 0 { - builder.WriteByte(',') + if !c.quiet { + log.Println("==========") + log.Println("Legal moves:", Game.Available) + + log.Println("Past:", result[0].Past) + + builder := &strings.Builder{} + builder.Write([]byte("Board: ")) + for i := range boardState { + if i != 0 { + builder.WriteByte(',') + } + builder.Write([]byte(strconv.Itoa(int(boardState[i])))) } - builder.Write([]byte(strconv.Itoa(int(boardState[i])))) - } - log.Println(builder.String()) + log.Println(builder.String()) - const padding = 3 - w := tabwriter.NewWriter(os.Stderr, 0, 0, padding, ' ', 0) - fmt.Fprintln(w, "Moves\tScore\tPlayer Score\tPips\tBlots\tHits\tOpponent Score\tPips\tBlots\tHits\t") - for _, r := range result { - fmt.Fprintf(w, "%s\t%.2f\t%.2f\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t\n", fmt.Sprint(r.Moves), r.Score, r.PlayerScore, r.Pips, r.Blots, r.Hits, r.OppScore, r.OppPips, r.OppBlots, r.OppHits) + const padding = 3 + w := tabwriter.NewWriter(os.Stderr, 0, 0, padding, ' ', 0) + fmt.Fprintln(w, "Moves\tScore\tPlayer Score\tPips\tBlots\tHits\tOpponent Score\tPips\tBlots\tHits\t") + for _, r := range result { + fmt.Fprintf(w, "%s\t%.2f\t%.2f\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t\n", fmt.Sprint(r.Moves), r.Score, r.PlayerScore, r.Pips, r.Blots, r.Hits, r.OppScore, r.OppPips, r.OppBlots, r.OppHits) + } + w.Flush() + log.Println("==========") } - w.Flush() - log.Println("==========") for _, move := range result[0].Moves { if move[0] == 0 || move[0] == 25 { @@ -453,6 +459,7 @@ func (c *Client) HandleEvents() { c.Out <- []byte(fmt.Sprintf("mv %d/%d", move[0], move[1])) } } + if c.thinkTime != 0 { s := time.Since(t) if s < c.thinkTime { diff --git a/go.mod b/go.mod index 099e227..8ff264c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.17 require ( code.rocket9labs.com/tslocum/bgammon v0.0.0-20231203064241-5abce61fabb1 - code.rocket9labs.com/tslocum/tabula v0.0.0-20231204061923-851c1f6fc691 + code.rocket9labs.com/tslocum/tabula v0.0.0-20231205082741-1703a504b560 nhooyr.io/websocket v1.8.10 ) diff --git a/go.sum b/go.sum index 224261e..30d44f3 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ code.rocket9labs.com/tslocum/bgammon v0.0.0-20231203064241-5abce61fabb1 h1:DU4h2XiS2bz4FdaeHRfYTBNwhWuaYGmnb/NA0VgsVMc= code.rocket9labs.com/tslocum/bgammon v0.0.0-20231203064241-5abce61fabb1/go.mod h1:u3nbSwxWnwOXbCNPQD4s3abGTfvA4/gi9U626f7ZN9Q= -code.rocket9labs.com/tslocum/tabula v0.0.0-20231204061923-851c1f6fc691 h1:+jMc7BBO73CiPUG3DwgOzziyy+SdzEyiVdZSSQT2I1E= -code.rocket9labs.com/tslocum/tabula v0.0.0-20231204061923-851c1f6fc691/go.mod h1:XtS6M8qcK0fnUn4k5I7h4JcZ2sz2WEAXOcF2DLaQBME= +code.rocket9labs.com/tslocum/tabula v0.0.0-20231205082741-1703a504b560 h1:P8pumjibx2We8C1+6gUQQEDxMozK4RJW+IwmI+vkU0o= +code.rocket9labs.com/tslocum/tabula v0.0.0-20231205082741-1703a504b560/go.mod h1:XtS6M8qcK0fnUn4k5I7h4JcZ2sz2WEAXOcF2DLaQBME= nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/main.go b/main.go index 8df85a3..d5db9cd 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { username string password string points int + quiet bool thinkTime time.Duration debug int ) @@ -28,6 +29,7 @@ func main() { flag.Float64Var(&tabula.WeightBlot, "weight-blot", tabula.WeightBlot, "Weight (multiplier) when scoring blots") flag.Float64Var(&tabula.WeightHit, "weight-hit", tabula.WeightHit, "Weight (multiplier) when scoring hits") flag.Float64Var(&tabula.WeightOppScore, "weight-score", tabula.WeightOppScore, "Weight (multiplier) when adding opponent score to overall score") + flag.BoolVar(&quiet, "quiet", false, "Disable logging") flag.DurationVar(&thinkTime, "think", 0, "Minimum think time") flag.IntVar(&debug, "debug", 0, "Port to serve debug information on (pprof)") flag.Parse() @@ -38,7 +40,7 @@ func main() { }() } - c := bot.NewClient(serverAddress, username, password, points, thinkTime) + c := bot.NewClient(serverAddress, username, password, points, quiet, thinkTime) go c.Connect() c.HandleEvents()