2024-03-23 18:34:18 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-04-01 18:42:48 -07:00
|
|
|
"log"
|
2024-03-23 18:34:18 -07:00
|
|
|
"net/http"
|
2024-04-01 18:42:48 -07:00
|
|
|
"time"
|
2024-03-23 18:34:18 -07:00
|
|
|
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
2024-04-06 15:15:47 -07:00
|
|
|
"github.com/Flashfyre/pokerogue-server/defs"
|
2024-04-01 18:42:48 -07:00
|
|
|
"github.com/go-co-op/gocron"
|
2024-03-23 18:34:18 -07:00
|
|
|
)
|
|
|
|
|
2024-04-01 18:42:48 -07:00
|
|
|
var (
|
2024-04-06 15:15:47 -07:00
|
|
|
statScheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
playerCount int
|
|
|
|
battleCount int
|
|
|
|
classicSessionCount int
|
2024-04-01 18:42:48 -07:00
|
|
|
)
|
2024-03-23 18:34:18 -07:00
|
|
|
|
2024-04-06 15:15:47 -07:00
|
|
|
func ScheduleStatRefresh() {
|
|
|
|
statScheduler.Every(10).Second().Do(updateStats)
|
|
|
|
statScheduler.StartAsync()
|
2024-04-01 18:42:48 -07:00
|
|
|
}
|
|
|
|
|
2024-04-06 15:15:47 -07:00
|
|
|
func updateStats() {
|
2024-04-01 18:42:48 -07:00
|
|
|
var err error
|
|
|
|
playerCount, err = db.FetchPlayerCount()
|
2024-03-23 18:34:18 -07:00
|
|
|
if err != nil {
|
2024-04-01 19:54:55 -07:00
|
|
|
log.Print(err)
|
2024-03-23 18:34:18 -07:00
|
|
|
}
|
2024-04-06 15:15:47 -07:00
|
|
|
battleCount, err = db.FetchBattleCount()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
classicSessionCount, err = db.FetchClassicSessionCount()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2024-04-01 18:42:48 -07:00
|
|
|
}
|
2024-03-23 18:34:18 -07:00
|
|
|
|
2024-04-01 18:42:48 -07:00
|
|
|
// /game/playercount - get player count
|
2024-04-07 14:22:34 -07:00
|
|
|
func (s *Server) handlePlayerCountGet(w http.ResponseWriter, r *http.Request) {
|
2024-03-23 18:34:18 -07:00
|
|
|
response, err := json.Marshal(playerCount)
|
|
|
|
if err != nil {
|
2024-04-07 14:22:34 -07:00
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
2024-03-23 18:34:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
}
|
2024-04-06 15:15:47 -07:00
|
|
|
|
|
|
|
// /game/titlestats - get title stats
|
2024-04-07 14:22:34 -07:00
|
|
|
func (s *Server) handleTitleStatsGet(w http.ResponseWriter, r *http.Request) {
|
2024-04-06 15:15:47 -07:00
|
|
|
titleStats := &defs.TitleStats{
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
BattleCount: battleCount,
|
|
|
|
}
|
|
|
|
response, err := json.Marshal(titleStats)
|
|
|
|
if err != nil {
|
2024-04-07 14:22:34 -07:00
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
2024-04-06 15:15:47 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// /game/classicsessioncount - get classic session count
|
2024-04-07 14:22:34 -07:00
|
|
|
func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter, r *http.Request) {
|
2024-04-06 15:15:47 -07:00
|
|
|
response, err := json.Marshal(classicSessionCount)
|
|
|
|
if err != nil {
|
2024-04-07 14:22:34 -07:00
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
2024-04-06 15:15:47 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
}
|