rogueserver/api/savedata.go

319 lines
8.4 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
2023-12-05 10:28:08 -08:00
2023-12-29 11:30:47 -08:00
func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
2023-12-31 13:12:20 -08:00
uuid, err := GetUuidFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
switch r.URL.Query().Get("datatype") {
case "0": // System
2024-03-18 16:55:02 -07:00
system, err := ReadSystemSaveData(uuid)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-03-16 18:51:13 -07:00
http.Error(w, 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 {
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
return
}
w.Write(saveJson)
case "1": // Session
2024-03-14 18:44:39 -07:00
slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
2024-03-18 16:55:02 -07:00
session, err := ReadSessionSaveData(uuid, slotId)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-03-16 18:51:13 -07:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2023-12-31 13:12:20 -08:00
return
}
saveJson, err := json.Marshal(session)
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
return
}
w.Write(saveJson)
default:
http.Error(w, "invalid data type", http.StatusBadRequest)
return
}
2023-12-05 10:28:08 -08:00
}
2023-12-29 12:15:16 -08:00
// /savedata/update - update save data
2023-12-05 10:28:08 -08:00
2023-12-29 11:30:47 -08:00
func (s *Server) HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
2023-12-31 13:12:20 -08:00
uuid, err := GetUuidFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
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")
}
2023-12-31 13:12:20 -08:00
hexUuid := hex.EncodeToString(uuid)
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 {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
2024-02-14 14:12:10 -08:00
if system.TrainerId == 0 && system.SecretId == 0 {
http.Error(w, "invalid system data", http.StatusInternalServerError)
return
}
2023-12-31 13:12:20 -08:00
var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(system)
if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
return
}
zstdWriter, err := zstd.NewWriter(nil)
if err != nil {
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
return
}
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
err = os.MkdirAll("userdata/"+hexUuid, 0755)
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsExist(err) {
2023-12-31 13:12:20 -08:00
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
return
}
err = os.WriteFile("userdata/"+hexUuid+"/system.pzs", compressed, 0644)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
return
}
case "1": // Session
2024-03-14 18:44:39 -07:00
slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
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 {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(session)
if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
return
}
zstdWriter, err := zstd.NewWriter(nil)
if err != nil {
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
return
}
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
err = os.MkdirAll("userdata/"+hexUuid, 0755)
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsExist(err) {
2023-12-31 13:12:20 -08:00
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
return
}
2024-03-15 12:38:03 -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 {
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
return
}
default:
http.Error(w, "invalid data type", http.StatusBadRequest)
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
2023-12-05 10:28:08 -08:00
2023-12-29 11:30:47 -08:00
func (s *Server) HandleSavedataDelete(w http.ResponseWriter, r *http.Request) {
2023-12-31 13:12:20 -08:00
uuid, err := GetUuidFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
2024-03-15 13:38:32 -07:00
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
}
2023-12-31 13:12:20 -08:00
hexUuid := hex.EncodeToString(uuid)
switch r.URL.Query().Get("datatype") {
case "0": // System
2024-02-14 14:12:10 -08:00
err := os.Remove("userdata/" + hexUuid + "/system.pzs")
2023-12-31 13:27:21 -08:00
if err != nil && !os.IsNotExist(err) {
2023-12-31 13:12:20 -08:00
http.Error(w, 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-03-14 18:44:39 -07:00
slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
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) {
2023-12-31 13:12:20 -08:00
http.Error(w, 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:
http.Error(w, "invalid data type", http.StatusBadRequest)
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
func (s *Server) HandleSavedataClear(w http.ResponseWriter, r *http.Request) {
uuid, err := GetUuidFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
log.Print("failed to update account last activity")
}
slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
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-03-17 08:34:11 -07:00
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
2024-03-16 18:51:13 -07:00
return
}
2024-03-18 16:55:02 -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 {
log.Printf("failed to add or update daily run record: %s", err.Error())
}
}
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-03-17 08:34:11 -07:00
log.Printf("failed to mark seed as completed: %s", err.Error())
2024-03-16 18:51:13 -07:00
}
}
response, err := json.Marshal(SavedataClearResponse{Success: newCompletion})
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return
}
2024-03-22 07:18:53 -07:00
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
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) {
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
return
2024-03-16 18:51:13 -07:00
}
w.Write(response)
}