Fix Item Profile Types + Start MCP Responses

This commit is contained in:
eccentric 2023-10-31 23:19:52 +00:00
parent 284fdba68c
commit 4cc7032ea0
10 changed files with 105 additions and 60 deletions

14
aid/print.go Normal file
View File

@ -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))
}

3
aid/type.go Normal file
View File

@ -0,0 +1,3 @@
package aid
type JSON map[string]interface{}

22
main.go
View File

@ -1,12 +1,12 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/config" "github.com/ectrc/snow/config"
"github.com/ectrc/snow/person" "github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
@ -49,7 +49,7 @@ func init() {
func init() { func init() {
if DROP_TABLES { if DROP_TABLES {
user := person.NewPerson() user := person.NewPerson()
snapshot := user.Snapshot() snapshot := user.AthenaProfile.Snapshot()
quest := person.NewQuest("Quest:Quest_1", "ChallengeBundle:Daily_1", "ChallengeBundleSchedule:Paid_1") quest := person.NewQuest("Quest:Quest_1", "ChallengeBundle:Daily_1", "ChallengeBundleSchedule:Paid_1")
{ {
@ -66,16 +66,16 @@ func init() {
giftBox := person.NewGift("GiftBox:GB_Default", 1, user.ID, "Hello, Bully!") 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) user.CommonCoreProfile.Gifts.AddGift(giftBox)
currency := person.NewItem("Currency:MtxPurchased", 100) currency := person.NewItem("Currency:MtxPurchased", 100)
user.CommonCoreProfile.Items.AddItem(currency) user.CommonCoreProfile.Items.AddItem(currency)
user.FindChanges(*snapshot)
user.Save() user.Save()
printjson(user.Snapshot()) user.AthenaProfile.Diff(snapshot)
aid.PrintJSON(user.CommonCoreProfile.Snapshot())
} }
go storage.Cache.CacheKiller() go storage.Cache.CacheKiller()
@ -88,19 +88,7 @@ func main() {
fmt.Println(person) fmt.Println(person)
} }
wait()
}
func wait() {
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc <-sc
} }
func printjson(v interface{}) {
json1, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(json1))
}

42
person/changes.go Normal file
View File

@ -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"`
}

View File

@ -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 { func FromDatabaseItem(item *storage.DB_Item, profileType *string) *Item {
variants := []*VariantChannel{} variants := []*VariantChannel{}

View File

@ -3,7 +3,6 @@ package person
import ( import (
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/r3labs/diff/v3"
) )
type Person struct { type Person struct {
@ -12,7 +11,6 @@ type Person struct {
AthenaProfile *Profile AthenaProfile *Profile
CommonCoreProfile *Profile CommonCoreProfile *Profile
Loadout *Loadout Loadout *Loadout
Changes []diff.Change
} }
type Option struct { type Option struct {
@ -24,8 +22,8 @@ func NewPerson() *Person {
return &Person{ return &Person{
ID: uuid.New().String(), ID: uuid.New().String(),
DisplayName: "Hello, Bully!", DisplayName: "Hello, Bully!",
AthenaProfile: NewProfile(), AthenaProfile: NewProfile("athena"),
CommonCoreProfile: NewProfile(), CommonCoreProfile: NewProfile("common_core"),
Loadout: NewLoadout(), Loadout: NewLoadout(),
} }
} }
@ -75,11 +73,6 @@ func (p *Person) Save() {
storage.Repo.SavePerson(p.ToDatabase()) 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 { func (p *Person) ToDatabase() *storage.DB_Person {
dbPerson := storage.DB_Person{ dbPerson := storage.DB_Person{
ID: p.ID, ID: p.ID,
@ -129,7 +122,3 @@ func (p *Person) ToDatabase() *storage.DB_Person {
return &dbPerson return &dbPerson
} }
func (p *Person) Snapshot() *PersonSnapshot {
return CreateSnapshot(p)
}

View File

@ -5,6 +5,7 @@ import (
"github.com/ectrc/snow/storage" "github.com/ectrc/snow/storage"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/r3labs/diff/v3"
) )
type Profile struct { type Profile struct {
@ -13,20 +14,23 @@ type Profile struct {
Gifts *GiftMutex Gifts *GiftMutex
Quests *QuestMutex Quests *QuestMutex
Attributes *sync.Map Attributes *sync.Map
Type string
Changes []diff.Change
} }
func NewProfile() *Profile { func NewProfile(profile string) *Profile {
return &Profile{ return &Profile{
ID: uuid.New().String(), ID: uuid.New().String(),
Items: NewItemMutex(), Items: NewItemMutex(profile),
Gifts: NewGiftMutex(), Gifts: NewGiftMutex(),
Quests: NewQuestMutex(), Quests: NewQuestMutex(),
Attributes: &sync.Map{}, Attributes: &sync.Map{},
Type: profile,
} }
} }
func FromDatabaseProfile(profile *storage.DB_Profile) *Profile { func FromDatabaseProfile(profile *storage.DB_Profile) *Profile {
items := NewItemMutex() items := NewItemMutex(profile.Type)
gifts := NewGiftMutex() gifts := NewGiftMutex()
quests := NewQuestMutex() quests := NewQuestMutex()
@ -53,6 +57,7 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile {
Gifts: gifts, Gifts: gifts,
Quests: quests, Quests: quests,
Attributes: attributes, 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 { type Loadout struct {
ID string ID string
Character string Character string

View File

@ -1,13 +1,5 @@
package person package person
type PersonSnapshot struct {
ID string
DisplayName string
AthenaProfile ProfileSnapshot
CommonCoreProfile ProfileSnapshot
Loadout Loadout
}
type ProfileSnapshot struct { type ProfileSnapshot struct {
ID string ID string
Items map[string]ItemSnapshot Items map[string]ItemSnapshot
@ -35,14 +27,3 @@ type GiftSnapshot struct {
Message string Message string
Loot []Item 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(),
}
}

View File

@ -6,13 +6,17 @@ import (
type ItemMutex struct { type ItemMutex struct {
sync.Map sync.Map
ProfileType string
} }
func NewItemMutex() *ItemMutex { func NewItemMutex(profile string) *ItemMutex {
return &ItemMutex{} return &ItemMutex{
ProfileType: profile,
}
} }
func (m *ItemMutex) AddItem(item *Item) { func (m *ItemMutex) AddItem(item *Item) {
item.ProfileType = m.ProfileType
m.Store(item.ID, item) m.Store(item.ID, item)
// storage.Repo.SaveItem(item) // storage.Repo.SaveItem(item)
} }
@ -48,6 +52,7 @@ func (m *ItemMutex) Count() int {
type GiftMutex struct { type GiftMutex struct {
sync.Map sync.Map
ProfileType string
} }
func NewGiftMutex() *GiftMutex { func NewGiftMutex() *GiftMutex {

View File

@ -13,7 +13,7 @@ Performance first, universal Fortnite backend written in Go.
```golang ```golang
user := person.NewPerson() user := person.NewPerson()
snapshot := user.Snapshot() snapshot := user.AthenaProfile.Snapshot()
quest := person.NewQuest("Quest:Quest_1", "ChallengeBundle:Daily_1", "ChallengeBundleSchedule:Paid_1") 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 := 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) user.CommonCoreProfile.Gifts.AddGift(giftBox)
currency := person.NewItem("Currency:MtxPurchased", 100) currency := person.NewItem("Currency:MtxPurchased", 100)
user.CommonCoreProfile.Items.AddItem(currency) user.CommonCoreProfile.Items.AddItem(currency)
user.FindChanges(*snapshot)
user.Save() user.Save()
user.AthenaProfile.Diff(snapshot)
``` ```
## What's next? ## What's next?