diff --git a/main.go b/main.go index ab3a19b..5a9d5ec 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,10 @@ func init() { if DROP_TABLES { user := person.NewPerson() { + user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("xp", 1030)) + user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("level", 100)) + user.CommonCoreProfile.Attributes.AddAttribute(person.NewAttribute("quest_manager", aid.JSON{})) + user.CommonCoreProfile.Items.AddItem(person.NewItem("Currency:MtxPurchased", 100)) user.CommonCoreProfile.Items.AddItem(person.NewItem("Token:CampaignAccess", 1)) @@ -79,13 +83,18 @@ func init() { user.CommonCoreProfile.Items.AddItem(person.NewItem("Token:ReceiveMtxCurrency", 1)) } user.CommonCoreProfile.Diff(snapshot) - - aid.PrintJSON(user.CommonCoreProfile.Changes) + user.Save() } go storage.Cache.CacheKiller() } func main() { + users := person.AllFromDatabase() + + for _, user := range users { + aid.PrintJSON(user.Snapshot()) + } + // aid.WaitForExit() } \ No newline at end of file diff --git a/person/attribute.go b/person/attribute.go index 15e879c..5b11a8c 100644 --- a/person/attribute.go +++ b/person/attribute.go @@ -2,21 +2,25 @@ package person import ( "encoding/json" + "reflect" "github.com/ectrc/snow/storage" + "github.com/google/uuid" ) type Attribute struct { + ID string Key string Value interface{} Type string } -func NewAttribute(key string, value interface{}, attributeType string) *Attribute { +func NewAttribute(key string, value interface{}) *Attribute { return &Attribute{ + ID: uuid.New().String(), Key: key, Value: value, - Type: attributeType, + Type: reflect.TypeOf(value).String(), } } @@ -28,6 +32,7 @@ func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute { } return &Attribute{ + ID: db.ID, Key: db.Key, Value: value, Type: db.Type, @@ -41,9 +46,14 @@ func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute { } return &storage.DB_PAttribute{ + ID: a.ID, ProfileID: profileId, - Key: a.Key, + Key: a.Key, ValueJSON: string(value), - Type: a.Type, + Type: a.Type, } +} + +func (a *Attribute) Delete() { + storage.Repo.DeleteAttribute(a.ID) } \ No newline at end of file diff --git a/person/gift.go b/person/gift.go index ca7a55f..1e11996 100644 --- a/person/gift.go +++ b/person/gift.go @@ -57,9 +57,11 @@ func (g *Gift) FillLoot(loot []*Item) { } func (g *Gift) Delete() { - g.Quantity = 0 - g.Loot = []*Item{} - //storage.Repo.DeleteGift(g.ID) + for _, item := range g.Loot { + item.DeleteLoot() + } + + storage.Repo.DeleteGift(g.ID) } func (g *Gift) ToDatabase(profileId string) *storage.DB_Gift { diff --git a/person/item.go b/person/item.go index 07596e5..b11af95 100644 --- a/person/item.go +++ b/person/item.go @@ -105,8 +105,11 @@ func (i *Item) GetAttribute(attribute string) interface{} { } func (i *Item) Delete() { - //storage.Repo.DeleteItem(i.ID) - i.Quantity = 0 + storage.Repo.DeleteItem(i.ID) +} + +func (i *Item) DeleteLoot() { + storage.Repo.DeleteLoot(i.ID) } func (i *Item) NewChannel(channel string, owned []string, active string) *VariantChannel { @@ -204,6 +207,7 @@ func (i *Item) Snapshot() ItemSnapshot { } type VariantChannel struct { + ID string ItemID string Channel string Owned []string @@ -212,6 +216,7 @@ type VariantChannel struct { func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel { return &VariantChannel{ + ID: variant.ID, ItemID: variant.ItemID, Channel: variant.Channel, Owned: variant.Owned, @@ -221,6 +226,7 @@ func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel { func (v *VariantChannel) ToDatabase() *storage.DB_VariantChannel { return &storage.DB_VariantChannel{ + ID: v.ID, ItemID: v.ItemID, Channel: v.Channel, Owned: v.Owned, diff --git a/person/profile.go b/person/profile.go index b04edaf..2082b09 100644 --- a/person/profile.go +++ b/person/profile.go @@ -3,7 +3,6 @@ package person import ( "fmt" - "github.com/ectrc/snow/aid" "github.com/ectrc/snow/storage" "github.com/google/uuid" "github.com/r3labs/diff/v3" @@ -119,7 +118,7 @@ func (p *Profile) Diff(snapshot *ProfileSnapshot) []diff.Change { return nil } - aid.PrintJSON(changes) + // aid.PrintJSON(changes) for _, change := range changes { switch change.Path[0] { diff --git a/person/quest.go b/person/quest.go index 5d5a076..ff71700 100644 --- a/person/quest.go +++ b/person/quest.go @@ -37,9 +37,7 @@ func FromDatabaseQuest(quest *storage.DB_Quest, profileType *string) *Quest { } func (q *Quest) Delete() { - //storage.Repo.DeleteQuest(q.ID) - q.ObjectiveCounts = []int64{} - q.Objectives = []string{} + storage.Repo.DeleteQuest(q.ID) } func (q *Quest) AddObjective(objective string, count int64) { diff --git a/person/sync.go b/person/sync.go index 5618fb0..a4861d9 100644 --- a/person/sync.go +++ b/person/sync.go @@ -22,6 +22,12 @@ func (m *ItemMutex) AddItem(item *Item) { } func (m *ItemMutex) DeleteItem(id string) { + item := m.GetItem(id) + if item == nil { + return + } + + item.Delete() m.Delete(id) // storage.Repo.DeleteItem(id) } diff --git a/storage/postgres.go b/storage/postgres.go index 32fe48a..76c4d3b 100644 --- a/storage/postgres.go +++ b/storage/postgres.go @@ -25,6 +25,10 @@ func (s *PostgresStorage) Migrate(table interface{}, tableName string) { s.Postgres.Table(tableName).AutoMigrate(table) } +func (s *PostgresStorage) DropTables() { + s.Postgres.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;`) +} + func (s *PostgresStorage) GetPerson(personId string) *DB_Person { var dbPerson DB_Person s.Postgres. @@ -64,6 +68,26 @@ func (s *PostgresStorage) SavePerson(person *DB_Person) { s.Postgres.Save(person) } -func (s *PostgresStorage) DropTables() { - s.Postgres.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;`) +func (s *PostgresStorage) DeleteItem(itemId string) { + s.Postgres.Delete(&DB_Item{}, "id = ?", itemId) +} + +func (s *PostgresStorage) DeleteVariant(variantId string) { + s.Postgres.Delete(&DB_VariantChannel{}, "id = ?", variantId) +} + +func (s *PostgresStorage) DeleteQuest(questId string) { + s.Postgres.Delete(&DB_Quest{}, "id = ?", questId) +} + +func (s *PostgresStorage) DeleteLoot(lootId string) { + s.Postgres.Delete(&DB_Loot{}, "id = ?", lootId) +} + +func (s *PostgresStorage) DeleteGift(giftId string) { + s.Postgres.Delete(&DB_Gift{}, "id = ?", giftId) +} + +func (s *PostgresStorage) DeleteAttribute(attributeId string) { + s.Postgres.Delete(&DB_PAttribute{}, "id = ?", attributeId) } \ No newline at end of file diff --git a/storage/storage.go b/storage/storage.go index b1d9518..0cb7c78 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -11,6 +11,13 @@ type Storage interface { GetPerson(personId string) *DB_Person GetAllPersons() []*DB_Person SavePerson(person *DB_Person) + + DeleteItem(itemId string) + DeleteVariant(variantId string) + DeleteQuest(questId string) + DeleteLoot(lootId string) + DeleteGift(giftId string) + DeleteAttribute(attributeId string) } type Repository struct { @@ -45,4 +52,28 @@ func (r *Repository) GetAllPersons() []*DB_Person { func (r *Repository) SavePerson(person *DB_Person) { Cache.SavePerson(person) r.Storage.SavePerson(person) +} + +func (r *Repository) DeleteItem(itemId string) { + r.Storage.DeleteItem(itemId) +} + +func (r *Repository) DeleteVariant(variantId string) { + r.Storage.DeleteVariant(variantId) +} + +func (r *Repository) DeleteQuest(questId string) { + r.Storage.DeleteQuest(questId) +} + +func (r *Repository) DeleteLoot(lootId string) { + r.Storage.DeleteLoot(lootId) +} + +func (r *Repository) DeleteGift(giftId string) { + r.Storage.DeleteGift(giftId) +} + +func (r *Repository) DeleteAttribute(attributeId string) { + r.Storage.DeleteAttribute(attributeId) } \ No newline at end of file diff --git a/storage/tables.go b/storage/tables.go index 30c95cf..5fac9dc 100644 --- a/storage/tables.go +++ b/storage/tables.go @@ -53,8 +53,9 @@ func (DB_Profile) TableName() string { } type DB_PAttribute struct { - Key string `gorm:"primary_key"` + ID string `gorm:"primary_key"` ProfileID string + Key string ValueJSON string Type string } @@ -78,10 +79,11 @@ func (DB_Item) TableName() string { } type DB_VariantChannel struct { - Channel string `gorm:"primary_key"` + ID string `gorm:"primary_key"` + ItemID string + Channel string Owned pq.StringArray `gorm:"type:text[]"` Active string - ItemID string } func (DB_VariantChannel) TableName() string {