You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
7.2 KiB
Go
274 lines
7.2 KiB
Go
12 months ago
|
package api
|
||
|
|
||
11 months ago
|
import (
|
||
8 months ago
|
"encoding/base64"
|
||
8 months ago
|
"encoding/gob"
|
||
8 months ago
|
"encoding/json"
|
||
|
"fmt"
|
||
8 months ago
|
"log"
|
||
11 months ago
|
"net/http"
|
||
8 months ago
|
"strconv"
|
||
|
|
||
7 months ago
|
"github.com/pagefaultgames/pokerogue-server/api/account"
|
||
|
"github.com/pagefaultgames/pokerogue-server/api/daily"
|
||
|
"github.com/pagefaultgames/pokerogue-server/api/savedata"
|
||
7 months ago
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
||
11 months ago
|
)
|
||
11 months ago
|
|
||
|
type Server struct {
|
||
|
Debug bool
|
||
|
}
|
||
|
|
||
8 months ago
|
/*
|
||
|
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.
|
||
|
*/
|
||
|
|
||
11 months ago
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
8 months ago
|
gob.Register([]interface{}{})
|
||
|
gob.Register(map[string]interface{}{})
|
||
|
|
||
11 months ago
|
if s.Debug {
|
||
11 months ago
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||
|
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||
11 months ago
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||
11 months ago
|
|
||
11 months ago
|
if r.Method == "OPTIONS" {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch r.URL.Path {
|
||
8 months ago
|
// /account
|
||
11 months ago
|
case "/account/info":
|
||
8 months ago
|
username, err := getUsernameFromRequest(r)
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
uuid, err := getUUIDFromRequest(r) // lazy
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
7 months ago
|
response, err := account.Info(username, uuid)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
err = json.NewEncoder(w).Encode(response)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||
8 months ago
|
return
|
||
|
}
|
||
11 months ago
|
case "/account/register":
|
||
7 months ago
|
var request account.RegisterRequest
|
||
8 months ago
|
err := json.NewDecoder(r.Body).Decode(&request)
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
7 months ago
|
err = account.Register(request)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.WriteHeader(http.StatusOK)
|
||
11 months ago
|
case "/account/login":
|
||
7 months ago
|
var request account.LoginRequest
|
||
8 months ago
|
err := json.NewDecoder(r.Body).Decode(&request)
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
7 months ago
|
response, err := account.Login(request)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
err = json.NewEncoder(w).Encode(response)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||
8 months ago
|
return
|
||
|
}
|
||
11 months ago
|
case "/account/logout":
|
||
8 months ago
|
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
|
||
|
}
|
||
|
|
||
7 months ago
|
err = account.Logout(token)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
11 months ago
|
|
||
8 months ago
|
w.WriteHeader(http.StatusOK)
|
||
|
|
||
8 months ago
|
// /game
|
||
8 months ago
|
case "/game/playercount":
|
||
8 months ago
|
w.Write([]byte(strconv.Itoa(playerCount)))
|
||
8 months ago
|
case "/game/titlestats":
|
||
8 months ago
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
||
8 months ago
|
PlayerCount: playerCount,
|
||
|
BattleCount: battleCount,
|
||
|
})
|
||
|
if err != nil {
|
||
8 months ago
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||
8 months ago
|
return
|
||
|
}
|
||
8 months ago
|
case "/game/classicsessioncount":
|
||
8 months ago
|
w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
||
8 months ago
|
|
||
8 months ago
|
// /savedata
|
||
8 months ago
|
case "/savedata/get", "/savedata/update", "/savedata/delete", "/savedata/clear":
|
||
|
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"))
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var slot int
|
||
|
if r.URL.Query().Has("slot") {
|
||
|
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var save any
|
||
8 months ago
|
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
|
||
|
if r.URL.Path != "/savedata/get" && r.URL.Path != "/savedata/delete" {
|
||
8 months ago
|
if datatype == 0 {
|
||
|
var system defs.SystemSaveData
|
||
|
err = json.NewDecoder(r.Body).Decode(&system)
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
save = system
|
||
|
// /savedata/clear doesn't specify datatype, it is assumed to be 1 (session)
|
||
|
} else if datatype == 1 || r.URL.Path == "/savedata/clear" {
|
||
|
var session defs.SessionSaveData
|
||
|
err = json.NewDecoder(r.Body).Decode(&session)
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
save = session
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch r.URL.Path {
|
||
|
case "/savedata/get":
|
||
7 months ago
|
save, err = savedata.Get(uuid, datatype, slot)
|
||
8 months ago
|
case "/savedata/update":
|
||
7 months ago
|
err = savedata.Update(uuid, slot, save)
|
||
8 months ago
|
case "/savedata/delete":
|
||
7 months ago
|
err = savedata.Delete(uuid, datatype, slot)
|
||
8 months ago
|
case "/savedata/clear":
|
||
|
// doesn't return a save, but it works
|
||
7 months ago
|
save, err = savedata.Clear(uuid, slot, daily.Seed(), save.(defs.SessionSaveData))
|
||
8 months ago
|
}
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
if save == nil || r.URL.Path == "/savedata/update" {
|
||
8 months ago
|
w.WriteHeader(http.StatusOK)
|
||
8 months ago
|
return
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
err = json.NewEncoder(w).Encode(save)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||
8 months ago
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
// /daily
|
||
8 months ago
|
case "/daily/seed":
|
||
7 months ago
|
w.Write([]byte(daily.Seed()))
|
||
8 months ago
|
case "/daily/rankings":
|
||
8 months ago
|
uuid, err := getUUIDFromRequest(r)
|
||
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var category int
|
||
|
if r.URL.Query().Has("category") {
|
||
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
page := 1
|
||
|
if r.URL.Query().Has("page") {
|
||
|
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to convert page: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
7 months ago
|
rankings, err := daily.Rankings(uuid, category, page)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 months ago
|
err = json.NewEncoder(w).Encode(rankings)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||
8 months ago
|
return
|
||
|
}
|
||
8 months ago
|
case "/daily/rankingpagecount":
|
||
8 months ago
|
var category int
|
||
|
if r.URL.Query().Has("category") {
|
||
|
var err error
|
||
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
||
|
if err != nil {
|
||
|
httpError(w, r, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
7 months ago
|
count, err := daily.RankingPageCount(category)
|
||
8 months ago
|
if err != nil {
|
||
|
httpError(w, r, err, http.StatusInternalServerError)
|
||
|
}
|
||
|
|
||
|
w.Write([]byte(strconv.Itoa(count)))
|
||
11 months ago
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
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)
|
||
|
}
|