rogueserver/api/savedata.go

321 lines
8.6 KiB
Go
Raw Normal View History

2023-12-05 10:28:08 -08:00
package api
2023-12-31 13:12:20 -08:00
import (
"bytes"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
2024-03-15 13:38:32 -07:00
"log"
2023-12-31 13:12:20 -08:00
"net/http"
"os"
2024-03-14 18:44:39 -07:00
"strconv"
2023-12-05 10:28:08 -08:00
2024-03-15 13:38:32 -07:00
"github.com/Flashfyre/pokerogue-server/db"
2024-03-17 10:18:51 -07:00
"github.com/Flashfyre/pokerogue-server/defs"
2023-12-31 13:12:20 -08:00
"github.com/klauspost/compress/zstd"
)
2023-12-05 10:28:08 -08:00
2024-03-14 18:44:39 -07:00
const sessionSlotCount = 3
2023-12-31 13:12:20 -08:00
// /savedata/get - get save data
2024-04-01 19:54:55 -07:00
func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
2024-04-08 15:15:09 -07:00
uuid, err := getUUIDFromRequest(r)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
switch r.URL.Query().Get("datatype") {
case "0": // System
2024-04-01 19:54:55 -07:00
system, err := readSystemSaveData(uuid)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
2023-12-05 10:28:08 -08:00
2023-12-31 13:12:20 -08:00
saveJson, err := json.Marshal(system)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
w.Write(saveJson)
case "1": // Session
2024-04-08 15:15:09 -07:00
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
2024-03-14 18:44:39 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
2024-04-08 15:15:09 -07:00
if slotID < 0 || slotID >= sessionSlotCount {
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
2024-04-08 15:15:09 -07:00
session, err := readSessionSaveData(uuid, slotID)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
saveJson, err := json.Marshal(session)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
w.Write(saveJson)
default:
2024-04-07 14:22:34 -07:00
httpError(w, r, "invalid data type", http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
2023-12-05 10:28:08 -08:00
}
2023-12-29 12:15:16 -08:00
// /savedata/update - update save data
2024-04-01 19:54:55 -07:00
func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
2024-04-08 15:15:09 -07:00
uuid, err := getUUIDFromRequest(r)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
2023-12-05 10:28:08 -08:00
2024-03-15 13:38:32 -07:00
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
}
2024-04-08 15:15:09 -07:00
hexUUID := hex.EncodeToString(uuid)
2023-12-31 13:12:20 -08:00
switch r.URL.Query().Get("datatype") {
case "0": // System
2024-03-17 10:18:51 -07:00
var system defs.SystemSaveData
2023-12-31 13:12:20 -08:00
err = json.NewDecoder(r.Body).Decode(&system)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
2024-04-08 15:15:09 -07:00
if system.TrainerID == 0 && system.SecretID == 0 {
2024-04-07 14:22:34 -07:00
httpError(w, r, "invalid system data", http.StatusInternalServerError)
2024-02-14 14:12:10 -08:00
return
}
2024-04-06 14:43:11 -07:00
err = db.UpdateAccountStats(uuid, system.GameStats)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest)
2024-04-06 14:43:11 -07:00
return
}
2023-12-31 13:12:20 -08:00
var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(system)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
zstdWriter, err := zstd.NewWriter(nil)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
2024-04-08 15:15:09 -07:00
err = os.MkdirAll("userdata/"+hexUUID, 0755)
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsExist(err) {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
2024-04-08 15:15:09 -07:00
err = os.WriteFile("userdata/"+hexUUID+"/system.pzs", compressed, 0644)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
case "1": // Session
2024-04-08 15:15:09 -07:00
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
2024-03-14 18:44:39 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
2024-04-08 15:15:09 -07:00
if slotID < 0 || slotID >= sessionSlotCount {
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
fileName := "session"
2024-04-08 15:15:09 -07:00
if slotID != 0 {
fileName += strconv.Itoa(slotID)
2024-03-14 18:44:39 -07:00
}
2024-03-17 10:18:51 -07:00
var session defs.SessionSaveData
2023-12-31 13:12:20 -08:00
err = json.NewDecoder(r.Body).Decode(&session)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(session)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
zstdWriter, err := zstd.NewWriter(nil)
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
2024-04-08 15:15:09 -07:00
err = os.MkdirAll("userdata/"+hexUUID, 0755)
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsExist(err) {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
2024-04-08 15:15:09 -07:00
err = os.WriteFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName), compressed, 0644)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
default:
2024-04-07 14:22:34 -07:00
httpError(w, r, "invalid data type", http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
w.WriteHeader(http.StatusOK)
2023-12-05 10:28:08 -08:00
}
2023-12-31 13:20:45 -08:00
// /savedata/delete - delete save data
2024-04-01 19:54:55 -07:00
func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) {
2024-04-08 15:15:09 -07:00
uuid, err := getUUIDFromRequest(r)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
2024-03-15 13:38:32 -07:00
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
}
2024-04-08 15:15:09 -07:00
hexUUID := hex.EncodeToString(uuid)
2023-12-31 13:12:20 -08:00
switch r.URL.Query().Get("datatype") {
case "0": // System
2024-04-08 15:15:09 -07:00
err := os.Remove("userdata/" + hexUUID + "/system.pzs")
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsNotExist(err) {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
2024-02-14 14:12:10 -08:00
return
2023-12-31 13:12:20 -08:00
}
case "1": // Session
2024-04-08 15:15:09 -07:00
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
2024-03-14 18:44:39 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
2024-04-08 15:15:09 -07:00
if slotID < 0 || slotID >= sessionSlotCount {
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
2024-03-14 18:44:39 -07:00
return
}
fileName := "session"
2024-04-08 15:15:09 -07:00
if slotID != 0 {
fileName += strconv.Itoa(slotID)
2024-03-14 18:44:39 -07:00
}
2024-04-08 15:15:09 -07:00
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName))
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsNotExist(err) {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
2024-02-14 14:12:10 -08:00
return
2023-12-31 13:12:20 -08:00
}
default:
2024-04-07 14:22:34 -07:00
httpError(w, r, "invalid data type", http.StatusBadRequest)
2023-12-31 13:12:20 -08:00
return
}
2023-12-05 10:28:08 -08:00
2023-12-31 13:12:20 -08:00
w.WriteHeader(http.StatusOK)
2023-12-05 10:28:08 -08:00
}
2024-03-16 18:51:13 -07:00
type SavedataClearResponse struct {
Success bool `json:"success"`
}
// /savedata/clear - mark session save data as cleared and delete
2024-04-01 19:54:55 -07:00
func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) {
2024-04-08 15:15:09 -07:00
uuid, err := getUUIDFromRequest(r)
2024-03-16 18:51:13 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, err.Error(), http.StatusBadRequest)
2024-03-16 18:51:13 -07:00
return
}
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
}
2024-04-08 15:15:09 -07:00
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
2024-03-16 18:51:13 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
2024-03-16 18:51:13 -07:00
return
}
2024-04-08 15:15:09 -07:00
if slotID < 0 || slotID >= sessionSlotCount {
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
2024-03-16 18:51:13 -07:00
return
}
2024-03-17 10:18:51 -07:00
var session defs.SessionSaveData
2024-03-17 08:34:11 -07:00
err = json.NewDecoder(r.Body).Decode(&session)
2024-03-16 18:51:13 -07:00
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
2024-03-16 18:51:13 -07:00
return
}
2024-04-01 19:54:55 -07:00
sessionCompleted := validateSessionCompleted(session)
2024-03-16 18:51:13 -07:00
newCompletion := false
if session.GameMode == 3 && session.Seed == dailyRunSeed {
waveCompleted := session.WaveIndex
2024-03-18 16:55:02 -07:00
if !sessionCompleted {
waveCompleted--
}
err = db.AddOrUpdateAccountDailyRun(uuid, session.Score, waveCompleted)
if err != nil {
2024-04-01 19:54:55 -07:00
log.Printf("failed to add or update daily run record: %s", err)
}
}
2024-03-16 18:51:13 -07:00
if sessionCompleted {
newCompletion, err = db.TryAddSeedCompletion(uuid, session.Seed, int(session.GameMode))
2024-03-16 18:51:13 -07:00
if err != nil {
2024-04-01 19:54:55 -07:00
log.Printf("failed to mark seed as completed: %s", err)
2024-03-16 18:51:13 -07:00
}
}
response, err := json.Marshal(SavedataClearResponse{Success: newCompletion})
if err != nil {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
2024-03-16 18:51:13 -07:00
return
}
2024-03-22 07:18:53 -07:00
fileName := "session"
2024-04-08 15:15:09 -07:00
if slotID != 0 {
fileName += strconv.Itoa(slotID)
2024-03-22 07:18:53 -07:00
}
2024-03-16 18:51:13 -07:00
2024-03-22 07:18:53 -07:00
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
if err != nil && !os.IsNotExist(err) {
2024-04-07 14:22:34 -07:00
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
2024-03-22 07:18:53 -07:00
return
2024-03-16 18:51:13 -07:00
}
w.Write(response)
}