Add player count handler
parent
2495c1ee9a
commit
7d23859dea
|
@ -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)
|
||||
}
|
|
@ -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":
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue