From 7d23859deae92f465a75b956c92c6ec58720f375 Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Sat, 23 Mar 2024 21:34:18 -0400 Subject: [PATCH] Add player count handler --- api/game.go | 27 +++++++++++++++++++++++++++ api/generic.go | 3 +++ db/game.go | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 api/game.go create mode 100644 db/game.go diff --git a/api/game.go b/api/game.go new file mode 100644 index 0000000..c958e6c --- /dev/null +++ b/api/game.go @@ -0,0 +1,27 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/Flashfyre/pokerogue-server/db" +) + +// /game/playercount - get player count + +func (s *Server) HandlePlayerCountGet(w http.ResponseWriter, r *http.Request) { + playerCount, err := db.FetchPlayerCount() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + response, err := json.Marshal(playerCount) + 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 9aef79d..0888bba 100644 --- a/api/generic.go +++ b/api/generic.go @@ -34,6 +34,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/account/logout": s.HandleAccountLogout(w, r) + case "/game/playercount": + s.HandlePlayerCountGet(w, r) + case "/savedata/get": s.HandleSavedataGet(w, r) case "/savedata/update": diff --git a/db/game.go b/db/game.go new file mode 100644 index 0000000..cb62b9f --- /dev/null +++ b/db/game.go @@ -0,0 +1,12 @@ +package db + +func FetchPlayerCount() (int, error) { + var playerCount int + + err := handle.QueryRow("SELECT COUNT(*) FROM accounts WHERE lastActivity > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 5 MINUTE)").Scan(&playerCount) + if err != nil { + return 0, err + } + + return playerCount, nil +}