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() scheduler.Start()
if os.Getenv("AWS_ENDPOINT_URL_S3") != "" { if os.Getenv("AWS_ENDPOINT_URL_S3") != "" && !db.isLocalInstance() {
go func() { go func() {
for { for {
err = S3SaveMigration() err = S3SaveMigration()
@ -139,7 +139,7 @@ func S3SaveMigration() error {
} }
for _, user := range accounts { for _, user := range accounts {
data, err := db.ReadSystemSaveData(user) data, err := db.ReadSystemSaveDataS3(user)
if err != nil { if err != nil {
continue 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 err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound) http.Error(w, err.Error(), http.StatusNotFound)
} else { } else {
httpError(w, r, fmt.Errorf("failed to get system save data: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to get system save data: %s", err), http.StatusInternalServerError)
} }
return return
} }

@ -24,15 +24,6 @@ import (
"github.com/pagefaultgames/rogueserver/defs" "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 { func UpdateSystem(uuid []byte, data defs.SystemSaveData) error {
if data.TrainerId == 0 && data.SecretId == 0 { if data.TrainerId == 0 && data.SecretId == 0 {
return fmt.Errorf("invalid system data") return fmt.Errorf("invalid system data")

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

@ -23,6 +23,7 @@ import (
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"os" "os"
"strconv"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
@ -59,7 +60,49 @@ func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
return count > 0, nil return count > 0, nil
} }
func isLocalInstance() bool {
isLocal, _ := strconv.ParseBool(os.Getenv("debug"))
return isLocal
}
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { 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 // get and return save from S3
system, err := GetSystemSaveFromS3(uuid) system, err := GetSystemSaveFromS3(uuid)
if err == nil { if err == nil {

Loading…
Cancel
Save