rogueserver/api/endpoints.go

261 lines
7.1 KiB
Go
Raw Permalink Normal View History

2023-12-05 10:28:08 -08:00
package api
import (
2024-04-08 17:44:36 -07:00
"encoding/base64"
"encoding/json"
"fmt"
2024-04-09 23:41:58 -07:00
"log"
"net/http"
2024-04-08 17:44:36 -07:00
"strconv"
"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"
2024-04-19 03:05:52 -07:00
"github.com/valyala/fasthttp"
)
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 03:05:52 -07:00
func HandleAccountInfo(ctx *fasthttp.RequestCtx) {
username, err := usernameFromTokenHeader(string(ctx.Request.Header.Peek("Authorization")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
return
}
2024-04-17 16:31:12 -07:00
2024-04-19 03:05:52 -07:00
uuid, err := uuidFromTokenHeader(string(ctx.Request.Header.Peek("Authorization"))) // lazy
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
return
}
2024-04-19 00:27:47 -07:00
response, err := account.Info(username, uuid)
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
2023-12-29 11:30:47 -08:00
}
2024-04-19 03:05:52 -07:00
err = json.NewEncoder(ctx.Response.BodyWriter()).Encode(response)
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleAccountRegister(ctx *fasthttp.RequestCtx) {
err := account.Register(string(ctx.PostArgs().Peek("username")), string(ctx.PostArgs().Peek("password")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
ctx.SetStatusCode(http.StatusOK)
2024-04-19 00:27:47 -07:00
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleAccountLogin(ctx *fasthttp.RequestCtx) {
response, err := account.Login(string(ctx.PostArgs().Peek("username")), string(ctx.PostArgs().Peek("password")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
err = json.NewEncoder(ctx.Response.BodyWriter()).Encode(response)
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleAccountLogout(ctx *fasthttp.RequestCtx) {
token, err := base64.StdEncoding.DecodeString(string(ctx.Request.Header.Peek("Authorization")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to decode token: %s", err), http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 00:27:47 -07:00
err = account.Logout(token)
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
2023-12-29 11:30:47 -08:00
2024-04-19 03:05:52 -07:00
ctx.SetStatusCode(http.StatusOK)
2024-04-19 00:27:47 -07:00
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleGamePlayerCount(ctx *fasthttp.RequestCtx) {
ctx.SetBody([]byte(strconv.Itoa(playerCount)))
2024-04-19 00:27:47 -07:00
}
2024-04-19 03:05:52 -07:00
func HandleGameTitleStats(ctx *fasthttp.RequestCtx) {
err := json.NewEncoder(ctx.Response.BodyWriter()).Encode(defs.TitleStats{
2024-04-19 00:27:47 -07:00
PlayerCount: playerCount,
BattleCount: battleCount,
})
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
}
2024-04-19 03:05:52 -07:00
func HandleGameClassicSessionCount(ctx *fasthttp.RequestCtx) {
ctx.SetBody([]byte(strconv.Itoa(classicSessionCount)))
2024-04-19 00:27:47 -07:00
}
2024-04-19 03:05:52 -07:00
func HandleSaveData(ctx *fasthttp.RequestCtx) {
uuid, err := uuidFromTokenHeader(string(ctx.Request.Header.Peek("Authorization")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
return
}
datatype := -1
2024-04-19 03:05:52 -07:00
if ctx.QueryArgs().Has("datatype") {
datatype, err = strconv.Atoi(string(ctx.QueryArgs().Peek("datatype")))
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, 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
2024-04-19 03:05:52 -07:00
if ctx.QueryArgs().Has("slot") {
slot, err = strconv.Atoi(string(ctx.QueryArgs().Peek("slot")))
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, 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
var save any
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body
2024-04-19 03:05:52 -07:00
if string(ctx.Path()) != "/api/savedata/get" && string(ctx.Path()) != "/api/savedata/delete" {
2024-04-19 00:27:47 -07:00
if datatype == 0 {
var system defs.SystemSaveData
2024-04-19 03:05:52 -07:00
err = json.Unmarshal(ctx.Request.Body(), &system)
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to unmarshal 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)
2024-04-19 03:05:52 -07:00
} else if datatype == 1 || string(ctx.Path()) == "/api/savedata/clear" {
2024-04-19 00:27:47 -07:00
var session defs.SessionSaveData
2024-04-19 03:05:52 -07:00
err = json.Unmarshal(ctx.Request.Body(), &session)
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to unmarshal 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 03:05:52 -07:00
switch string(ctx.Path()) {
2024-04-19 00:27:47 -07:00
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 {
2024-04-19 03:05:52 -07:00
httpError(ctx, 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 {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
if save == nil || string(ctx.Path()) == "/api/savedata/update" {
ctx.SetStatusCode(http.StatusOK)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
err = json.NewEncoder(ctx.Response.BodyWriter()).Encode(save)
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleDailySeed(ctx *fasthttp.RequestCtx) {
ctx.Response.SetBody([]byte(daily.Seed()))
2024-04-19 00:27:47 -07:00
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleDailyRankings(ctx *fasthttp.RequestCtx) {
uuid, err := uuidFromTokenHeader(string(ctx.Request.Header.Peek("Authorization")))
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
return
}
2024-04-08 17:44:36 -07:00
2024-04-19 00:27:47 -07:00
var category int
2024-04-19 03:05:52 -07:00
if ctx.QueryArgs().Has("category") {
category, err = strconv.Atoi(string(ctx.QueryArgs().Peek("category")))
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, 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
2024-04-19 03:05:52 -07:00
if ctx.QueryArgs().Has("page") {
page, err = strconv.Atoi(string(ctx.QueryArgs().Peek("page")))
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, 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 {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
2024-04-19 03:05:52 -07:00
err = json.NewEncoder(ctx.Response.BodyWriter()).Encode(rankings)
2024-04-19 00:27:47 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
2024-04-19 00:27:47 -07:00
return
}
}
2024-04-08 17:44:36 -07:00
2024-04-19 03:05:52 -07:00
func HandleDailyRankingPageCount(ctx *fasthttp.RequestCtx) {
2024-04-19 00:27:47 -07:00
var category int
2024-04-19 03:05:52 -07:00
if ctx.QueryArgs().Has("category") {
2024-04-19 00:27:47 -07:00
var err error
2024-04-19 03:05:52 -07:00
category, err = strconv.Atoi(string(ctx.QueryArgs().Peek("category")))
2024-04-08 17:44:36 -07:00
if err != nil {
2024-04-19 03:05:52 -07:00
httpError(ctx, fmt.Errorf("failed to convert category: %s", err), http.StatusBadRequest)
2024-04-19 00:27:47 -07:00
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 {
2024-04-19 03:05:52 -07:00
httpError(ctx, err, http.StatusInternalServerError)
return
2023-12-29 11:30:47 -08:00
}
2024-04-19 00:27:47 -07:00
2024-04-19 03:05:52 -07:00
ctx.SetBody([]byte(strconv.Itoa(count)))
2023-12-29 11:30:47 -08:00
}
2024-04-19 03:05:52 -07:00
func httpError(ctx *fasthttp.RequestCtx, err error, code int) {
log.Printf("%s: %s\n", ctx.Path(), err)
ctx.Error(err.Error(), code)
2024-04-09 23:41:58 -07:00
}