2023-12-05 10:28:08 -08:00
|
|
|
package api
|
|
|
|
|
2023-12-31 14:39:11 -08:00
|
|
|
import (
|
2024-04-08 17:44:36 -07:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-04-09 23:41:58 -07:00
|
|
|
"log"
|
2023-12-31 14:39:11 -08:00
|
|
|
"net/http"
|
2024-04-08 17:44:36 -07:00
|
|
|
"strconv"
|
|
|
|
|
2024-04-14 17:03:53 -07:00
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
|
|
|
"github.com/pagefaultgames/pokerogue-server/api/savedata"
|
2024-04-14 16:25:36 -07:00
|
|
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
2023-12-31 14:39:11 -08:00
|
|
|
)
|
2023-12-29 11:30:47 -08:00
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
Handlers should not return serialized JSON, instead return the struct itself.
|
|
|
|
*/
|
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
username, err := getUsernameFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 16:31:12 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
uuid, err := getUUIDFromRequest(r) // lazy
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-12-31 14:39:11 -08:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
response, err := account.Info(username, uuid)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
2023-12-29 11:30:47 -08:00
|
|
|
}
|
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
err = account.Register(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to parse request form: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
response, err := account.Login(r.Form.Get("username"), r.Form.Get("password"))
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
err = account.Logout(token)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-12-29 11:30:47 -08:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleGamePlayerCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
|
|
|
PlayerCount: playerCount,
|
|
|
|
BattleCount: battleCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
datatype := -1
|
|
|
|
if r.URL.Query().Has("datatype") {
|
|
|
|
datatype, err = strconv.Atoi(r.URL.Query().Get("datatype"))
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
2024-03-23 18:34:18 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
var slot int
|
|
|
|
if r.URL.Query().Has("slot") {
|
|
|
|
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
var save any
|
|
|
|
// /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 datatype == 0 {
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
save = system
|
|
|
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
|
|
|
} else if datatype == 1 || r.URL.Path == "/api/savedata/clear" {
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
save = session
|
2024-04-08 17:44:36 -07:00
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/api/savedata/get":
|
|
|
|
save, err = savedata.Get(uuid, datatype, slot)
|
|
|
|
case "/api/savedata/update":
|
|
|
|
err = savedata.Update(uuid, slot, save)
|
|
|
|
case "/api/savedata/delete":
|
|
|
|
err = savedata.Delete(uuid, datatype, slot)
|
|
|
|
case "/api/savedata/clear":
|
|
|
|
s, ok := save.(defs.SessionSaveData)
|
|
|
|
if !ok {
|
|
|
|
httpError(w, r, fmt.Errorf("save data is not type SessionSaveData"), http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
// doesn't return a save, but it works
|
|
|
|
save, err = savedata.Clear(uuid, slot, daily.Seed(), s)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
if save == nil || r.URL.Path == "/api/savedata/update" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
err = json.NewEncoder(w).Encode(save)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleDailySeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(daily.Seed()))
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
var category int
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
page := 1
|
|
|
|
if r.URL.Query().Has("page") {
|
|
|
|
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, fmt.Errorf("failed to convert page: %s", err), http.StatusBadRequest)
|
2024-04-08 17:44:36 -07:00
|
|
|
return
|
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rankings, err := daily.Rankings(uuid, category, page)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(rankings)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var category int
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
var err error
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
2024-04-08 17:44:36 -07:00
|
|
|
if err != nil {
|
2024-04-19 00:27:47 -07:00
|
|
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
2024-04-08 17:44:36 -07:00
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
|
2024-04-19 00:27:47 -07:00
|
|
|
count, err := daily.RankingPageCount(category)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
2023-12-29 11:30:47 -08:00
|
|
|
}
|
2024-04-19 00:27:47 -07:00
|
|
|
|
|
|
|
w.Write([]byte(strconv.Itoa(count)))
|
2023-12-29 11:30:47 -08:00
|
|
|
}
|
|
|
|
|
2024-04-09 23:41:58 -07:00
|
|
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
|
|
|
log.Printf("%s: %s\n", r.URL.Path, err)
|
|
|
|
http.Error(w, err.Error(), code)
|
|
|
|
}
|