diff --git a/api/game.go b/api/game.go index 46240e8..f3e1b54 100644 --- a/api/game.go +++ b/api/game.go @@ -8,25 +8,36 @@ import ( "time" "github.com/Flashfyre/pokerogue-server/db" + "github.com/Flashfyre/pokerogue-server/defs" "github.com/go-co-op/gocron" ) var ( - playerCountScheduler = gocron.NewScheduler(time.UTC) - playerCount int + statScheduler = gocron.NewScheduler(time.UTC) + playerCount int + battleCount int + classicSessionCount int ) -func SchedulePlayerCountRefresh() { - playerCountScheduler.Every(10).Second().Do(updatePlayerCount) - playerCountScheduler.StartAsync() +func ScheduleStatRefresh() { + statScheduler.Every(10).Second().Do(updateStats) + statScheduler.StartAsync() } -func updatePlayerCount() { +func updateStats() { var err error playerCount, err = db.FetchPlayerCount() if err != nil { log.Print(err) } + battleCount, err = db.FetchBattleCount() + if err != nil { + log.Print(err) + } + classicSessionCount, err = db.FetchClassicSessionCount() + if err != nil { + log.Print(err) + } } // /game/playercount - get player count @@ -39,3 +50,29 @@ func (s *Server) handlePlayerCountGet(w http.ResponseWriter) { w.Write(response) } + +// /game/titlestats - get title stats +func (s *Server) handleTitleStatsGet(w http.ResponseWriter) { + titleStats := &defs.TitleStats{ + PlayerCount: playerCount, + BattleCount: battleCount, + } + response, err := json.Marshal(titleStats) + if err != nil { + http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + return + } + + w.Write(response) +} + +// /game/classicsessioncount - get classic session count +func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter) { + response, err := json.Marshal(classicSessionCount) + if err != nil { + http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + return + } + + w.Write(response) +} diff --git a/api/generic.go b/api/generic.go index f24935f..fec36a9 100644 --- a/api/generic.go +++ b/api/generic.go @@ -36,6 +36,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/game/playercount": s.handlePlayerCountGet(w) + case "/game/titlestats": + s.handleTitleStatsGet(w) + case "/game/classicsessioncount": + s.handleClassicSessionCountGet(w) case "/savedata/get": s.handleSavedataGet(w, r) diff --git a/db/game.go b/db/game.go index 98af758..17baa1c 100644 --- a/db/game.go +++ b/db/game.go @@ -9,3 +9,23 @@ func FetchPlayerCount() (int, error) { return playerCount, nil } + +func FetchBattleCount() (int, error) { + var battleCount int + err := handle.QueryRow("SELECT SUM(battles) FROM accountStats").Scan(&battleCount) + if err != nil { + return 0, err + } + + return battleCount, nil +} + +func FetchClassicSessionCount() (int, error) { + var classicSessionCount int + err := handle.QueryRow("SELECT SUM(classicSessionsPlayed) FROM accountStats").Scan(&classicSessionCount) + if err != nil { + return 0, err + } + + return classicSessionCount, nil +} diff --git a/defs/game.go b/defs/game.go new file mode 100644 index 0000000..d7c58ea --- /dev/null +++ b/defs/game.go @@ -0,0 +1,6 @@ +package defs + +type TitleStats struct { + PlayerCount int `json:"playerCount"` + BattleCount int `json:"battleCount"` +} diff --git a/pokerogue-server.go b/pokerogue-server.go index f755e9b..4fa0677 100644 --- a/pokerogue-server.go +++ b/pokerogue-server.go @@ -43,7 +43,7 @@ func main() { os.Chmod(*addr, 0777) } - api.SchedulePlayerCountRefresh() + api.ScheduleStatRefresh() api.ScheduleDailyRunRefresh() api.InitDailyRun()