2023-12-05 10:28:08 -08:00
|
|
|
package api
|
|
|
|
|
2023-12-31 13:12:20 -08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2024-03-15 13:38:32 -07:00
|
|
|
"log"
|
2023-12-31 13:12:20 -08:00
|
|
|
"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-08 17:44:36 -07:00
|
|
|
func handleSavedataGet(uuid []byte, datatype, slot int) (any, error) {
|
|
|
|
switch 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-08 17:44:36 -07:00
|
|
|
return nil, err
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2023-12-05 10:28:08 -08:00
|
|
|
|
2024-04-09 19:22:00 -07:00
|
|
|
compensations, err := db.FetchAndClaimAccountCompensations(uuid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch compensations: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range compensations {
|
|
|
|
typeKey := strconv.Itoa(k)
|
|
|
|
system.VoucherCounts[typeKey] += v
|
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
return system, nil
|
|
|
|
case 1: // Session
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
return nil, fmt.Errorf("slot id %d out of range", slot)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
session, err := readSessionSaveData(uuid, slot)
|
2024-03-14 18:44:39 -07:00
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return nil, err
|
2024-03-14 18:44:39 -07:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
return session, nil
|
2023-12-31 13:12:20 -08:00
|
|
|
default:
|
2024-04-08 17:44:36 -07:00
|
|
|
return nil, fmt.Errorf("invalid data type")
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2023-12-05 10:28:08 -08:00
|
|
|
}
|
|
|
|
|
2023-12-29 12:15:16 -08:00
|
|
|
// /savedata/update - update save data
|
2024-04-08 17:44:36 -07:00
|
|
|
func handleSavedataUpdate(uuid []byte, slot int, save any) error {
|
|
|
|
err := db.UpdateAccountLastActivity(uuid)
|
2024-03-15 13:38:32 -07:00
|
|
|
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
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
switch save := save.(type) {
|
|
|
|
case defs.SystemSaveData: // System
|
2024-04-08 22:28:56 -07:00
|
|
|
if save.TrainerId == 0 && save.SecretId == 0 {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("invalid system data")
|
2024-02-14 14:12:10 -08:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
err = db.UpdateAccountStats(uuid, save.GameStats)
|
2024-04-06 14:43:11 -07:00
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to update account stats: %s", err)
|
2024-04-06 14:43:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-31 13:12:20 -08:00
|
|
|
var gobBuffer bytes.Buffer
|
2024-04-08 17:44:36 -07:00
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(save)
|
2023-12-31 13:12:20 -08:00
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to serialize save: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
zstdWriter, err := zstd.NewWriter(nil)
|
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to create zstd writer, %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to create userdata folder: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to write save file: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
case defs.SessionSaveData: // Session
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
return fmt.Errorf("slot id %d out of range", slot)
|
2024-03-14 18:44:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fileName := "session"
|
2024-04-08 17:44:36 -07:00
|
|
|
if slot != 0 {
|
|
|
|
fileName += strconv.Itoa(slot)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var gobBuffer bytes.Buffer
|
2024-04-08 17:44:36 -07:00
|
|
|
err = gob.NewEncoder(&gobBuffer).Encode(save)
|
2023-12-31 13:12:20 -08:00
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to serialize save: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
zstdWriter, err := zstd.NewWriter(nil)
|
|
|
|
if err != nil {
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to create zstd writer, %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to write save file: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2024-04-09 19:22:00 -07:00
|
|
|
|
|
|
|
db.DeleteClaimedAccountCompensations(uuid)
|
2023-12-31 13:12:20 -08:00
|
|
|
default:
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("invalid data type")
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
return nil
|
2023-12-05 10:28:08 -08:00
|
|
|
}
|
|
|
|
|
2023-12-31 13:20:45 -08:00
|
|
|
// /savedata/delete - delete save data
|
2024-04-08 17:44:36 -07:00
|
|
|
func handleSavedataDelete(uuid []byte, datatype, slot int) error {
|
|
|
|
err := db.UpdateAccountLastActivity(uuid)
|
2024-03-15 13:38:32 -07:00
|
|
|
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
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
switch 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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to delete save file: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
case 1: // Session
|
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
return fmt.Errorf("slot id %d out of range", slot)
|
2024-03-14 18:44:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fileName := "session"
|
2024-04-08 17:44:36 -07:00
|
|
|
if slot != 0 {
|
|
|
|
fileName += strconv.Itoa(slot)
|
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-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("failed to delete save file: %s", err)
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
|
|
|
default:
|
2024-04-08 17:44:36 -07:00
|
|
|
return fmt.Errorf("invalid data type")
|
2023-12-31 13:12:20 -08:00
|
|
|
}
|
2023-12-05 10:28:08 -08:00
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
return nil
|
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-08 17:44:36 -07:00
|
|
|
func handleSavedataClear(uuid []byte, slot int, save defs.SessionSaveData) (SavedataClearResponse, error) {
|
|
|
|
err := db.UpdateAccountLastActivity(uuid)
|
2024-03-16 18:51:13 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Print("failed to update account last activity")
|
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
if slot < 0 || slot >= sessionSlotCount {
|
|
|
|
return SavedataClearResponse{}, fmt.Errorf("slot id %d out of range", slot)
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
sessionCompleted := validateSessionCompleted(save)
|
2024-03-16 18:51:13 -07:00
|
|
|
newCompletion := false
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
if save.GameMode == 3 && save.Seed == dailyRunSeed {
|
|
|
|
waveCompleted := save.WaveIndex
|
2024-03-18 16:55:02 -07:00
|
|
|
if !sessionCompleted {
|
2024-03-17 17:48:49 -07:00
|
|
|
waveCompleted--
|
|
|
|
}
|
2024-04-08 17:44:36 -07:00
|
|
|
err = db.AddOrUpdateAccountDailyRun(uuid, save.Score, waveCompleted)
|
2024-03-17 17:48:49 -07:00
|
|
|
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-17 17:48:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 18:51:13 -07:00
|
|
|
if sessionCompleted {
|
2024-04-08 17:44:36 -07:00
|
|
|
newCompletion, err = db.TryAddSeedCompletion(uuid, save.Seed, int(save.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 07:18:53 -07:00
|
|
|
fileName := "session"
|
2024-04-08 17:44:36 -07:00
|
|
|
if slot != 0 {
|
|
|
|
fileName += strconv.Itoa(slot)
|
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-08 17:44:36 -07:00
|
|
|
return SavedataClearResponse{}, fmt.Errorf("failed to delete save file: %s", err)
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-04-08 17:44:36 -07:00
|
|
|
return SavedataClearResponse{Success: newCompletion}, nil
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|