From 809e30f933ff3967f2ed419b3024f05e951370bc Mon Sep 17 00:00:00 2001 From: ggmolly Date: Tue, 11 Jun 2024 22:05:05 +0200 Subject: [PATCH] add: meowfficers tables / import / has many relation --- misc/update_data.go | 39 ++++++++++++++++++++++++++++++--------- orm/commander.go | 19 ++++++++++--------- orm/database.go | 2 ++ orm/meowfficer.go | 19 +++++++++++++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 orm/meowfficer.go diff --git a/misc/update_data.go b/misc/update_data.go index 7cf6594..9721329 100644 --- a/misc/update_data.go +++ b/misc/update_data.go @@ -20,17 +20,18 @@ const ( var ( dataFn = map[string]func(string, *gorm.DB) error{ - "Items": importItems, - "Buffs": importBuffs, - "Ships": importShips, - "Skins": importSkins, - "Resources": importResources, - "Pools": importPools, - "BuildTimes": importBuildTimes, - "ShopOffers": importShopOffers, + "Items": importItems, + "Buffs": importBuffs, + "Ships": importShips, + "Skins": importSkins, + "Resources": importResources, + "Pools": importPools, + "BuildTimes": importBuildTimes, + "ShopOffers": importShopOffers, + "Meowfficers": importMeowfficers, } // Golang maps are unordered, so we need to keep track of the order of the keys ourselves - order = []string{"Items", "Buffs", "Ships", "Skins", "Resources", "Pools", "BuildTimes", "ShopOffers"} + order = []string{"Items", "Buffs", "Ships", "Skins", "Resources", "Pools", "BuildTimes", "ShopOffers", "Meowfficers"} ) func getBelfastData(region string, file string) (*json.Decoder, *http.Response, error) { @@ -249,6 +250,26 @@ func importShopOffers(region string, tx *gorm.DB) error { return nil } +func importMeowfficers(region string, tx *gorm.DB) error { + decoder, resp, err := getBelfastData(region, "commander_data_template.json") + if err != nil { + return err + } + defer resp.Body.Close() + decoder.Token() // Consume the start of the array '[' + + // Decode each elements + for decoder.More() { + var meofficer orm.Meowfficer + if err := decoder.Decode(&meofficer); err != nil { + return err + } else if err := tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(&meofficer).Error; err != nil { + return err + } + } + return nil +} + // XXX: The database can end in a limbo state if an error occurs while updating the data (e.g. network error, invalid JSON, etc.) // upon restarting Belfast, the database won't be re-populated because some tables were already populated // this could be fixed by passing a single transaction to all the data import functions, but requires some refactoring diff --git a/orm/commander.go b/orm/commander.go index 3a9b479..970180a 100644 --- a/orm/commander.go +++ b/orm/commander.go @@ -22,15 +22,16 @@ type Commander struct { RoomID uint32 `gorm:"default:0;not_null"` ExchangeCount uint32 `gorm:"default:0;not_null"` // Number of times the commander has built ships, can be exchanged for UR ships - Punishments []Punishment `gorm:"foreignKey:PunishedID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - Ships []OwnedShip `gorm:"foreignKey:OwnerID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - Items []CommanderItem `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - MiscItems []CommanderMiscItem `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - OwnedResources []OwnedResource `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - Builds []Build `gorm:"foreignKey:BuilderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - Mails []Mail `gorm:"foreignKey:ReceiverID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - OwnedSkins []OwnedSkin `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` - Secretaries []*OwnedShip `gorm:"-"` + Punishments []Punishment `gorm:"foreignKey:PunishedID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Ships []OwnedShip `gorm:"foreignKey:OwnerID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Items []CommanderItem `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + MiscItems []CommanderMiscItem `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + OwnedResources []OwnedResource `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Builds []Build `gorm:"foreignKey:BuilderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Mails []Mail `gorm:"foreignKey:ReceiverID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + OwnedSkins []OwnedSkin `gorm:"foreignKey:CommanderID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + OwnedMeowfficers []OwnedMeowfficer `gorm:"foreignKey:OwnerID;references:CommanderID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Secretaries []*OwnedShip `gorm:"-"` // These maps will be populated by the Load() method OwnedShipsMap map[uint32]*OwnedShip `gorm:"-"` diff --git a/orm/database.go b/orm/database.go index dbf16f6..62b7715 100644 --- a/orm/database.go +++ b/orm/database.go @@ -62,6 +62,7 @@ func InitDatabase() bool { &MailAttachment{}, &ShopOffer{}, &Message{}, + &Meowfficer{}, // Servers &ServerState{}, &Server{}, @@ -72,6 +73,7 @@ func InitDatabase() bool { &YostarusMap{}, &OwnedShip{}, &OwnedSkin{}, + &OwnedMeowfficer{}, &Punishment{}, &Commander{}, &CommanderItem{}, diff --git a/orm/meowfficer.go b/orm/meowfficer.go new file mode 100644 index 0000000..f1c4ab7 --- /dev/null +++ b/orm/meowfficer.go @@ -0,0 +1,19 @@ +package orm + +import "github.com/lib/pq" + +type Meowfficer struct { + ID uint32 `gorm:"primaryKey" json:"id"` + Name string `json:"name"` + Rarity uint32 `json:"rarity"` + Exp uint32 `json:"exp"` + ExpCost uint32 `json:"exp_cost"` + GroupType uint32 `json:"group_type"` + Nationality uint32 `json:"nationality"` + AbilityShow pq.Int32Array `json:"ability_show" gorm:"type:integer[]"` +} + +type OwnedMeowfficer struct { + OwnerID uint32 `gorm:"type:int;not_null"` + ID uint32 `gorm:"primary_key"` +} \ No newline at end of file