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