From 654a70c867da642a25adb6e73dd75c13c33d48b1 Mon Sep 17 00:00:00 2001 From: frutescens Date: Sun, 10 Nov 2024 19:24:32 -0800 Subject: [PATCH] Reverted some changes and added new methods for local servers --- api/daily/common.go | 4 ++-- api/endpoints.go | 3 +-- api/savedata/system.go | 9 --------- db/db.go | 16 ++++++++-------- db/savedata.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/api/daily/common.go b/api/daily/common.go index 637d134..5190540 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -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 } diff --git a/api/endpoints.go b/api/endpoints.go index 478b434..98045a0 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -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 } diff --git a/api/savedata/system.go b/api/savedata/system.go index 6c02f2a..62b2749 100644 --- a/api/savedata/system.go +++ b/api/savedata/system.go @@ -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") diff --git a/db/db.go b/db/db.go index d086abb..a96b860 100644 --- a/db/db.go +++ b/db/db.go @@ -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 { diff --git a/db/savedata.go b/db/savedata.go index 8855c2e..2c9d344 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -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 {