2023-11-03 23:48:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
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-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"))
|
|
|
|
}
|
2023-11-05 01:58:00 +00:00
|
|
|
defer person.Save()
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
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-03 23:48:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Body"))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, itemId := range body.ItemIds {
|
|
|
|
item := profile.Items.GetItem(itemId)
|
|
|
|
if item == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
item.HasSeen = true
|
2023-11-05 02:08:16 +00:00
|
|
|
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 {
|
|
|
|
SlotName string `json:"slotName"`
|
|
|
|
ItemToSlot string `json:"itemToSlot"`
|
|
|
|
IndexWithinSlot int `json:"indexWithinSlot"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&body)
|
|
|
|
if err != nil {
|
|
|
|
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Body"))
|
|
|
|
}
|
|
|
|
|
|
|
|
item := profile.Items.GetItem(body.ItemToSlot)
|
|
|
|
if item == nil {
|
|
|
|
return c.Status(400).JSON(aid.ErrorBadRequest("Item not found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := profile.Attributes.GetAttributeByKey("favorite_" + strings.ToLower(body.SlotName))
|
|
|
|
if attr == nil {
|
|
|
|
return c.Status(400).JSON(aid.ErrorBadRequest("Attribute not found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 02:08:16 +00:00
|
|
|
attr.Save()
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
return nil
|
|
|
|
}
|