|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
|
|
@ -14,13 +15,36 @@ import (
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
|
|
|
|
Debug bool
|
|
|
|
|
|
|
|
Exit *sync.RWMutex
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
The caller of endpoint handler functions are responsible for extracting the necessary data from the request.
|
|
|
|
The caller of endpoint handler functions are responsible for extracting the necessary data from the request.
|
|
|
|
Handler functions are responsible for checking the validity of this data and returning a result or error.
|
|
|
|
Handler functions are responsible for checking the validity of this data and returning a result or error.
|
|
|
|
Handlers should not return serialized JSON, instead return the struct itself.
|
|
|
|
Handlers should not return serialized JSON, instead return the struct itself.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// kind of misusing the RWMutex but it doesn't matter
|
|
|
|
|
|
|
|
s.Exit.RLock()
|
|
|
|
|
|
|
|
defer s.Exit.RUnlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
|
|
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "*")
|
|
|
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
|
|
|
// /account
|
|
|
|
|
|
|
|
case "/account/info":
|
|
|
|
username, err := getUsernameFromRequest(r)
|
|
|
|
username, err := getUsernameFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
@ -44,9 +68,7 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "/account/register":
|
|
|
|
|
|
|
|
|
|
|
|
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
@ -60,9 +82,7 @@ func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
case "/account/login":
|
|
|
|
|
|
|
|
|
|
|
|
func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
@ -80,9 +100,7 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "/account/logout":
|
|
|
|
|
|
|
|
|
|
|
|
func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
|
|
|
|
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
|
|
|
@ -96,13 +114,11 @@ func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleGamePlayerCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// /game
|
|
|
|
|
|
|
|
case "/game/playercount":
|
|
|
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
|
|
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
|
|
|
}
|
|
|
|
case "/game/titlestats":
|
|
|
|
|
|
|
|
|
|
|
|
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
|
|
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
BattleCount: battleCount,
|
|
|
|
BattleCount: battleCount,
|
|
|
@ -111,13 +127,11 @@ func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "/game/classicsessioncount":
|
|
|
|
|
|
|
|
|
|
|
|
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
|
|
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// /savedata
|
|
|
|
|
|
|
|
case "/savedata/get", "/savedata/update", "/savedata/delete", "/savedata/clear":
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
@ -144,7 +158,7 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
var save any
|
|
|
|
var save any
|
|
|
|
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
|
|
|
|
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
|
|
|
|
if r.URL.Path != "/api/savedata/get" && r.URL.Path != "/api/savedata/delete" {
|
|
|
|
if r.URL.Path != "/savedata/get" && r.URL.Path != "/savedata/delete" {
|
|
|
|
if datatype == 0 {
|
|
|
|
if datatype == 0 {
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
@ -155,7 +169,7 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
save = system
|
|
|
|
save = system
|
|
|
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
|
|
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
|
|
|
} else if datatype == 1 || r.URL.Path == "/api/savedata/clear" {
|
|
|
|
} else if datatype == 1 || r.URL.Path == "/savedata/clear" {
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -168,13 +182,13 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/api/savedata/get":
|
|
|
|
case "/savedata/get":
|
|
|
|
save, err = savedata.Get(uuid, datatype, slot)
|
|
|
|
save, err = savedata.Get(uuid, datatype, slot)
|
|
|
|
case "/api/savedata/update":
|
|
|
|
case "/savedata/update":
|
|
|
|
err = savedata.Update(uuid, slot, save)
|
|
|
|
err = savedata.Update(uuid, slot, save)
|
|
|
|
case "/api/savedata/delete":
|
|
|
|
case "/savedata/delete":
|
|
|
|
err = savedata.Delete(uuid, datatype, slot)
|
|
|
|
err = savedata.Delete(uuid, datatype, slot)
|
|
|
|
case "/api/savedata/clear":
|
|
|
|
case "/savedata/clear":
|
|
|
|
s, ok := save.(defs.SessionSaveData)
|
|
|
|
s, ok := save.(defs.SessionSaveData)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
httpError(w, r, fmt.Errorf("save data is not type SessionSaveData"), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("save data is not type SessionSaveData"), http.StatusBadRequest)
|
|
|
@ -189,7 +203,7 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if save == nil || r.URL.Path == "/api/savedata/update" {
|
|
|
|
if save == nil || r.URL.Path == "/savedata/update" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -199,13 +213,11 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailySeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// /daily
|
|
|
|
|
|
|
|
case "/daily/seed":
|
|
|
|
w.Write([]byte(daily.Seed()))
|
|
|
|
w.Write([]byte(daily.Seed()))
|
|
|
|
}
|
|
|
|
case "/daily/rankings":
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
@ -241,9 +253,7 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "/daily/rankingpagecount":
|
|
|
|
|
|
|
|
|
|
|
|
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
var category int
|
|
|
|
var category int
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
@ -260,6 +270,7 @@ func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Write([]byte(strconv.Itoa(count)))
|
|
|
|
w.Write([]byte(strconv.Itoa(count)))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
|
|
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
|
|
|