mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-07-14 20:32:18 +02:00
Compare commits
No commits in common. "8439519d8e78a3cd312974c40567f6477cd68176" and "81853b1863b2214066739fc622d20da76e158f30" have entirely different histories.
8439519d8e
...
81853b1863
@ -19,7 +19,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -49,14 +48,14 @@ func Init(mux *http.ServeMux) error {
|
|||||||
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
|
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
|
||||||
|
|
||||||
// savedata
|
// savedata
|
||||||
mux.HandleFunc("GET /savedata/get", handleGetSaveData)
|
mux.HandleFunc("GET /savedata/get", handleSaveData)
|
||||||
mux.HandleFunc("POST /savedata/update", handleSaveData)
|
mux.HandleFunc("POST /savedata/update", handleSaveData)
|
||||||
mux.HandleFunc("GET /savedata/delete", handleSaveData) // TODO use deleteSystemSave
|
mux.HandleFunc("GET /savedata/delete", handleSaveData)
|
||||||
mux.HandleFunc("POST /savedata/clear", handleSaveData) // TODO use clearSessionData
|
mux.HandleFunc("POST /savedata/clear", handleSaveData)
|
||||||
mux.HandleFunc("GET /savedata/newclear", handleNewClear)
|
mux.HandleFunc("GET /savedata/newclear", handleNewClear)
|
||||||
|
|
||||||
// new session
|
// new session
|
||||||
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
|
mux.HandleFunc("POST /savedata/updateall", handleSaveData2)
|
||||||
|
|
||||||
// daily
|
// daily
|
||||||
mux.HandleFunc("GET /daily/seed", handleDailySeed)
|
mux.HandleFunc("GET /daily/seed", handleDailySeed)
|
||||||
@ -83,34 +82,20 @@ func tokenFromRequest(r *http.Request) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func uuidFromRequest(r *http.Request) ([]byte, error) {
|
func uuidFromRequest(r *http.Request) ([]byte, error) {
|
||||||
_, uuid, err := tokenAndUuidFromRequest(r)
|
|
||||||
return uuid, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func tokenAndUuidFromRequest(r *http.Request) ([]byte, []byte, error) {
|
|
||||||
token, err := tokenFromRequest(r)
|
token, err := tokenFromRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid, err := db.FetchUUIDFromToken(token)
|
uuid, err := db.FetchUUIDFromToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to validate token: %s", err)
|
return nil, fmt.Errorf("failed to validate token: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return token, uuid, nil
|
return uuid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
|
||||||
log.Printf("%s: %s\n", r.URL.Path, err)
|
log.Printf("%s: %s\n", r.URL.Path, err)
|
||||||
http.Error(w, err.Error(), code)
|
http.Error(w, err.Error(), code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonResponse(w http.ResponseWriter, r *http.Request, data any) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
err := json.NewEncoder(w).Encode(data)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
291
api/endpoints.go
291
api/endpoints.go
@ -20,7 +20,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -59,7 +58,13 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, response)
|
err = json.NewEncoder(w).Encode(response)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -91,7 +96,13 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, response)
|
err = json.NewEncoder(w).Encode(response)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
|
func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -133,238 +144,26 @@ func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// game
|
// game
|
||||||
|
|
||||||
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
||||||
stats := defs.TitleStats{
|
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
||||||
PlayerCount: playerCount,
|
PlayerCount: playerCount,
|
||||||
BattleCount: battleCount,
|
BattleCount: battleCount,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, stats)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
_, _ = w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetSaveData(w http.ResponseWriter, r *http.Request) {
|
|
||||||
token, uuid, err := tokenAndUuidFromRequest(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
|
|
||||||
if datatype == 0 {
|
|
||||||
err = db.UpdateActiveSession(uuid, token)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
save, err = savedata.Get(uuid, datatype, slot)
|
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonResponse(w, r, save)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME UNFINISHED!!!
|
|
||||||
func clearSessionData(w http.ResponseWriter, r *http.Request) {
|
|
||||||
token, uuid, err := tokenAndUuidFromRequest(r)
|
|
||||||
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
|
|
||||||
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
|
|
||||||
|
|
||||||
var active bool
|
|
||||||
active, err = db.IsActiveSession(token)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var trainerId, secretId int
|
|
||||||
if r.URL.Query().Has("trainerId") && r.URL.Query().Has("secretId") {
|
|
||||||
trainerId, err = strconv.Atoi(r.URL.Query().Get("trainerId"))
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
secretId, err = strconv.Atoi(r.URL.Query().Get("secretId"))
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if storedTrainerId > 0 || storedSecretId > 0 {
|
|
||||||
if trainerId != storedTrainerId || secretId != storedSecretId {
|
|
||||||
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err = db.UpdateTrainerIds(trainerId, secretId, uuid)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("unable to update traienr ID: %s", err), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !active {
|
|
||||||
save = savedata.ClearResponse{Error: "session out of date"}
|
|
||||||
}
|
|
||||||
|
|
||||||
var seed string
|
|
||||||
seed, err = db.GetDailyRunSeed()
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := savedata.Clear(uuid, slot, seed, save.(defs.SessionSaveData))
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonResponse(w, r, response)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME UNFINISHED!!!
|
|
||||||
func deleteSystemSave(w http.ResponseWriter, r *http.Request) {
|
|
||||||
token, uuid, err := tokenAndUuidFromRequest(r)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
datatype := 0
|
|
||||||
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 active bool
|
|
||||||
active, err = db.IsActiveSession(token)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !active {
|
|
||||||
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var trainerId, secretId int
|
|
||||||
|
|
||||||
if r.URL.Query().Has("trainerId") && r.URL.Query().Has("secretId") {
|
|
||||||
trainerId, err = strconv.Atoi(r.URL.Query().Get("trainerId"))
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
secretId, err = strconv.Atoi(r.URL.Query().Get("secretId"))
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if storedTrainerId > 0 || storedSecretId > 0 {
|
|
||||||
if trainerId != storedTrainerId || secretId != storedSecretId {
|
|
||||||
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = savedata.Delete(uuid, datatype, slot)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, err, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
||||||
token, uuid, err := tokenAndUuidFromRequest(r)
|
uuid, err := uuidFromRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
httpError(w, r, err, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -413,6 +212,13 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var token []byte
|
||||||
|
token, err = tokenFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var active bool
|
var active bool
|
||||||
if r.URL.Path == "/savedata/get" {
|
if r.URL.Path == "/savedata/get" {
|
||||||
if datatype == 0 {
|
if datatype == 0 {
|
||||||
@ -513,7 +319,13 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, save)
|
err = json.NewEncoder(w).Encode(save)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
type CombinedSaveData struct {
|
type CombinedSaveData struct {
|
||||||
@ -523,9 +335,15 @@ type CombinedSaveData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO wrap this in a transaction
|
// TODO wrap this in a transaction
|
||||||
func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
|
func handleSaveData2(w http.ResponseWriter, r *http.Request) {
|
||||||
var token []byte
|
var token []byte
|
||||||
token, uuid, err := tokenAndUuidFromRequest(r)
|
token, err := tokenFromRequest(r)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, err, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid, err := uuidFromRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, r, err, http.StatusBadRequest)
|
httpError(w, r, err, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -606,7 +424,13 @@ func handleNewClear(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, newClear)
|
err = json.NewEncoder(w).Encode(newClear)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
// daily
|
// daily
|
||||||
@ -618,10 +442,7 @@ func handleDailySeed(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write([]byte(seed))
|
_, _ = w.Write([]byte(seed))
|
||||||
if err != nil {
|
|
||||||
httpError(w, r, fmt.Errorf("failed to write seed: %s", err), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -651,7 +472,13 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(w, r, rankings)
|
err = json.NewEncoder(w).Encode(rankings)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user