Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
add: meowfficers tables / import / has many relation
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmolly committed Jun 11, 2024
1 parent 7fce4d8 commit 809e30f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
39 changes: 30 additions & 9 deletions misc/update_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions orm/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down
2 changes: 2 additions & 0 deletions orm/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func InitDatabase() bool {
&MailAttachment{},
&ShopOffer{},
&Message{},
&Meowfficer{},
// Servers
&ServerState{},
&Server{},
Expand All @@ -72,6 +73,7 @@ func InitDatabase() bool {
&YostarusMap{},
&OwnedShip{},
&OwnedSkin{},
&OwnedMeowfficer{},
&Punishment{},
&Commander{},
&CommanderItem{},
Expand Down
19 changes: 19 additions & 0 deletions orm/meowfficer.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 809e30f

Please sign in to comment.