Beispiel #1
0
        private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet)
        {
            await e.message.DeleteAsync();

            bm.player.AddToHand(bm.deck.DrawRandom());

            if (bm.Worth(bm.player) > 21)
            {
                await OnBlackjackDead(e, bm, instanceMessage, bet);
            }
            else
            {
                if (bm.player.Hand.Count == 5)
                {
                    await OnBlackjackHold(e, bm, instanceMessage, bet, true);

                    return;
                }
                else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21)
                {
                    await OnBlackjackWin(e, bm, instanceMessage, bet);

                    return;
                }
                else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21)
                {
                    await OnBlackjackDead(e, bm, instanceMessage, bet);

                    return;
                }
                await bm.CreateEmbed(e).ModifyMessage(instanceMessage);
            }
        }
Beispiel #2
0
        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));
            }
        }
Beispiel #3
0
        private async Task OnBlackjackHold(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage,
                                           int bet, bool charlie = false)
        {
            bm.dealer.Hand.ForEach(x => x.isPublic = true);

            if (!charlie)
            {
                IDiscordGuildUser me = await e.Guild.GetSelfAsync();

                //if (me.GetPermissions(e.Channel as IDiscordGuildUser).Has(ChannelPermission.ManageMessages))
                //{
                //	await e.message.DeleteAsync();
                //}
            }

            while (true)
            {
                if (bm.Worth(bm.dealer) >= Math.Max(bm.Worth(bm.player), 17))
                {
                    if (charlie)
                    {
                        if (bm.dealer.Hand.Count == 5)
                        {
                            if (bm.Worth(bm.dealer) == bm.Worth(bm.player))
                            {
                                await OnBlackjackDraw(e, bm, instanceMessage, bet);

                                return;
                            }
                            await OnBlackjackDead(e, bm, instanceMessage, bet);

                            return;
                        }
                    }
                    else
                    {
                        if (bm.Worth(bm.dealer) == bm.Worth(bm.player))
                        {
                            await OnBlackjackDraw(e, bm, instanceMessage, bet);

                            return;
                        }
                        await OnBlackjackDead(e, bm, instanceMessage, bet);

                        return;
                    }
                }

                bm.dealer.AddToHand(bm.deck.DrawRandom());

                if (bm.Worth(bm.dealer) > 21)
                {
                    await OnBlackjackWin(e, bm, instanceMessage, bet);

                    return;
                }
            }
        }
Beispiel #4
0
        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));
            }
        }
Beispiel #5
0
        private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet)
        {
            IDiscordGuildUser me = await e.Guild.GetSelfAsync();

            //if (me.GetPermissions(e.Channel as IDiscordGuildChannel).Has(ChannelPermission.ManageMessages))
            //{
            //	await e.message.DeleteAsync();
            //}

            bm.player.AddToHand(bm.deck.DrawRandom());

            if (bm.Worth(bm.player) > 21)
            {
                await OnBlackjackDead(e, bm, instanceMessage, bet);
            }
            else
            {
                if (bm.player.Hand.Count == 5)
                {
                    await OnBlackjackHold(e, bm, instanceMessage, bet, true);

                    return;
                }
                else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21)
                {
                    await OnBlackjackWin(e, bm, instanceMessage, bet);

                    return;
                }
                else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21)
                {
                    await OnBlackjackDead(e, bm, instanceMessage, bet);

                    return;
                }

                await instanceMessage.EditAsync(new EditMessageArgs
                {
                    embed = bm.CreateEmbed(e).ToEmbed()
                });
            }
        }
Beispiel #6
0
        private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IUserMessage instanceMessage, int bet)
        {
            IGuildUser me = await e.Guild.GetCurrentUserAsync();

            if (me.GetPermissions(e.Channel as IGuildChannel).Has(ChannelPermission.ManageMessages))
            {
                await e.message.DeleteAsync();
            }

            bm.player.AddToHand(bm.deck.DrawRandom());

            if (bm.Worth(bm.player) > 21)
            {
                await OnBlackjackDead(e, bm, instanceMessage, bet);
            }
            else
            {
                if (bm.player.Hand.Count == 5)
                {
                    await OnBlackjackHold(e, bm, instanceMessage, bet, true);

                    return;
                }
                else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21)
                {
                    await OnBlackjackWin(e, bm, instanceMessage, bet);

                    return;
                }
                else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21)
                {
                    await OnBlackjackDead(e, bm, instanceMessage, bet);

                    return;
                }

                await instanceMessage.ModifyAsync(x =>
                {
                    x.Embed = bm.CreateEmbed(e);
                });
            }
        }
