Add ID to all database tables + add deleting from storage!

This commit is contained in:
eccentric 2023-11-01 21:33:25 +00:00
parent 8e3cd016d9
commit 226241588c
10 changed files with 108 additions and 21 deletions

13
main.go
View File

@ -45,6 +45,10 @@ func init() {
if DROP_TABLES { if DROP_TABLES {
user := person.NewPerson() 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("Currency:MtxPurchased", 100))
user.CommonCoreProfile.Items.AddItem(person.NewItem("Token:CampaignAccess", 1)) 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.Items.AddItem(person.NewItem("Token:ReceiveMtxCurrency", 1))
} }
user.CommonCoreProfile.Diff(snapshot) user.CommonCoreProfile.Diff(snapshot)
user.Save()
aid.PrintJSON(user.CommonCoreProfile.Changes)
} }
go storage.Cache.CacheKiller() go storage.Cache.CacheKiller()
} }
func main() { func main() {
users := person.AllFromDatabase()
for _, user := range users {
aid.PrintJSON(user.Snapshot())
}
// aid.WaitForExit() // aid.WaitForExit()
} }

View File

@ -2,21 +2,25 @@ package person
import ( import (
"encoding/json" "encoding/json"
"reflect"
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
"github.com/google/uuid"
) )
type Attribute struct { type Attribute struct {
ID string
Key string Key string
Value interface{} Value interface{}
Type string Type string
} }
func NewAttribute(key string, value interface{}, attributeType string) *Attribute { func NewAttribute(key string, value interface{}) *Attribute {
return &Attribute{ return &Attribute{
ID: uuid.New().String(),
Key: key, Key: key,
Value: value, Value: value,
Type: attributeType, Type: reflect.TypeOf(value).String(),
} }
} }
@ -28,6 +32,7 @@ func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
} }
return &Attribute{ return &Attribute{
ID: db.ID,
Key: db.Key, Key: db.Key,
Value: value, Value: value,
Type: db.Type, Type: db.Type,
@ -41,9 +46,14 @@ func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
} }
return &storage.DB_PAttribute{ return &storage.DB_PAttribute{
ID: a.ID,
ProfileID: profileId, ProfileID: profileId,
Key: a.Key, Key: a.Key,
ValueJSON: string(value), ValueJSON: string(value),
Type: a.Type, Type: a.Type,
} }
}
func (a *Attribute) Delete() {
storage.Repo.DeleteAttribute(a.ID)
} }

View File

