snow/person/profile.go

183 lines
3.8 KiB
Go
Raw Normal View History

2023-10-31 22:40:14 +00:00
package person
import (
2023-11-01 00:05:17 +00:00
"fmt"
2023-10-31 22:40:14 +00:00
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
"github.com/r3labs/diff/v3"
2023-10-31 22:40:14 +00:00
)
type Profile struct {
ID string
Items *ItemMutex
Gifts *GiftMutex
Quests *QuestMutex
2023-11-01 00:05:17 +00:00
Attributes *AttributeMutex
Type string
Changes []diff.Change
2023-10-31 22:40:14 +00:00
}
func NewProfile(profile string) *Profile {
2023-10-31 22:40:14 +00:00
return &Profile{
ID: uuid.New().String(),
Items: NewItemMutex(profile),
2023-10-31 22:40:14 +00:00
Gifts: NewGiftMutex(),
Quests: NewQuestMutex(),
2023-11-01 00:05:17 +00:00
Attributes: NewAttributeMutex(),
Type: profile,
2023-10-31 22:40:14 +00:00
}
}
func FromDatabaseProfile(profile *storage.DB_Profile) *Profile {
items := NewItemMutex(profile.Type)
2023-10-31 22:40:14 +00:00
gifts := NewGiftMutex()
quests := NewQuestMutex()
2023-11-01 00:05:17 +00:00
attributes := NewAttributeMutex()
2023-10-31 22:40:14 +00:00
for _, item := range profile.Items {
items.AddItem(FromDatabaseItem(&item, &profile.Type))
}
for _, gift := range profile.Gifts {
gifts.AddGift(FromDatabaseGift(&gift))
}
for _, quest := range profile.Quests {
quests.AddQuest(FromDatabaseQuest(&quest, &profile.Type))
}
for _, attribute := range profile.Attributes {
2023-11-01 00:05:17 +00:00
parsed := FromDatabaseAttribute(&attribute)
if parsed == nil {
fmt.Printf("error getting attribute from database")
continue
}
attributes.AddAttribute(parsed)
2023-10-31 22:40:14 +00:00
}
return &Profile{
ID: profile.ID,
Items: items,
Gifts: gifts,
Quests: quests,
Attributes: attributes,
Type: profile.Type,
2023-10-31 22:40:14 +00:00
}
}
func (p *Profile) Save() {
//storage.Repo.SaveProfile(p.ToDatabase())
}
func (p *Profile) Snapshot() *ProfileSnapshot {
items := map[string]ItemSnapshot{}
gifts := map[string]GiftSnapshot{}
quests := map[string]Quest{}
2023-11-01 00:05:17 +00:00
attributes := map[string]Attribute{}
2023-10-31 22:40:14 +00:00
p.Items.RangeItems(func(id string, item *Item) bool {
items[id] = item.Snapshot()
return true
})
p.Gifts.RangeGifts(func(id string, gift *Gift) bool {
gifts[id] = gift.Snapshot()
return true
})
p.Quests.RangeQuests(func(id string, quest *Quest) bool {
quests[id] = *quest
return true
})
2023-11-01 00:05:17 +00:00
p.Attributes.RangeAttributes(func(key string, value *Attribute) bool {
attributes[key] = *value
2023-10-31 22:40:14 +00:00
return true
})
return &ProfileSnapshot{
ID: p.ID,
Items: items,
Gifts: gifts,
Quests: quests,
2023-11-01 00:05:17 +00:00
Attributes: attributes,
2023-10-31 22:40:14 +00:00
}
}
func (p *Profile) Diff(snapshot *ProfileSnapshot) []diff.Change {
changes, _ := diff.Diff(p.Snapshot(), snapshot)
p.Changes = changes
return changes
}
2023-10-31 22:40:14 +00:00
type Loadout struct {
ID string
Character string
Backpack string
Pickaxe string
Glider string
Dances []string
ItemWraps []string
LoadingScreen string
SkyDiveContrail string
MusicPack string
BannerIcon string
BannerColor string
}
func NewLoadout() *Loadout {
return &Loadout{
ID: uuid.New().String(),
Character: "",
Backpack: "",
Pickaxe: "",
Glider: "",
Dances: []string{"", "", "", "", "", ""},
ItemWraps: []string{"", "", "", "", "", "", ""},
LoadingScreen: "",
SkyDiveContrail: "",
MusicPack: "",
BannerIcon: "",
BannerColor: "",
}
}
func FromDatabaseLoadout(loadout *storage.DB_Loadout) *Loadout {
return &Loadout{
ID: loadout.ID,
Character: loadout.Character,
Backpack: loadout.Backpack,
Pickaxe: loadout.Pickaxe,
Glider: loadout.Glider,
Dances: loadout.Dances,
ItemWraps: loadout.ItemWraps,
LoadingScreen: loadout.LoadingScreen,
SkyDiveContrail: loadout.SkyDiveContrail,
MusicPack: loadout.MusicPack,
BannerIcon: loadout.BannerIcon,
BannerColor: loadout.BannerColor,
}
}
func (l *Loadout) ToDatabase() *storage.DB_Loadout {
return &storage.DB_Loadout{
ID: l.ID,
Character: l.Character,
Backpack: l.Backpack,
Pickaxe: l.Pickaxe,
Glider: l.Glider,
Dances: l.Dances,
ItemWraps: l.ItemWraps,
LoadingScreen: l.LoadingScreen,
SkyDiveContrail: l.SkyDiveContrail,
MusicPack: l.MusicPack,
BannerIcon: l.BannerIcon,
BannerColor: l.BannerColor,
}
}
func (l *Loadout) Save() {
//storage.Repo.SaveLoadout(l.ToDatabase())
}