Clean up savedata.go

pull/6/head
maru 7 months ago
parent 405a578e8c
commit 6cb179b553
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -20,6 +20,7 @@ package db
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
) )
@ -41,42 +42,61 @@ func TryAddDailyRunCompletion(uuid []byte, seed string, mode int) (bool, error)
} }
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
var system defs.SystemSaveData
var data []byte var data []byte
err := handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data) err := handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
if err != nil {
return system, err
}
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&system)
if err != nil {
return system, err
}
reader := bytes.NewReader(data) return system, nil
system := defs.SystemSaveData{}
err = gob.NewDecoder(reader).Decode(&system)
return system, err
} }
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error { func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
var buf bytes.Buffer var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(data) err := gob.NewEncoder(&buf).Encode(data)
if err != nil { if err != nil {
return err return err
} }
_, err = handle.Exec("INSERT INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = VALUES(data), timestamp = VALUES(timestamp)", uuid, buf.Bytes()) _, err = handle.Exec("REPLACE INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP())", uuid, buf.Bytes())
if err != nil {
return err
}
return err return nil
} }
func DeleteSystemSaveData(uuid []byte) error { func DeleteSystemSaveData(uuid []byte) error {
_, err := handle.Exec("DELETE FROM systemSaveData WHERE uuid = ?", uuid) _, err := handle.Exec("DELETE FROM systemSaveData WHERE uuid = ?", uuid)
return err if err != nil {
return err
}
return nil
} }
func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) { func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) {
var session defs.SessionSaveData
var data []byte var data []byte
err := handle.QueryRow("SELECT data FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot).Scan(&data) err := handle.QueryRow("SELECT data FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot).Scan(&data)
if err != nil {
return session, err
}
reader := bytes.NewReader(data) err = gob.NewDecoder(bytes.NewReader(data)).Decode(&session)
save := defs.SessionSaveData{} if err != nil {
err = gob.NewDecoder(reader).Decode(&save) return session, err
}
return save, err return session, nil
} }
func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) { func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) {
@ -90,7 +110,6 @@ func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) {
} }
func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error { func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error {
var buf bytes.Buffer var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(data) err := gob.NewEncoder(&buf).Encode(data)
if err != nil { if err != nil {
@ -98,11 +117,18 @@ func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) erro
} }
_, err = handle.Exec("REPLACE INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, slot, buf.Bytes()) _, err = handle.Exec("REPLACE INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, slot, buf.Bytes())
if err != nil {
return err
}
return err return nil
} }
func DeleteSessionSaveData(uuid []byte, slot int) error { func DeleteSessionSaveData(uuid []byte, slot int) error {
_, err := handle.Exec("DELETE FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot) _, err := handle.Exec("DELETE FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot)
return err if err != nil {
return err
}
return nil
} }

Loading…
Cancel
Save