|
|
|
@ -4,10 +4,8 @@ import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/gob"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
@ -19,228 +17,146 @@ import (
|
|
|
|
|
const sessionSlotCount = 3
|
|
|
|
|
|
|
|
|
|
// /savedata/get - get save data
|
|
|
|
|
func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r.URL.Query().Get("datatype") {
|
|
|
|
|
case "0": // System
|
|
|
|
|
func handleSavedataGet(uuid []byte, datatype, slot int) (any, error) {
|
|
|
|
|
switch datatype {
|
|
|
|
|
case 0: // System
|
|
|
|
|
system, err := readSystemSaveData(uuid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveJson, err := json.Marshal(system)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return system, nil
|
|
|
|
|
case 1: // Session
|
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
|
return nil, fmt.Errorf("slot id %d out of range", slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(saveJson)
|
|
|
|
|
case "1": // Session
|
|
|
|
|
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
|
session, err := readSessionSaveData(uuid, slot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if slotID < 0 || slotID >= sessionSlotCount {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session, err := readSessionSaveData(uuid, slotID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveJson, err := json.Marshal(session)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(saveJson)
|
|
|
|
|
return session, nil
|
|
|
|
|
default:
|
|
|
|
|
httpError(w, r, "invalid data type", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
return nil, fmt.Errorf("invalid data type")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /savedata/update - update save data
|
|
|
|
|
func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = db.UpdateAccountLastActivity(uuid)
|
|
|
|
|
func handleSavedataUpdate(uuid []byte, slot int, save any) error {
|
|
|
|
|
err := db.UpdateAccountLastActivity(uuid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("failed to update account last activity")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hexUUID := hex.EncodeToString(uuid)
|
|
|
|
|
|
|
|
|
|
switch r.URL.Query().Get("datatype") {
|
|
|
|
|
case "0": // System
|
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if system.TrainerID == 0 && system.SecretID == 0 {
|
|
|
|
|
httpError(w, r, "invalid system data", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
switch save := save.(type) {
|
|
|
|
|
case defs.SystemSaveData: // System
|
|
|
|
|
if save.TrainerID == 0 && save.SecretID == 0 {
|
|
|
|
|
return fmt.Errorf("invalid system data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = db.UpdateAccountStats(uuid, system.GameStats)
|
|
|
|
|
err = db.UpdateAccountStats(uuid, save.GameStats)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to update account stats: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var gobBuffer bytes.Buffer
|
|
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(system)
|
|
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(save)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to serialize save: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zstdWriter, err := zstd.NewWriter(nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to create zstd writer, %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll("userdata/"+hexUUID, 0755)
|
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to create userdata folder: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.WriteFile("userdata/"+hexUUID+"/system.pzs", compressed, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to write save file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
case "1": // Session
|
|
|
|
|
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if slotID < 0 || slotID >= sessionSlotCount {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
case defs.SessionSaveData: // Session
|
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
|
return fmt.Errorf("slot id %d out of range", slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := "session"
|
|
|
|
|
if slotID != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slotID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
if slot != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var gobBuffer bytes.Buffer
|
|
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(session)
|
|
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(save)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to serialize save: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zstdWriter, err := zstd.NewWriter(nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to create zstd writer, %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll("userdata/"+hexUUID, 0755)
|
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.WriteFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName), compressed, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to write save file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
httpError(w, r, "invalid data type", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("invalid data type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /savedata/delete - delete save data
|
|
|
|
|
func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
uuid, err := getUUIDFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = db.UpdateAccountLastActivity(uuid)
|
|
|
|
|
func handleSavedataDelete(uuid []byte, datatype, slot int) error {
|
|
|
|
|
err := db.UpdateAccountLastActivity(uuid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("failed to update account last activity")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hexUUID := hex.EncodeToString(uuid)
|
|
|
|
|
|
|
|
|
|
switch r.URL.Query().Get("datatype") {
|
|
|
|
|
case "0": // System
|
|
|
|
|
switch datatype {
|
|
|
|
|
case 0: // System
|
|
|
|
|
err := os.Remove("userdata/" + hexUUID + "/system.pzs")
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to delete save file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
case "1": // Session
|
|
|
|
|
slotID, err := strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if slotID < 0 || slotID >= sessionSlotCount {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
case 1: // Session
|
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
|
return fmt.Errorf("slot id %d out of range", slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := "session"
|
|
|
|
|
if slotID != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slotID)
|
|
|
|
|
if slot != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName))
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("failed to delete save file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
httpError(w, r, "invalid data type", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
return fmt.Errorf("invalid data type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SavedataClearResponse struct {
|
|
|
|
@ -248,73 +164,46 @@ type SavedataClearResponse struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /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 {
|
|
|
|
|
httpError(w, r, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = db.UpdateAccountLastActivity(uuid)
|
|
|
|
|
func handleSavedataClear(uuid []byte, slot int, save defs.SessionSaveData) (SavedataClearResponse, error) {
|
|
|
|
|
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 {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if slotID < 0 || slotID >= sessionSlotCount {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("slot id %d out of range", slotID), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
|
return SavedataClearResponse{}, fmt.Errorf("slot id %d out of range", slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionCompleted := validateSessionCompleted(session)
|
|
|
|
|
sessionCompleted := validateSessionCompleted(save)
|
|
|
|
|
newCompletion := false
|
|
|
|
|
|
|
|
|
|
if session.GameMode == 3 && session.Seed == dailyRunSeed {
|
|
|
|
|
waveCompleted := session.WaveIndex
|
|
|
|
|
if save.GameMode == 3 && save.Seed == dailyRunSeed {
|
|
|
|
|
waveCompleted := save.WaveIndex
|
|
|
|
|
if !sessionCompleted {
|
|
|
|
|
waveCompleted--
|
|
|
|
|
}
|
|
|
|
|
err = db.AddOrUpdateAccountDailyRun(uuid, session.Score, waveCompleted)
|
|
|
|
|
err = db.AddOrUpdateAccountDailyRun(uuid, save.Score, waveCompleted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("failed to add or update daily run record: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sessionCompleted {
|
|
|
|
|
newCompletion, err = db.TryAddSeedCompletion(uuid, session.Seed, int(session.GameMode))
|
|
|
|
|
newCompletion, err = db.TryAddSeedCompletion(uuid, save.Seed, int(save.GameMode))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("failed to mark seed as completed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := json.Marshal(SavedataClearResponse{Success: newCompletion})
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := "session"
|
|
|
|
|
if slotID != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slotID)
|
|
|
|
|
if slot != 0 {
|
|
|
|
|
fileName += strconv.Itoa(slot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return SavedataClearResponse{}, fmt.Errorf("failed to delete save file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
return SavedataClearResponse{Success: newCompletion}, nil
|
|
|
|
|
}
|
|
|
|
|