Stop player from hacking themself, don't check for trainer role, clean up

This commit is contained in:
Joseph Ferano 2022-01-30 17:49:37 +07:00
parent 5f4bb71245
commit d1cf329521
3 changed files with 27 additions and 28 deletions

View File

@ -18,6 +18,12 @@ let getTimeTillCooldownFinishes (timespan : TimeSpan) timestamp =
else else
$"{timeRemaining.Seconds} seconds" $"{timeRemaining.Seconds} seconds"
let checkIfPlayerIsAttackingThemselves defender attacker =
match attacker.DiscordId = defender.DiscordId with
| true -> Error "You think you're clever? You can't hack yourself, pal."
| false -> Ok attacker
let checkForExistingHack defenderId attacker = let checkForExistingHack defenderId attacker =
let updatedAttacks = let updatedAttacks =
attacker.Attacks attacker.Attacks
@ -81,13 +87,8 @@ let successfulHack (event : ComponentInteractionCreateEventArgs) attacker defend
do! updateCombatants attacker defender hack prize do! updateCombatants attacker defender hack prize
let embed = DiscordEmbedBuilder() let embed = Embeds.responseSuccessfulHack defender.Name hack prize
embed.ImageUrl <- Embeds.getHackGif hack do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, embed)
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.AddEmbed embed |> ignore
builder.Content <- $"Successfully hacked {defender.Name} using {hack}! You just won {prize} GoodBoyTokenz!"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask |> Async.AwaitTask
let builder = Embeds.eventSuccessfulHack event defender.DiscordId prize let builder = Embeds.eventSuccessfulHack event defender.DiscordId prize
@ -125,6 +126,7 @@ let attack (ctx : InteractionContext) (target : DiscordUser) =
let hackAttempt = let hackAttempt =
checkForExistingHack defender.DiscordId attacker checkForExistingHack defender.DiscordId attacker
|> Result.bind checkIfInventoryIsEmpty |> Result.bind checkIfInventoryIsEmpty
|> Result.bind (checkIfPlayerIsAttackingThemselves defender)
match hackAttempt with match hackAttempt with
| Ok _ -> | Ok _ ->
let embed = Embeds.pickHack "Attack" attacker defender let embed = Embeds.pickHack "Attack" attacker defender
@ -150,7 +152,6 @@ let handleAttack (event : ComponentInteractionCreateEventArgs) =
let! resultPlayer = DbService.tryFindPlayer event.User.Id let! resultPlayer = DbService.tryFindPlayer event.User.Id
let! resultTarget = DbService.tryFindPlayer targetId let! resultTarget = DbService.tryFindPlayer targetId
// TODO: Do not let player hack themselves
match resultPlayer , resultTarget , true , resultId with match resultPlayer , resultTarget , true , resultId with
| Some attacker , Some defender , true , true -> | Some attacker , Some defender , true , true ->
do! checkForExistingHack defender.DiscordId attacker do! checkForExistingHack defender.DiscordId attacker
@ -207,13 +208,8 @@ let handleDefense (event : ComponentInteractionCreateEventArgs) =
match alreadyUsedShield , updatedDefenses.Length < 2 with match alreadyUsedShield , updatedDefenses.Length < 2 with
| false , true -> | false , true ->
let builder = DiscordInteractionResponseBuilder() let embed = Embeds.responseCreatedShield shield
let embed = DiscordEmbedBuilder() do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, embed)
embed.ImageUrl <- Embeds.getShieldGif shield
builder.IsEphemeral <- true
builder.AddEmbed embed |> ignore
builder.Content <- $"Mounted a {shield} defense for 6 hours"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask |> Async.AwaitTask
let defense = { DefenseType = shield ; Timestamp = DateTime.UtcNow } let defense = { DefenseType = shield ; Timestamp = DateTime.UtcNow }
do! DbService.updatePlayer <| { player with Defenses = Array.append [| defense |] player.Defenses } do! DbService.updatePlayer <| { player with Defenses = Array.append [| defense |] player.Defenses }
@ -274,16 +270,14 @@ type HackerGame() =
[<SlashCommand("hack", "Send a hack attack to another player")>] [<SlashCommand("hack", "Send a hack attack to another player")>]
member this.AttackCommand (ctx : InteractionContext, [<Option("target", "The player you want to hack")>] target : DiscordUser) = member this.AttackCommand (ctx : InteractionContext, [<Option("target", "The player you want to hack")>] target : DiscordUser) =
let hasTraineeRole = Seq.exists (fun (r : DiscordRole) -> r.Id = GuildEnvironment.roleTrainee) ctx.Member.Roles if ctx.Channel.Id = GuildEnvironment.channelTraining then
if ctx.Channel.Id = GuildEnvironment.channelTraining && hasTraineeRole then
Trainer.attack ctx target Trainer.attack ctx target
else else
attack ctx target attack ctx target
[<SlashCommand("defend", "Create a passive defense that will last 24 hours")>] [<SlashCommand("defend", "Create a passive defense that will last 24 hours")>]
member this.DefendCommand (ctx : InteractionContext) = member this.DefendCommand (ctx : InteractionContext) =
let hasTraineeRole = Seq.exists (fun (r : DiscordRole) -> r.Id = GuildEnvironment.roleTrainee) ctx.Member.Roles if ctx.Channel.Id = GuildEnvironment.channelTraining then
if ctx.Channel.Id = GuildEnvironment.channelTraining && hasTraineeRole then
Trainer.defend ctx Trainer.defend ctx
else else
defend ctx defend ctx

