rogueserver/api/game.go

42 lines
804 B
Go
Raw Normal View History

2024-03-23 18:34:18 -07:00
package api
import (
"encoding/json"
"fmt"
"log"
2024-03-23 18:34:18 -07:00
"net/http"
"time"
2024-03-23 18:34:18 -07:00
"github.com/Flashfyre/pokerogue-server/db"
"github.com/go-co-op/gocron"
2024-03-23 18:34:18 -07:00
)
var (
playerCountScheduler = gocron.NewScheduler(time.UTC)
2024-04-01 19:54:55 -07:00
playerCount int
)
2024-03-23 18:34:18 -07:00
func SchedulePlayerCountRefresh() {
2024-04-01 19:54:55 -07:00
playerCountScheduler.Every(10).Second().Do(updatePlayerCount)
playerCountScheduler.StartAsync()
}
2024-04-01 19:54:55 -07:00
func updatePlayerCount() {
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-03-23 18:34:18 -07:00
// /game/playercount - get player count
2024-04-01 19:54:55 -07:00
func (s *Server) handlePlayerCountGet(w http.ResponseWriter) {
2024-03-23 18:34:18 -07:00
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)
}