rogueserver/api/savedata-helper.go

85 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-03-17 10:18:51 -07:00
func GetSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
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-03-17 10:18:51 -07:00
func GetSessionSaveData(uuid []byte, slotId int) (defs.SessionSaveData, error) {
var session defs.SessionSaveData
2024-03-16 18:51:13 -07:00
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
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-03-17 10:18:51 -07:00
func ValidateSessionCompleted(session defs.SessionSaveData) bool {
2024-03-16 18:51:13 -07:00
switch session.GameMode {
case 0:
return session.WaveIndex == 200
case 3:
return session.WaveIndex == 50
}
return false
}