snow/person/gift.go

144 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-10-31 22:40:14 +00:00
package person
import (
"time"
2023-11-02 17:50:52 +00:00
"github.com/ectrc/snow/aid"
2023-10-31 22:40:14 +00:00
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
)
type Gift struct {
ID string
ProfileID string
2023-10-31 22:40:14 +00:00
TemplateID string
Quantity int
FromID string
GiftedAt int64
Message string
Loot []*Item
2023-10-31 22:40:14 +00:00
}
func NewGift(templateID string, quantity int, fromID string, message string) *Gift {
return &Gift{
ID: uuid.New().String(),
2023-10-31 22:40:14 +00:00
TemplateID: templateID,
Quantity: quantity,
FromID: fromID,
GiftedAt: time.Now().Unix(),
Message: message,
Loot: []*Item{},
2023-10-31 22:40:14 +00:00
}
}
func FromDatabaseGift(gift *storage.DB_Gift) *Gift {
loot := []*Item{}
for _, item := range gift.Loot {
2024-02-04 15:21:16 +00:00
loot = append(loot, FromDatabaseGiftLoot(&item))
2023-10-31 22:40:14 +00:00
}
return &Gift{
ID: gift.ID,
ProfileID: gift.ProfileID,
2023-10-31 22:40:14 +00:00
TemplateID: gift.TemplateID,
Quantity: gift.Quantity,
FromID: gift.FromID,
GiftedAt: gift.GiftedAt,
Message: gift.Message,
Loot: loot,
2023-10-31 22:40:14 +00:00
}
}
2023-11-02 17:50:52 +00:00
func (g *Gift) GenerateFortniteGiftEntry() aid.JSON {
json := aid.JSON{
"templateId": g.TemplateID,
"attributes": aid.JSON{
"params": aid.JSON{},
"lootList": []aid.JSON{},
"fromAccountId": g.FromID,
"giftedOn": time.Unix(g.GiftedAt, 0).Format(time.RFC3339),
},
"quantity": 1,
}
for _, loot := range g.Loot {
json["attributes"].(aid.JSON)["lootList"] = append(json["attributes"].(aid.JSON)["lootList"].([]aid.JSON), aid.JSON{
"itemGuid": loot.ID,
"itemType": loot.TemplateID,
"itemProfile": loot.ProfileType,
"quantity": loot.Quantity,
})
}
if g.Message != "" {
json["attributes"].(aid.JSON)["params"].(aid.JSON)["userMessage"] = g.Message
}
return json
}
2023-10-31 22:40:14 +00:00
func (g *Gift) AddLoot(loot *Item) {
g.Loot = append(g.Loot, loot)
//storage.Repo.SaveGiftLoot(g.ID, loot)
}
func (g *Gift) FillLoot(loot []*Item) {
g.Loot = loot
}
func (g *Gift) Delete() {
for _, item := range g.Loot {
item.DeleteLoot()
}
storage.Repo.DeleteGift(g.ID)
2023-10-31 22:40:14 +00:00
}
func (g *Gift) ToDatabase(profileId string) *storage.DB_Gift {
2024-02-04 02:05:31 +00:00
profileLoot := []storage.DB_GiftLoot{}
2023-10-31 22:40:14 +00:00
for _, item := range g.Loot {
2024-02-04 15:21:16 +00:00
profileLoot = append(profileLoot, *item.ToGiftLootDatabase(g.ID))
2023-10-31 22:40:14 +00:00
}
return &storage.DB_Gift{
ID: g.ID,
2023-10-31 22:40:14 +00:00
ProfileID: profileId,
TemplateID: g.TemplateID,
Quantity: g.Quantity,
FromID: g.FromID,
GiftedAt: g.GiftedAt,
Message: g.Message,
Loot: profileLoot,
2023-10-31 22:40:14 +00:00
}
}
func (g *Gift) Save() {
if g.ProfileID == "" {
return
}
storage.Repo.SaveGift(g.ToDatabase(g.ProfileID))
2024-02-19 01:49:14 +00:00
for _, item := range g.Loot {
item.SaveLoot(g.ID)
}
2023-10-31 22:40:14 +00:00
}
func (g *Gift) Snapshot() GiftSnapshot {
loot := []Item{}
for _, item := range g.Loot {
loot = append(loot, *item)
}
return GiftSnapshot{
ID: g.ID,
2023-10-31 22:40:14 +00:00
TemplateID: g.TemplateID,
Quantity: g.Quantity,
FromID: g.FromID,
GiftedAt: g.GiftedAt,
Message: g.Message,
Loot: loot,
2023-10-31 22:40:14 +00:00
}
}