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 +}