View File

@ -52,7 +52,6 @@ module Commands =
} }
if newPlayer then if newPlayer then
// TODO: Add a better registration message that shows which weapon and how much currency they start with
do! ctx.CreateResponseAsync("You are now an elite haxxor", true) do! ctx.CreateResponseAsync("You are now an elite haxxor", true)
|> Async.AwaitTask |> Async.AwaitTask
else else

View File

@ -7,6 +7,9 @@ open DSharpPlus.EventArgs
open DSharpPlus.SlashCommands open DSharpPlus.SlashCommands
open Degenz.Shared open Degenz.Shared
let defaultHack = Hack.Virus
let defaultShield = Shield.Firewall
let sendInitialEmbed (client : DiscordClient) = let sendInitialEmbed (client : DiscordClient) =
async { async {
let! channel = client.GetChannelAsync(GuildEnvironment.channelTraining) |> Async.AwaitTask let! channel = client.GetChannelAsync(GuildEnvironment.channelTraining) |> Async.AwaitTask
@ -47,7 +50,7 @@ let handleTrainerStep2 (event : ComponentInteractionCreateEventArgs) =
let! result = DbService.tryFindPlayer event.User.Id let! result = DbService.tryFindPlayer event.User.Id
match result with match result with
| Some player -> | Some player ->
let weaponName = player.Shields |> Array.tryHead |> Option.defaultValue Shield.Firewall let weaponName = player.Shields |> Array.tryHead |> Option.defaultValue defaultShield
do! Message.sendInteractionEvent event do! Message.sendInteractionEvent event
($"First things first, let's get your system protected. Let's enable a shield to protect you from potential hackers. " ($"First things first, let's get your system protected. Let's enable a shield to protect you from potential hackers. "
+ $"You currently have {weaponName} in your arsenal. To enable it and protect your system, you can use the `/defend` slash command to choose a shield." + $"You currently have {weaponName} in your arsenal. To enable it and protect your system, you can use the `/defend` slash command to choose a shield."
@ -63,7 +66,7 @@ let defend (ctx : InteractionContext) =
| Some player -> | Some player ->
let playerWithShields = let playerWithShields =
match player.Shields with match player.Shields with
| [||] -> { player with Shields = [| Shield.Firewall |] } | [||] -> { player with Shields = [| defaultShield |] }
| _ -> player | _ -> player
let embed = Embeds.pickDefense "Trainer-3" playerWithShields let embed = Embeds.pickDefense "Trainer-3" playerWithShields
@ -89,8 +92,10 @@ let handleDefense (event : ComponentInteractionCreateEventArgs) =
match result with match result with
| Some player -> | Some player ->
let prize = 0.223f let prize = 0.223f
do! sendMessage' $"{event.User.Username} has protected their system!" let shield = player.Shields |> Array.tryHead |> Option.defaultValue defaultShield
do! Async.Sleep 3000 let embed = Embeds.responseCreatedShieldTrainer shield
do! event.Interaction.CreateFollowupMessageAsync(embed) |> Async.AwaitTask |> Async.Ignore
do! Async.Sleep 2000
do! sendMessage' "Ok, good, let me make sure that worked. I'll try to hack you now" do! sendMessage' "Ok, good, let me make sure that worked. I'll try to hack you now"
do! Async.Sleep 4000 do! Async.Sleep 4000
do! sendMessage' $"Hacking attempt failed! {player.Name} defended hack from Degenz-Trainer and took {prize} from them! " do! sendMessage' $"Hacking attempt failed! {player.Name} defended hack from Degenz-Trainer and took {prize} from them! "
@ -107,8 +112,7 @@ let handleTrainerStep4 (event : ComponentInteractionCreateEventArgs) =
let! result = DbService.tryFindPlayer event.User.Id let! result = DbService.tryFindPlayer event.User.Id
match result with match result with
| Some player -> | Some player ->
// TODO: There's a potential bug here where if the player sold their weapons, they'll get stuck let weaponName = player.Weapons |> Array.tryHead |> Option.defaultValue defaultHack
let weaponName = player.Weapons |> Array.tryHead |> Option.defaultValue Hack.Virus
do! Message.sendInteractionEvent event do! Message.sendInteractionEvent event
($"Next why don't you try hacking me. You currently have {weaponName} equipped. To hack me and get some money, " ($"Next why don't you try hacking me. You currently have {weaponName} equipped. To hack me and get some money, "
+ $" you can use the '/hack' slash command and select a user to hack, then choose the hack attack you wish to use." + $" you can use the '/hack' slash command and select a user to hack, then choose the hack attack you wish to use."
@ -156,11 +160,13 @@ let handleAttack (event : ComponentInteractionCreateEventArgs) =
| Some player -> | Some player ->
let prize = 2 let prize = 2
do! Async.Sleep 1000 do! Async.Sleep 1000
do! sendMessage' $"{player.Name} successfully hacked Degenz-Trainer for a total of {prize} GoodBoyTokenz" let hack = player.Weapons |> Array.tryHead |> Option.defaultValue defaultHack
let embed = Embeds.responseSuccessfulHackTrainer $"<@{GuildEnvironment.botHackerBattle}>" hack prize
do! event.Interaction.CreateFollowupMessageAsync(embed) |> Async.AwaitTask |> Async.Ignore
do! Async.Sleep 3000 do! Async.Sleep 3000
do! sendMessage' ("Look at that, you are now officially an elite haxor! By successfully hacking other people you can earn GoodBoyTokenz. " do! sendMessage' ("Look at that, you are now officially an elite haxor! By successfully hacking other people you can earn GoodBoyTokenz. "
+ "Hacks take time to recover so check back in later once you've used all your hacks.") + "Hacks take time to recover so check back in later once you've used all your hacks.")
do! Async.Sleep 6000 do! Async.Sleep 7000
let msg = ("I think we're done. You are going to need more hacks and shields if you want to survive in this crazy world. " let msg = ("I think we're done. You are going to need more hacks and shields if you want to survive in this crazy world. "
+ "Remember to go check out the store and purchase whatever you need to add to your arsenal." + "Remember to go check out the store and purchase whatever you need to add to your arsenal."
+ "\n\nAlright you degenerate, off you go!") + "\n\nAlright you degenerate, off you go!")