rogueserver/api/stats.go

40 lines
660 B
Go
Raw Normal View History

2024-03-23 18:34:18 -07:00
package api
import (
"log"
"time"
2024-03-23 18:34:18 -07:00
"github.com/go-co-op/gocron"
2024-04-14 16:25:36 -07:00
"github.com/pagefaultgames/pokerogue-server/db"
2024-03-23 18:34:18 -07:00
)
var (
2024-04-06 15:15:47 -07:00
statScheduler = gocron.NewScheduler(time.UTC)
playerCount int
battleCount int
classicSessionCount int
)
2024-03-23 18:34:18 -07:00
func scheduleStatRefresh() {
2024-04-06 15:15:47 -07:00
statScheduler.Every(10).Second().Do(updateStats)
statScheduler.StartAsync()
}
2024-04-06 15:15:47 -07:00
func updateStats() {
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-08 17:44:36 -07:00
2024-04-06 15:15:47 -07:00
battleCount, err = db.FetchBattleCount()
if err != nil {
log.Print(err)
}
2024-04-08 17:44:36 -07:00
2024-04-06 15:15:47 -07:00
classicSessionCount, err = db.FetchClassicSessionCount()
if err != nil {
log.Print(err)
}
}