bgammon-benchmark/main.go
2024-08-17 09:53:02 -07:00

142 lines
3.3 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"
"code.rocket9labs.com/tslocum/bgammon"
"code.rocket9labs.com/tslocum/bgammon/pkg/server"
)
var (
engineA string
engineB string
wildbgAddress string
matchDelay int
thinkTime int
)
func joinMatches(conns chan net.Conn) {
i := 1
for {
conn := <-conns
botName := "BOT_random_%d"
if wildbgAddress != "" {
botName = "BOT_wildbg_%d"
}
c := NewLocalClient(conn, "", fmt.Sprintf(botName, i), "", 1, nil)
JOINGAME:
for {
e := <-c.Events
switch ev := e.(type) {
case *bgammon.EventList:
for _, g := range ev.Games {
if g.Players == 1 {
c.Out <- []byte(fmt.Sprintf("j %d", g.ID))
break JOINGAME
}
}
time.Sleep(2 * time.Second)
c.Out <- []byte("ls")
}
}
go c.HandleEvents()
i++
}
}
var quietOutput bool
func main() {
var (
serverAddress string
count int
points int
outDir string
variant int
verbose bool
debug int
)
flag.StringVar(&serverAddress, "tcp", "localhost:1337", "Server listen address (TCP)")
flag.StringVar(&engineA, "a", "", "Engine address (BEI protocol)")
flag.StringVar(&engineB, "b", "", "Engine address (BEI protocol)")
flag.IntVar(&count, "count", 0, "Play specified number of matches")
flag.IntVar(&points, "points", 1, "Match points")
flag.StringVar(&outDir, "match-dir", "", "Write .match files to specified directory")
flag.StringVar(&wildbgAddress, "wildbg", "", "wildbg service address (REST)")
flag.IntVar(&matchDelay, "delay", 0, "Time (in seconds) to wait before starting the match")
flag.IntVar(&thinkTime, "think", 0, "Time (in milliseconds) to wait between moves")
flag.IntVar(&variant, "variant", 0, "Variant (0: Backgammon, 1: Acey-deucey, 2: Tabula)")
flag.BoolVar(&quietOutput, "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 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
if points < 1 {
points = 1
}
if outDir != "" {
_ = os.MkdirAll(outDir, 0700)
}
// Remove trailing slashes.
engineA = strings.TrimSuffix(engineA, "/")
engineB = strings.TrimSuffix(engineB, "/")
server := server.NewServer("", "", "", "", "", true, verbose, false)
server.Listen("tcp", serverAddress)
conns := server.ListenLocal()
if engineA != "" && engineB != "" {
{
beiClient := NewBEIClient(engineA, count, outDir, true, quietOutput)
err := beiClient.Connect()
if err != nil {
log.Fatalf("failed to connect to engine at %s: %s", engineA, err)
}
botName := "A"
conn := <-conns
c := NewLocalClient(conn, "", botName, "", 1, beiClient)
c.Out <- []byte(fmt.Sprintf("c public %d %d", points, variant))
go c.HandleEvents()
}
time.Sleep(500 * time.Millisecond)
{
beiClient := NewBEIClient(engineB, 0, "", false, quietOutput)
err := beiClient.Connect()
if err != nil {
log.Fatalf("failed to connect to engine at %s: %s", engineB, err)
}
botName := "B"
conn := <-conns
c := NewLocalClient(conn, "", botName, "", 1, beiClient)
c.Out <- []byte("j 1")
go c.HandleEvents()
}
select {}
}
joinMatches(conns)
}