Fix Athena Profile has 0 anything!

This commit is contained in:
eccentric 2023-11-01 00:05:17 +00:00
parent 4cc7032ea0
commit 6b09cd5e82
9 changed files with 164 additions and 39 deletions

13
aid/aid.go Normal file
View File

@ -0,0 +1,13 @@
package aid
import (
"os"
"os/signal"
"syscall"
)
func WaitForExit() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}

21
main.go
View File

@ -1,11 +1,6 @@
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/config"
"github.com/ectrc/snow/person"
@ -71,11 +66,10 @@ func init() {
user.CommonCoreProfile.Gifts.AddGift(giftBox)
currency := person.NewItem("Currency:MtxPurchased", 100)
user.CommonCoreProfile.Items.AddItem(currency)
user.AthenaProfile.Items.AddItem(currency)
user.Save()
user.AthenaProfile.Diff(snapshot)
aid.PrintJSON(user.CommonCoreProfile.Snapshot())
}
go storage.Cache.CacheKiller()
@ -84,11 +78,14 @@ func init() {
func main() {
persons := person.AllFromDatabase()
for _, person := range persons {
fmt.Println(person)
for _, p := range persons {
p.AthenaProfile.Items.RangeItems(func(id string, item *person.Item) bool {
aid.PrintJSON(item)
return true
})
aid.PrintJSON(p.Snapshot())
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
aid.WaitForExit()
}

49
person/attribute.go Normal file
View File

@ -0,0 +1,49 @@
package person
import (
"encoding/json"
"github.com/ectrc/snow/storage"
)
type Attribute struct {
Key string
Value interface{}
Type string
}
func NewAttribute(key string, value interface{}, attributeType string) *Attribute {
return &Attribute{
Key: key,
Value: value,
Type: attributeType,
}
}
func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
var value interface{}
err := json.Unmarshal([]byte(db.ValueJSON), &value)
if err != nil {
return nil
}
return &Attribute{
Key: db.Key,
Value: value,
Type: db.Type,
}
}
func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
value, err := json.Marshal(a.Value)
if err != nil {
return nil
}
return &storage.DB_PAttribute{
ProfileID: profileId,
Key: a.Key,
ValueJSON: string(value),
Type: a.Type,
}
}

View File

@ -35,13 +35,13 @@ func FromDatabase(personId string) *Person {
}
loadout := FromDatabaseLoadout(&person.Loadout)
athenaProfile := &Profile{}
commonCoreProfile := &Profile{}
athenaProfile := NewProfile("athena")
commonCoreProfile := NewProfile("common_core")
for _, profile := range person.Profiles {
if profile.Type == "athena" {
athenaProfile := FromDatabaseProfile(&profile)
athenaProfile.ID = profile.ID
athenaProfile = FromDatabaseProfile(&profile)
}
if profile.Type == "common_core" {
@ -81,7 +81,10 @@ func (p *Person) ToDatabase() *storage.DB_Person {
Loadout: *p.Loadout.ToDatabase(),
}
profilesToConvert := map[string]*Profile{"athena": p.AthenaProfile, "common_core": p.CommonCoreProfile}
profilesToConvert := map[string]*Profile{
"common_core": p.CommonCoreProfile,
"athena": p.AthenaProfile,
}
for profileType, profile := range profilesToConvert {
dbProfile := storage.DB_Profile{
@ -108,12 +111,8 @@ func (p *Person) ToDatabase() *storage.DB_Person {
return true
})
profile.Attributes.Range(func(key, value interface{}) bool {
dbProfile.Attributes = append(dbProfile.Attributes, storage.DB_PAttribute{
ProfileID: profile.ID,
Key: key.(string),
Value: value.(string),
})
profile.Attributes.RangeAttributes(func(key string, value *Attribute) bool {
dbProfile.Attributes = append(dbProfile.Attributes, *value.ToDatabase(p.ID))
return true
})
@ -122,3 +121,26 @@ func (p *Person) ToDatabase() *storage.DB_Person {
return &dbPerson
}
func (p *Person) AddAttribute(value *Attribute) {
p.AthenaProfile.Attributes.AddAttribute(value)
}
func (p *Person) GetAttribute(key string) *Attribute {
attribute := p.AthenaProfile.Attributes.GetAttribute(key)
return attribute
}
func (p *Person) RemoveAttribute(key string) {
p.AthenaProfile.Attributes.DeleteAttribute(key)
}
func (p *Person) Snapshot() *PersonSnapshot {
return &PersonSnapshot{
ID: p.ID,
DisplayName: p.DisplayName,
AthenaProfile: *p.AthenaProfile.Snapshot(),
CommonCoreProfile:* p.CommonCoreProfile.Snapshot(),
Loadout: *p.Loadout,
}
}

View File

@ -1,7 +1,7 @@
package person
import (
"sync"
"fmt"
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
@ -13,7 +13,7 @@ type Profile struct {
Items *ItemMutex
Gifts *GiftMutex
Quests *QuestMutex
Attributes *sync.Map
Attributes *AttributeMutex
Type string
Changes []diff.Change
}
@ -24,7 +24,7 @@ func NewProfile(profile string) *Profile {
Items: NewItemMutex(profile),
Gifts: NewGiftMutex(),
Quests: NewQuestMutex(),
Attributes: &sync.Map{},
Attributes: NewAttributeMutex(),
Type: profile,
}
}
@ -33,6 +33,7 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile {
items := NewItemMutex(profile.Type)
gifts := NewGiftMutex()
quests := NewQuestMutex()
attributes := NewAttributeMutex()
for _, item := range profile.Items {
items.AddItem(FromDatabaseItem(&item, &profile.Type))
@ -46,9 +47,14 @@ func FromDatabaseProfile(profile *storage.DB_Profile) *Profile {
quests.AddQuest(FromDatabaseQuest(&quest, &profile.Type))
}
attributes := &sync.Map{}
for _, attribute := range profile.Attributes {
attributes.Store(attribute.Key, attribute.Value)
parsed := FromDatabaseAttribute(&attribute)
if parsed == nil {
fmt.Printf("error getting attribute from database")
continue
}
attributes.AddAttribute(parsed)
}
return &Profile{
@ -69,7 +75,7 @@ func (p *Profile) Snapshot() *ProfileSnapshot {
items := map[string]ItemSnapshot{}
gifts := map[string]GiftSnapshot{}
quests := map[string]Quest{}
attributes := map[string]string{}
attributes := map[string]Attribute{}
p.Items.RangeItems(func(id string, item *Item) bool {
items[id] = item.Snapshot()
@ -86,8 +92,8 @@ func (p *Profile) Snapshot() *ProfileSnapshot {
return true
})
p.Attributes.Range(func(key, value interface{}) bool {
attributes[key.(string)] = value.(string)
p.Attributes.RangeAttributes(func(key string, value *Attribute) bool {
attributes[key] = *value
return true
})
@ -96,7 +102,7 @@ func (p *Profile) Snapshot() *ProfileSnapshot {
Items: items,
Gifts: gifts,
Quests: quests,
Attributes: attributes,
Attributes: attributes,
}
}

View File

@ -1,11 +1,19 @@
package person
type PersonSnapshot struct {
ID string
DisplayName string
AthenaProfile ProfileSnapshot
CommonCoreProfile ProfileSnapshot
Loadout Loadout
}
type ProfileSnapshot struct {
ID string
Items map[string]ItemSnapshot
Gifts map[string]GiftSnapshot
Quests map[string]Quest
Attributes map[string]string
Attributes map[string]Attribute
}
type ItemSnapshot struct {

View File

@ -134,3 +134,36 @@ func (m *QuestMutex) Count() int {
})
return count
}
type AttributeMutex struct {
sync.Map
}
func NewAttributeMutex() *AttributeMutex {
return &AttributeMutex{}
}
func (m *AttributeMutex) AddAttribute(attribute *Attribute) {
m.Store(attribute.Key, attribute)
// storage.Repo.SaveAttribute(key, value)
}
func (m *AttributeMutex) DeleteAttribute(key string) {
m.Delete(key)
// storage.Repo.DeleteAttribute(key)
}
func (m *AttributeMutex) GetAttribute(key string) *Attribute {
value, ok := m.Load(key)
if !ok {
return nil
}
return value.(*Attribute)
}
func (m *AttributeMutex) RangeAttributes(f func(key string, value *Attribute) bool) {
m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(*Attribute))
})
}

View File

@ -35,8 +35,6 @@ func (s *PostgresStorage) GetPerson(personId string) *DB_Person {
Preload("Profiles.Items").
Preload("Profiles.Gifts").
Preload("Profiles.Quests").
Preload("Profiles.Items.Variants").
Preload("Profiles.Gifts.Loot").
Find(&dbPerson)
if dbPerson.ID == "" {
@ -57,8 +55,6 @@ func (s *PostgresStorage) GetAllPersons() []*DB_Person {
Preload("Profiles.Items").
Preload("Profiles.Gifts").
Preload("Profiles.Quests").
Preload("Profiles.Items.Variants").
Preload("Profiles.Gifts.Loot").
Find(&dbPersons)
return dbPersons

View File

@ -54,7 +54,8 @@ func (DB_Profile) TableName() string {
type DB_PAttribute struct {
Key string `gorm:"primary_key"`
ProfileID string
Value string
ValueJSON string
Type string
}
func (DB_PAttribute) TableName() string {