Beispiel #7
0
        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;
                }
            }
        }
Beispiel #8
0
        private async Task OnBlackjackHold(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage,
                                           int bet, bool charlie = false)
        {
            bm.dealer.Hand.ForEach(x => x.isPublic = true);

            if (!charlie)
            {
                await e.message.DeleteAsync();
            }

            while (true)
            {
                if (bm.Worth(bm.dealer) >= bm.Worth(bm.player))
                {
                    if (charlie)
                    {
                        if (bm.dealer.Hand.Count == 5)
                        {
                            if (bm.Worth(bm.dealer) == bm.Worth(bm.player))
                            {
                                await OnBlackjackDraw(e, bm, instanceMessage, bet);

                                return;
                            }
                            await OnBlackjackDead(e, bm, instanceMessage, bet);

                            return;
                        }
                    }
                    else
                    {
                        if (bm.Worth(bm.dealer) == bm.Worth(bm.player))
                        {
                            await OnBlackjackDraw(e, bm, instanceMessage, bet);

                            return;
                        }
                        await OnBlackjackDead(e, bm, instanceMessage, bet);

                        return;
                    }
                }

                bm.dealer.AddToHand(bm.deck.DrawRandom());

                if (bm.Worth(bm.dealer) > 21)
                {
                    await OnBlackjackWin(e, bm, instanceMessage, bet);

                    return;
                }
            }
        }
Beispiel #9
0
        public async Task BlackjackAsync(EventContext e)
        {
            Locale locale = e.Channel.GetLocale();

            if (Bot.instance.Events.PrivateCommandHandlerExist(e.Author.Id, e.Channel.Id))
            {
                await e.ErrorEmbed(e.GetResource("blackjack_error_instance_exists"))
                .SendToChannel(e.Channel);

                return;
            }

            if (int.TryParse(e.arguments, out int bet))
            {
                if (bet < 1)
                {
                    return;
                }

                using (var context = new MikiContext())
                {
                    User user = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                    if (user.Currency >= bet)
                    {
                        await user.RemoveCurrencyAsync(context, null, bet);
                    }
                    else
                    {
                        await e.ErrorEmbed(e.GetResource("miki_mekos_insufficient"))
                        .SendToChannel(e.Channel);

                        return;
                    }
                }

                BlackjackManager bm = new BlackjackManager();

                IDiscordMessage message = await bm.CreateEmbed(e).SendToChannel(e.Channel);

                CommandHandler c = new CommandHandlerBuilder(Bot.instance.Events)
                                   .AddPrefix("")
                                   .SetOwner(e.message)
                                   .AddCommand(
                    new RuntimeCommandEvent("hit")
                    .Default(async(ec) =>
                {
                    await ec.message.DeleteAsync();

                    bm.player.AddToHand(bm.deck.DrawRandom());

                    if (bm.Worth(bm.player) > 21)
                    {
                        await OnBlackjackDead(ec, bm, message, bet);
                    }
                    else
                    {
                        await bm.CreateEmbed(e).ModifyMessage(message);
                    }
                }))
                                   .AddCommand(
                    new RuntimeCommandEvent("stand")
                    .SetAliases("knock", "stay", "stop")
                    .Default(async(ec) =>
                {
                    bm.dealer.Hand.ForEach(x => x.isPublic = true);

                    await ec.message.DeleteAsync();
                    bool dealerQuit = false;

                    while (!dealerQuit)
                    {
                        if (bm.Worth(bm.dealer) >= bm.Worth(bm.player))
                        {
                            if (bm.Worth(bm.dealer) == bm.Worth(bm.player))
                            {
                                await OnBlackjackDraw(ec, bm, message, bet);
                            }
                            else
                            {
                                await OnBlackjackDead(ec, bm, message, bet);
                            }
                            dealerQuit = true;
                        }
                        else
                        {
                            bm.dealer.AddToHand(bm.deck.DrawRandom());
                            if (bm.Worth(bm.dealer) > 21)
                            {
                                await OnBlackjackWin(ec, bm, message, bet);
                                dealerQuit = true;
                            }
                        }
                        await Task.Delay(500);
                    }
                })).Build();

                Bot.instance.Events.AddPrivateCommandHandler(e.message, c);
            }
            else
            {
                await Utils.ErrorEmbed(e.Channel.GetLocale(), e.GetResource("miki_blackjack_no_arg"))
                .SendToChannel(e.Channel);
            }
        }