@ -57,9 +57,11 @@ func (g *Gift) FillLoot(loot []*Item) {
} }
func (g *Gift) Delete() { func (g *Gift) Delete() {
g.Quantity = 0 for _, item := range g.Loot {
g.Loot = []*Item{} item.DeleteLoot()
//storage.Repo.DeleteGift(g.ID) }
storage.Repo.DeleteGift(g.ID)
} }
func (g *Gift) ToDatabase(profileId string) *storage.DB_Gift { func (g *Gift) ToDatabase(profileId string) *storage.DB_Gift {

View File

@ -105,8 +105,11 @@ func (i *Item) GetAttribute(attribute string) interface{} {
} }
func (i *Item) Delete() { func (i *Item) Delete() {
//storage.Repo.DeleteItem(i.ID) storage.Repo.DeleteItem(i.ID)
i.Quantity = 0 }
func (i *Item) DeleteLoot() {
storage.Repo.DeleteLoot(i.ID)
} }
func (i *Item) NewChannel(channel string, owned []string, active string) *VariantChannel { func (i *Item) NewChannel(channel string, owned []string, active string) *VariantChannel {
@ -204,6 +207,7 @@ func (i *Item) Snapshot() ItemSnapshot {
} }
type VariantChannel struct { type VariantChannel struct {
ID string
ItemID string ItemID string
Channel string Channel string
Owned []string Owned []string
@ -212,6 +216,7 @@ type VariantChannel struct {
func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel { func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel {
return &VariantChannel{ return &VariantChannel{
ID: variant.ID,
ItemID: variant.ItemID, ItemID: variant.ItemID,
Channel: variant.Channel, Channel: variant.Channel,
Owned: variant.Owned, Owned: variant.Owned,
@ -221,6 +226,7 @@ func FromDatabaseVariant(variant *storage.DB_VariantChannel) *VariantChannel {
func (v *VariantChannel) ToDatabase() *storage.DB_VariantChannel { func (v *VariantChannel) ToDatabase() *storage.DB_VariantChannel {
return &storage.DB_VariantChannel{ return &storage.DB_VariantChannel{
ID: v.ID,
ItemID: v.ItemID, ItemID: v.ItemID,
Channel: v.Channel, Channel: v.Channel,
Owned: v.Owned, Owned: v.Owned,

View File

@ -3,7 +3,6 @@ package person
import ( import (
"fmt" "fmt"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/r3labs/diff/v3" "github.com/r3labs/diff/v3"
@ -119,7 +118,7 @@ func (p *Profile) Diff(snapshot *ProfileSnapshot) []diff.Change {
return nil return nil
} }
aid.PrintJSON(changes) // aid.PrintJSON(changes)
for _, change := range changes { for _, change := range changes {
switch change.Path[0] { switch change.Path[0] {

View File

@ -37,9 +37,7 @@ func FromDatabaseQuest(quest *storage.DB_Quest, profileType *string) *Quest {
} }
func (q *Quest) Delete() { func (q *Quest) Delete() {
//storage.Repo.DeleteQuest(q.ID) storage.Repo.DeleteQuest(q.ID)
q.ObjectiveCounts = []int64{}
q.Objectives = []string{}
} }
func (q *Quest) AddObjective(objective string, count int64) { func (q *Quest) AddObjective(objective string, count int64) {

View File

@ -22,6 +22,12 @@ func (m *ItemMutex) AddItem(item *Item) {
} }
func (m *ItemMutex) DeleteItem(id string) { func (m *ItemMutex) DeleteItem(id string) {
item := m.GetItem(id)
if item == nil {
return
}
item.Delete()
m.Delete(id) m.Delete(id)
// storage.Repo.DeleteItem(id) // storage.Repo.DeleteItem(id)
} }

View File

@ -25,6 +25,10 @@ func (s *PostgresStorage) Migrate(table interface{}, tableName string) {
s.Postgres.Table(tableName).AutoMigrate(table) 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 { func (s *PostgresStorage) GetPerson(personId string) *DB_Person {
var dbPerson DB_Person var dbPerson DB_Person
s.Postgres. s.Postgres.
@ -64,6 +68,26 @@ func (s *PostgresStorage) SavePerson(person *DB_Person) {
s.Postgres.Save(person) s.Postgres.Save(person)
} }
func (s *PostgresStorage) DropTables() { func (s *PostgresStorage) DeleteItem(itemId string) {
s.Postgres.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;`) 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)
} }

View File

@ -11,6 +11,13 @@ type Storage interface {
GetPerson(personId string) *DB_Person GetPerson(personId string) *DB_Person
GetAllPersons() []*DB_Person GetAllPersons() []*DB_Person
SavePerson(person *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 { type Repository struct {
@ -45,4 +52,28 @@ func (r *Repository) GetAllPersons() []*DB_Person {
func (r *Repository) SavePerson(person *DB_Person) { func (r *Repository) SavePerson(person *DB_Person) {
Cache.SavePerson(person) Cache.SavePerson(person)
r.Storage.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)
} }

View File

@ -53,8 +53,9 @@ func (DB_Profile) TableName() string {
} }
type DB_PAttribute struct { type DB_PAttribute struct {
Key string `gorm:"primary_key"` ID string `gorm:"primary_key"`
ProfileID string ProfileID string
Key string
ValueJSON string ValueJSON string
Type string Type string
} }
@ -78,10 +79,11 @@ func (DB_Item) TableName() string {
} }
type DB_VariantChannel struct { type DB_VariantChannel struct {
Channel string `gorm:"primary_key"` ID string `gorm:"primary_key"`
ItemID string
Channel string
Owned pq.StringArray `gorm:"type:text[]"` Owned pq.StringArray `gorm:"type:text[]"`
Active string Active string
ItemID string
} }
func (DB_VariantChannel) TableName() string { func (DB_VariantChannel) TableName() string {