2024-04-29 14:26:46 -07:00
|
|
|
/*
|
|
|
|
Copyright (C) 2024 Pagefault Games
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2024-04-29 12:32:58 -07:00
|
|
|
|
2024-03-16 18:51:13 -07:00
|
|
|
package db
|
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2024-05-07 17:38:12 -07:00
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
"github.com/pagefaultgames/rogueserver/defs"
|
|
|
|
)
|
|
|
|
|
2024-05-10 15:07:14 -07:00
|
|
|
func TryAddSeedCompletion(uuid []byte, seed string, mode int) (bool, error) {
|
2024-03-16 18:51:13 -07:00
|
|
|
var count int
|
2024-04-25 00:40:22 -07:00
|
|
|
err := handle.QueryRow("SELECT COUNT(*) FROM dailyRunCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
|
2024-03-16 18:51:13 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if count > 0 {
|
2024-03-17 17:48:49 -07:00
|
|
|
return false, nil
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-04-25 00:40:22 -07:00
|
|
|
_, err = handle.Exec("INSERT INTO dailyRunCompletions (uuid, seed, mode, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, seed, mode)
|
2024-03-16 18:51:13 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2024-03-17 17:48:49 -07:00
|
|
|
return true, nil
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-10 15:07:14 -07:00
|
|
|
func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
|
|
|
|
var count int
|
|
|
|
err := handle.QueryRow("SELECT COUNT(*) FROM dailyRunCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return count > 0, nil
|
|
|
|
}
|
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
2024-05-07 17:38:12 -07:00
|
|
|
var system defs.SystemSaveData
|
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
var data []byte
|
|
|
|
err := handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&system)
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-07 17:38:12 -07:00
|
|
|
return system, nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := gob.NewEncoder(&buf).Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-08 14:23:07 -07:00
|
|
|
_, err = handle.Exec("INSERT INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, buf.Bytes(), buf.Bytes())
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-07 17:38:12 -07:00
|
|
|
return nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteSystemSaveData(uuid []byte) error {
|
|
|
|
_, err := handle.Exec("DELETE FROM systemSaveData WHERE uuid = ?", uuid)
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) {
|
2024-05-07 17:38:12 -07:00
|
|
|
var session defs.SessionSaveData
|
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
var data []byte
|
|
|
|
err := handle.QueryRow("SELECT data FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot).Scan(&data)
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-07 17:38:12 -07:00
|
|
|
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&session)
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-07 17:38:12 -07:00
|
|
|
return session, nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|
|
|
|
|
2024-05-07 16:14:07 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-07 14:10:33 -07:00
|
|
|
func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := gob.NewEncoder(&buf).Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-08 14:23:07 -07:00
|
|
|
_, err = handle.Exec("INSERT INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, slot, buf.Bytes(), buf.Bytes())
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-07 14:10:33 -07:00
|
|
|
|
2024-05-07 17:38:12 -07:00
|
|
|
return nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteSessionSaveData(uuid []byte, slot int) error {
|
|
|
|
_, err := handle.Exec("DELETE FROM sessionSaveData WHERE uuid = ? AND slot = ?", uuid, slot)
|
2024-05-07 17:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-05-07 14:10:33 -07:00
|
|
|
}
|