diff --git a/Bot/Bot.fs b/Bot/Bot.fs index 42e576b..8f82a14 100644 --- a/Bot/Bot.fs +++ b/Bot/Bot.fs @@ -5,6 +5,7 @@ open DSharpPlus open DSharpPlus.SlashCommands open Degenz open Degenz.HackerBattle +open Degenz.RockPaperScissors open Degenz.Store open Degenz.Thief open Emzi0767.Utilities @@ -43,6 +44,7 @@ let storeCommands = storeBot.UseSlashCommands() hackerCommands.RegisterCommands(guild); hackerCommands.RegisterCommands(guild); +hackerCommands.RegisterCommands(guild); storeCommands.RegisterCommands(guild); //sc3.RegisterCommands(guild); diff --git a/Bot/Game.fs b/Bot/Game.fs index f2f64fd..86b9627 100644 --- a/Bot/Game.fs +++ b/Bot/Game.fs @@ -82,18 +82,28 @@ module Player = let getShields (player : PlayerData) = getItems ItemType.Shield player let getAttacks player = player.Actions - |> Array.filter (fun act -> match act.Type with Attack _ -> true | _ -> false) - let getDefenses player = player.Actions |> Array.filter (fun act -> match act.Type with Defense -> true | _ -> false) + |> Array.filter (fun act -> match act.Type with Attack _ -> true | _ -> false || act.ActionId < 12) + let getDefenses player = + player.Actions + |> Array.filter (fun act -> match act.Type with Defense -> true | _ -> false || act.ActionId < 12) let removeExpiredActions filterByAttackCooldown player = let actions = player.Actions |> Array.filter (fun (act : Action) -> - let item = Armory.getItem act.ActionId + let itemCooldown = + if act.ActionId < 12 then + (Armory.getItem act.ActionId).Cooldown + else + match act.Type with + | Attack _ -> 1 + | Defense -> 720 + |> int + match act.Type , filterByAttackCooldown with - | Attack _ , true -> System.DateTime.UtcNow - act.Timestamp < System.TimeSpan.FromMinutes(int item.Cooldown) + | Attack _ , true -> System.DateTime.UtcNow - act.Timestamp < System.TimeSpan.FromMinutes(itemCooldown) | Attack _ , false -> System.DateTime.UtcNow - act.Timestamp < Game.SameTargetAttackCooldown - | Defense , _ -> System.DateTime.UtcNow - act.Timestamp < System.TimeSpan.FromMinutes(int item.Cooldown)) + | Defense , _ -> System.DateTime.UtcNow - act.Timestamp < System.TimeSpan.FromMinutes(itemCooldown)) { player with Actions = actions } let modifyBank (player : PlayerData) amount = { player with Bank = max (player.Bank + amount) 0 } @@ -110,7 +120,10 @@ module Arsenal = match actions with | [||] -> "None" | _ -> - let hacks , defenses = actions |> Array.partition (fun act -> match act.Type with Attack _ -> true | Defense -> false) + let hacks , defenses = + actions + |> Array.filter (fun act -> act.ActionId < 12) + |> Array.partition (fun act -> match act.Type with Attack _ -> true | Defense -> false) let hacks = hacks |> Array.take (min hacks.Length 10) hacks |> Array.append defenses diff --git a/Bot/HackerBattle.fs b/Bot/HackerBattle.fs index ebb2b41..df47de6 100644 --- a/Bot/HackerBattle.fs +++ b/Bot/HackerBattle.fs @@ -178,7 +178,7 @@ let handleDefense (ctx : IDiscordContext) = |> checkPlayerOwnsWeapon shieldId >>= checkPlayerHasShieldSlotsAvailable shield >>= checkItemHasCooldown shieldId - |> handleResultWithResponse ctx (fun p -> async { + |> handleResultWithResponse ctx (fun _ -> async { // Don't use this player, it removes player cooldowns let embed = Embeds.responseCreatedShield (Armory.getItem shieldId) do! ctx.FollowUp embed |> Async.AwaitTask let defense = { ActionId = shieldId ; Type = Defense ; Timestamp = DateTime.UtcNow } @@ -192,21 +192,6 @@ let handleDefense (ctx : IDiscordContext) = }) }) -let handleButtonEvent (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) = - let eventCtx = DiscordEventContext event :> IDiscordContext - match event.Id with - | id when id.StartsWith("Attack") -> handleAttack eventCtx - | id when id.StartsWith("Defend") -> handleDefense eventCtx - | id when id.StartsWith("Trainer") -> Trainer.handleButtonEvent eventCtx |> Async.StartAsTask :> Task - | id when id.StartsWith("Steal") -> Thief.handleSteal eventCtx - | _ -> - task { - let builder = DiscordInteractionResponseBuilder() - builder.IsEphemeral <- true - builder.Content <- $"Incorrect Action identifier {eventCtx.GetInteractionId()}" - do! eventCtx.Respond InteractionResponseType.ChannelMessageWithSource builder |> Async.AwaitTask - } - let arsenal (ctx : IDiscordContext) = Game.executePlayerAction ctx (fun player -> async { let updatedPlayer = Player.removeExpiredActions false player @@ -219,6 +204,22 @@ let arsenal (ctx : IDiscordContext) = do! DbService.updatePlayer updatedPlayer }) +let handleButtonEvent (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) = + let eventCtx = DiscordEventContext event :> IDiscordContext + match event.Id with + | id when id.StartsWith("Attack") -> handleAttack eventCtx + | id when id.StartsWith("Defend") -> handleDefense eventCtx + | id when id.StartsWith("Trainer") -> Trainer.handleButtonEvent eventCtx |> Async.StartAsTask :> Task + | id when id.StartsWith("Steal") -> Thief.handleSteal eventCtx + | id when id.StartsWith("Steal") -> RockPaperScissors.handleRPS eventCtx + | _ -> + task { + let builder = DiscordInteractionResponseBuilder() + builder.IsEphemeral <- true + builder.Content <- $"Incorrect Action identifier {eventCtx.GetInteractionId()}" + do! eventCtx.Respond InteractionResponseType.ChannelMessageWithSource builder |> Async.AwaitTask + } + type HackerGame() = inherit ApplicationCommandModule () diff --git a/Bot/RockPaperScissors.fs b/Bot/RockPaperScissors.fs index 5000587..06f9211 100644 --- a/Bot/RockPaperScissors.fs +++ b/Bot/RockPaperScissors.fs @@ -29,11 +29,22 @@ let player1Won p1m p2m = let playRPS target ctx = Game.executePlayerActionWithTarget target ctx (fun attacker defender -> async { + let buttons = + [ DiscordButtonComponent(ButtonStyle.Primary, $"RPS-rock-{defender.DiscordId}-{defender.Name}", "🪨 Rock") + DiscordButtonComponent(ButtonStyle.Primary, $"RPS-paper", "📜 Paper") + DiscordButtonComponent(ButtonStyle.Primary, $"RPS-scissors", "✂ Scissors") ] + |> Seq.cast - return () + let builder = DiscordFollowupMessageBuilder() + builder.AddComponents(buttons) |> ignore + builder.IsEphemeral <- true + + do! ctx.FollowUp builder |> Async.AwaitTask }) -type StealGame() = +let handleRPS ctx = async.Zero() |> Async.StartAsTask + +type RPSGame() = inherit ApplicationCommandModule () let enforceChannel (ctx : IDiscordContext) (storeFn : IDiscordContext -> Task) = diff --git a/Bot/Thief.fs b/Bot/Thief.fs index 1701d83..9e8a621 100644 --- a/Bot/Thief.fs +++ b/Bot/Thief.fs @@ -173,6 +173,7 @@ let handleSteal (ctx : IDiscordContext) = let targetId = uint64 split.[2] Game.executePlayerActionWithTargetId targetId ctx (fun attacker defender -> async { do! attacker + |> Player.removeExpiredActions false |> checkVictimStealingCooldown defender >>= checkThiefCooldown |> handleResultWithResponse ctx handleYes