Check if the player has an empty hack inventory

This commit is contained in:
Joseph Ferano 2022-01-25 17:26:35 +07:00
parent d329714aef
commit 9924460001
2 changed files with 26 additions and 19 deletions

View File

@ -18,7 +18,7 @@ let getTimeTillCooldownFinishes (timespan : TimeSpan) timestamp =
else else
$"{timeRemaining.Seconds} seconds" $"{timeRemaining.Seconds} seconds"
let checkForExistingHack attacker defenderId = let checkForExistingHack defenderId attacker =
let updatedAttacks = let updatedAttacks =
attacker.Attacks attacker.Attacks
|> removeExpiredActions (TimeSpan.FromHours(24)) (fun atk -> atk.Timestamp) |> removeExpiredActions (TimeSpan.FromHours(24)) (fun atk -> atk.Timestamp)
@ -29,21 +29,26 @@ let checkForExistingHack attacker defenderId =
let cooldown = getTimeTillCooldownFinishes (TimeSpan.FromHours(24)) attack.Timestamp let cooldown = getTimeTillCooldownFinishes (TimeSpan.FromHours(24)) attack.Timestamp
Error $"You can only hack the same target once every 24 hours, wait {cooldown} to attempt another hack on {attack.Target.Name}." Error $"You can only hack the same target once every 24 hours, wait {cooldown} to attempt another hack on {attack.Target.Name}."
| None -> | None ->
Ok updatedAttacks Ok attacker
let checkIfHackHasCooldown hack updatedAttacks = let checkIfHackHasCooldown hack attacker =
let mostRecentHackAttack = let mostRecentHackAttack =
updatedAttacks attacker.Attacks
|> Array.tryFind (fun a -> a.HackType = hack) |> Array.tryFind (fun a -> a.HackType = hack)
|> function |> function
| Some a -> a.Timestamp | Some a -> a.Timestamp
| None -> DateTime.MinValue | None -> DateTime.MinValue
if DateTime.UtcNow - mostRecentHackAttack > TimeSpan.FromMinutes(5) then if DateTime.UtcNow - mostRecentHackAttack > TimeSpan.FromMinutes(5) then
Ok updatedAttacks Ok attacker
else else
let cooldown = getTimeTillCooldownFinishes (TimeSpan.FromMinutes(5)) mostRecentHackAttack let cooldown = getTimeTillCooldownFinishes (TimeSpan.FromMinutes(5)) mostRecentHackAttack
Error $"{hack} is currently on cooldown, wait {cooldown} to use it again." Error $"{hack} is currently on cooldown, wait {cooldown} to use it again."
let checkIfInventoryIsEmpty attacker =
match attacker.Weapons with
| [||] -> Error $"You currently do not have any Hacks to use against others. Please go to the store and purchase one."
| _ -> Ok attacker
let calculateDamage (hack: int) (shield: int) = let calculateDamage (hack: int) (shield: int) =
let hackClass = getClass hack let hackClass = getClass hack
let protectionClass = getClass shield let protectionClass = getClass shield
@ -117,8 +122,10 @@ let attack (ctx : InteractionContext) (target : DiscordUser) =
let! defender = DbService.tryFindPlayer target.Id let! defender = DbService.tryFindPlayer target.Id
match attacker , defender with match attacker , defender with
| Some attacker , Some defender -> | Some attacker , Some defender ->
let existingHack = checkForExistingHack attacker defender.DiscordId let hackAttempt =
match existingHack with checkForExistingHack defender.DiscordId attacker
|> Result.bind checkIfInventoryIsEmpty
match hackAttempt with
| Ok _ -> | Ok _ ->
let embed = Embeds.pickHack "Attack" attacker defender let embed = Embeds.pickHack "Attack" attacker defender
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, embed) do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, embed)
@ -146,7 +153,7 @@ let handleAttack (event : ComponentInteractionCreateEventArgs) =
// TODO: Do not let player hack themselves // 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 attacker defender.DiscordId do! checkForExistingHack defender.DiscordId attacker
|> Result.bind (checkIfHackHasCooldown hack) |> Result.bind (checkIfHackHasCooldown hack)
|> function |> function
| Ok _ -> | Ok _ ->

View File

@ -7,23 +7,23 @@ open MongoDB.Bson
open MongoDB.Bson.Serialization open MongoDB.Bson.Serialization
open MongoDB.Driver open MongoDB.Driver
let mongo = MongoClient(Environment.GetEnvironmentVariable("CONN_STRING")) let mongo = MongoClient(Environment.GetEnvironmentVariable("CONN_STRING"))
let db = mongo.GetDatabase("degenz") let db = mongo.GetDatabase("degenz")
let players = db.GetCollection<BsonDocument>("players") let players = db.GetCollection<BsonDocument>("players")
let tryFindPlayer (id : uint64) = let tryFindPlayer (id : uint64) =
async { async {
let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id); let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id)
let! player = players.Find<BsonDocument>(filter).FirstOrDefaultAsync() |> Async.AwaitTask let! player = players.FindAsync<BsonDocument>(filter) |> Async.AwaitTask
return match player with match player.FirstOrDefault() with
| null -> None | null -> return None
| player -> | p ->
player return p
.GetValue("Player") .GetValue("Player")
.ToBsonDocument() .ToBsonDocument()
|> BsonSerializer.Deserialize<Player> |> BsonSerializer.Deserialize<Player>
|> Some |> Some
} }
let insertNewPlayer (player : Player) = let insertNewPlayer (player : Player) =