Add player count handler

pull/1/head
Flashfyre 2024-03-23 21:34:18 -04:00
parent 2495c1ee9a
commit 7d23859dea
3 changed files with 42 additions and 0 deletions

27
api/game.go Normal file
View File

@ -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)
}

View File

@ -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":

12
db/game.go Normal file
View File

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