snow/person/person.go

339 lines
7.7 KiB
Go
Raw Normal View History

2023-10-31 22:40:14 +00:00
package person
import (
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
)
type Person struct {
ID string
2023-10-31 22:40:14 +00:00
DisplayName string
2023-12-14 19:47:54 +00:00
Permissions []string
IsBanned bool
AthenaProfile *Profile
2023-10-31 22:40:14 +00:00
CommonCoreProfile *Profile
CommonPublicProfile *Profile
Profile0Profile *Profile
CollectionsProfile *Profile
2023-12-08 15:35:00 +00:00
CreativeProfile *Profile
Discord *storage.DB_DiscordPerson
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-12-14 19:47:54 +00:00
Permissions: []string{},
IsBanned: false,
AthenaProfile: NewProfile("athena"),
CommonCoreProfile: NewProfile("common_core"),
CommonPublicProfile: NewProfile("common_public"),
Profile0Profile: NewProfile("profile0"),
CollectionsProfile: NewProfile("collections"),
2023-12-08 15:35:00 +00:00
CreativeProfile: NewProfile("creative"),
2023-10-31 22:40:14 +00:00
}
}
func Find(personId string) *Person {
if cache == nil {
cache = NewPersonsCacheMutex()
2023-10-31 22:40:14 +00:00
}
cachedPerson := cache.GetPerson(personId)
if cachedPerson != nil {
return cachedPerson
2023-10-31 22:40:14 +00:00
}
person := storage.Repo.GetPersonFromDB(personId)
if person == nil {
return nil
}
return findHelper(person)
}
func FindByDisplay(displayName string) *Person {
if cache == nil {
cache = NewPersonsCacheMutex()
}
cachedPerson := cache.GetPersonByDisplay(displayName)
if cachedPerson != nil {
return cachedPerson
}
person := storage.Repo.GetPersonByDisplayFromDB(displayName)
if person == nil {
return nil
}
return findHelper(person)
}
func FindByDiscord(discordId string) *Person {
if cache == nil {
cache = NewPersonsCacheMutex()
}
cachedPerson := cache.GetPersonByDiscordID(discordId)
if cachedPerson != nil {
return cachedPerson
}
person := storage.Repo.GetPersonByDiscordIDFromDB(discordId)
if person == nil {
return nil
}
return findHelper(person)
}
func findHelper(databasePerson *storage.DB_Person) *Person {
athenaProfile := NewProfile("athena")
commonCoreProfile := NewProfile("common_core")
commonPublicProfile := NewProfile("common_public")
profile0 := NewProfile("profile0")
collectionsProfile := NewProfile("collections")
2023-12-08 15:35:00 +00:00
creativeProfile := NewProfile("creative")
for _, profile := range databasePerson.Profiles {
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)
}
if profile.Type == "collections" {
collectionsProfile.ID = profile.ID
collectionsProfile = FromDatabaseProfile(&profile)
}
2023-12-08 15:35:00 +00:00
if profile.Type == "creative" {
creativeProfile.ID = profile.ID
creativeProfile = FromDatabaseProfile(&profile)
}
}
person := &Person{
ID: databasePerson.ID,
DisplayName: databasePerson.DisplayName,
2023-12-14 19:47:54 +00:00
Permissions: databasePerson.Permissions,
IsBanned: databasePerson.IsBanned,
AthenaProfile: athenaProfile,
CommonCoreProfile: commonCoreProfile,
CommonPublicProfile: commonPublicProfile,
Profile0Profile: profile0,
CollectionsProfile: collectionsProfile,
2023-12-08 15:35:00 +00:00
CreativeProfile: creativeProfile,
Discord: &databasePerson.Discord,
2023-10-31 22:40:14 +00:00
}
cache.SavePerson(person)
return person
2023-10-31 22:40:14 +00:00
}
func AllFromDatabase() []*Person {
var persons []*Person
for _, person := range storage.Repo.GetAllPersons() {
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
}
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":
return p.Profile0Profile
case "collections":
return p.CollectionsProfile
2023-12-08 15:35:00 +00:00
case "creative":
return p.CreativeProfile
}
return nil
}
2023-10-31 22:40:14 +00:00
func (p *Person) Save() {
dbPerson := p.ToDatabase()
storage.Repo.SavePerson(dbPerson)
2023-10-31 22:40:14 +00:00
}
2023-12-14 19:47:54 +00:00
func (p *Person) Ban() {
p.IsBanned = true
p.Save()
}
func (p *Person) Unban() {
p.IsBanned = false
p.Save()
}
func (p *Person) AddPermission(permission string) {
p.Permissions = append(p.Permissions, permission)
p.Save()
}
func (p *Person) RemovePermission(permission string) {
for i, perm := range p.Permissions {
if perm == permission {
p.Permissions = append(p.Permissions[:i], p.Permissions[i+1:]...)
break
}
}
p.Save()
}
func (p *Person) HasPermission(permission Permission) bool {
for _, perm := range p.Permissions {
if perm == string(PermissionAll) {
return true
}
if perm == string(permission) {
return true
}
}
return false
}
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,
2023-12-14 19:47:54 +00:00
Permissions: p.Permissions,
IsBanned: p.IsBanned,
2023-10-31 22:40:14 +00:00
Profiles: []storage.DB_Profile{},
2023-12-11 00:06:41 +00:00
Stats: []storage.DB_SeasonStat{},
2023-12-14 19:47:54 +00:00
Discord: storage.DB_DiscordPerson{},
2023-12-10 23:54:31 +00:00
}
if p.Discord != nil {
dbPerson.Discord = *p.Discord
2023-10-31 22:40:14 +00:00
}
2023-11-01 00:05:17 +00:00
profilesToConvert := map[string]*Profile{
"common_core": p.CommonCoreProfile,
"athena": p.AthenaProfile,
"common_public": p.CommonPublicProfile,
"profile0": p.Profile0Profile,
"collections": p.CollectionsProfile,
2023-12-08 15:35:00 +00:00
"creative": p.CreativeProfile,
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{},
Quests: []storage.DB_Quest{},
Loadouts: []storage.DB_Loadout{},
2023-10-31 22:40:14 +00:00
Attributes: []storage.DB_PAttribute{},
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
})
profile.Loadouts.RangeLoadouts(func(id string, loadout *Loadout) bool {
dbProfile.Loadouts = append(dbProfile.Loadouts, *loadout.ToDatabase(p.ID))
return true
})
2023-10-31 22:40:14 +00:00
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,
2023-12-14 19:47:54 +00:00
Permissions: p.Permissions,
IsBanned: p.IsBanned,
2023-11-01 00:05:17 +00:00
AthenaProfile: *p.AthenaProfile.Snapshot(),
CommonCoreProfile: *p.CommonCoreProfile.Snapshot(),
CommonPublicProfile: *p.CommonPublicProfile.Snapshot(),
Profile0Profile: *p.Profile0Profile.Snapshot(),
CollectionsProfile: *p.CollectionsProfile.Snapshot(),
2023-12-08 15:35:00 +00:00
CreativeProfile: *p.CreativeProfile.Snapshot(),
Discord: *p.Discord,
2023-11-01 00:05:17 +00:00
}
2023-12-13 22:52:16 +00:00
}
func (p *Person) Delete() {
storage.Repo.DeletePerson(p.ID)
}