Initial modeling of hacker game with LiteDB

This commit is contained in:
Joseph Ferano 2022-01-07 15:08:22 +07:00
parent bacb5d3923
commit a770eaf82b
4 changed files with 58 additions and 127 deletions

View File

@ -4,34 +4,43 @@ open DSharpPlus
open DSharpPlus.Entities open DSharpPlus.Entities
open DSharpPlus.SlashCommands open DSharpPlus.SlashCommands
open Emzi0767.Utilities open Emzi0767.Utilities
open LiteDB
open LiteDB.FSharp
type Move = type HackType =
| Rock | Virus = 0
| Paper | Ransom = 1
| Scissor | DDos = 2
| Worm = 3
| Crack = 4
type RoundResult = type DefenseType =
| P1Win | Firewall
| P2Win | PortScan
| Draw | Encryption
| Cypher
| Hardening
type Player = [<CLIMutable>]
{ name : string type Weapon = {
health : string } Id : int
Name : string
Damage : single
}
type Player = {
Id : int
Name : string
Nickname : string
DiscordId : uint16
Weapons : int array
}
type Turn =
| WaitingForBoth
| WaitingForOne of Move
| WaitingForTwo of Move
| BothCompleted of Move * Move
type Match = type Match =
{ player1 : DiscordUser { scorePlayer1 : int
player2 : DiscordUser
round : int round : int
scorePlayer1 : int scorePlayer2 : int }
scorePlayer2 : int
turn : Turn }
type JoeBot() = type JoeBot() =
inherit ApplicationCommandModule () inherit ApplicationCommandModule ()
@ -41,15 +50,6 @@ type JoeBot() =
[<SlashCommand("challenge", "Challenge another user")>] [<SlashCommand("challenge", "Challenge another user")>]
member _.StartMatch (ctx : InteractionContext, [<Option("player", "Player you want to challenge")>] player : DiscordUser) = member _.StartMatch (ctx : InteractionContext, [<Option("player", "Player you want to challenge")>] player : DiscordUser) =
async { async {
currentMatch <- Some {
player1 = ctx.User
player2 = player
round = 0
scorePlayer1 = 0
scorePlayer2 = 0
turn = WaitingForBoth
}
// We won't be able to find the user if they are Away or Sleeping apparently // We won't be able to find the user if they are Away or Sleeping apparently
let ( result , discordMember ) = ctx.Guild.Members.TryGetValue(player.Id) let ( result , discordMember ) = ctx.Guild.Members.TryGetValue(player.Id)
@ -90,109 +90,20 @@ type JoeBot() =
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<SlashCommand("move", "Challenge another member to combat")>] [<SlashCommand("hack", "Send a hack attack to the player")>]
member _.SendMove (ctx : InteractionContext, [<Option("move", "choose between rock, paper, scissors")>] moveString : string) = member _.Hack (ctx : InteractionContext, [<Option("hack", "weapon attack")>] hackType : HackType) =
async {
let move =
match moveString.ToLower() with
| "rock" -> Some Rock
| "paper" -> Some Paper
| "scissors" -> Some Scissor
| _ -> None
match currentMatch , move with
| Some mtc , Some move ->
let updatedTurn =
match mtc.turn with
| WaitingForBoth ->
match ctx.User with
| mem when mem = mtc.player1 -> WaitingForTwo move
| mem when mem = mtc.player2 -> WaitingForOne move
| _ -> mtc.turn
| WaitingForOne p1m ->
match ctx.User with
| mem when mem = mtc.player1 -> BothCompleted ( p1m , move )
| mem when mem = mtc.player2 -> mtc.turn
| _ -> mtc.turn
| WaitingForTwo p2m ->
match ctx.User with
| mem when mem = mtc.player1 -> mtc.turn
| mem when mem = mtc.player2 -> BothCompleted ( p2m , move )
| _ -> mtc.turn
| _ -> mtc.turn
match updatedTurn with
| BothCompleted ( p1m , p2m ) ->
let result =
match p1m , p2m with
| Rock , Paper -> P2Win
| Rock , Scissor -> P1Win
| Paper , Rock -> P1Win
| Paper , Scissor -> P2Win
| Scissor , Rock -> P2Win
| Scissor , Paper -> P1Win
| _ -> Draw
match result with
| P1Win | P2Win ->
let winner = match result with P1Win -> mtc.player1 | P2Win -> mtc.player2 | Draw -> mtc.player1
let winningMove = match result with P1Win -> p1m | P2Win -> p2m | Draw -> p1m
let losingMove = match result with P1Win -> p2m | P2Win -> p1m | Draw -> p1m
let message = $"{winningMove} beats {losingMove}! {winner.Username} takes the round!"
currentMatch <- Some
{ mtc with
round = mtc.round + 1
scorePlayer1 = mtc.scorePlayer1 + (match result with P1Win -> 1 | _ -> 0)
scorePlayer2 = mtc.scorePlayer2 + (match result with P2Win -> 1 | _ -> 0)
turn = WaitingForBoth
}
if mtc.round >= 3 then
let winnerScore = match result with P1Win -> mtc.scorePlayer1 | P2Win -> mtc.scorePlayer2 | Draw -> 0
let loserScore = match result with P1Win -> mtc.scorePlayer2 | P2Win -> mtc.scorePlayer1 | Draw -> 0
currentMatch <- None
do! ctx.CreateResponseAsync $"{winner} wins the match {winnerScore} to {loserScore}! Awarding a 100 genz!"
|> Async.AwaitTask
else
do! ctx.CreateResponseAsync message |> Async.AwaitTask
| Draw ->
currentMatch <- Some { mtc with turn = WaitingForBoth }
do! ctx.CreateResponseAsync $"{mtc.player1.Username} and {mtc.player2.Username} both did {p1m}. Round was a draw! Go again!"
|> Async.AwaitTask
| _ ->
if updatedTurn <> mtc.turn then
currentMatch <- Some { mtc with turn = updatedTurn }
let builder = DiscordInteractionResponseBuilder()
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(($"Move received by {ctx.Member.DisplayName}!")))
|> Async.AwaitTask
else
do! async{ return () }
| None , _ ->
do! ctx.CreateResponseAsync ("No match has been found, please use the '/start-match' command and mention the two players")
|> Async.AwaitTask
|> Async.Ignore
| _ , None ->
do! ctx.CreateResponseAsync $"Could not recognize move '{moveString}', please try again. Valid moves are 'rock', 'paper', or 'scissor'"
|> Async.AwaitTask
|> Async.Ignore
} |> Async.StartAsTask
:> Task
[<SlashCommand("status", "Status of the match")>]
member _.Status (ctx : InteractionContext) =
async { async {
let builder = DiscordInteractionResponseBuilder() let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(sprintf "%A" hackType))
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(sprintf "%A" currentMatch))
|> Async.AwaitTask |> Async.AwaitTask
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
let mapper = FSharpBsonMapper()
let db = new LiteDatabase("hacker-game.db", mapper)
let config = DiscordConfiguration() let config = DiscordConfiguration()
config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk" config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk"

