|
|
@ -3,20 +3,35 @@ package api
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
|
|
|
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// /game/playercount - get player count
|
|
|
|
var (
|
|
|
|
|
|
|
|
playerCountScheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
|
|
|
|
playerCount = 0
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) HandlePlayerCountGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func SchedulePlayerCountRefresh() {
|
|
|
|
playerCount, err := db.FetchPlayerCount()
|
|
|
|
playerCountScheduler.Every(10).Second().Do(UpdatePlayerCount)
|
|
|
|
|
|
|
|
playerCountScheduler.StartAsync()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func UpdatePlayerCount() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
playerCount, err = db.FetchPlayerCount()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Print(err.Error())
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /game/playercount - get player count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) HandlePlayerCountGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
response, err := json.Marshal(playerCount)
|
|
|
|
response, err := json.Marshal(playerCount)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|