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{}

28
main.go
View File

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

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 {
variants := []*VariantChannel{}

View File

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

View File

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

View File

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

View File

@ -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 {

View File

@ -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?