Skip to content

Commit

Permalink
Fixed and improved bj command
Browse files Browse the repository at this point in the history
  • Loading branch information
khesir committed Jul 17, 2023
1 parent 9194918 commit aa642db
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 24 deletions.
8 changes: 8 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
62 changes: 39 additions & 23 deletions Command/FunCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,60 @@ public async Task SendEmbedMessage(CommandContext ctx)
[Command("blackjack")]
public async Task playBlackJack(CommandContext ctx)
{
List<string> logs = new List<string>();
var deck = new Deck();
deck.Shuffle();

var playerHand = new Hand();
var dealerHand = new Hand();


// Deal initial cards
playerHand.AddCard(deck.DrawCard());
playerHand.AddCard(deck.DrawCard());

dealerHand.AddCard(deck.DrawCard());
dealerHand.AddCard(deck.DrawCard());

var gameEmbed = new DiscordEmbedBuilder()
.WithThumbnail(ctx.Client.CurrentUser.AvatarUrl)
.AddField("Player hand",$"**Your Hand:** {playerHand} \n = {playerHand.GetHandValue()}",true)
.AddField("Dealer hand", $"**Your Hand:** {dealerHand} \n = {dealerHand.GetHandValue()}", false);
.WithColor(DiscordColor.White)
.AddField("Player hand", $"**Your Hand:** {playerHand} \n = {playerHand.GetHandValue()}", true)
.AddField("Dealer hand", $"**Your Hand:** {dealerHand} \n = {dealerHand.GetHandValue()}", false)
.AddField("Game Logs","There are no action", false);


//Add Reactions for player moves
DiscordEmoji[] optionEmoji = { DiscordEmoji.FromName(ctx.Client, ":punch:", false),
DiscordEmoji.FromName(ctx.Client, ":raised_hand:", false)};

var reactionEmbed = await ctx.Channel.SendMessageAsync(embed: gameEmbed);


foreach (var emoji in optionEmoji)
{
await reactionEmbed.CreateReactionAsync(emoji);
}

//Event handler for reaction added
async Task OnReactionAdded(DiscordClient sender,MessageReactionAddEventArgs e)
{

if (e.User.Id != ctx.User.Id || e.Message.Id != reactionEmbed.Id)
return;
if (e.Emoji == optionEmoji[0])
{
//Player draws another card
var card = deck.DrawCard();
playerHand.AddCard(card);
await ctx.RespondAsync($"Player draws: {card}");

//Update player hand message
var newEmbed = gameEmbed.Build();
await message.modifyAsync()
//await playerHandMessage.ModifyAsync($"Player Hand: {playerHand} | Total {playerHand.GetHandValue()}");
logs.Add($"Player draws: {card}");

await ctx.Channel.SendMessageAsync(embed: DisplayEmbed(logs, playerHand, dealerHand));


// Check if player busts
if(playerHand.IsBust())
if (playerHand.IsBust())
{
await ctx.RespondAsync("Player busts! Dealer wins!");
logs.Add("Player busts! Dealer wins!");

await ctx.Channel.SendMessageAsync(embed: DisplayEmbed(logs, playerHand, dealerHand));
ctx.Client.MessageReactionAdded -= OnReactionAdded;
return;
}
Expand All @@ -98,34 +101,36 @@ await message.modifyAsync()
else if(e.Emoji== optionEmoji[1])
{
//Player stands, and the dealer plays
await ctx.RespondAsync($"Player stands. Dealer reveals {dealerHand}");
logs.Add($"Player stands. Dealer reveals {dealerHand}");


//await dealerHandMessage.ModifyAsync($"Dealer Hand: {dealerHand}");
//Update player hand message
await ctx.Channel.SendMessageAsync(embed: DisplayEmbed(logs, playerHand, dealerHand));

while(dealerHand.GetHandValue() < 17)
while (dealerHand.GetHandValue() < 17)
{
var card = deck.DrawCard();
dealerHand.AddCard(card);
await ctx.RespondAsync($"Dealer draws: {card}");
logs.Add($"Dealer draws: {card}");

//Update dealer hand message after each card drawn.

await ctx.Channel.SendMessageAsync(embed: DisplayEmbed(logs, playerHand, dealerHand));
//await dealerHandMessage.ModifyAsync($"Dealer Hand: {dealerHand}");
}
// Determine the winner
if(dealerHand.IsBust() || playerHand.GetHandValue() > dealerHand.GetHandValue())
{
await ctx.RespondAsync("Player wins!");
logs.Add("Player wins!");
}
else if (playerHand.GetHandValue() < dealerHand.GetHandValue())
{
await ctx.RespondAsync("Dealer wins!");
logs.Add("Dealer wins!");
}
else
{
await ctx.RespondAsync("It's a tie!");
logs.Add("It's a tie!");
}

await ctx.Channel.SendMessageAsync(embed: DisplayEmbed(logs, playerHand, dealerHand));
ctx.Client.MessageReactionAdded -= OnReactionAdded;
return;
}
Expand All @@ -135,5 +140,16 @@ await message.modifyAsync()
// Subscribe to thhe reaction added event
ctx.Client.MessageReactionAdded += OnReactionAdded;
}

private DiscordEmbedBuilder DisplayEmbed(List<string> logs, Hand playerHand, Hand dealerHand)
{
var modifiedEmbed = new DiscordEmbedBuilder()
.WithColor(DiscordColor.White)
.AddField("Player hand", $"**Your Hand:** {playerHand} \n Total value = {playerHand.GetHandValue()}", true)
.AddField("Dealer hand", $"**Your Hand:** {dealerHand} \n Total value = {dealerHand.GetHandValue()}", false)
.AddField("Game Logs", string.Join("\n", logs), false);

return modifiedEmbed;
}
}
}
2 changes: 1 addition & 1 deletion External Classes/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Card

