continue on existing save (#3)

* long blob

* fix continue

* one slot only?

* fallback if there is no slot data yet

* Revert "one slot only?"

This reverts commit 20997e9cd8.
pull/4/head
Up 8 months ago committed by GitHub
parent 1f95f7c042
commit 0d6539a87b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -18,11 +18,7 @@
package account package account
import ( import (
"fmt" "github.com/pagefaultgames/rogueserver/db"
"os"
"strconv"
"time"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
) )
@ -33,24 +29,16 @@ type InfoResponse struct {
// /account/info - get account info // /account/info - get account info
func Info(username string, uuid []byte) (InfoResponse, error) { func Info(username string, uuid []byte) (InfoResponse, error) {
var latestSave time.Time response := InfoResponse{Username: username, LastSessionSlot: -1}
latestSaveID := -1
for id := range defs.SessionSlotCount {
fileName := "session"
if id != 0 {
fileName += strconv.Itoa(id)
}
stat, err := os.Stat(fmt.Sprintf("userdata/%x/%s.pzs", uuid, fileName)) slot, err := db.GetLatestSessionSaveDataSlot(uuid)
if err != nil { if err != nil {
continue response.LastSessionSlot = -1
} }
if stat.ModTime().After(latestSave) { if slot >= defs.SessionSlotCount {
latestSave = stat.ModTime() response.LastSessionSlot = -1
latestSaveID = id
}
} }
return InfoResponse{Username: username, LastSessionSlot: latestSaveID}, nil return response, nil
} }

@ -42,8 +42,8 @@ func Init(username, password, protocol, address, database string) error {
if err != nil { if err != nil {
panic(err) panic(err)
} }
tx.Exec("CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data BLOB, timestamp TIMESTAMP)") tx.Exec("CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP)")
tx.Exec("CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data BLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot))") tx.Exec("CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data LONGBLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot))")
err = tx.Commit() err = tx.Commit()
if err != nil { if err != nil {
panic(err) panic(err)

@ -79,6 +79,16 @@ func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) {
return save, err return save, err
} }
func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) {
var slot int
err := handle.QueryRow("SELECT slot FROM sessionSaveData WHERE uuid = ? ORDER BY timestamp DESC, slot ASC LIMIT 1", uuid).Scan(&slot)
if err != nil {
return -1, err
}
return slot, nil
}
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

Loading…
Cancel
Save