Allow specifying minimum think time

This commit is contained in:
Trevor Slocum 2023-11-30 23:58:33 -08:00
parent d3f00bbc52
commit 712ea96039
4 changed files with 44 additions and 14 deletions

View file

@ -35,24 +35,26 @@ type Client struct {
sentGreeting bool
rolled bool
lastActivity time.Time
thinkTime time.Duration
}
func NewClient(address string, username string, password string, points int) *Client {
func NewClient(address string, username string, password string, points int, thinkTime time.Duration) *Client {
const bufferSize = 10
c := &Client{
Address: address,
Username: username,
Password: password,
Events: make(chan interface{}, bufferSize),
Out: make(chan []byte, bufferSize),
points: points,
Address: address,
Username: username,
Password: password,
Events: make(chan interface{}, bufferSize),
Out: make(chan []byte, bufferSize),
points: points,
thinkTime: thinkTime,
}
go c.handleTimeout()
return c
}
func NewLocalClient(conn net.Conn, address string, username string, password string, points int) {
c := NewClient(address, username, password, points)
func NewLocalClient(conn net.Conn, address string, username string, password string, points int, thinkTime time.Duration) {
c := NewClient(address, username, password, points, thinkTime)
c.connecting = true
go c.connectTCP(conn)
c.HandleEvents()
@ -399,6 +401,10 @@ func (c *Client) HandleEvents() {
continue
}
var t time.Time
if c.thinkTime != 0 {
t = time.Now()
}
log.Println("==========")
log.Println("Legal moves:", Game.Available)
@ -431,6 +437,14 @@ func (c *Client) HandleEvents() {
} else {
c.Out <- []byte(fmt.Sprintf("mv %d/%d", move[0], move[1]))
}
if c.thinkTime != 0 {
t2 := time.Now()
s := t2.Sub(t)
if s < c.thinkTime {
time.Sleep(c.thinkTime - s)
}
t = time.Now()
}
}
c.Out <- []byte("ok")
c.lastActivity = time.Now()

4
go.mod
View file

@ -1,10 +1,10 @@
module code.rocket9labs.com/tslocum/bgammon-tabula-bot
go 1.21.4
go 1.17
require (
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231201021429-50943c96c43d
nhooyr.io/websocket v1.8.10
)
require code.rocket9labs.com/tslocum/tabula v0.0.0-20231201072036-2559cbf7f7ea
require code.rocket9labs.com/tslocum/tabula v0.0.0-20231202165815-2dfbc8157dc8

4
go.sum
View file

@ -1,6 +1,6 @@
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231201021429-50943c96c43d h1:4FGA2vc8j2gCjioYyzrjbW1hOiNxz06QgqvnjctzKoM=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20231201021429-50943c96c43d/go.mod h1:+dgD9v8BlzZjzpIB+/g0lgog90YUoPzEZsp1iAchl30=
code.rocket9labs.com/tslocum/tabula v0.0.0-20231201072036-2559cbf7f7ea h1:cWNtNyZYPCBseJSVvrYcf7Wp0wXMcReqyAuNteBJ/3A=
code.rocket9labs.com/tslocum/tabula v0.0.0-20231201072036-2559cbf7f7ea/go.mod h1:DZdQq8KZDVw6rdeo7Zerk74rrX/KQHuKoQgiF+jVmYA=
code.rocket9labs.com/tslocum/tabula v0.0.0-20231202165815-2dfbc8157dc8 h1:Y1vO47W7Jof3/GILxVlxzcYS+GQ1vhrHU8lYA/9IcCE=
code.rocket9labs.com/tslocum/tabula v0.0.0-20231202165815-2dfbc8157dc8/go.mod h1:XtS6M8qcK0fnUn4k5I7h4JcZ2sz2WEAXOcF2DLaQBME=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=

18
main.go
View file

@ -2,6 +2,12 @@ package main
import (
"flag"
"fmt"
"log"
"time"
"net/http"
_ "net/http/pprof"
"code.rocket9labs.com/tslocum/bgammon-tabula-bot/bot"
"code.rocket9labs.com/tslocum/tabula"
@ -13,6 +19,8 @@ func main() {
username string
password string
points int
thinkTime time.Duration
debug int
)
flag.StringVar(&serverAddress, "address", "bgammon.org:1337", "Server address")
flag.StringVar(&username, "username", "", "Username")
@ -21,9 +29,17 @@ 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.DurationVar(&thinkTime, "think", 0, "Minimum think time")
flag.IntVar(&debug, "debug", 0, "Port to serve debug information on (pprof)")
flag.Parse()
c := bot.NewClient(serverAddress, username, password, points)
if debug > 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
c := bot.NewClient(serverAddress, username, password, points, thinkTime)
go c.Connect()
c.HandleEvents()