You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rogueserver/api/savedata.go

215 lines
5.4 KiB
Go

11 months ago
package api
11 months ago
import (
"encoding/gob"
"encoding/hex"
"fmt"
"log"
11 months ago
"os"
"strconv"
11 months ago
"github.com/Flashfyre/pokerogue-server/db"
"github.com/Flashfyre/pokerogue-server/defs"
11 months ago
"github.com/klauspost/compress/zstd"
)
11 months ago
const sessionSlotCount = 3
11 months ago
// /savedata/get - get save data
func handleSavedataGet(uuid []byte, datatype, slot int) (any, error) {
switch datatype {
case 0: // System
7 months ago
system, err := readSystemSaveData(uuid)
11 months ago
if err != nil {
return nil, err
11 months ago
}
11 months ago
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
}
return system, nil
case 1: // Session
if slot < 0 || slot >= sessionSlotCount {
return nil, fmt.Errorf("slot id %d out of range", slot)
11 months ago
}
session, err := readSessionSaveData(uuid, slot)
if err != nil {
return nil, err
}
return session, nil
11 months ago
default:
return nil, fmt.Errorf("invalid data type")
11 months ago
}
11 months ago
}
// /savedata/update - update save data
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)
11 months ago
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, save.GameStats)
if err != nil {
return fmt.Errorf("failed to update account stats: %s", err)
}
err = os.MkdirAll("userdata/"+hexUUID, 0755)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create userdata folder: %s", err)
11 months ago
}
file, err := os.OpenFile("userdata/"+hexUUID+"/system.pzs", os.O_WRONLY | os.O_TRUNC, 0644)
11 months ago
if err != nil {
return fmt.Errorf("failed to open save file for writing: %s", err)
11 months ago
}
zstdEncoder, err := zstd.NewWriter(file)
if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err)
11 months ago
}
err = gob.NewEncoder(zstdEncoder).Encode(save)
11 months ago
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
11 months ago
}
case defs.SessionSaveData: // Session
if slot < 0 || slot >= sessionSlotCount {
return fmt.Errorf("slot id %d out of range", slot)
}
fileName := "session"
if slot != 0 {
fileName += strconv.Itoa(slot)
11 months ago
}
err = os.MkdirAll("userdata/"+hexUUID, 0755)
if err != nil && !os.IsExist(err) {
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
11 months ago
}
file, err := os.OpenFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName), os.O_WRONLY | os.O_TRUNC, 0644)
11 months ago
if err != nil {
return fmt.Errorf("failed to open save file for writing: %s", err)
11 months ago
}
zstdEncoder, err := zstd.NewWriter(file)
if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err)
11 months ago
}
err = gob.NewEncoder(zstdEncoder).Encode(save)
11 months ago
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
11 months ago
}
db.DeleteClaimedAccountCompensations(uuid)
11 months ago
default:
return fmt.Errorf("invalid data type")
11 months ago
}
return nil
11 months ago
}
11 months ago
// /savedata/delete - delete save data
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)
11 months ago
switch datatype {
case 0: // System
err := os.Remove("userdata/" + hexUUID + "/system.pzs")
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete save file: %s", err)
11 months ago
}
case 1: // Session
if slot < 0 || slot >= sessionSlotCount {
return fmt.Errorf("slot id %d out of range", slot)
}
fileName := "session"
if slot != 0 {
fileName += strconv.Itoa(slot)
}
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUUID, fileName))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete save file: %s", err)
11 months ago
}
default:
return fmt.Errorf("invalid data type")
11 months ago
}
11 months ago
return nil
11 months ago
}
type SavedataClearResponse struct {
Success bool `json:"success"`
}
// /savedata/clear - mark session save data as cleared and delete
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")
}
if slot < 0 || slot >= sessionSlotCount {
return SavedataClearResponse{}, fmt.Errorf("slot id %d out of range", slot)
}
sessionCompleted := validateSessionCompleted(save)
newCompletion := false
if save.GameMode == 3 && save.Seed == dailyRunSeed {
waveCompleted := save.WaveIndex
if !sessionCompleted {
waveCompleted--
}
err = db.AddOrUpdateAccountDailyRun(uuid, save.Score, waveCompleted)
if err != nil {
7 months ago
log.Printf("failed to add or update daily run record: %s", err)
}
}
if sessionCompleted {
newCompletion, err = db.TryAddSeedCompletion(uuid, save.Seed, int(save.GameMode))
if err != nil {
7 months ago
log.Printf("failed to mark seed as completed: %s", err)
}
}
fileName := "session"
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) {
return SavedataClearResponse{}, fmt.Errorf("failed to delete save file: %s", err)
}
return SavedataClearResponse{Success: newCompletion}, nil
}