|
|
@ -8,25 +8,36 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
|
|
|
|
|
|
|
"github.com/Flashfyre/pokerogue-server/defs"
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
playerCountScheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
statScheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
playerCount int
|
|
|
|
playerCount int
|
|
|
|
|
|
|
|
battleCount int
|
|
|
|
|
|
|
|
classicSessionCount int
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func SchedulePlayerCountRefresh() {
|
|
|
|
func ScheduleStatRefresh() {
|
|
|
|
playerCountScheduler.Every(10).Second().Do(updatePlayerCount)
|
|
|
|
statScheduler.Every(10).Second().Do(updateStats)
|
|
|
|
playerCountScheduler.StartAsync()
|
|
|
|
statScheduler.StartAsync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func updatePlayerCount() {
|
|
|
|
func updateStats() {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
playerCount, err = db.FetchPlayerCount()
|
|
|
|
playerCount, err = db.FetchPlayerCount()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
battleCount, err = db.FetchBattleCount()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Print(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
classicSessionCount, err = db.FetchClassicSessionCount()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Print(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// /game/playercount - get player count
|
|
|
|
// /game/playercount - get player count
|
|
|
@ -39,3 +50,29 @@ func (s *Server) handlePlayerCountGet(w http.ResponseWriter) {
|
|
|
|
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
w.Write(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /game/titlestats - get title stats
|
|
|
|
|
|
|
|
func (s *Server) handleTitleStatsGet(w http.ResponseWriter) {
|
|
|
|
|
|
|
|
titleStats := &defs.TitleStats{
|
|
|
|
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
|
|
|
|
BattleCount: battleCount,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := json.Marshal(titleStats)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /game/classicsessioncount - get classic session count
|
|
|
|
|
|
|
|
func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter) {
|
|
|
|
|
|
|
|
response, err := json.Marshal(classicSessionCount)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
|
|
|
}
|
|
|
|