module Degenz.Store open System.Threading.Tasks open DSharpPlus.Entities open DSharpPlus open DSharpPlus.EventArgs open DSharpPlus.SlashCommands open Degenz open Degenz.Embeds open Degenz.Messaging let viewStore (ctx : InteractionContext) = async { do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, Embeds.storeListing Armory.battleItems) |> Async.AwaitTask } |> Async.StartAsTask :> Task let buyItem (ctx : InteractionContext) itemId = Game.executePlayerInteraction ctx (fun player -> async { let item = Armory.getItem itemId let newBalance = player.Bank - item.Cost if newBalance >= 0 then let playerHasItem = player.Arsenal |> Array.exists (fun w -> item.Id = w.Id) if not playerHasItem then let p = { player with Bank = newBalance ; Arsenal = Array.append [| item |] player.Arsenal } do! DbService.updatePlayer p do! sendFollowUpMessageFromCtx ctx $"Successfully purchased {item.Name}! You now have {newBalance} 💰$GBT remaining" else do! sendFollowUpMessageFromCtx ctx $"You already own this item!" else do! sendFollowUpMessageFromCtx ctx $"You do not have sufficient funds to buy this item! Current balance: {player.Bank} GBT" }) let sell (ctx : InteractionContext) = Game.executePlayerInteraction ctx (fun player -> async { let hasInventoryToSell = Array.length player.Arsenal > 0 if hasInventoryToSell then let builder = DiscordFollowupMessageBuilder() builder.AddEmbed (constructEmbed "Pick the item you wish to sell.") |> ignore Array.chunkBySize 5 player.Arsenal |> Array.iter (fun wps -> wps |> Array.map (fun w -> DiscordButtonComponent(ButtonStyle.Primary, $"{w.Type}-{w.Id}", $"{w.Name}")) |> Seq.cast |> builder.AddComponents |> ignore) builder.AsEphemeral true |> ignore do! ctx.FollowUpAsync(builder) |> Async.AwaitTask |> Async.Ignore else do! sendSimpleResponse ctx "You currently have no inventory to sell" }) let handleBuyItem (ctx : InteractionContext) itemId = Game.executePlayerInteraction ctx (fun player -> async { let item = Armory.battleItems |> Array.find (fun w -> w.Id = itemId) let newBalance = player.Bank - item.Cost if newBalance >= 0 then let playerHasItem = player.Arsenal |> Array.exists (fun w -> item.Id = w.Id) if not playerHasItem then let p = { player with Bank = newBalance ; Arsenal = Array.append [| item |] player.Arsenal } do! DbService.updatePlayer p do! sendFollowUpMessageFromCtx ctx $"Successfully purchased {item.Name}! You now have {newBalance} 💰$GBT remaining" else do! sendFollowUpMessageFromCtx ctx $"You already own this item!" else do! sendFollowUpMessageFromCtx ctx $"You do not have sufficient funds to buy this item! Current balance: {player.Bank} GBT" }) let handleSellButtonEvents (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) = Game.executePlayerEvent event (fun player -> async { let itemId = int <| event.Id.Split("-").[1] let item = Armory.getItem itemId let updatedPlayer = { player with Bank = player.Bank + item.Cost ; Arsenal = player.Arsenal |> Array.filter (fun w -> w.Id <> itemId)} do! DbService.updatePlayer updatedPlayer let builder = DiscordFollowupMessageBuilder() builder.IsEphemeral <- true builder.Content <- $"Sold {item.Type} {item.Name} for {item.Cost}! Current Balance: {updatedPlayer.Bank}" do! event.Interaction.CreateFollowupMessageAsync(builder) |> Async.AwaitTask |> Async.Ignore }) let status (ctx : InteractionContext) = Game.executePlayerInteraction ctx (fun player -> async { let updatedPlayer = Player.removeExpiredActions player let builder = DiscordFollowupMessageBuilder() let embed = DiscordEmbedBuilder() embed.AddField("Arsenal", Messaging.statusFormat updatedPlayer) |> ignore builder.AddEmbed(embed) |> ignore builder.IsEphemeral <- true do! ctx.FollowUpAsync(builder) |> Async.AwaitTask |> Async.Ignore do! DbService.updatePlayer updatedPlayer }) type Store() = inherit ApplicationCommandModule () [] member _.Armory (ctx : InteractionContext) = viewStore ctx [] member this.Arsenal (ctx : InteractionContext) = status ctx [] member _.BuyHack (ctx : InteractionContext, [] hackId : HackId) = buyItem ctx (int hackId) [] member this.BuyShield (ctx : InteractionContext, [] shieldId : ShieldId) = buyItem ctx (int shieldId) [] member this.SellItem (ctx : InteractionContext) = sell ctx