discord-bot-game/Bot/Games/SlotMachine.fs
2022-04-11 14:16:23 +07:00

224 lines
9.1 KiB
Forth

module Degenz.SlotMachine
open System
open System.IO
open System.Threading.Tasks
open DSharpPlus
open DSharpPlus.Entities
open DSharpPlus.EventArgs
open DSharpPlus.SlashCommands
open Degenz.Messaging
open Degenz.Types
type SlotSymbols =
| Eye
| AnonMask
| CircuitBoard
| Obey
| OldTv
| Pills
| Pizza
| Ramen
| Rat
| Alcohol
| BigBrother
| Sushi
let slots =
[| "https://s7.gifyu.com/images/aneye.png"
"https://s7.gifyu.com/images/anonmask.png"
"https://s7.gifyu.com/images/circuitboard.png"
"https://s7.gifyu.com/images/obey.png"
"https://s7.gifyu.com/images/oldtv.png"
"https://s7.gifyu.com/images/pills.png"
"https://s7.gifyu.com/images/pizza0d47578733961746.png"
"https://s7.gifyu.com/images/ramen0515f00869e1f4eb.png"
"https://s7.gifyu.com/images/rat69609f842a0eb9f5.png"
"https://s7.gifyu.com/images/alcohol.png"
"https://s7.gifyu.com/images/bigbrother.png"
"https://s7.gifyu.com/images/sushi.png" |]
// [| "https://s7.gifyu.com/images/A-bottle-of-pills0a3006d0170e08df.png"
// "https://s7.gifyu.com/images/an-eyec362d8152ae2382b.png"
// "https://s7.gifyu.com/images/anon-face-mask6c7624821c89fc08.png"
// "https://s7.gifyu.com/images/a-piece-of-sushi77071d30f60a89c6.png"
// "https://s7.gifyu.com/images/Circuit-board89056017b80f1d13.png"
// "https://s7.gifyu.com/images/OBEYf2a8234109836c03.png"
// "https://s7.gifyu.com/images/old-tv-screendc6bc9d4b6c1fd65.png"
// "https://s7.gifyu.com/images/pizza030ffc00ff50da0e.png"
// "https://s7.gifyu.com/images/ramen08336d448018c98f.png"
// "https://s7.gifyu.com/images/rat14f65f54f0d75036.png" |]
let slotEmojis =
[| "<:sushi:>"
"<:pizza:>"
"<:ramen:>"
"<:circuitboard:>"
"<:obey:>"
"<:pills:>"
"<:oldtv:>"
"<:rat:>"
"<:aneye:>"
"<:anon:>" |]
let slotsImages =
[| "./images/anonmask.png"
"./images/circuitboard.png"
"./images/aneye.png"
"./images/obey.png"
"./images/oldtv.png"
"./images/pills.png"
"./images/pizza.png"
"./images/ramen.png"
"./images/rat.png"
"./images/bigbrother.png"
"./images/alcohol.png"
"./images/sushi.png" |]
|> Array.map (fun path -> ( Path.GetFileNameWithoutExtension(path) , File.OpenRead(path) ))
let payTable = [| |]
let PlayPrice = 10<GBT>
let twoOfAKindPrize = 100<GBT>
let threeOfAKindPrize = 1000<GBT>
let sleepTime = 1000
let spinEmbeds (results : int array) (ctx : IDiscordContext) =
async {
let itx = ctx.GetInteraction()
let builder = DiscordFollowupMessageBuilder()
builder.Content <- "Spinning!"
builder.IsEphemeral <- true
let e1 = DiscordEmbedBuilder().WithImageUrl(slots.[results[0]])
let e2 = DiscordEmbedBuilder().WithImageUrl(slots.[results[1]])
let e3 = DiscordEmbedBuilder().WithImageUrl(slots.[results[2]])
let! followUp = itx.CreateFollowupMessageAsync(builder) |> Async.AwaitTask
do! Async.Sleep sleepTime
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().AddEmbeds([ e1.Build() ])) |> Async.AwaitTask
do! Async.Sleep sleepTime
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().AddEmbeds([ e1.Build() ; e2.Build() ])) |> Async.AwaitTask
do! Async.Sleep sleepTime
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().AddEmbeds([ e1.Build() ; e2.Build() ; e3.Build() ])) |> Async.AwaitTask
do! Async.Sleep sleepTime
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().AddEmbeds([ e1.Build() ; e2.Build() ; e3.Build() ])) |> Async.AwaitTask
return ()
}
let spinEmojis (results : int array) (ctx : IDiscordContext) =
async {
let itx = ctx.GetInteraction()
let emojis = ctx.GetGuild().Emojis |> Seq.map (fun kvp -> kvp.Value) |> Seq.toArray
let builder = DiscordFollowupMessageBuilder()
builder.Content <- "Spinning!"
builder.IsEphemeral <- true
let! followUp = itx.CreateFollowupMessageAsync(builder) |> Async.AwaitTask
do! Async.Sleep sleepTime
let content = $"{Formatter.Emoji(emojis.[results.[0]])}"
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent(content)) |> Async.AwaitTask
do! Async.Sleep sleepTime
let content = $"{Formatter.Emoji(emojis.[results.[0]])}{Formatter.Emoji(emojis.[results.[1]])}"
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent(content)) |> Async.AwaitTask
do! Async.Sleep sleepTime
let content = $"{Formatter.Emoji(emojis.[results.[0]])}{Formatter.Emoji(emojis.[results.[1]])}{Formatter.Emoji(emojis.[results.[2]])}"
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder().WithContent(content)) |> Async.AwaitTask
return ()
}
let spinFiles (results : int array) (ctx : IDiscordContext) =
async {
let itx = ctx.GetInteraction()
let builder = DiscordFollowupMessageBuilder()
builder.Content <- "Spinning!"
builder.IsEphemeral <- true
let! followUp = itx.CreateFollowupMessageAsync(builder) |> Async.AwaitTask
do! Async.Sleep sleepTime
let ( name , stream ) = slotsImages.[results.[0]]
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder()
.AddFile(name + "1.png", stream)) |> Async.AwaitTask
do! Async.Sleep sleepTime
let ( name , stream ) = slotsImages.[results.[1]]
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder()
.AddFile(name + "2.png", stream)) |> Async.AwaitTask
do! Async.Sleep sleepTime
let ( name , stream ) = slotsImages.[results.[2]]
let! _ = itx.EditFollowupMessageAsync(followUp.Id, DiscordWebhookBuilder()
.AddFile(name + "3.png", stream)) |> Async.AwaitTask
return ()
}
let spin spinType (ctx : IDiscordContext) =
PlayerInteractions.executePlayerAction ctx (fun player -> async {
let random = Random(System.Guid.NewGuid().GetHashCode())
let slotCount = slots.Length
let results = [| random.Next(0, slotCount) ; random.Next(0, slotCount) ; random.Next(0, slotCount) |]
let winConditions = (results.[0] = results.[1] && results.[0] = results.[2])
if winConditions
then do! DbService.updatePlayerCurrency twoOfAKindPrize player |> Async.Ignore
else do! DbService.updatePlayerCurrency -PlayPrice player |> Async.Ignore
match spinType with
| "Embeds" -> do! spinEmbeds results ctx
| "Emojis" -> do! spinEmojis results ctx
| "Files" -> do! spinFiles results ctx
| _ -> ()
do! Async.Sleep sleepTime
let builder = DiscordFollowupMessageBuilder()
builder.IsEphemeral <- true
let embed4 = DiscordEmbedBuilder()
embed4.Title <- "Slot Machine"
if winConditions then
embed4.Description <- $"You win {threeOfAKindPrize} GBT!"
else
builder.Content <- "Better luck next time! You paid 1 $GBT"
builder.AddEmbed(embed4) |> ignore
do! ctx.FollowUp(builder) |> Async.AwaitTask |> Async.Ignore
return ()
})
let handleSpin (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) =
let ctx = DiscordEventContext event
task {
match event.Id with
| "spin-embeds" -> do! spin "Embeds" ctx
| "spin-emojis" -> do! spin "Emojis" ctx
| "spin-files" -> do! spin "Files" ctx
| _ ->
printfn "Wrong Spin ID"
return ()
} :> Task
let sendInitialEmbed (ctx : IDiscordContext) =
async {
try
let channel = ctx.GetGuild().GetChannel(GuildEnvironment.channelSlots)
let builder = DiscordMessageBuilder()
let embed = DiscordEmbedBuilder()
embed.Title <- "Degenz Slot Machine"
embed.Description <- "Hello I am an embed"
embed.ImageUrl <- "https://i.kym-cdn.com/photos/images/original/001/169/608/a43.gif"
builder.AddEmbed(embed) |> ignore
let button1 = DiscordButtonComponent(ButtonStyle.Success, $"spin-embeds", $"Spin Embeds 1 $GBT") :> DiscordComponent
let button2 = DiscordButtonComponent(ButtonStyle.Success, $"spin-emojis", $"Spin Emojis 5 $GBT") :> DiscordComponent
let button3 = DiscordButtonComponent(ButtonStyle.Success, $"spin-files", $"Spin Files 10 $GBT") :> DiscordComponent
builder.AddComponents [| button1 ; button2 ; button3 |] |> ignore
do! GuildEnvironment.botClientSlots.Value.SendMessageAsync(channel, builder)
|> Async.AwaitTask
|> Async.Ignore
with e ->
printfn $"Error trying to get channel Training Dojo\n\n{e.Message}"
} |> Async.RunSynchronously