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-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 (
|
|
|
|
playerCountScheduler = gocron.NewScheduler(time.UTC)
|
2024-04-01 19:54:55 -07:00
|
|
|
playerCount int
|
2024-04-01 18:42:48 -07:00
|
|
|
)
|
2024-03-23 18:34:18 -07:00
|
|
|
|
2024-04-01 18:42:48 -07:00
|
|
|
func SchedulePlayerCountRefresh() {
|
2024-04-01 19:54:55 -07:00
|
|
|
playerCountScheduler.Every(10).Second().Do(updatePlayerCount)
|
2024-04-01 18:42:48 -07:00
|
|
|
playerCountScheduler.StartAsync()
|
|
|
|
}
|
|
|
|
|
2024-04-01 19:54:55 -07:00
|
|
|
func updatePlayerCount() {
|
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-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-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)
|
|
|
|
}
|