diff --git a/aid/print.go b/aid/print.go new file mode 100644 index 0000000..c1d2a16 --- /dev/null +++ b/aid/print.go @@ -0,0 +1,14 @@ +package aid + +import ( + "encoding/json" + "fmt" +) + +func PrintJSON(v interface{}) { + json1, err := json.MarshalIndent(v, "", " ") + if err != nil { + panic(err) + } + fmt.Println(string(json1)) +} \ No newline at end of file diff --git a/aid/type.go b/aid/type.go new file mode 100644 index 0000000..8b87c23 --- /dev/null +++ b/aid/type.go @@ -0,0 +1,3 @@ +package aid + +type JSON map[string]interface{} \ No newline at end of file diff --git a/main.go b/main.go index 747d27a..aa8c14e 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,12 @@ package main import ( - "encoding/json" "fmt" "os" "os/signal" "syscall" + "github.com/ectrc/snow/aid" "github.com/ectrc/snow/config" "github.com/ectrc/snow/person" "github.com/ectrc/snow/storage" @@ -41,7 +41,7 @@ func init() { device = postgresStorage } - + storage.Repo = storage.NewStorage(device) storage.Cache = storage.NewPersonsCacheMutex() } @@ -49,33 +49,33 @@ func init() { func init() { if DROP_TABLES { user := person.NewPerson() - snapshot := user.Snapshot() + snapshot := user.AthenaProfile.Snapshot() quest := person.NewQuest("Quest:Quest_1", "ChallengeBundle:Daily_1", "ChallengeBundleSchedule:Paid_1") { quest.AddObjective("quest_objective_eliminateplayers", 0) quest.AddObjective("quest_objective_top1", 0) quest.AddObjective("quest_objective_place_top10", 0) - + quest.UpdateObjectiveCount("quest_objective_eliminateplayers", 10) quest.UpdateObjectiveCount("quest_objective_place_top10", -3) - + quest.RemoveObjective("quest_objective_top1") } user.AthenaProfile.Quests.AddQuest(quest) giftBox := person.NewGift("GiftBox:GB_Default", 1, user.ID, "Hello, Bully!") { - giftBox.AddLoot(person.NewItem("AthenaCharacter:CID_002_Athena_Commando_F_Default", 1)) + giftBox.AddLoot(person.NewItemWithType("AthenaCharacter:CID_002_Athena_Commando_F_Default", 1, "athena")) } user.CommonCoreProfile.Gifts.AddGift(giftBox) currency := person.NewItem("Currency:MtxPurchased", 100) user.CommonCoreProfile.Items.AddItem(currency) - user.FindChanges(*snapshot) user.Save() - printjson(user.Snapshot()) + user.AthenaProfile.Diff(snapshot) + aid.PrintJSON(user.CommonCoreProfile.Snapshot()) } go storage.Cache.CacheKiller() @@ -88,19 +88,7 @@ func main() { fmt.Println(person) } - wait() -} - -func wait() { sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc -} - -func printjson(v interface{}) { - json1, err := json.MarshalIndent(v, "", " ") - if err != nil { - panic(err) - } - fmt.Println(string(json1)) } \ No newline at end of file diff --git a/person/changes.go b/person/changes.go new file mode 100644 index 0000000..6bd6273 --- /dev/null +++ b/person/changes.go @@ -0,0 +1,42 @@ +package person + +import "github.com/ectrc/snow/aid" + +type ProfileChange struct { + ChangeType string `json:"changeType"` +} + +type FullProfileUpdate struct { + ChangeType string `json:"changeType"` + Profile aid.JSON `json:"profile"` +} + +type StatModified struct { + ChangeType string `json:"changeType"` + Name string `json:"name"` + Value interface{} `json:"value"` +} + +type ItemAdded struct { + ChangeType string `json:"changeType"` + ItemId string `json:"itemId"` + Item aid.JSON `json:"item"` +} + +type ItemRemoved struct { + ChangeType string `json:"changeType"` + ItemId string `json:"itemId"` +} + +type ItemAttributeChanged struct { + ChangeType string `json:"changeType"` + ItemId string `json:"itemId"` + AttributeName string `json:"attributeName"` + AttributeValue interface{} `json:"attributeValue"` +} + +type ItemQuantityChanged struct { + ChangeType string `json:"changeType"` + ItemId string `json:"itemId"` + Quantity int `json:"quantity"` +} \ No newline at end of file diff --git a/person/item.go b/person/item.go index 61c5a2d..8fb8696 100644 --- a/person/item.go +++ b/person/item.go @@ -26,6 +26,18 @@ func NewItem(templateID string, quantity int) *Item { } } +func NewItemWithType(templateID string, quantity int, profile string) *Item { + return &Item{ + ID: uuid.New().String(), + TemplateID: templateID, + Quantity: quantity, + Favorite: false, + HasSeen: false, + Variants: []*VariantChannel{}, + ProfileType: profile, + } +} + func FromDatabaseItem(item *storage.DB_Item, profileType *string) *Item { variants := []*VariantChannel{} diff --git a/person/person.go b/person/person.go index 7ea65f7..aada928 100644 --- a/person/person.go +++ b/person/person.go @@ -3,7 +3,6 @@ package person import ( "github.com/ectrc/snow/storage" "github.com/google/uuid" - "github.com/r3labs/diff/v3" ) type Person struct { @@ -12,7 +11,6 @@ type Person struct { AthenaProfile *Profile CommonCoreProfile *Profile Loadout *Loadout - Changes []diff.Change } type Option struct { @@ -24,8 +22,8 @@ func NewPerson() *Person { return &Person{ ID: uuid.New().String(), DisplayName: "Hello, Bully!", - AthenaProfile: NewProfile(), - CommonCoreProfile: NewProfile(), + AthenaProfile: NewProfile("athena"), + CommonCoreProfile: NewProfile("common_core"), Loadout: NewLoadout(), } } @@ -75,11 +73,6 @@ func (p *Person) Save() { storage.Repo.SavePerson(p.ToDatabase()) } -func (p *Person) FindChanges(snapshot PersonSnapshot) { - changes, _ := diff.Diff(snapshot, *CreateSnapshot(p)) - p.Changes = changes -} - func (p *Person) ToDatabase() *storage.DB_Person { dbPerson := storage.DB_Person{ ID: p.ID, @@ -128,8 +121,4 @@ func (p *Person) ToDatabase() *storage.DB_Person { } return &dbPerson -} - -func (p *Person) Snapshot() *PersonSnapshot { - return CreateSnapshot(p) } \ No newline at end of file diff --git a/person/profile.go b/person/profile.go index b7d5b55..8b4cc33 100644 --- a/person/profile.go +++ b/person/profile.go @@ -5,6 +5,7 @@ import ( "github.com/ectrc/snow/storage" "github.com/google/uuid" + "github.com/r3labs/diff/v3" ) type Profile struct { @@ -13,20 +14,23 @@ type Profile struct { Gifts *GiftMutex Quests *QuestMutex Attributes *sync.Map + Type string + Changes []diff.Change } -func NewProfile() *Profile { +func NewProfile(profile string) *Profile { return &Profile{ ID: uuid.New().String(), - Items: NewItemMutex(), + Items: NewItemMutex(profile), Gifts: NewGiftMutex(), Quests: NewQuestMutex(), Attributes: &sync.Map{}, + Type: profile, } } func FromDatabaseProfile(profile *storage.DB_Profile) *Profile { - items := NewItemMutex() + items := NewItemMutex(profile.Type) gifts := NewGiftMutex() quests := NewQuestMutex() @@ -53,6 +57,7 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile { Gifts: gifts, Quests: quests, Attributes: attributes, + Type: profile.Type, } } @@ -95,6 +100,12 @@ func (p *Profile) Snapshot() *ProfileSnapshot { } } +func (p *Profile) Diff(snapshot *ProfileSnapshot) []diff.Change { + changes, _ := diff.Diff(p.Snapshot(), snapshot) + p.Changes = changes + return changes +} + type Loadout struct { ID string Character string diff --git a/person/snapshot.go b/person/snapshot.go index 9a764a2..10b648b 100644 --- a/person/snapshot.go +++ b/person/snapshot.go @@ -1,13 +1,5 @@ package person -type PersonSnapshot struct { - ID string - DisplayName string - AthenaProfile ProfileSnapshot - CommonCoreProfile ProfileSnapshot - Loadout Loadout -} - type ProfileSnapshot struct { ID string Items map[string]ItemSnapshot @@ -34,15 +26,4 @@ type GiftSnapshot struct { GiftedAt int64 Message string Loot []Item -} - -// Snapshot returns a snapshot of the person. No pointers as it has to compare the value not the address. -func CreateSnapshot(person *Person) *PersonSnapshot { - return &PersonSnapshot{ - ID: person.ID, - DisplayName: person.DisplayName, - Loadout: *person.Loadout, - AthenaProfile: *person.AthenaProfile.Snapshot(), - CommonCoreProfile: *person.CommonCoreProfile.Snapshot(), - } } \ No newline at end of file diff --git a/person/sync.go b/person/sync.go index 6de360d..6d0b120 100644 --- a/person/sync.go +++ b/person/sync.go @@ -6,13 +6,17 @@ import ( type ItemMutex struct { sync.Map + ProfileType string } -func NewItemMutex() *ItemMutex { - return &ItemMutex{} +func NewItemMutex(profile string) *ItemMutex { + return &ItemMutex{ + ProfileType: profile, + } } func (m *ItemMutex) AddItem(item *Item) { + item.ProfileType = m.ProfileType m.Store(item.ID, item) // storage.Repo.SaveItem(item) } @@ -48,6 +52,7 @@ func (m *ItemMutex) Count() int { type GiftMutex struct { sync.Map + ProfileType string } func NewGiftMutex() *GiftMutex { diff --git a/readme.md b/readme.md index 62a7f11..496c836 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ Performance first, universal Fortnite backend written in Go. ```golang user := person.NewPerson() -snapshot := user.Snapshot() +snapshot := user.AthenaProfile.Snapshot() quest := person.NewQuest("Quest:Quest_1", "ChallengeBundle:Daily_1", "ChallengeBundleSchedule:Paid_1") { @@ -30,15 +30,15 @@ user.AthenaProfile.Quests.AddQuest(quest) giftBox := person.NewGift("GiftBox:GB_Default", 1, user.ID, "Hello, Bully!") { - giftBox.AddLoot(person.NewItem("AthenaCharacter:CID_002_Athena_Commando_F_Default", 1)) + giftBox.AddLoot(person.NewItemWithType("AthenaCharacter:CID_002_Athena_Commando_F_Default", 1, "athena")) } user.CommonCoreProfile.Gifts.AddGift(giftBox) currency := person.NewItem("Currency:MtxPurchased", 100) user.CommonCoreProfile.Items.AddItem(currency) -user.FindChanges(*snapshot) user.Save() +user.AthenaProfile.Diff(snapshot) ``` ## What's next?