Print tabula bot statistics

This commit is contained in:
Trevor Slocum 2023-12-02 22:42:31 -08:00
parent 92fc564d00
commit 5abce61fab
6 changed files with 30 additions and 15 deletions

2
go.mod
View file

@ -11,7 +11,7 @@ require (
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect

4
go.sum
View file

@ -8,8 +8,8 @@ github.com/gobwas/ws v1.3.1 h1:Qi34dfLMWJbiKaNbDVzM9x27nZBjmkaW6i4+Ku+pGVU=
github.com/gobwas/ws v1.3.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw=
github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=

View file

@ -147,7 +147,7 @@ func serverStats(tz *time.Location) (*serverStatsResult, error) {
return result, nil
}
func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
func botStats(name string, tz *time.Location) (*botStatsResult, error) {
tx, err := begin()
if err != nil {
return nil, err
@ -155,7 +155,7 @@ func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
defer tx.Commit(context.Background())
var earliestGame int64
rows, err := tx.Query(context.Background(), "SELECT started FROM game WHERE player1 = 'BOT_wildbg' OR player2 = 'BOT_wildbg' ORDER BY started ASC LIMIT 1")
rows, err := tx.Query(context.Background(), "SELECT started FROM game WHERE player1 = $1 OR player2 = $2 ORDER BY started ASC LIMIT 1", name, name)
if err != nil {
return nil, err
}
@ -169,12 +169,12 @@ func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
return nil, err
}
result := &wildBGStatsResult{}
result := &botStatsResult{}
earliest := midnight(time.Unix(earliestGame, 0).In(tz))
rangeStart, rangeEnd := earliest.Unix(), earliest.AddDate(0, 0, 1).Unix()
var winCount, lossCount int
for {
rows, err := tx.Query(context.Background(), "SELECT COUNT(*) FROM game WHERE started >= $1 AND started < $2 AND (player1 = 'BOT_wildbg' OR player2 = 'BOT_wildbg')", rangeStart, rangeEnd)
rows, err := tx.Query(context.Background(), "SELECT COUNT(*) FROM game WHERE started >= $1 AND started < $2 AND (player1 = $3 OR player2 = $4)", rangeStart, rangeEnd, name, name)
if err != nil {
return nil, err
}
@ -188,7 +188,7 @@ func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
return nil, err
}
rows, err = tx.Query(context.Background(), "SELECT COUNT(*) FROM game WHERE started >= $1 AND started < $2 AND ((player1 = 'BOT_wildbg' AND winner = 1) OR (player2 = 'BOT_wildbg' AND winner = 2))", rangeStart, rangeEnd)
rows, err = tx.Query(context.Background(), "SELECT COUNT(*) FROM game WHERE started >= $1 AND started < $2 AND ((player1 = $3 AND winner = 1) OR (player2 = $4 AND winner = 2))", rangeStart, rangeEnd, name, name)
if err != nil {
return nil, err
}
@ -204,7 +204,7 @@ func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
lossCount -= winCount
if winCount != 0 || lossCount != 0 {
result.History = append(result.History, &wildBGStatsEntry{
result.History = append(result.History, &botStatsEntry{
Date: earliest.Format("2006-01-02"),
Percent: (float64(winCount) / float64(winCount+lossCount)),
Wins: winCount,

View file

@ -9,13 +9,13 @@ type serverStatsResult struct {
History []*serverStatsEntry
}
type wildBGStatsEntry struct {
type botStatsEntry struct {
Date string
Percent float64
Wins int
Losses int
}
type wildBGStatsResult struct {
History []*wildBGStatsEntry
type botStatsResult struct {
History []*botStatsEntry
}

View file

@ -27,6 +27,6 @@ func serverStats(tz *time.Location) (*serverStatsResult, error) {
return &serverStatsResult{}, nil
}
func wildBGStats(tz *time.Location) (*wildBGStatsResult, error) {
return &wildBGStatsResult{}, nil
func botStats(name string, tz *time.Location) (*botStatsResult, error) {
return &botStatsResult{}, nil
}

View file

@ -150,10 +150,24 @@ func (s *server) handlePrintStats(w http.ResponseWriter, r *http.Request) {
w.Write(buf)
}
func (s *server) handlePrintTabulaStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
stats, err := botStats("BOT_tabula", s.tz)
if err != nil {
log.Fatalf("failed to fetch tabula statistics: %s", err)
}
buf, err := json.Marshal(stats)
if err != nil {
log.Fatalf("failed to fetch serialize tabula statistics: %s", err)
}
w.Write(buf)
}
func (s *server) handlePrintWildBGStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
stats, err := wildBGStats(s.tz)
stats, err := botStats("BOT_wildbg", s.tz)
if err != nil {
log.Fatalf("failed to fetch wildbg statistics: %s", err)
}
@ -193,6 +207,7 @@ func (s *server) listenWebSocket(address string) {
mux := http.NewServeMux()
mux.HandleFunc("/matches", s.handleListMatches)
mux.HandleFunc("/stats", s.handlePrintStats)
mux.HandleFunc("/stats-tabula", s.handlePrintTabulaStats)
mux.HandleFunc("/stats-wildbg", s.handlePrintWildBGStats)
mux.HandleFunc("/", s.handleWebSocket)