From 6b09cd5e82b19c53b5e8a44895a264934b2ccab6 Mon Sep 17 00:00:00 2001 From: eccentric Date: Wed, 1 Nov 2023 00:05:17 +0000 Subject: [PATCH] Fix Athena Profile has 0 anything! --- aid/aid.go | 13 ++++++++++++ main.go | 21 +++++++++---------- person/attribute.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ person/person.go | 46 +++++++++++++++++++++++++++++++----------- person/profile.go | 24 +++++++++++++--------- person/snapshot.go | 10 ++++++++- person/sync.go | 33 ++++++++++++++++++++++++++++++ storage/postgres.go | 4 ---- storage/tables.go | 3 ++- 9 files changed, 164 insertions(+), 39 deletions(-) create mode 100644 aid/aid.go create mode 100644 person/attribute.go diff --git a/aid/aid.go b/aid/aid.go new file mode 100644 index 0000000..61580f5 --- /dev/null +++ b/aid/aid.go @@ -0,0 +1,13 @@ +package aid + +import ( + "os" + "os/signal" + "syscall" +) + +func WaitForExit() { + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-sc +} \ No newline at end of file diff --git a/main.go b/main.go index aa8c14e..e2a9751 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,6 @@ package main import ( - "fmt" - "os" - "os/signal" - "syscall" - "github.com/ectrc/snow/aid" "github.com/ectrc/snow/config" "github.com/ectrc/snow/person" @@ -71,11 +66,10 @@ func init() { user.CommonCoreProfile.Gifts.AddGift(giftBox) currency := person.NewItem("Currency:MtxPurchased", 100) - user.CommonCoreProfile.Items.AddItem(currency) + user.AthenaProfile.Items.AddItem(currency) user.Save() user.AthenaProfile.Diff(snapshot) - aid.PrintJSON(user.CommonCoreProfile.Snapshot()) } go storage.Cache.CacheKiller() @@ -84,11 +78,14 @@ func init() { func main() { persons := person.AllFromDatabase() - for _, person := range persons { - fmt.Println(person) + for _, p := range persons { + p.AthenaProfile.Items.RangeItems(func(id string, item *person.Item) bool { + aid.PrintJSON(item) + return true + }) + + aid.PrintJSON(p.Snapshot()) } - sc := make(chan os.Signal, 1) - signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) - <-sc + aid.WaitForExit() } \ No newline at end of file diff --git a/person/attribute.go b/person/attribute.go new file mode 100644 index 0000000..15e879c --- /dev/null +++ b/person/attribute.go @@ -0,0 +1,49 @@ +package person + +import ( + "encoding/json" + + "github.com/ectrc/snow/storage" +) + +type Attribute struct { + Key string + Value interface{} + Type string +} + +func NewAttribute(key string, value interface{}, attributeType string) *Attribute { + return &Attribute{ + Key: key, + Value: value, + Type: attributeType, + } +} + +func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute { + var value interface{} + err := json.Unmarshal([]byte(db.ValueJSON), &value) + if err != nil { + return nil + } + + return &Attribute{ + Key: db.Key, + Value: value, + Type: db.Type, + } +} + +func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute { + value, err := json.Marshal(a.Value) + if err != nil { + return nil + } + + return &storage.DB_PAttribute{ + ProfileID: profileId, + Key: a.Key, + ValueJSON: string(value), + Type: a.Type, + } +} \ No newline at end of file diff --git a/person/person.go b/person/person.go index aada928..3100f57 100644 --- a/person/person.go +++ b/person/person.go @@ -35,13 +35,13 @@ func FromDatabase(personId string) *Person { } loadout := FromDatabaseLoadout(&person.Loadout) - athenaProfile := &Profile{} - commonCoreProfile := &Profile{} + athenaProfile := NewProfile("athena") + commonCoreProfile := NewProfile("common_core") for _, profile := range person.Profiles { if profile.Type == "athena" { - athenaProfile := FromDatabaseProfile(&profile) athenaProfile.ID = profile.ID + athenaProfile = FromDatabaseProfile(&profile) } if profile.Type == "common_core" { @@ -49,7 +49,7 @@ func FromDatabase(personId string) *Person { commonCoreProfile = FromDatabaseProfile(&profile) } } - + return &Person{ ID: person.ID, DisplayName: person.DisplayName, @@ -81,7 +81,10 @@ func (p *Person) ToDatabase() *storage.DB_Person { Loadout: *p.Loadout.ToDatabase(), } - profilesToConvert := map[string]*Profile{"athena": p.AthenaProfile, "common_core": p.CommonCoreProfile} + profilesToConvert := map[string]*Profile{ + "common_core": p.CommonCoreProfile, + "athena": p.AthenaProfile, + } for profileType, profile := range profilesToConvert { dbProfile := storage.DB_Profile{ @@ -108,12 +111,8 @@ func (p *Person) ToDatabase() *storage.DB_Person { return true }) - profile.Attributes.Range(func(key, value interface{}) bool { - dbProfile.Attributes = append(dbProfile.Attributes, storage.DB_PAttribute{ - ProfileID: profile.ID, - Key: key.(string), - Value: value.(string), - }) + profile.Attributes.RangeAttributes(func(key string, value *Attribute) bool { + dbProfile.Attributes = append(dbProfile.Attributes, *value.ToDatabase(p.ID)) return true }) @@ -121,4 +120,27 @@ func (p *Person) ToDatabase() *storage.DB_Person { } return &dbPerson -} \ No newline at end of file +} + +func (p *Person) AddAttribute(value *Attribute) { + p.AthenaProfile.Attributes.AddAttribute(value) +} + +func (p *Person) GetAttribute(key string) *Attribute { + attribute := p.AthenaProfile.Attributes.GetAttribute(key) + return attribute +} + +func (p *Person) RemoveAttribute(key string) { + p.AthenaProfile.Attributes.DeleteAttribute(key) +} + +func (p *Person) Snapshot() *PersonSnapshot { + return &PersonSnapshot{ + ID: p.ID, + DisplayName: p.DisplayName, + AthenaProfile: *p.AthenaProfile.Snapshot(), + CommonCoreProfile:* p.CommonCoreProfile.Snapshot(), + Loadout: *p.Loadout, + } +} \ No newline at end of file diff --git a/person/profile.go b/person/profile.go index 8b4cc33..720d62a 100644 --- a/person/profile.go +++ b/person/profile.go @@ -1,7 +1,7 @@ package person import ( - "sync" + "fmt" "github.com/ectrc/snow/storage" "github.com/google/uuid" @@ -13,7 +13,7 @@ type Profile struct { Items *ItemMutex Gifts *GiftMutex Quests *QuestMutex - Attributes *sync.Map + Attributes *AttributeMutex Type string Changes []diff.Change } @@ -24,7 +24,7 @@ func NewProfile(profile string) *Profile { Items: NewItemMutex(profile), Gifts: NewGiftMutex(), Quests: NewQuestMutex(), - Attributes: &sync.Map{}, + Attributes: NewAttributeMutex(), Type: profile, } } @@ -33,6 +33,7 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile { items := NewItemMutex(profile.Type) gifts := NewGiftMutex() quests := NewQuestMutex() + attributes := NewAttributeMutex() for _, item := range profile.Items { items.AddItem(FromDatabaseItem(&item, &profile.Type)) @@ -46,9 +47,14 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile { quests.AddQuest(FromDatabaseQuest(&quest, &profile.Type)) } - attributes := &sync.Map{} for _, attribute := range profile.Attributes { - attributes.Store(attribute.Key, attribute.Value) + parsed := FromDatabaseAttribute(&attribute) + if parsed == nil { + fmt.Printf("error getting attribute from database") + continue + } + + attributes.AddAttribute(parsed) } return &Profile{ @@ -69,7 +75,7 @@ func (p *Profile) Snapshot() *ProfileSnapshot { items := map[string]ItemSnapshot{} gifts := map[string]GiftSnapshot{} quests := map[string]Quest{} - attributes := map[string]string{} + attributes := map[string]Attribute{} p.Items.RangeItems(func(id string, item *Item) bool { items[id] = item.Snapshot() @@ -86,8 +92,8 @@ func (p *Profile) Snapshot() *ProfileSnapshot { return true }) - p.Attributes.Range(func(key, value interface{}) bool { - attributes[key.(string)] = value.(string) + p.Attributes.RangeAttributes(func(key string, value *Attribute) bool { + attributes[key] = *value return true }) @@ -96,7 +102,7 @@ func (p *Profile) Snapshot() *ProfileSnapshot { Items: items, Gifts: gifts, Quests: quests, - Attributes: attributes, + Attributes: attributes, } } diff --git a/person/snapshot.go b/person/snapshot.go index 10b648b..b0cc658 100644 --- a/person/snapshot.go +++ b/person/snapshot.go @@ -1,11 +1,19 @@ package person +type PersonSnapshot struct { + ID string + DisplayName string + AthenaProfile ProfileSnapshot + CommonCoreProfile ProfileSnapshot + Loadout Loadout +} + type ProfileSnapshot struct { ID string Items map[string]ItemSnapshot Gifts map[string]GiftSnapshot Quests map[string]Quest - Attributes map[string]string + Attributes map[string]Attribute } type ItemSnapshot struct { diff --git a/person/sync.go b/person/sync.go index 6d0b120..64fbfc9 100644 --- a/person/sync.go +++ b/person/sync.go @@ -133,4 +133,37 @@ func (m *QuestMutex) Count() int { return true }) return count +} + +type AttributeMutex struct { + sync.Map +} + +func NewAttributeMutex() *AttributeMutex { + return &AttributeMutex{} +} + +func (m *AttributeMutex) AddAttribute(attribute *Attribute) { + m.Store(attribute.Key, attribute) + // storage.Repo.SaveAttribute(key, value) +} + +func (m *AttributeMutex) DeleteAttribute(key string) { + m.Delete(key) + // storage.Repo.DeleteAttribute(key) +} + +func (m *AttributeMutex) GetAttribute(key string) *Attribute { + value, ok := m.Load(key) + if !ok { + return nil + } + + return value.(*Attribute) +} + +func (m *AttributeMutex) RangeAttributes(f func(key string, value *Attribute) bool) { + m.Range(func(key, value interface{}) bool { + return f(key.(string), value.(*Attribute)) + }) } \ No newline at end of file diff --git a/storage/postgres.go b/storage/postgres.go index 1225061..32fe48a 100644 --- a/storage/postgres.go +++ b/storage/postgres.go @@ -35,8 +35,6 @@ func (s *PostgresStorage) GetPerson(personId string) *DB_Person { Preload("Profiles.Items"). Preload("Profiles.Gifts"). Preload("Profiles.Quests"). - Preload("Profiles.Items.Variants"). - Preload("Profiles.Gifts.Loot"). Find(&dbPerson) if dbPerson.ID == "" { @@ -57,8 +55,6 @@ func (s *PostgresStorage) GetAllPersons() []*DB_Person { Preload("Profiles.Items"). Preload("Profiles.Gifts"). Preload("Profiles.Quests"). - Preload("Profiles.Items.Variants"). - Preload("Profiles.Gifts.Loot"). Find(&dbPersons) return dbPersons diff --git a/storage/tables.go b/storage/tables.go index f02a9ba..91d22fa 100644 --- a/storage/tables.go +++ b/storage/tables.go @@ -54,7 +54,8 @@ func (DB_Profile) TableName() string { type DB_PAttribute struct { Key string `gorm:"primary_key"` ProfileID string - Value string + ValueJSON string + Type string } func (DB_PAttribute) TableName() string {