public override string ToString()
{
return $"{Rank} of {Suit}";
return $"{Rank} {Suit}";
}
}
}
33 changes: 33 additions & 0 deletions SlvyDiscordBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
<Reference Include="ConcurrentCollections, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ConcurrentHashSet.1.3.0\lib\net461\ConcurrentCollections.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Commands, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Commands.3.11.0\lib\net461\Discord.Net.Commands.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Core, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Core.3.11.0\lib\net461\Discord.Net.Core.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Interactions, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Interactions.3.11.0\lib\net461\Discord.Net.Interactions.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Rest, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Rest.3.11.0\lib\net461\Discord.Net.Rest.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.Webhook, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Webhook.3.11.0\lib\netstandard2.0\Discord.Net.Webhook.dll</HintPath>
</Reference>
<Reference Include="Discord.Net.WebSocket, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.WebSocket.3.11.0\lib\net461\Discord.Net.WebSocket.dll</HintPath>
</Reference>
<Reference Include="DSharpPlus, Version=4.4.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DSharpPlus.4.4.2\lib\netstandard2.0\DSharpPlus.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -75,11 +93,17 @@
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Interactive.Async, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Interactive.Async.5.0.0\lib\net461\System.Interactive.Async.dll</HintPath>
</Reference>
<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Linq.Async, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Linq.Async.5.0.0\lib\net461\System.Linq.Async.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
Expand All @@ -102,6 +126,9 @@
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Reactive, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reactive.5.0.0\lib\net472\System.Reactive.dll</HintPath>
</Reference>
<Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -141,11 +168,17 @@
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bot.cs" />
Expand Down
11 changes: 11 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ConcurrentHashSet" version="1.3.0" targetFramework="net48" />
<package id="Discord.Net" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.Commands" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.Core" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.Interactions" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.Rest" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.Webhook" version="3.11.0" targetFramework="net48" />
<package id="Discord.Net.WebSocket" version="3.11.0" targetFramework="net48" />
<package id="DSharpPlus" version="4.4.2" targetFramework="net48" />
<package id="DSharpPlus.CommandsNext" version="4.4.2" targetFramework="net48" />
<package id="DSharpPlus.Interactivity" version="4.4.2" targetFramework="net48" />
Expand All @@ -13,12 +20,15 @@
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Collections.Immutable" version="7.0.0" targetFramework="net48" />
<package id="System.Interactive.Async" version="5.0.0" targetFramework="net48" />
<package id="System.IO" version="4.3.0" targetFramework="net48" />
<package id="System.Linq.Async" version="5.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net48" />
<package id="System.Net.WebSockets" version="4.3.0" targetFramework="net48" />
<package id="System.Net.WebSockets.Client" version="4.3.2" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Reactive" version="5.0.0" targetFramework="net48" />
<package id="System.Runtime" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
Expand All @@ -28,4 +38,5 @@
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net48" />
<package id="System.Threading.Channels" version="5.0.0" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net48" />
</packages>

0 comments on commit aa642db

Please sign in to comment.