Add bot stats page
This commit is contained in:
parent
caf4b3e2d8
commit
f63f21b5ad
2 changed files with 104 additions and 1 deletions
|
@ -148,6 +148,91 @@ func serverStats(conn *pgx.Conn, tz *time.Location) (*serverStatsResult, error)
|
|||
return result, nil
|
||||
}
|
||||
|
||||
type wildBGStatsEntry struct {
|
||||
Date string
|
||||
Percent float64
|
||||
Wins int
|
||||
Losses int
|
||||
}
|
||||
|
||||
type wildBGStatsResult struct {
|
||||
History []*wildBGStatsEntry
|
||||
}
|
||||
|
||||
func wildBGStats(conn *pgx.Conn, tz *time.Location) (*wildBGStatsResult, error) {
|
||||
tx, err := begin(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = rows.Scan(&earliestGame)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &wildBGStatsResult{}
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = rows.Scan(&lossCount)
|
||||
}
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = rows.Scan(&winCount)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lossCount -= winCount
|
||||
|
||||
if winCount != 0 || lossCount != 0 {
|
||||
result.History = append(result.History, &wildBGStatsEntry{
|
||||
Date: earliest.Format("2006-01-02"),
|
||||
Percent: (float64(winCount) / float64(winCount+lossCount)),
|
||||
Wins: winCount,
|
||||
Losses: lossCount,
|
||||
})
|
||||
}
|
||||
|
||||
earliest = earliest.AddDate(0, 0, 1)
|
||||
rangeStart, rangeEnd = rangeEnd, earliest.AddDate(0, 0, 1).Unix()
|
||||
if rangeStart >= time.Now().Unix() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func midnight(t time.Time) time.Time {
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
|
|
@ -147,7 +147,24 @@ func (s *server) handlePrintStats(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
buf, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to fetch serialize statistics: %s", err)
|
||||
log.Fatalf("failed to fetch serialize server statistics: %s", err)
|
||||
}
|
||||
w.Write(buf)
|
||||
}
|
||||
|
||||
func (s *server) handlePrintWildBGStats(w http.ResponseWriter, r *http.Request) {
|
||||
if s.db == nil {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
stats, err := wildBGStats(s.db, s.tz)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to fetch wildbg statistics: %s", err)
|
||||
}
|
||||
buf, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to fetch serialize wildbg statistics: %s", err)
|
||||
}
|
||||
w.Write(buf)
|
||||
}
|
||||
|
@ -181,6 +198,7 @@ func (s *server) listenWebSocket(address string) {
|
|||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/matches", s.handleListMatches)
|
||||
mux.HandleFunc("/stats", s.handlePrintStats)
|
||||
mux.HandleFunc("/stats-wildbg", s.handlePrintWildBGStats)
|
||||
mux.HandleFunc("/", s.handleWebSocket)
|
||||
|
||||
err := http.ListenAndServe(address, mux)
|
||||
|
|
Loading…
Reference in a new issue