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.
79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
7 months ago
|
package savedata
|
||
8 months ago
|
|
||
|
import (
|
||
|
"encoding/gob"
|
||
|
"encoding/hex"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/klauspost/compress/zstd"
|
||
7 months ago
|
"github.com/pagefaultgames/pokerogue-server/defs"
|
||
8 months ago
|
)
|
||
|
|
||
8 months ago
|
func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
||
8 months ago
|
var system defs.SystemSaveData
|
||
8 months ago
|
|
||
8 months ago
|
file, err := os.Open("userdata/" + hex.EncodeToString(uuid) + "/system.pzs")
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
return system, fmt.Errorf("failed to open save file for reading: %s", err)
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
defer file.Close()
|
||
|
|
||
8 months ago
|
zstdDecoder, err := zstd.NewReader(file)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
return system, fmt.Errorf("failed to create zstd decoder: %s", err)
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
defer zstdDecoder.Close()
|
||
|
|
||
8 months ago
|
err = gob.NewDecoder(zstdDecoder).Decode(&system)
|
||
8 months ago
|
if err != nil {
|
||
|
return system, fmt.Errorf("failed to deserialize save: %s", err)
|
||
|
}
|
||
|
|
||
|
return system, nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func readSessionSaveData(uuid []byte, slotID int) (defs.SessionSaveData, error) {
|
||
8 months ago
|
var session defs.SessionSaveData
|
||
8 months ago
|
|
||
|
fileName := "session"
|
||
8 months ago
|
if slotID != 0 {
|
||
|
fileName += strconv.Itoa(slotID)
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
file, err := os.Open(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
return session, fmt.Errorf("failed to open save file for reading: %s", err)
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
defer file.Close()
|
||
|
|
||
8 months ago
|
zstdDecoder, err := zstd.NewReader(file)
|
||
8 months ago
|
if err != nil {
|
||
8 months ago
|
return session, fmt.Errorf("failed to create zstd decoder: %s", err)
|
||
8 months ago
|
}
|
||
|
|
||
8 months ago
|
defer zstdDecoder.Close()
|
||
|
|
||
8 months ago
|
err = gob.NewDecoder(zstdDecoder).Decode(&session)
|
||
8 months ago
|
if err != nil {
|
||
|
return session, fmt.Errorf("failed to deserialize save: %s", err)
|
||
|
}
|
||
|
|
||
|
return session, nil
|
||
|
}
|
||
|
|
||
8 months ago
|
func validateSessionCompleted(session defs.SessionSaveData) bool {
|
||
8 months ago
|
switch session.GameMode {
|
||
|
case 0:
|
||
8 months ago
|
return session.BattleType == 2 && session.WaveIndex == 200
|
||
8 months ago
|
case 3:
|
||
8 months ago
|
return session.BattleType == 2 && session.WaveIndex == 50
|
||
8 months ago
|
}
|
||
8 months ago
|
|
||
8 months ago
|
return false
|
||
|
}
|