From f6056108a2345b19506568dfbaaecc6a98fd149f Mon Sep 17 00:00:00 2001 From: Eccentric Date: Wed, 3 Jan 2024 19:51:46 +0000 Subject: [PATCH] add server actions --- discord/admin.go | 7 ++-- handlers/{profile.go => client.go} | 48 ++++++++++++------------- handlers/server.go | 58 ++++++++++++++++++++++++++++++ main.go | 4 +-- 4 files changed, 88 insertions(+), 29 deletions(-) rename handlers/{profile.go => client.go} (84%) create mode 100644 handlers/server.go diff --git a/discord/admin.go b/discord/admin.go index 2d99601..bf99060 100644 --- a/discord/admin.go +++ b/discord/admin.go @@ -317,13 +317,14 @@ func giveEverythingHandler(s *discordgo.Session, i *discordgo.InteractionCreate) return } - s.InteractionResponseEdit(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseDefferedChannelMessageWithSource, + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, }) fortnite.GiveEverything(player) + str := player.DisplayName + "has been granted everything." s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ - Content: player.DisplayName + " has been granted everything.", + Content: &str, }) } \ No newline at end of file diff --git a/handlers/profile.go b/handlers/client.go similarity index 84% rename from handlers/profile.go rename to handlers/client.go index c3dbc88..508804e 100644 --- a/handlers/profile.go +++ b/handlers/client.go @@ -14,21 +14,21 @@ import ( ) var ( - profileActions = map[string]func(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { - "QueryProfile": PostQueryProfileAction, - "ClientQuestLogin": PostQueryProfileAction, - "MarkItemSeen": PostMarkItemSeenAction, - "SetItemFavoriteStatusBatch": PostSetItemFavoriteStatusBatchAction, - "EquipBattleRoyaleCustomization": PostEquipBattleRoyaleCustomizationAction, - "SetBattleRoyaleBanner": PostSetBattleRoyaleBannerAction, - "SetCosmeticLockerSlot": PostSetCosmeticLockerSlotAction, - "SetCosmeticLockerBanner": PostSetCosmeticLockerBannerAction, - "PurchaseCatalogEntry": PostPurchaseCatalogEntryAction, + clientActions = map[string]func(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { + "QueryProfile": clientQueryProfileAction, + "ClientQuestLogin": clientQueryProfileAction, + "MarkItemSeen": clientMarkItemSeenAction, + "SetItemFavoriteStatusBatch": clientSetItemFavoriteStatusBatchAction, + "EquipBattleRoyaleCustomization": clientEquipBattleRoyaleCustomizationAction, + "SetBattleRoyaleBanner": clientSetBattleRoyaleBannerAction, + "SetCosmeticLockerSlot": clientSetCosmeticLockerSlotAction, + "SetCosmeticLockerBanner": clientSetCosmeticLockerBannerAction, + "PurchaseCatalogEntry": clientPurchaseCatalogEntryAction, } ) -func PostProfileAction(c *fiber.Ctx) error { - person := p.Find(c.Params("accountId")) +func PostClientProfileAction(c *fiber.Ctx) error { + person := c.Locals("person").(*p.Person) if person == nil { return c.Status(404).JSON(aid.ErrorBadRequest("No Account Found")) } @@ -52,7 +52,7 @@ func PostProfileAction(c *fiber.Ctx) error { notifications := []aid.JSON{} - action, ok := profileActions[c.Params("action")]; + action, ok := clientActions[c.Params("action")]; if ok && profile != nil { if err := action(c, person, profile, ¬ifications); err != nil { return c.Status(400).JSON(aid.ErrorBadRequest(err.Error())) @@ -107,9 +107,9 @@ func PostProfileAction(c *fiber.Ctx) error { return c.Status(200).JSON(aid.JSON{ "profileId": c.Query("profileId"), - "profileRevision": revision, - "profileCommandRevision": revision, - "profileChangesBaseRevision": revision - 1, + "profileRevision": profile.Revision, + "profileCommandRevision": profile.Revision, + "profileChangesBaseRevision": profile.Revision - 1, "profileChanges": profile.Changes, "multiUpdate": multiUpdate, "notifications": notifications, @@ -118,12 +118,12 @@ func PostProfileAction(c *fiber.Ctx) error { }) } -func PostQueryProfileAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientQueryProfileAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { profile.CreateFullProfileUpdateChange() return nil } -func PostMarkItemSeenAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientMarkItemSeenAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { ItemIds []string `json:"itemIds"` } @@ -146,7 +146,7 @@ func PostMarkItemSeenAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, return nil } -func PostEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { SlotName string `json:"slotName" binding:"required"` ItemToSlot string `json:"itemToSlot"` @@ -191,7 +191,7 @@ func PostEquipBattleRoyaleCustomizationAction(c *fiber.Ctx, person *p.Person, pr return nil } -func PostSetBattleRoyaleBannerAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientSetBattleRoyaleBannerAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { HomebaseBannerColorID string `json:"homebaseBannerColorId" binding:"required"` HomebaseBannerIconID string `json:"homebaseBannerIconId" binding:"required"` @@ -232,7 +232,7 @@ func PostSetBattleRoyaleBannerAction(c *fiber.Ctx, person *p.Person, profile *p. return nil } -func PostSetItemFavoriteStatusBatchAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientSetItemFavoriteStatusBatchAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { ItemIds []string `json:"itemIds" binding:"required"` Favorite []bool `json:"itemFavStatus" binding:"required"` @@ -256,7 +256,7 @@ func PostSetItemFavoriteStatusBatchAction(c *fiber.Ctx, person *p.Person, profil return nil } -func PostSetCosmeticLockerSlotAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientSetCosmeticLockerSlotAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { Category string `json:"category" binding:"required"` // item type e.g. Character ItemToSlot string `json:"itemToSlot" binding:"required"` // template id @@ -325,7 +325,7 @@ func PostSetCosmeticLockerSlotAction(c *fiber.Ctx, person *p.Person, profile *p. return nil } -func PostSetCosmeticLockerBannerAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientSetCosmeticLockerBannerAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct { LockerItem string `json:"lockerItem" binding:"required"` // locker id BannerColorTemplateName string `json:"bannerColorTemplateName" binding:"required"` // template id @@ -361,7 +361,7 @@ func PostSetCosmeticLockerBannerAction(c *fiber.Ctx, person *p.Person, profile * return nil } -func PostPurchaseCatalogEntryAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { +func clientPurchaseCatalogEntryAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, notifications *[]aid.JSON) error { var body struct{ OfferID string `json:"offerId" binding:"required"` PurchaseQuantity int `json:"purchaseQuantity" binding:"required"` diff --git a/handlers/server.go b/handlers/server.go new file mode 100644 index 0000000..7b6d76c --- /dev/null +++ b/handlers/server.go @@ -0,0 +1,58 @@ +package handlers + +import ( + "time" + + "github.com/ectrc/snow/aid" + p "github.com/ectrc/snow/person" + "github.com/gofiber/fiber/v2" +) + +var ( + serverActions = map[string]func(c *fiber.Ctx, person *p.Person, profile *p.Profile, profileChanges, multiUpdate, notifications *[]aid.JSON) error{ + "QueryProfile": serverQueryProfileAction, + } +) + +func PostServerProfileAction(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")) + if profile == nil { + return c.Status(404).JSON(aid.ErrorBadRequest("No Profile Found")) + } + + profileChanges := []aid.JSON{} + multiUpdate := []aid.JSON{} + notifications := []aid.JSON{} + + if action, ok := serverActions[c.Query("action")]; ok { + if err := action(c, person, profile, &profileChanges, &multiUpdate, ¬ifications); err != nil { + return c.Status(500).JSON(aid.ErrorBadRequest(err.Error())) + } + } + + return c.Status(200).JSON(aid.JSON{ + "profileId": c.Query("profileId"), + "profileRevision": profile.Revision, + "profileCommandRevision": profile.Revision, + "profileChangesBaseRevision": profile.Revision - 1, + "profileChanges": []aid.JSON{}, + "multiUpdate": []aid.JSON{}, + "notifications": []aid.JSON{}, + "responseVersion": 1, + "serverTime": time.Now().Format("2006-01-02T15:04:05.999Z"), + }) +} + +func serverQueryProfileAction(c *fiber.Ctx, person *p.Person, profile *p.Profile, profileChanges, multiUpdate, notifications *[]aid.JSON) error { + *profileChanges = append(*profileChanges, aid.JSON{ + "changeType": "fullProfileUpdate", + "profile": profile.GenerateFortniteProfileEntry(), + }) + + return nil +} \ No newline at end of file diff --git a/main.go b/main.go index a51a3dc..00e951a 100644 --- a/main.go +++ b/main.go @@ -107,8 +107,8 @@ func main() { profile := game.Group("/profile/:accountId") profile.Use(handlers.MiddlewareFortnite) - profile.Post("/client/:action", handlers.PostProfileAction) - profile.Post("/dedicated_server/:action", handlers.PostProfileAction) + profile.Post("/client/:action", handlers.PostClientProfileAction) + profile.Post("/dedicated_server/:action", handlers.PostServerProfileAction) lightswitch := r.Group("/lightswitch/api") lightswitch.Use(handlers.MiddlewareFortnite)