View File

@ -9,3 +9,6 @@ source https://nuget.emzi0767.com/api/v3/index.json
nuget DSharpPlus >= 4.2.0-nightly-01054 nuget DSharpPlus >= 4.2.0-nightly-01054
nuget DSharpPlus.SlashCommands >= 4.2.0-nightly-01054 nuget DSharpPlus.SlashCommands >= 4.2.0-nightly-01054
nuget LiteDB.FSharp 2.16.0
nuget LiteDB 4.1.4

View File

@ -22,6 +22,14 @@ NUGET
System.Runtime.CompilerServices.Unsafe (>= 5.0) System.Runtime.CompilerServices.Unsafe (>= 5.0)
System.ValueTuple (>= 4.5) System.ValueTuple (>= 4.5)
FSharp.Core (6.0.1) FSharp.Core (6.0.1)
LiteDB (4.1.4)
System.Reflection (>= 4.3)
System.Reflection.TypeExtensions (>= 4.3)
LiteDB.FSharp (2.16)
FSharp.Core (>= 4.7.2)
LiteDB (>= 4.1.4 < 5.0)
Newtonsoft.Json (>= 13.0.1)
TypeShape (>= 9.0)
Microsoft.Bcl.AsyncInterfaces (6.0) - restriction: || (&& (== net6.0) (>= net461)) (&& (== net6.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net461)) Microsoft.Bcl.AsyncInterfaces (6.0) - restriction: || (&& (== net6.0) (>= net461)) (&& (== net6.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net461))
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net6.0) (>= net461)) (&& (== net6.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net461)) System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net6.0) (>= net461)) (&& (== net6.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net461))
Microsoft.Extensions.DependencyInjection (6.0) Microsoft.Extensions.DependencyInjection (6.0)
@ -286,6 +294,9 @@ NUGET
System.IO (>= 4.3) System.IO (>= 4.3)
System.Reflection.Primitives (>= 4.3) System.Reflection.Primitives (>= 4.3)
System.Runtime (>= 4.3) System.Runtime (>= 4.3)
System.Reflection.Emit.ILGeneration (4.7) - restriction: || (&& (== net6.0) (< netcoreapp2.0) (< netstandard2.1)) (&& (== net6.0) (< netstandard2.0)) (&& (== net6.0) (< portable-net45+wp8)) (&& (== net6.0) (>= uap10.1)) (== netstandard2.0) (&& (== netstandard2.1) (< netstandard2.0)) (&& (== netstandard2.1) (< portable-net45+wp8)) (&& (== netstandard2.1) (>= uap10.1))
System.Reflection.Emit.LightWeight (4.7) - restriction: || (&& (== net6.0) (< netcoreapp3.1)) (== netstandard2.0) (== netstandard2.1)
System.Reflection.Emit.ILGeneration (>= 4.7) - restriction: || (&& (== net6.0) (< netcoreapp2.0) (< netstandard2.1)) (&& (== net6.0) (< netstandard2.0)) (&& (== net6.0) (< portable-net45+wp8)) (&& (== net6.0) (>= uap10.1)) (== netstandard2.0) (&& (== netstandard2.1) (< netstandard2.0)) (&& (== netstandard2.1) (< portable-net45+wp8)) (&& (== netstandard2.1) (>= uap10.1))
System.Reflection.Extensions (4.3) System.Reflection.Extensions (4.3)
Microsoft.NETCore.Platforms (>= 1.1) Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1) Microsoft.NETCore.Targets (>= 1.1)
@ -295,6 +306,7 @@ NUGET
Microsoft.NETCore.Platforms (>= 1.1) Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1) Microsoft.NETCore.Targets (>= 1.1)
System.Runtime (>= 4.3) System.Runtime (>= 4.3)
System.Reflection.TypeExtensions (4.7)
System.Resources.ResourceManager (4.3) System.Resources.ResourceManager (4.3)
Microsoft.NETCore.Platforms (>= 1.1) Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1) Microsoft.NETCore.Targets (>= 1.1)
@ -452,3 +464,6 @@ NUGET
Microsoft.NETCore.Targets (>= 1.1) Microsoft.NETCore.Targets (>= 1.1)
System.Runtime (>= 4.3) System.Runtime (>= 4.3)
System.ValueTuple (4.5) System.ValueTuple (4.5)
TypeShape (10.0)
FSharp.Core (>= 4.5.4)
System.Reflection.Emit.LightWeight (>= 4.7) - restriction: || (&& (== net6.0) (< netcoreapp3.1)) (== netstandard2.0) (== netstandard2.1)

View File

@ -3,3 +3,5 @@ DSharpPlus
// DSharpPlus.CommandsNext // DSharpPlus.CommandsNext
// DSharpPlus.Interactivity // DSharpPlus.Interactivity
DSharpPlus.SlashCommands DSharpPlus.SlashCommands
LiteDB
LiteDB.FSharp