Fix bugs, better embeds, offload to separate thread

This commit is contained in:
Joseph Ferano 2022-04-20 09:42:56 +07:00
parent 5b1124e5e0
commit 937eeb4cc2

View File

@ -131,6 +131,7 @@ let slotEmojiNames =
let PlayPricex1 = 5<GBT> let PlayPricex1 = 5<GBT>
let PlayPricex2 = 10<GBT> let PlayPricex2 = 10<GBT>
let PlayPricex3 = 20<GBT> let PlayPricex3 = 20<GBT>
let BaseJackpotAmount = 0<GBT>
let sleepTime = 1500 let sleepTime = 1500
let mutable guildEmojis : Map<string, DiscordEmoji> option = None let mutable guildEmojis : Map<string, DiscordEmoji> option = None
let mutable anyEmoji : DiscordEmoji option = None let mutable anyEmoji : DiscordEmoji option = None
@ -147,6 +148,7 @@ let getJackpotAmount () =
|> Sql.connect |> Sql.connect
|> Sql.query "SELECT stock FROM item WHERE symbol = 'JACKPOT'" |> Sql.query "SELECT stock FROM item WHERE symbol = 'JACKPOT'"
|> Sql.executeRowAsync (fun read -> (read.int "stock") * 1<GBT>) |> Sql.executeRowAsync (fun read -> (read.int "stock") * 1<GBT>)
|> Async.AwaitTask
let incrementJackpot amount = let incrementJackpot amount =
GuildEnvironment.connectionString GuildEnvironment.connectionString
@ -154,6 +156,15 @@ let incrementJackpot amount =
|> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ] |> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ]
|> Sql.query "UPDATE item SET stock = stock + @amount WHERE symbol = 'JACKPOT'" |> Sql.query "UPDATE item SET stock = stock + @amount WHERE symbol = 'JACKPOT'"
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask
let resetJackpot amount =
GuildEnvironment.connectionString
|> Sql.connect
|> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ]
|> Sql.query "UPDATE item SET stock = amount WHERE symbol = 'JACKPOT'"
|> Sql.executeNonQueryAsync
|> Async.AwaitTask
let handlePrizeTable (ctx : IDiscordContext) = let handlePrizeTable (ctx : IDiscordContext) =
task { task {
@ -188,13 +199,9 @@ let handlePrizeTable (ctx : IDiscordContext) =
| _ , _ -> return () | _ , _ -> return ()
} :> Task } :> Task
let spinEmojis (results : SlotSymbol array) (ctx : IDiscordContext) = let spinEmojis (builder : DiscordFollowupMessageBuilder) (results : SlotSymbol array) (itx : DiscordInteraction) =
async { async {
let itx = ctx.GetInteraction()
let builder = DiscordFollowupMessageBuilder()
builder.Content <- "Spinning!" builder.Content <- "Spinning!"
builder.IsEphemeral <- true
let! followUp = itx.CreateFollowupMessageAsync(builder) |> Async.AwaitTask let! followUp = itx.CreateFollowupMessageAsync(builder) |> Async.AwaitTask
match guildEmojis with match guildEmojis with
@ -211,8 +218,10 @@ let spinEmojis (results : SlotSymbol array) (ctx : IDiscordContext) =
do! Async.Sleep sleepTime do! Async.Sleep sleepTime
let content = $"{e1}{e2}{e3}" let content = $"{e1}{e2}{e3}"
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent(content)) |> Async.AwaitTask let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent(content)) |> Async.AwaitTask
return () return followUp , content
| None -> return () | None ->
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent("An error occurred, please contact an Admin")) |> Async.AwaitTask
return followUp , ""
} }
let spin multiplier (ctx : IDiscordContext) = let spin multiplier (ctx : IDiscordContext) =
@ -229,49 +238,75 @@ let spin multiplier (ctx : IDiscordContext) =
| Symbol s1' , Any , Any when s1'.index = symbols.[0].index -> Some prize | Symbol s1' , Any , Any when s1'.index = symbols.[0].index -> Some prize
| _ -> None) | _ -> None)
do! spinEmojis symbols ctx
do! Async.Sleep 2000
let builder = DiscordFollowupMessageBuilder() let builder = DiscordFollowupMessageBuilder()
builder.IsEphemeral <- true builder.IsEphemeral <- true
let embed4 = DiscordEmbedBuilder()
embed4.Title <- "Results" let itx = ctx.GetInteraction()
let! followUpMessage , slotsContent = spinEmojis builder symbols itx
do! Async.Sleep 2000
let embed = DiscordEmbedBuilder()
embed.Author <- DiscordEmbedBuilder.EmbedAuthor()
embed.Author.Name <- player.Name
embed.Author.IconUrl <- ctx.GetDiscordMember().AvatarUrl
embed.Title <- "Slots Results"
let addGBTField (embed : DiscordEmbedBuilder) prize =
let sym = if prize > 0<GBT> then "+" else "-"
embed.AddField("New 💰$GBT Balance", $"`💰` {player.Bank} `💰` {player.Bank + prize} `({sym}{abs prize} $GBT)`") |> ignore
match prize with match prize with
| Some (Money amount) -> | Some (Money amount) ->
do! DbService.updatePlayerCurrency (amount * multiplier) player |> Async.Ignore let prizeWithMultiplier = amount * multiplier
embed4.ImageUrl <- "https://s7.gifyu.com/images/youwin.png" do! DbService.updatePlayerCurrency prizeWithMultiplier player |> Async.Ignore
embed4.Description <- $"You win **{amount}** GBT!" embed.ImageUrl <- "https://s7.gifyu.com/images/youwin.png"
embed.Color <- DiscordColor.Green
embed.Color <- DiscordColor.Purple
embed.Description <- $"You win **{prizeWithMultiplier}** GBT!"
embed.AddField("Result", $"{slotsContent}", true) |> ignore
addGBTField embed prizeWithMultiplier
| Some (Jackpot) -> | Some (Jackpot) ->
embed4.ImageUrl <- "https://s7.gifyu.com/images/jackpot2ac30c9823f6a91c.png" let! jackpot = getJackpotAmount ()
embed4.Description <- $"YOU HIT THE JACKPOT!!!" embed.ImageUrl <- "https://s7.gifyu.com/images/jackpot2ac30c9823f6a91c.png"
embed.Color <- DiscordColor.Purple
embed.Description <- $"YOU HIT THE JACKPOT!!!"
embed.AddField("Result", $"{slotsContent}", true) |> ignore
addGBTField embed jackpot
do! resetJackpot BaseJackpotAmount |> Async.Ignore
| None -> | None ->
let playAmount = let playAmount =
match multiplier with match multiplier with
| 0 -> PlayPricex1 | 1 -> PlayPricex1
| 1 -> PlayPricex2 | 2 -> PlayPricex2
| _ -> PlayPricex3 | _ -> PlayPricex3
// TODO: We have to make sure we don't go below zero
do! DbService.updatePlayerCurrency -playAmount player |> Async.Ignore do! DbService.updatePlayerCurrency -playAmount player |> Async.Ignore
do! incrementJackpot PlayPricex1 |> Async.AwaitTask |> Async.Ignore do! incrementJackpot playAmount |> Async.Ignore
embed4.Description <- $"Better luck next time! You paid {playAmount} $GBT" let! jackpot = getJackpotAmount ()
builder.AddComponents embedButtons |> ignore embed.Description <- $"Better luck next time! You paid {playAmount} $GBT"
builder.AddEmbed(embed4) |> ignore embed.Color <- DiscordColor.Red
do! ctx.FollowUp(builder) |> Async.AwaitTask |> Async.Ignore embed.AddField("New 🎉JACKPOT🎉", $"`💰` {jackpot} `$GBT`", true) |> ignore
embed.AddField("Result", $"{slotsContent}", true) |> ignore
addGBTField embed -playAmount
let dwb = DiscordWebhookBuilder()
dwb.AddComponents(embedButtons).AddEmbed(embed).WithContent(slotsContent) |> ignore
do! itx.EditFollowupMessageAsync(followUpMessage.Id, dwb) |> Async.AwaitTask |> Async.Ignore
return () return ()
}) })
let handleButton (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) = let handleButton (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) =
let ctx = DiscordEventContext event let ctx = DiscordEventContext event
task { // TODO: We have to make sure the player has enough money
match event.Id with match event.Id with
| "spin-1x" -> do! spin 1 ctx | "spin-1x" -> spin 1 ctx
| "spin-2x" -> do! spin 2 ctx | "spin-2x" -> spin 2 ctx
| "spin-3x" -> do! spin 3 ctx | "spin-3x" -> spin 3 ctx
| "prizes" -> do! handlePrizeTable ctx | "prizes" -> handlePrizeTable ctx
| _ -> | _ ->
printfn "Wrong Spin ID" printfn "Wrong Spin ID"
return () Task.CompletedTask
} :> Task |> Async.AwaitTask
|> Async.Start
Task.CompletedTask
let handleGuildDownloadCompleted (_ : DiscordClient) (event : GuildDownloadCompletedEventArgs) = let handleGuildDownloadCompleted (_ : DiscordClient) (event : GuildDownloadCompletedEventArgs) =
task { task {
@ -296,7 +331,7 @@ let sendInitialEmbed (ctx : IDiscordContext) =
let embed = DiscordEmbedBuilder() let embed = DiscordEmbedBuilder()
embed.Title <- "Degenz Slot Machine" embed.Title <- "Degenz Slot Machine"
embed.Description <- "Want to try your luck?" embed.Description <- "Want to try your luck?"
embed.ImageUrl <- "https://s7.gifyu.com/images/ezgif.com-gif-maker-241e68d2027b0efd54.gif" embed.ImageUrl <- "https://s7.gifyu.com/images/ezgif.com-gif-maker-268ecb6e4d28bd55a0.gif"
builder.AddEmbed(embed) |> ignore builder.AddEmbed(embed) |> ignore