private async Task OnBlackjackHold(EventContext e, bool charlie = false) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(cache, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); Dealer.Hand.ForEach(x => x.isPublic = true); while (true) { if (bm.Worth(Dealer) >= Math.Max(bm.Worth(Player), 17)) { if (charlie) { if (Dealer.Hand.Count == 5) { if (bm.Worth(Dealer) == bm.Worth(Player)) { await OnBlackjackDraw(e, bm); return; } await OnBlackjackDead(e, bm); return; } } else { if (bm.Worth(Dealer) == bm.Worth(Player)) { await OnBlackjackDraw(e, bm); return; } await OnBlackjackDead(e, bm); return; } } bm.DealTo(Dealer); if (bm.Worth(Dealer) > 21) { await OnBlackjackWin(e, bm); return; } } }
public int Worth(CardHand hand) { int x = 0; hand.Hand.ForEach(card => { if (card.isPublic) { x += CardWorth[card.value](x); } }); return(x); }
public async Task OnBlackjackNew(EventContext e) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); int?bet = await ValidateBetAsync(e); if (bet.HasValue) { if (await cache.ExistsAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}")) { await e.ErrorEmbedResource(new LanguageResource("blackjack_session_exists")) .ToEmbed().QueueToChannelAsync(e.Channel); return; } using (var context = new MikiContext()) { var user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user == null) { return; } user.RemoveCurrency(bet.Value); await context.SaveChangesAsync(); } BlackjackManager manager = new BlackjackManager(bet.Value); CardHand dealer = manager.AddPlayer(0); CardHand player = manager.AddPlayer(e.Author.Id); manager.DealAll(); manager.DealAll(); dealer.Hand[1].isPublic = false; IDiscordMessage message = await manager.CreateEmbed(e) .ToEmbed().SendToChannel(e.Channel); manager.MessageId = message.Id; await cache.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", manager.ToContext(), TimeSpan.FromHours(24)); } }
public async Task OnBlackjackNew(EventContext e, ArgObject args) { int?bet = await ValidateBetAsync(e, args); if (bet.HasValue) { using (var context = new MikiContext()) { var user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user == null) { return; } user.Currency -= bet.Value; await context.SaveChangesAsync(); } if (await Global.RedisClient.ExistsAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}")) { e.ErrorEmbed("You still have a blackjack game running here, please either stop it by using `>blackjack stay` or finish playing it. This game will expire in 24 hours.") .ToEmbed().QueueToChannel(e.Channel); return; } BlackjackManager manager = new BlackjackManager(bet.Value); CardHand dealer = manager.AddPlayer(0); CardHand player = manager.AddPlayer(e.Author.Id); manager.DealAll(); manager.DealAll(); dealer.Hand[1].isPublic = false; IDiscordMessage message = await manager.CreateEmbed(e) .ToEmbed() .SendToChannel(e.Channel); manager.MessageId = message.Id; await Global.RedisClient.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", manager.ToContext(), TimeSpan.FromHours(24)); } }
private async Task OnBlackjackHit(EventContext e) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); var api = (IApiClient)e.Services.GetService(typeof(IApiClient)); BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(cache, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); bm.DealTo(Player); if (bm.Worth(Player) > 21) { await OnBlackjackDead(e, bm); } else { if (Player.Hand.Count == 5) { await OnBlackjackHold(e, true); return; } else if (bm.Worth(Player) == 21 && bm.Worth(Dealer) != 21) { await OnBlackjackWin(e, bm); return; } else if (bm.Worth(Dealer) == 21 && bm.Worth(Player) != 21) { await OnBlackjackDead(e, bm); return; } await api.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e).ToEmbed() }); await cache.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", bm.ToContext(), TimeSpan.FromHours(24)); } }
private async Task OnBlackjackHit(EventContext e) { BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(Global.RedisClient, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); bm.DealTo(Player); if (bm.Worth(Player) > 21) { await OnBlackjackDead(e, bm); } else { if (Player.Hand.Count == 5) { await OnBlackjackHold(e, true); return; } else if (bm.Worth(Player) == 21 && bm.Worth(Dealer) != 21) { await OnBlackjackWin(e, bm); return; } else if (bm.Worth(Dealer) == 21 && bm.Worth(Player) != 21) { await OnBlackjackDead(e, bm); return; } await Global.Client.ChatClient._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e).ToEmbed() }); await Global.RedisClient.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", bm.ToContext(), TimeSpan.FromHours(24)); } }