rogueserver/api/savedata-helper.go

86 lines
2.0 KiB
Go
Raw Normal View History

2024-03-16 18:51:13 -07:00
package api
import (
"bytes"
"encoding/gob"
"encoding/hex"
"fmt"
"os"
"strconv"
2024-03-17 10:18:51 -07:00
"github.com/Flashfyre/pokerogue-server/defs"
2024-03-16 18:51:13 -07:00
"github.com/klauspost/compress/zstd"
)
2024-04-01 19:54:55 -07:00
func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
2024-03-17 10:18:51 -07:00
var system defs.SystemSaveData
2024-03-16 18:51:13 -07:00
save, err := os.ReadFile("userdata/" + hex.EncodeToString(uuid) + "/system.pzs")
if err != nil {
return system, fmt.Errorf("failed to read save file: %s", err)
}
zstdReader, err := zstd.NewReader(nil)
if err != nil {
return system, fmt.Errorf("failed to create zstd reader: %s", err)
}
decompressed, err := zstdReader.DecodeAll(save, nil)
if err != nil {
return system, fmt.Errorf("failed to decompress save file: %s", err)
}
gobDecoderBuf := bytes.NewBuffer(decompressed)
err = gob.NewDecoder(gobDecoderBuf).Decode(&system)
if err != nil {
return system, fmt.Errorf("failed to deserialize save: %s", err)
}
return system, nil
}
2024-04-08 15:15:09 -07:00
func readSessionSaveData(uuid []byte, slotID int) (defs.SessionSaveData, error) {
2024-03-17 10:18:51 -07:00
var session defs.SessionSaveData
2024-03-16 18:51:13 -07:00
fileName := "session"
2024-04-08 15:15:09 -07:00
if slotID != 0 {
fileName += strconv.Itoa(slotID)
2024-03-16 18:51:13 -07:00
}
save, err := os.ReadFile(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
if err != nil {
return session, fmt.Errorf("failed to read save file: %s", err)
}
zstdReader, err := zstd.NewReader(nil)
if err != nil {
return session, fmt.Errorf("failed to create zstd reader: %s", err)
}
decompressed, err := zstdReader.DecodeAll(save, nil)
if err != nil {
return session, fmt.Errorf("failed to decompress save file: %s", err)
}
gobDecoderBuf := bytes.NewBuffer(decompressed)
err = gob.NewDecoder(gobDecoderBuf).Decode(&session)
if err != nil {
return session, fmt.Errorf("failed to deserialize save: %s", err)
}
return session, nil
}
2024-04-01 19:54:55 -07:00
func validateSessionCompleted(session defs.SessionSaveData) bool {
2024-03-16 18:51:13 -07:00
switch session.GameMode {
case 0:
2024-03-18 16:55:02 -07:00
return session.BattleType == 2 && session.WaveIndex == 200
2024-03-16 18:51:13 -07:00
case 3:
2024-03-18 16:55:02 -07:00
return session.BattleType == 2 && session.WaveIndex == 50
2024-03-16 18:51:13 -07:00
}
2024-04-01 19:54:55 -07:00
2024-03-16 18:51:13 -07:00
return false
}