2023-11-03 23:48:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-11-09 19:35:39 +00:00
|
|
|
"fmt"
|
2023-11-05 01:58:00 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-11-03 23:48:50 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ectrc/snow/aid"
|
|
|
|
p "github.com/ectrc/snow/person"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
profileActions = map[string]func(c *fiber.Ctx, person *p.Person, profile *p.Profile) error {
|
|
|
|
"QueryProfile": PostQueryProfileAction,
|
|
|
|
"ClientQuestLogin": PostQueryProfileAction,
|
2023-11-05 01:58:00 +00:00
|
|
|
"MarkItemSeen": PostMarkItemSeenAction,
|
|
|
|
"EquipBattleRoyaleCustomization": PostEquipBattleRoyaleCustomizationAction,
|
2023-11-09 19:35:39 +00:00
|
|
|
"SetBattleRoyaleBanner": PostSetBattleRoyaleBannerAction,
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func PostProfileAction(c *fiber.Ctx) error {
|
|
|
|
person := p.Find(c.Params("accountId"))
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(404).JSON(aid.ErrorBadRequest("No Account Found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
profile := person.GetProfileFromType(c.Query("profileId"))
|
2023-11-05 01:58:00 +00:00
|
|
|
defer profile.ClearProfileChanges()
|
2023-11-03 23:48:50 +00:00
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
before := profile.Snapshot()
|
2023-11-03 23:48:50 +00:00
|
|
|
if action, ok := profileActions[c.Params("action")]; ok {
|
2023-11-05 01:58:00 +00:00
|
|
|
if err := action(c, person, profile); err != nil {
|
2023-11-09 18:53:19 +00:00
|
|
|
return c.Status(400).JSON(aid.ErrorBadRequest(err.Error()))
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-05 02:08:16 +00:00
|
|
|
profile.Diff(before)
|
2023-11-05 01:58:00 +00:00
|
|
|
|
|
|
|
revision, _ := strconv.Atoi(c.Query("rvn"))
|
|
|
|
if revision == -1 {
|
|
|
|
revision = profile.Revision
|
|
|
|
}
|
|
|
|
revision++
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
return c.Status(200).JSON(aid.JSON{
|
|
|
|
"profileId": profile.Type,
|
2023-11-05 01:58:00 +00:00
|
|
|
"profileRevision": revision,
|
|
|
|
"profileCommandRevision": revision,
|
|
|
|
"profileChangesBaseRevision": revision - 1,
|
2023-11-03 23:48:50 +00:00
|
|
|
"profileChanges": profile.Changes,
|
|
|
|
"multiUpdate": []aid.JSON{},
|
|
|
|
"notifications": []aid.JSON{},
|
|
|
|
"responseVersion": 1,
|
|
|
|
"serverTime": time.Now().Format("2006-01-02T15:04:05.999Z"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostQueryProfileAction(c *fiber.Ctx, person *p.Person, profile *p.Profile) error {
|
|
|
|
profile.CreateFullProfileUpdateChange()
|
2023-11-05 01:58:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostMarkItemSeenAction(c *fiber.Ctx, person *p.Person, profile *p.Profile) error {
|
|
|
|
var body struct {
|
|
|
|
ItemIds []string `json:"itemIds"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&body)
|
|
|
|
if err != nil {
|
2023-11-09 19:35:39 +00:00
|
|
|
return fmt.Errorf("invalid Body")
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, itemId := range body.ItemIds {
|
|
|
|
item := profile.Items.GetItem(itemId)
|
|
|
|
if item == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
item.HasSeen = true
|
2023-11-05 02:43:21 +00:00
|
|
|
go item.Save()
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, profile *p.Profile) error {
|
|
|
|
var body struct {
|
2023-11-09 18:53:19 +00:00
|
|
|
SlotName string `json:"slotName" binding:"required"`
|
2023-11-05 01:58:00 +00:00
|
|
|
ItemToSlot string `json:"itemToSlot"`
|
|
|
|
IndexWithinSlot int `json:"indexWithinSlot"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&body)
|
|
|
|
if err != nil {
|
2023-11-09 19:35:39 +00:00
|
|
|
return fmt.Errorf("invalid Body")
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item := profile.Items.GetItem(body.ItemToSlot)
|
|
|
|
if item == nil {
|
2023-11-09 19:35:39 +00:00
|
|
|
if body.ItemToSlot != "" && !strings.Contains(strings.ToLower(body.ItemToSlot), "random") {
|
|
|
|
return fmt.Errorf("item not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
item = &p.Item{
|
|
|
|
ID: body.ItemToSlot,
|
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attr := profile.Attributes.GetAttributeByKey("favorite_" + strings.ToLower(body.SlotName))
|
|
|
|
if attr == nil {
|
2023-11-09 19:35:39 +00:00
|
|
|
return fmt.Errorf("attribute not found")
|
2023-11-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch body.SlotName {
|
|
|
|
case "Dance":
|
|
|
|
value := aid.JSONParse(attr.ValueJSON)
|
|
|
|
value.([]any)[body.IndexWithinSlot] = item.ID
|
|
|
|
attr.ValueJSON = aid.JSONStringify(value)
|
|
|
|
case "ItemWrap":
|
|
|
|
value := aid.JSONParse(attr.ValueJSON)
|
|
|
|
value.([]any)[body.IndexWithinSlot] = item.ID
|
|
|
|
attr.ValueJSON = aid.JSONStringify(value)
|
|
|
|
default:
|
|
|
|
attr.ValueJSON = aid.JSONStringify(item.ID)
|
|
|
|
}
|
|
|
|
|
2023-11-09 19:35:39 +00:00
|
|
|
go attr.Save()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostSetBattleRoyaleBannerAction(c *fiber.Ctx, person *p.Person, profile *p.Profile) error {
|
|
|
|
var body struct {
|
|
|
|
HomebaseBannerColorID string `json:"homebaseBannerColorId" binding:"required"`
|
|
|
|
HomebaseBannerIconID string `json:"homebaseBannerIconId" binding:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid Body")
|
|
|
|
}
|
|
|
|
|
|
|
|
colorItem := person.CommonCoreProfile.Items.GetItemByTemplateID("HomebaseBannerColor:"+body.HomebaseBannerColorID)
|
|
|
|
if colorItem == nil {
|
|
|
|
return fmt.Errorf("color item not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
iconItem := person.CommonCoreProfile.Items.GetItemByTemplateID("HomebaseBannerIcon:"+body.HomebaseBannerIconID)
|
|
|
|
if iconItem == nil {
|
|
|
|
return fmt.Errorf("icon item not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
iconAttr := profile.Attributes.GetAttributeByKey("banner_icon")
|
|
|
|
if iconAttr == nil {
|
|
|
|
return fmt.Errorf("icon attribute not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
colorAttr := profile.Attributes.GetAttributeByKey("banner_color")
|
|
|
|
if colorAttr == nil {
|
|
|
|
return fmt.Errorf("color attribute not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
iconAttr.ValueJSON = aid.JSONStringify(strings.Split(iconItem.TemplateID, ":")[1])
|
|
|
|
colorAttr.ValueJSON = aid.JSONStringify(strings.Split(colorItem.TemplateID, ":")[1])
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
iconAttr.Save()
|
|
|
|
colorAttr.Save()
|
|
|
|
}()
|
2023-11-03 23:48:50 +00:00
|
|
|
return nil
|
|
|
|
}
|