From aa2f02d2757ebb94f1e00eb70f237afb0bbb2c3f Mon Sep 17 00:00:00 2001 From: Frederico Santos Date: Tue, 15 Oct 2024 22:44:02 +0100 Subject: [PATCH 1/4] feat: Implement S3 integration for system save data migration and retrieval --- api/daily/common.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/daily/common.go b/api/daily/common.go index 84b8167..056aeb3 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -40,8 +40,9 @@ import ( const secondsPerDay = 60 * 60 * 24 var ( - scheduler = cron.New(cron.WithLocation(time.UTC)) - secret []byte + scheduler = cron.New(cron.WithLocation(time.UTC)) + s3scheduler = cron.New(cron.WithLocation(time.UTC)) + secret []byte ) func Init() error { From d5c900a7cc2a6631628c1c1ce4a27013f5c46044 Mon Sep 17 00:00:00 2001 From: Frederico Santos Date: Thu, 17 Oct 2024 00:51:26 +0100 Subject: [PATCH 2/4] fix: Clean up S3 migration code by removing unnecessary blank lines and improving logging messages --- api/daily/common.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/daily/common.go b/api/daily/common.go index 056aeb3..637d134 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -159,6 +159,7 @@ func S3SaveMigration() error { Key: aws.String(username), Body: bytes.NewReader(json), }) + if err != nil { log.Printf("error while saving data in S3 for user %s: %s", username, err) continue From 654a70c867da642a25adb6e73dd75c13c33d48b1 Mon Sep 17 00:00:00 2001 From: frutescens Date: Sun, 10 Nov 2024 19:24:32 -0800 Subject: [PATCH 3/4] 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 { From 923e63e98aa4e966ba286e9f36fba1ba63bc1526 Mon Sep 17 00:00:00 2001 From: frutescens Date: Sun, 10 Nov 2024 19:46:08 -0800 Subject: [PATCH 4/4] Updated instructions. --- README.md | 71 ++++++++++++++++++++++++++++++++++--------------------- beta.env | 1 + 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2040c8e..6236197 100644 --- a/README.md +++ b/README.md @@ -11,41 +11,55 @@ There is a sample docker-compose file for setting up a docker container to setup - npm: [how to install](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) ## Installation: -The docker compose file should automatically implement a container with mariadb with an empty database and the default user and password combo of pokerogue:pokerogue - -### src/utils.ts:224-225 (in pokerogue) -Replace both URLs (one on each line) with the local API server address from rogueserver.go (0.0.0.0:8001) (or whatever port you picked) - -# If you are on Windows - -Now that all of the files are configured: start up powershell as administrator: +### First Steps +- Edit beta.env + - Setting VITE_BYPASS_LOGIN to 0 helps provide access to PokeRogue's accounts features + - If testing locally without an S3 instance, set local to true. + - gameurl should reference the IP of your development machine + - callbackurl should reference the IP of your server +- Edit docker-compose.Example.yml + - Under services->server, add the following lines right above image: rogueserver:latest + ``` + env_file: + - beta.env + ``` + - Under services->db, add the port the database will use. + ``` + ports: + - "3306:3306" + ``` +### Booting up Rogueserver +- First, compile the code with ``` -cd C:\api\server\location\ go build . -.\rogueserver.exe --debug --dbuser yourusername --dbpass yourpassword ``` -The other available flags are located in rogueserver.go:34-43. - -Then in another run this the first time then run `npm run start` from the rogueserver location from then on: +- Then run the command ``` -powershell -ep bypass -cd C:\server\location\ -npm install -npm run start +docker build ./ -t rogueserver ``` -You will need to allow the port youre running the API (8001) on and port 8000 to accept inbound connections through the [Windows Advanced Firewall](https://www.youtube.com/watch?v=9llH5_CON-Y). - -# If you are on Linux -In whatever shell you prefer, run the following: +- Finally, run the command with the Docker file you just configured! ``` -cd /api/server/location/ -go build . -./rogueserver --debug --dbuser yourusername --dbpass yourpassword & - -cd /server/location/ -npm run start +docker-compose -f docker-compose.Example.yml up -d ``` +### Connecting your PokeRogue to RogueServer +- Find .env.development in your PokeRogue repo and update it with the following changes + - To access PokeRogue's account features, you need to set VITE_BYPASS_LOGIN to 0 here too + - Update VITE_SERVER_URL with the correct machine if your server is running on a different machine than your development machine. +- In utils.ts, around lines 280-300, remove the Secure headers from the document.cookie variables. For example: +``` +document.cookie = `${cName}=${cValue};Secure;SameSite=Strict;Domain=${window.location.hostname};Path=/;Expires=${expiration.toUTCString()}`; +``` +should be changed to +``` +document.cookie = `${cName}=${cValue};SameSite=Strict;Domain=${window.location.hostname};Path=/;Expires=${expiration.toUTCString()}`; +``` + +The docker compose file should automatically implement a container with mariadb with an empty database and the default user and password combo of pokerogue:pokerogue +# If you are on Windows +You will need to allow the port youre running the API (8001) on and port 8000 to accept inbound connections through the [Windows Advanced Firewall](https://www.youtube.com/watch?v=9llH5_CON-Y). + +# If you are on Linux If you have a firewall running such as ufw on your linux machine, make sure to allow inbound connections on the ports youre running the API and the pokerogue server (8000,8001). An example to allow incoming connections using UFW: ``` @@ -80,4 +94,7 @@ Make sure that both 8000 and 8001 are portforwarded on your router. Test that the server's game and game authentication works from other machines both in and outside of the network. Once this is complete, enjoy! +### Contributors +- Instructions by Opaquer + diff --git a/beta.env b/beta.env index d472e37..b76d3c6 100644 --- a/beta.env +++ b/beta.env @@ -5,6 +5,7 @@ VITE_DISCORD_CLIENT_ID=1248062921129459756 VITE_GOOGLE_CLIENT_ID=955345393540-2k6lfftf0fdnb0krqmpthjnqavfvvf73.apps.googleusercontent.com VITE_I18N_DEBUG=1 debug=true +local=false dbaddr=db dbuser=pokerogue dbpass=pokerogue