From 4fb7687522e250e3bfac461738b255246c67e7b6 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Tue, 16 Apr 2024 23:45:41 +0200 Subject: [PATCH 1/9] init: unit tests for `Commander` struct --- .github/workflows/go.yml | 4 +- go.mod | 2 + go.sum | 4 + orm/database.go | 16 ++- tests/commander_test.go | 288 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 311 insertions(+), 3 deletions(-) create mode 100644 tests/commander_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4623382..ac6cf4d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -24,5 +24,5 @@ jobs: - name: Build run: go build ./... - # - name: Test - # run: go test ./... + - name: Test + run: go test ./... diff --git a/go.mod b/go.mod index dc4bf88..d35c3f8 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.52.0 // indirect @@ -42,4 +43,5 @@ require ( golang.org/x/net v0.24.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.19.0 // indirect + gorm.io/driver/sqlite v1.5.5 // indirect ) diff --git a/go.sum b/go.sum index 3ee5339..77ee2af 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -86,5 +88,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA= +gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= +gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8= gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/orm/database.go b/orm/database.go index 7423d82..b2b7cb7 100644 --- a/orm/database.go +++ b/orm/database.go @@ -9,6 +9,7 @@ import ( "github.com/ggmolly/belfast/logger" "google.golang.org/protobuf/proto" "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -21,6 +22,8 @@ const ( RootPacketDir = "protobuf" ) +type openFuncType func(dsn string) gorm.Dialector + func InitDatabase() bool { dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=Europe/Paris", os.Getenv("POSTGRES_HOST"), @@ -30,7 +33,14 @@ func InitDatabase() bool { os.Getenv("POSTGRES_DB"), ) var err error - GormDB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{ + var openFunc openFuncType + if os.Getenv("MODE") == "test" { + openFunc = sqlite.Open + dsn = "file::memory:?cache=shared" + } else { + openFunc = postgres.Open + } + GormDB, err = gorm.Open(openFunc(dsn), &gorm.Config{ PrepareStmt: true, }) if err != nil { @@ -72,6 +82,10 @@ func InitDatabase() bool { if err != nil { panic("failed to migrate database " + err.Error()) } + if os.Getenv("MODE") == "test" { + logger.LogEvent("ORM", "Init", "Skipping database seeding in test mode", logger.LOG_LEVEL_INFO) + return true + } // Pre-populate debug names table, user will be able to rename them later var count int64 GormDB.Model(&DebugName{}).Count(&count) diff --git a/tests/commander_test.go b/tests/commander_test.go new file mode 100644 index 0000000..0641912 --- /dev/null +++ b/tests/commander_test.go @@ -0,0 +1,288 @@ +package tests + +import ( + "os" + "testing" + + "github.com/ggmolly/belfast/orm" +) + +var fakeCommander orm.Commander + +var ( + fakeResources []orm.Resource + fakeItems []orm.Item +) + +func seedDb() { + tx := orm.GormDB.Begin() + for _, r := range fakeResources { + tx.Save(&r) + } + for _, i := range fakeItems { + tx.Save(&i) + } + if err := tx.Commit().Error; err != nil { + panic(err) + } +} + +func init() { + fakeCommander.AccountID = 1 + fakeCommander.CommanderID = 1 + fakeCommander.Name = "Fake Commander" + fakeResources = []orm.Resource{ + {ID: 1, Name: "Gold"}, + {ID: 2, Name: "Fake resource"}, + } + fakeItems = []orm.Item{ + {ID: 20001, Name: "Wisdom Cube"}, + {ID: 45, Name: "Fake Item"}, + {ID: 60, Name: "Fake Item 2"}, + } + + // Init the database + os.Setenv("MODE", "test") + orm.InitDatabase() + seedDb() + + fakeCommander.OwnedResourcesMap = make(map[uint32]*orm.OwnedResource) + fakeCommander.CommanderItemsMap = make(map[uint32]*orm.CommanderItem) + + tx := orm.GormDB.Begin() + // Fake resources + fakeResourcesCnt := []uint32{100, 30} + for i := 0; i < len(fakeResources); i++ { + resource := orm.OwnedResource{ + ResourceID: fakeResources[i].ID, + Amount: fakeResourcesCnt[i], + CommanderID: fakeCommander.CommanderID, + } + tx.Create(&resource) + fakeCommander.OwnedResources = append(fakeCommander.OwnedResources, resource) + fakeCommander.OwnedResourcesMap[fakeResources[i].ID] = &fakeCommander.OwnedResources[i] + } + + // Fake items + fakeItemsCnt := []uint32{5, 50, 3} + + for i := 0; i < len(fakeItems); i++ { + item := orm.CommanderItem{ + ItemID: fakeItems[i].ID, + Count: fakeItemsCnt[i], + CommanderID: fakeCommander.CommanderID, + } + tx.Create(&item) + fakeCommander.Items = append(fakeCommander.Items, item) + fakeCommander.CommanderItemsMap[fakeItems[i].ID] = &fakeCommander.Items[i] + } + + tx.Create(&fakeCommander) + if err := tx.Commit().Error; err != nil { + panic(err) + } +} + +// Tests the behavior of orm.Commander.HasEnoughGold +func TestEnoughGold(t *testing.T) { + if !fakeCommander.HasEnoughGold(100) { + t.Errorf("Expected enough gold, has %d, need %d", fakeCommander.OwnedResourcesMap[1].Amount, 100) + } + if !fakeCommander.HasEnoughGold(50) { + t.Errorf("Expected enough gold, has %d, need %d", fakeCommander.OwnedResourcesMap[1].Amount, 50) + } + if !fakeCommander.HasEnoughGold(0) { + t.Errorf("Expected enough gold, has %d, need %d", fakeCommander.OwnedResourcesMap[1].Amount, 0) + } + if fakeCommander.HasEnoughGold(1000) { + t.Errorf("Expected not enough gold, has %d, need %d", fakeCommander.OwnedResourcesMap[1].Amount, 1000) + } +} + +// Tests the behavior of orm.Commander.HasEnoughCube +func TestEnoughCube(t *testing.T) { + if fakeCommander.HasEnoughCube(10) { + t.Errorf("Expected not enough cube, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 10) + } + if !fakeCommander.HasEnoughCube(5) { + t.Errorf("Expected enough cube, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 5) + } + if !fakeCommander.HasEnoughCube(0) { + t.Errorf("Expected enough cube, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 0) + } + if fakeCommander.HasEnoughCube(1000) { + t.Errorf("Expected not enough cube, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 1000) + } +} + +// Tests the behavior of orm.Commander.HasEnoughResource +func TestEnoughResource(t *testing.T) { + if !fakeCommander.HasEnoughResource(2, 1) { + t.Errorf("Expected enough resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 1) + } + if !fakeCommander.HasEnoughResource(2, 30) { + t.Errorf("Expected enough resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 30) + } + if !fakeCommander.HasEnoughResource(2, 0) { + t.Errorf("Expected enough resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 0) + } + if fakeCommander.HasEnoughResource(2, 1000) { + t.Errorf("Expected not enough resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 1000) + } + if fakeCommander.HasEnoughResource(3, 1) { // Resource not owned + t.Errorf("Expected not enough resource, has -, need %d", 1) + } +} + +// Tests the behavior of orm.Commander.HasEnoughItem +func TestEnoughItem(t *testing.T) { + if !fakeCommander.HasEnoughItem(20001, 5) { + t.Errorf("Expected enough item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 5) + } + if !fakeCommander.HasEnoughItem(20001, 0) { + t.Errorf("Expected enough item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 0) + } + if fakeCommander.HasEnoughItem(20001, 6) { + t.Errorf("Expected not enough item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 6) + } + if fakeCommander.HasEnoughItem(20002, 1) { // Item not owned + t.Errorf("Expected not enough item, has -, need %d", 1) + } +} + +// Tests the behavior of ConsumeItem +func TestConsumeItem(t *testing.T) { + seedDb() + if err := fakeCommander.ConsumeItem(20001, 5); err != nil { + t.Errorf("Expected consume item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 5) + } + if err := fakeCommander.ConsumeItem(20001, 0); err != nil { + t.Errorf("Expected not consume item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 0) + } + if err := fakeCommander.ConsumeItem(20001, 1); err == nil { + t.Errorf("Expected not consume item, has -, need %d", 1) + } + if err := fakeCommander.ConsumeItem(20002, 1); err == nil { + t.Errorf("Expected not consume item, has -, need %d", 1) + } + if err := fakeCommander.ConsumeItem(20001, 400); err == nil { + t.Errorf("Expected not consume item, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 400) + } +} + +// Tests the behavior of ConsumeResource +func TestConsumeResource(t *testing.T) { + seedDb() + if err := fakeCommander.ConsumeResource(2, 5); err != nil { + t.Errorf("Expected consume resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 5) + } + if err := fakeCommander.ConsumeResource(2, 0); err != nil { + t.Errorf("Expected consume resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 0) + } + if err := fakeCommander.ConsumeResource(2, 1); err != nil { + t.Errorf("Expected consume resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 1) + } + if err := fakeCommander.ConsumeResource(3, 1); err == nil { + t.Errorf("Expected not consume resource, has -, need %d", 1) + } + if err := fakeCommander.ConsumeResource(2, 1000); err == nil { + t.Errorf("Expected not consume resource, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 1000) + } +} + +// Tests the behavior of AddItem +func TestAddItem(t *testing.T) { + seedDb() + base := fakeCommander.CommanderItemsMap[20001].Count + if err := fakeCommander.AddItem(20001, 5); err != nil { + t.Errorf("Attempt to add %d items (id: %d) failed", 5, 20001) + } + if fakeCommander.CommanderItemsMap[20001].Count != base+5 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, base+5) + } else { + base += 5 + } + if err := fakeCommander.AddItem(20001, 0); err != nil { + t.Errorf("Attempt to add %d items (id: %d) failed", 0, 20001) + } + if fakeCommander.CommanderItemsMap[20001].Count != base { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, base) + } + if err := fakeCommander.AddItem(20002, 1); err != nil { + t.Errorf("Attempt to add %d items (id: %d) failed", 1, 20002) + } +} + +// Tests the behavior of AddResource +func TestAddResource(t *testing.T) { + seedDb() + base := fakeCommander.OwnedResourcesMap[2].Amount + if err := fakeCommander.AddResource(2, 5); err != nil { + t.Errorf("Attempt to add %d resources (id: %d) failed", 5, 2) + } + if fakeCommander.OwnedResourcesMap[2].Amount != base+5 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, base+5) + } else { + base += 5 + } + if err := fakeCommander.AddResource(2, 0); err != nil { + t.Errorf("Attempt to add %d resources (id: %d) failed", 0, 2) + } + if fakeCommander.OwnedResourcesMap[2].Amount != base { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, base) + } + if err := fakeCommander.AddResource(3, 1); err != nil { + t.Errorf("Attempt to add %d resources (id: %d) failed", 1, 3) + } +} + +// Test set resource +func TestSetResource(t *testing.T) { + seedDb() + if err := fakeCommander.SetResource(2, 10); err != nil { + t.Errorf("Attempt to set resource failed") + } + if fakeCommander.OwnedResourcesMap[2].Amount != 10 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 10) + } + if err := fakeCommander.SetResource(2, 0); err != nil { + t.Errorf("Attempt to set resource failed") + } + if fakeCommander.OwnedResourcesMap[2].Amount != 0 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.OwnedResourcesMap[2].Amount, 0) + } + if err := fakeCommander.SetResource(999, 1); err != nil { + t.Errorf("Attempt to set resource %d failed", 1) + } +} + +// Test set item +func TestSetItem(t *testing.T) { + seedDb() + if err := fakeCommander.SetItem(20001, 10); err != nil { + t.Errorf("Attempt to set item failed") + } + if fakeCommander.CommanderItemsMap[20001].Count != 10 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 10) + } + if err := fakeCommander.SetItem(20001, 0); err != nil { + t.Errorf("Attempt to set item failed") + } + if fakeCommander.CommanderItemsMap[20001].Count != 0 { + t.Errorf("Count mismatch, has %d, need %d", fakeCommander.CommanderItemsMap[20001].Count, 0) + } + if err := fakeCommander.SetItem(20001, 1); err != nil { + t.Errorf("Attempt to set item %d failed", 20001) + } +} + +// Test the behavior of orm.Commander.GetItemCount +func TestGetItemCount(t *testing.T) { + seedDb() + if fakeCommander.GetItemCount(20001) != 1 { + t.Errorf("Count mismatch, has %d, expected %d", fakeCommander.GetItemCount(20001), 1) + } + if fakeCommander.GetItemCount(8546213) != 0 { + t.Errorf("Count mismatch, has %d, expected %d", fakeCommander.GetItemCount(8546213), 0) + } +} From fd92e3f24832e051d84a160142849cff03a320d1 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Tue, 16 Apr 2024 23:47:05 +0200 Subject: [PATCH 2/9] update: allow github actions not only on `main` --- .github/workflows/go.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ac6cf4d..0962a92 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -2,11 +2,9 @@ name: Go on: push: - branches: [ "main" ] paths: - '**.go' pull_request: - branches: [ "main" ] paths: - '**.go' From 5d412f5726ac4d2b5693449d840669ab3def8d66 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Tue, 16 Apr 2024 23:47:10 +0200 Subject: [PATCH 3/9] rm: newline --- tests/commander_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/commander_test.go b/tests/commander_test.go index 0641912..4cfe8f0 100644 --- a/tests/commander_test.go +++ b/tests/commander_test.go @@ -65,7 +65,6 @@ func init() { // Fake items fakeItemsCnt := []uint32{5, 50, 3} - for i := 0; i < len(fakeItems); i++ { item := orm.CommanderItem{ ItemID: fakeItems[i].ID, From b5adb8fe785eae990f97b8c2f1bb57a5bcf8b4f6 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Tue, 16 Apr 2024 23:49:29 +0200 Subject: [PATCH 4/9] update: GitHub Actions workflow to include go.yml file & rename task --- .github/workflows/go.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0962a92..57012f0 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,23 +4,20 @@ on: push: paths: - '**.go' + - '.github/workflows/go.yml' pull_request: paths: - '**.go' + - '.github/workflows/go.yml' jobs: - build: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up Go uses: actions/setup-go@v4 with: go-version: '1.20' - - - name: Build - run: go build ./... - - name: Test run: go test ./... From fa94c173f64d73f10a03f8c1fc0526986d8aba17 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Wed, 17 Apr 2024 18:42:51 +0200 Subject: [PATCH 5/9] add: rarities names & IDs when first-time populating --- orm/database.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/orm/database.go b/orm/database.go index b2b7cb7..cc2f0d0 100644 --- a/orm/database.go +++ b/orm/database.go @@ -167,6 +167,28 @@ func InitDatabase() bool { Port: 80, StateID: proto.Uint32(2), }) + + logger.LogEvent("ORM", "Populating", "Adding rarities...", logger.LOG_LEVEL_INFO) + tx.Save(&Rarity{ + ID: 2, + Name: "Common", + }) + tx.Save(&Rarity{ + ID: 3, + Name: "Rare", + }) + tx.Save(&Rarity{ + ID: 4, + Name: "Elite", + }) + tx.Save(&Rarity{ + ID: 5, + Name: "Super Rare", + }) + tx.Save(&Rarity{ + ID: 6, + Name: "Ultra Rare", + }) tx.Commit() } return count == 0 From d20a4d1189b1cd551c533422e38c698ee57077b6 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Wed, 17 Apr 2024 22:39:13 +0200 Subject: [PATCH 6/9] revert: region IDs --- connection/client.go | 2 -- orm/yostarus.go | 1 - 2 files changed, 3 deletions(-) diff --git a/connection/client.go b/connection/client.go index d075340..d7f4093 100644 --- a/connection/client.go +++ b/connection/client.go @@ -8,7 +8,6 @@ import ( "syscall" "time" - "github.com/ggmolly/belfast/consts" "github.com/ggmolly/belfast/logger" "github.com/ggmolly/belfast/orm" "google.golang.org/protobuf/proto" @@ -40,7 +39,6 @@ func (client *Client) CreateCommander(arg2 uint32) (uint32, error) { if err := orm.GormDB.Create(&orm.YostarusMap{ Arg2: arg2, AccountID: accountId, - Region: consts.REGION_EN, }).Error; err != nil { logger.LogEvent("Client", "CreateCommander", fmt.Sprintf("failed to create account for arg2 %d: %v", arg2, err), logger.LOG_LEVEL_ERROR) return 0, err diff --git a/orm/yostarus.go b/orm/yostarus.go index 2ad6dac..1374ef3 100644 --- a/orm/yostarus.go +++ b/orm/yostarus.go @@ -6,7 +6,6 @@ package orm type YostarusMap struct { Arg2 uint32 `gorm:"primary_key"` AccountID uint32 `gorm:"not_null;uniqueIndex;auto_increment"` - Region uint8 `gorm:"not_null;type:tinyint(2)"` Commander Commander `gorm:"foreignkey:AccountID;references:AccountID"` } From 3905da39cb0e6a9a24e75e6fa9c0427fe8da3c1e Mon Sep 17 00:00:00 2001 From: ggmolly Date: Wed, 17 Apr 2024 22:39:42 +0200 Subject: [PATCH 7/9] add: `--reseed` / `-s` flag to forcefully re-insert game's data --- go.mod | 1 + go.sum | 2 ++ main.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/go.mod b/go.mod index d35c3f8..2b9a77f 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( ) require ( + github.com/akamensky/argparse v1.4.0 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/go.sum b/go.sum index 77ee2af..b7ea83d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= +github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/main.go b/main.go index 5f18b6a..6c2d348 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" + "github.com/akamensky/argparse" "github.com/ggmolly/belfast/answer" "github.com/ggmolly/belfast/connection" "github.com/ggmolly/belfast/logger" @@ -27,6 +28,20 @@ var validRegions = map[string]interface{}{ } func main() { + parser := argparse.NewParser("belfast", "Azur Lane server emulator") + reseed := parser.Flag("s", "reseed", &argparse.Options{ + Required: false, + Help: "Forces the reseed of the database with the latest data", + Default: false, + }) + if err := parser.Parse(os.Args); err != nil { + fmt.Print(parser.Usage(err)) + os.Exit(1) + } + if *reseed { + logger.LogEvent("Reseed", "Forced", "Forcing reseed of the database...", logger.LOG_LEVEL_INFO) + misc.UpdateAllData(os.Getenv("AL_REGION")) + } server := connection.NewServer("0.0.0.0", 80, packets.Dispatch) // wait for SIGINT sigChannel := make(chan os.Signal, 1) From 60fbb4aeb8b96c946d8821821fbb3f543be83d26 Mon Sep 17 00:00:00 2001 From: ggmolly Date: Wed, 17 Apr 2024 22:42:56 +0200 Subject: [PATCH 8/9] rm: `Rarity` preloading --- orm/ship.go | 1 - 1 file changed, 1 deletion(-) diff --git a/orm/ship.go b/orm/ship.go index cab9c64..07c18bf 100644 --- a/orm/ship.go +++ b/orm/ship.go @@ -77,7 +77,6 @@ func GetRandomPoolShip(poolId uint32) (Ship, error) { } var randomShip Ship err := GormDB. - Preload("Rarity"). Where("pool_id = ? AND rarity_id = ?", poolId, rarity). Order("RANDOM()"). First(&randomShip).Error From 6c5a66e82033d3b7f42df641caeef7bcdd79726c Mon Sep 17 00:00:00 2001 From: ggmolly Date: Wed, 17 Apr 2024 22:48:36 +0200 Subject: [PATCH 9/9] add: dummy handler for CS_11019 --- answer/update_common_flag_command.go | 14 ++++++++++++++ main.go | 3 +++ 2 files changed, 17 insertions(+) create mode 100644 answer/update_common_flag_command.go diff --git a/answer/update_common_flag_command.go b/answer/update_common_flag_command.go new file mode 100644 index 0000000..c1ed196 --- /dev/null +++ b/answer/update_common_flag_command.go @@ -0,0 +1,14 @@ +package answer + +import ( + "github.com/ggmolly/belfast/connection" + "github.com/ggmolly/belfast/protobuf" + "google.golang.org/protobuf/proto" +) + +func UpdateCommonFlagCommand(buffer *[]byte, client *connection.Client) (int, int, error) { + response := protobuf.SC_11020{ + Result: proto.Uint32(0), + } + return client.SendMessage(11020, &response) +} diff --git a/main.go b/main.go index 6c2d348..a468560 100644 --- a/main.go +++ b/main.go @@ -218,4 +218,7 @@ func init() { } return c.SendMessage(13108, &response) }}) + + // UpdateCommonFlagCommand, unknown what it does + packets.RegisterPacketHandler(11019, []packets.PacketHandler{answer.UpdateCommonFlagCommand}) }