223 lines
10 KiB
Forth

module Degenz.Thief
open System
open System.Threading.Tasks
open DSharpPlus
open DSharpPlus.Entities
open DSharpPlus.SlashCommands
open Degenz.Messaging
let ThiefCooldown = TimeSpan.FromMinutes(1)
let VictimRecovery = TimeSpan.FromHours(6)
type StealResult =
| Success
| WentToPrison
| VictimRanAway
//let getRandomStealBtnLabels () =
// let rand = Random(Guid.NewGuid().GetHashCode())
// let affirmative = [| "LFG" ; "YOLO" ; "IDGAF" |]
// let negative = [| "NOPE" ; "IM OUT" ; "BAIL" |]
// ( affirmative.[rand.Next(0, 3)] , negative.[rand.Next(0, 3)] )
//let chanceOfSuccessMsg = function
// | amt when amt < 0.20 -> "Looking bad"
// | amt when amt < 0.50 -> "I mean, maybe"
// | amt when amt < 0.80 -> "I think you got this"
// | _ -> "Totally worth it"
//let targetEvaluationMsg = function
// | amt when amt < 0.20 -> "but man, they look swole"
// | amt when amt < 0.50 -> "but they look a little confident"
// | amt when amt < 0.80 -> "and they're looking a little nervous"
// | _ -> "and they look weak af man"
let payout defenderBank chance =
let rand = Random(Guid.NewGuid().GetHashCode())
let baseAmount = defenderBank * 0.1 * (1.0 - chance)
let randomBonus = baseAmount * rand.NextDouble() * chance |> ceil
let randomAmount = baseAmount + randomBonus |> int
max 1 randomAmount
let getStealEmbed (chance : double) prize (target : PlayerData) =
let chance = int (chance * 100.0)
let buttons =
// let yes , no = getRandomStealBtnLabels ()
let btnId = $"Steal-yes-{target.DiscordId}-{target.Name}-{chance}-{prize}"
[ DiscordButtonComponent(ButtonStyle.Success, btnId, "Do it") ]
// DiscordButtonComponent(ButtonStyle.Danger, $"Steal-no", no) ]
|> Seq.cast<DiscordComponent>
let embed =
DiscordEmbedBuilder()
.AddField("Chance of Success", $"{chance}%%", true)
.AddField("Payout", $"{prize}", true)
.WithTitle($"Steal $GBT from {target.Name}")
DiscordFollowupMessageBuilder()
.AddEmbed(embed)
.AddComponents(buttons)
.AsEphemeral(true)
let getResultEmbed targetName result =
let resultMsg , msg , img =
match result with
| Success ->
"You successfully robbed the poor bastard"
, "Your mean ugly face and athletic physique intimidated your poor victim into giving you their money"
, "https://f8n-production-collection-assets.imgix.net/0x3B3ee1931Dc30C1957379FAc9aba94D1C48a5405/127502/QmPLPg1CLovKzS7mP8QkrMoHws1D4VZTzpfbZBALLwKZ5b/nft.png"
| WentToPrison ->
"Looks like you went to prison"
, $"{targetName} knows Karate and elbowed you in the nose. While unconscious, they called the cops. You're on your way to prison now... "
, "https://thumbs.dreamstime.com/b/vector-pixel-art-prisoner-isolated-cartoon-vector-pixel-art-prisoner-129807237.jpg"
| VictimRanAway ->
"You tried to snatch their money and they ran away"
, $"{targetName} got nervous seeing a shadowy figure and ran in the opposite direction"
, "https://i.imgur.com/NLHMvVK.jpg"
DiscordEmbedBuilder()
// .AddField("Result" , resultMsg)
.WithDescription(msg)
// .WithImageUrl(img)
.WithTitle($"Robbery Results")
let checkVictimStealingCooldown defender attacker =
defender
|> Player.removeExpiredActions false
|> Player.getShieldEvents
|> Array.tryFind (fun pe -> pe.Type = PlayerEventType.Steal && pe.Result = PlayerEventResult.Negative)
|> function
| Some act ->
let cooldown = VictimRecovery - (DateTime.UtcNow - act.Timestamp)
let hours = if cooldown.Hours = 0 then "hour" else $"{cooldown.Hours} hours"
Error $"{defender.Name} was robbed recently so they won't be going out for at least another {hours}."
| None -> Ok attacker
// TODO: Look for ways to generalize checking for action cooldowns
let checkThiefCooldown attacker =
attacker
|> Player.getHackEvents
|> Array.tryFind (fun pe -> pe.Type = PlayerEventType.Steal)
|> function
| Some act ->
if ThiefCooldown > (DateTime.UtcNow - act.Timestamp) then
let cooldown = ThiefCooldown - (DateTime.UtcNow - act.Timestamp)
let minutes = if cooldown.Minutes = 0 then "minute" else $"{cooldown.Minutes} minutes"
Error $"Whoa there you clepto, wait at least another {minutes} before you try stealing again."
else
Ok attacker
| None -> Ok attacker
let calculateWinPercentage amountRequested bank attackerStrength defenderStrength =
let powerPercentage = float (attackerStrength - defenderStrength) * 0.005 + 0.25
printfn $"{(attackerStrength - defenderStrength)}"
printfn $"{powerPercentage}"
let cappedAmount = float bank * 0.5
let cappedRequest = min amountRequested (cappedAmount |> ceil)
let wagerPercentage = 1.0 - (cappedRequest / cappedAmount)
// Max chance of success is 97.5%
( cappedRequest , max 0.0 (wagerPercentage * 0.7 + powerPercentage * 1.3 ) / 2.05 )
//calculateWinPercentage 500 1000 100 50
let steal target amount (ctx : IDiscordContext) =
Game.executePlayerActionWithTarget target ctx (fun attacker defender -> async {
do!
attacker
|> checkVictimStealingCooldown defender
>>= checkThiefCooldown
|> handleResultWithResponse ctx (fun _ -> async {
let cappedPrize , winPercentage = calculateWinPercentage amount (int defender.Bank) attacker.Traits.Strength defender.Traits.Strength
let embed = getStealEmbed winPercentage cappedPrize defender
do! ctx.FollowUp(embed) |> Async.AwaitTask
})
})
let handleSteal (ctx : IDiscordContext) =
let split = ctx.GetInteractionId().Split("-")
let answer = split.[1]
let handleYes (player : PlayerData) = async {
let targetId = uint64 split.[2]
let targetName = split.[3]
let chance = double split.[4]
let prize = int split.[5] * 1<GBT>
let rand = Random(Guid.NewGuid().GetHashCode())
let result = chance >= rand.NextDouble() , rand.Next(0,3) = 0
let dp = { DiscordPlayer.Id = targetId ; DiscordPlayer.Name = targetName }
let stealAction result = { ItemId = -1 ; Type = PlayerEventType.Steal ; Result = result ; Adversary = dp ; Timestamp = DateTime.UtcNow }
// TODO: Send event to the hall of privacy
// TODO: We need to check if the player is on cooldown
match result with
| true , _ ->
let xp = 25
let embed = getResultEmbed targetName Success
embed.AddField("$GBT Stolen", string prize) |> ignore
embed.AddField("XP Gained", $"{xp}") |> ignore
do! Messaging.sendFollowUpEmbed ctx (embed.Build())
match! DbService.tryFindPlayer targetId with
| Some t ->
let mugged = { ItemId = -1 ; Type = PlayerEventType.Steal ; Result = PlayerEventResult.Negative ; Adversary = player.basicPlayer ; Timestamp = DateTime.UtcNow }
let actions = t |> Player.removeExpiredActions false |> fun p -> Array.append [| mugged |] p.Events
do! DbService.updatePlayer { t with Bank = max (t.Bank - prize) 0<GBT> ; Events = actions }
| None -> ()
let stole = { ItemId = -1 ; Type = PlayerEventType.Steal ; Result = PlayerEventResult.Positive ; Adversary = dp ; Timestamp = DateTime.UtcNow }
let actions = player |> Player.removeExpiredActions false |> fun p -> Array.append [| stole |] p.Events
do! DbService.updatePlayer { player with Bank = player.Bank + prize ; XP = player.XP + xp ; Events = actions }
let newLevel = XP.getLevel (player.XP + xp)
// if XP.getLevel player.XP < newLevel then
do! Async.Sleep 2000
do! ctx.FollowUp (XP.getRewardsEmbed 1 player) |> Async.AwaitTask
| false , false ->
let embed = getResultEmbed targetName VictimRanAway
do! DbService.updatePlayer { player with Events = Array.append [| stealAction PlayerEventResult.Neutral |] player.Events }
do! Messaging.sendFollowUpEmbed ctx (embed.Build())
| false , true ->
let embed = getResultEmbed targetName WentToPrison
do! DbService.updatePlayer { player with Events = Array.append [| stealAction PlayerEventResult.Neutral |] player.Events }
do! Messaging.sendFollowUpEmbed ctx (embed.Build())
do! Async.Sleep 2000
let role = ctx.GetGuild().GetRole(GuildEnvironment.rolePrisoner)
do! ctx.GetDiscordMember().GrantRoleAsync(role) |> Async.AwaitTask
}
if answer = "yes" then
let targetId = uint64 split.[2]
Game.executePlayerActionWithTargetId true targetId ctx (fun attacker defender -> async {
do! attacker
|> Player.removeExpiredActions false
|> checkVictimStealingCooldown defender
>>= checkThiefCooldown
|> handleResultWithResponse ctx handleYes
})
else
async {
let builder = DiscordInteractionResponseBuilder()
builder.Content <- "I thought better of it"
do! ctx.Respond InteractionResponseType.UpdateMessage builder |> Async.AwaitTask
} |> Async.StartAsTask :> Task
type StealGame() =
inherit ApplicationCommandModule ()
let enforceChannel (ctx : IDiscordContext) (storeFn : IDiscordContext -> Task) =
match ctx.GetChannel().Id with
| id when id = GuildEnvironment.channelThievery -> storeFn ctx
| _ ->
task {
let msg = $"You must go to <#{GuildEnvironment.channelThievery}> channel if you want to mug some unsuspecting doofs"
do! Messaging.sendSimpleResponse ctx msg
}
[<SlashCommand("steal", "Steal some money from another player, but you might go to prison if caught")>]
member this.Steal (ctx : InteractionContext,
[<Option("target", "Who do you want to steal from?")>] target : DiscordUser,
[<Option("amount", "How much you would like to steal")>] amount : double) =
// enforceChannel (DiscordInteractionContext ctx) (steal target amount)
steal target amount (DiscordInteractionContext ctx)