Reverted some changes and added new methods for local servers

pull/61/head
frutescens 2 weeks ago
parent d5c900a7cc
commit 654a70c867

@ -91,7 +91,7 @@ func Init() error {
scheduler.Start()
if os.Getenv("AWS_ENDPOINT_URL_S3") != "" {
if os.Getenv("AWS_ENDPOINT_URL_S3") != "" && !db.isLocalInstance() {
go func() {
for {
err = S3SaveMigration()
@ -139,7 +139,7 @@ func S3SaveMigration() error {
}
for _, user := range accounts {
data, err := db.ReadSystemSaveData(user)
data, err := db.ReadSystemSaveDataS3(user)
if err != nil {
continue
}

@ -415,14 +415,13 @@ func handleSystem(w http.ResponseWriter, r *http.Request) {
}
}
save, err := savedata.GetSystem(uuid)
save, err := db.ReadSystemSaveData(uuid)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
httpError(w, r, fmt.Errorf("failed to get system save data: %s", err), http.StatusInternalServerError)
}
return
}

@ -24,15 +24,6 @@ import (
"github.com/pagefaultgames/rogueserver/defs"
)
func GetSystem(uuid []byte) (defs.SystemSaveData, error) {
system, err := db.ReadSystemSaveData(uuid)
if err != nil {
return system, err
}
return system, nil
}
func UpdateSystem(uuid []byte, data defs.SystemSaveData) error {
if data.TrainerId == 0 && data.SecretId == 0 {
return fmt.Errorf("invalid system data")

@ -21,6 +21,8 @@ import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
_ "github.com/go-sql-driver/mysql"
)
@ -44,8 +46,8 @@ func Init(username, password, protocol, address, database string) error {
if err != nil {
log.Fatal(err)
}
err = setupDb(tx)
local, _ := strconv.ParseBool(os.Getenv("debug"))
err = setupDb(tx, local)
if err != nil {
tx.Rollback()
log.Fatal(err)
@ -59,7 +61,7 @@ func Init(username, password, protocol, address, database string) error {
return nil
}
func setupDb(tx *sql.Tx) error {
func setupDb(tx *sql.Tx, local bool) error {
queries := []string{
// MIGRATION 000
@ -108,13 +110,11 @@ func setupDb(tx *sql.Tx) error {
// MIGRATION 004
`ALTER TABLE accounts ADD COLUMN IF NOT EXISTS isInLocalDb TINYINT(1) NOT NULL DEFAULT 1`,
// ----------------------------------
}
if !local {
// MIGRATION 005
`ALTER TABLE accounts DROP COLUMN IF EXISTS isInLocalDb`,
queries = append(queries, `ALTER TABLE accounts DROP COLUMN IF EXISTS isInLocalDb`)
}
for _, q := range queries {
_, err := tx.Exec(q)
if err != nil {

@ -23,6 +23,7 @@ import (
"encoding/gob"
"encoding/json"
"os"
"strconv"
"github.com/klauspost/compress/zstd"
"github.com/pagefaultgames/rogueserver/defs"
@ -59,7 +60,49 @@ func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
return count > 0, nil
}
func isLocalInstance() bool {
isLocal, _ := strconv.ParseBool(os.Getenv("debug"))
return isLocal
}
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
var system defs.SystemSaveData
var err error
if isLocalInstance() {
system, err = ReadSystemSaveDataLocal(uuid)
} else {
system, err = ReadSystemSaveDataS3(uuid)
}
if err != nil {
return system, err
}
return system, nil
}
func ReadSystemSaveDataLocal(uuid []byte) (defs.SystemSaveData, error) {
var system defs.SystemSaveData
_, err := isSaveInLocalDb(uuid)
if err != nil {
return system, err
}
var data []byte
err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
if err != nil {
return system, err
}
return system, nil
}
func isSaveInLocalDb(uuid []byte) (bool, error) {
var isLocal bool
err := handle.QueryRow("SELECT isInLocalDb FROM accounts WHERE uuid = ?", uuid).Scan(&isLocal)
if err != nil {
return false, err
}
return isLocal, nil
}
func ReadSystemSaveDataS3(uuid []byte) (defs.SystemSaveData, error) {
// get and return save from S3
system, err := GetSystemSaveFromS3(uuid)
if err == nil {

Loading…
Cancel
Save