2023-10-31 22:40:14 +00:00
|
|
|
package person
|
|
|
|
|
|
|
|
import (
|
2023-11-05 01:58:00 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
"github.com/ectrc/snow/storage"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Person struct {
|
2023-11-05 01:58:00 +00:00
|
|
|
ID string
|
2023-10-31 22:40:14 +00:00
|
|
|
DisplayName string
|
2023-11-03 23:48:50 +00:00
|
|
|
AccessKey string
|
2023-11-05 01:58:00 +00:00
|
|
|
AthenaProfile *Profile
|
2023-10-31 22:40:14 +00:00
|
|
|
CommonCoreProfile *Profile
|
2023-11-03 23:48:50 +00:00
|
|
|
CommonPublicProfile *Profile
|
2023-11-06 22:41:52 +00:00
|
|
|
Profile0Profile *Profile
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPerson() *Person {
|
|
|
|
return &Person{
|
|
|
|
ID: uuid.New().String(),
|
2023-11-01 01:24:42 +00:00
|
|
|
DisplayName: uuid.New().String(),
|
2023-11-03 23:48:50 +00:00
|
|
|
AccessKey: "",
|
2023-10-31 23:19:52 +00:00
|
|
|
AthenaProfile: NewProfile("athena"),
|
|
|
|
CommonCoreProfile: NewProfile("common_core"),
|
2023-11-03 23:48:50 +00:00
|
|
|
CommonPublicProfile: NewProfile("common_public"),
|
2023-11-06 22:41:52 +00:00
|
|
|
Profile0Profile: NewProfile("profile0"),
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
func Find(personId string) *Person {
|
2023-11-05 01:58:00 +00:00
|
|
|
if cache == nil {
|
|
|
|
cache = NewPersonsCacheMutex()
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
cachedPerson := cache.GetPerson(personId)
|
|
|
|
if cachedPerson != nil {
|
|
|
|
return cachedPerson
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
|
|
|
|
person := storage.Repo.GetPersonFromDB(personId)
|
|
|
|
if person == nil {
|
|
|
|
return nil
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
|
|
|
|
return findHelper(person)
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FindByDisplay(displayName string) *Person {
|
2023-11-05 01:58:00 +00:00
|
|
|
if cache == nil {
|
|
|
|
cache = NewPersonsCacheMutex()
|
|
|
|
}
|
|
|
|
|
|
|
|
person := storage.Repo.GetPersonByDisplayFromDB(displayName)
|
2023-11-03 23:48:50 +00:00
|
|
|
if person == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
cachedPerson := cache.GetPerson(person.ID)
|
|
|
|
if cachedPerson != nil {
|
|
|
|
return cachedPerson
|
|
|
|
}
|
|
|
|
|
|
|
|
return findHelper(person)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findHelper(databasePerson *storage.DB_Person) *Person {
|
2023-11-03 23:48:50 +00:00
|
|
|
athenaProfile := NewProfile("athena")
|
|
|
|
commonCoreProfile := NewProfile("common_core")
|
|
|
|
commonPublicProfile := NewProfile("common_public")
|
|
|
|
profile0 := NewProfile("profile0")
|
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
for _, profile := range databasePerson.Profiles {
|
2023-11-03 23:48:50 +00:00
|
|
|
if profile.Type == "athena" {
|
|
|
|
athenaProfile.ID = profile.ID
|
|
|
|
athenaProfile = FromDatabaseProfile(&profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile.Type == "common_core" {
|
|
|
|
commonCoreProfile.ID = profile.ID
|
|
|
|
commonCoreProfile = FromDatabaseProfile(&profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile.Type == "common_public" {
|
|
|
|
commonPublicProfile.ID = profile.ID
|
|
|
|
commonPublicProfile = FromDatabaseProfile(&profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile.Type == "profile0" {
|
|
|
|
profile0.ID = profile.ID
|
|
|
|
profile0 = FromDatabaseProfile(&profile)
|
|
|
|
}
|
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
|
|
|
|
person := &Person{
|
|
|
|
ID: databasePerson.ID,
|
|
|
|
DisplayName: databasePerson.DisplayName,
|
|
|
|
AccessKey: databasePerson.AccessKey,
|
2023-11-03 23:48:50 +00:00
|
|
|
AthenaProfile: athenaProfile,
|
|
|
|
CommonCoreProfile: commonCoreProfile,
|
|
|
|
CommonPublicProfile: commonPublicProfile,
|
2023-11-06 22:41:52 +00:00
|
|
|
Profile0Profile: profile0,
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
|
|
|
|
cache.Store(person.ID, &CacheEntry{
|
|
|
|
Entry: person,
|
|
|
|
LastAccessed: time.Now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return person
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AllFromDatabase() []*Person {
|
|
|
|
var persons []*Person
|
|
|
|
for _, person := range storage.Repo.GetAllPersons() {
|
2023-11-03 23:48:50 +00:00
|
|
|
persons = append(persons, Find(person.ID))
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return persons
|
|
|
|
}
|
|
|
|
|
2023-11-05 02:43:21 +00:00
|
|
|
func AllFromCache() []*Person {
|
|
|
|
if cache == nil {
|
|
|
|
cache = NewPersonsCacheMutex()
|
|
|
|
}
|
|
|
|
|
|
|
|
var persons []*Person
|
|
|
|
cache.RangeEntry(func(key string, value *CacheEntry) bool {
|
|
|
|
persons = append(persons, value.Entry)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return persons
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
func (p *Person) GetProfileFromType(profileType string) *Profile {
|
|
|
|
switch profileType {
|
|
|
|
case "athena":
|
|
|
|
return p.AthenaProfile
|
|
|
|
case "common_core":
|
|
|
|
return p.CommonCoreProfile
|
|
|
|
case "common_public":
|
|
|
|
return p.CommonPublicProfile
|
|
|
|
case "profile0":
|
2023-11-06 22:41:52 +00:00
|
|
|
return p.Profile0Profile
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:40:14 +00:00
|
|
|
func (p *Person) Save() {
|
2023-11-05 01:58:00 +00:00
|
|
|
dbPerson := p.ToDatabase()
|
|
|
|
storage.Repo.SavePerson(dbPerson)
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Person) ToDatabase() *storage.DB_Person {
|
|
|
|
dbPerson := storage.DB_Person{
|
|
|
|
ID: p.ID,
|
|
|
|
DisplayName: p.DisplayName,
|
|
|
|
Profiles: []storage.DB_Profile{},
|
2023-11-03 23:48:50 +00:00
|
|
|
AccessKey: p.AccessKey,
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 00:05:17 +00:00
|
|
|
profilesToConvert := map[string]*Profile{
|
|
|
|
"common_core": p.CommonCoreProfile,
|
2023-11-03 23:48:50 +00:00
|
|
|
"athena": p.AthenaProfile,
|
|
|
|
"common_public": p.CommonPublicProfile,
|
2023-11-06 22:41:52 +00:00
|
|
|
"profile0": p.Profile0Profile,
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
2023-10-31 22:40:14 +00:00
|
|
|
|
|
|
|
for profileType, profile := range profilesToConvert {
|
|
|
|
dbProfile := storage.DB_Profile{
|
|
|
|
ID: profile.ID,
|
|
|
|
PersonID: p.ID,
|
|
|
|
Type: profileType,
|
|
|
|
Items: []storage.DB_Item{},
|
|
|
|
Gifts: []storage.DB_Gift{},
|
2023-11-05 01:58:00 +00:00
|
|
|
Quests: []storage.DB_Quest{},
|
2023-10-31 22:40:14 +00:00
|
|
|
Attributes: []storage.DB_PAttribute{},
|
2023-11-05 01:58:00 +00:00
|
|
|
Revision: profile.Revision,
|
2023-10-31 22:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
profile.Items.RangeItems(func(id string, item *Item) bool {
|
|
|
|
dbProfile.Items = append(dbProfile.Items, *item.ToDatabase(p.ID))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
profile.Gifts.RangeGifts(func(id string, gift *Gift) bool {
|
|
|
|
dbProfile.Gifts = append(dbProfile.Gifts, *gift.ToDatabase(p.ID))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
profile.Quests.RangeQuests(func(id string, quest *Quest) bool {
|
|
|
|
dbProfile.Quests = append(dbProfile.Quests, *quest.ToDatabase(p.ID))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2023-11-01 00:05:17 +00:00
|
|
|
profile.Attributes.RangeAttributes(func(key string, value *Attribute) bool {
|
|
|
|
dbProfile.Attributes = append(dbProfile.Attributes, *value.ToDatabase(p.ID))
|
2023-10-31 22:40:14 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
dbPerson.Profiles = append(dbPerson.Profiles, dbProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dbPerson
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2023-11-05 01:58:00 +00:00
|
|
|
CommonCoreProfile: *p.CommonCoreProfile.Snapshot(),
|
2023-11-06 22:41:52 +00:00
|
|
|
CommonPublicProfile: *p.CommonPublicProfile.Snapshot(),
|
|
|
|
Profile0Profile: *p.Profile0Profile.Snapshot(),
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
|
|
|
}
|