Пример #1
0
        public async Task StartBlackjack(EventContext e, int bet)
        {
            using (var context = new MikiContext())
            {
                User user = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                await user.RemoveCurrencyAsync(context, null, bet);
            }

            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 OnBlackjackHit(ec, bm, message, bet)))
                               .AddCommand(
                new RuntimeCommandEvent("stand")
                .SetAliases("knock", "stay", "stop")
                .Default(async(ec) => await OnBlackjackHold(ec, bm, message, bet))
                ).Build();

            Bot.instance.Events.AddPrivateCommandHandler(e.message, c);
        }
Пример #2
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);
            }
        }
Пример #3
0
        private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage,
                                           int bet)
        {
            await e.commandHandler.RequestDisposeAsync();

            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(bet, e.Channel);

                    await context.SaveChangesAsync();
                }
            }

            await bm.CreateEmbed(e)
            .SetAuthor(e.GetResource("miki_blackjack_draw_title") + " | " + e.Author.Username, e.Author.AvatarUrl, "https://patreon.com/mikibot")
            .SetDescription(e.GetResource("blackjack_draw_description") + "\n" +
                            e.GetResource("miki_blackjack_current_balance", user.Currency))
            .ModifyMessage(instanceMessage);
        }
Пример #4
0
        private async Task OnBlackjackDraw(CommandContext e, BlackjackManager bm)
        {
            var cache = e.GetService <ICacheClient>();
            var api   = e.GetService <IApiClient>();

            User user;
            var  context = e.GetService <MikiDbContext>();

            user = await context.Users.FindAsync(e.Author.Id.ToDbLong());

            if (user != null)
            {
                await user.AddCurrencyAsync(bm.Bet, e.Channel);

                await context.SaveChangesAsync();
            }

            await api.EditMessageAsync(e.Channel.Id, bm.MessageId,
                                       new EditMessageArgs
            {
                embed = bm.CreateEmbed(e)
                        .SetAuthor(
                    e.Locale.GetString("blackjack_draw_title") + " | " + e.Author.Username,
                    e.Author.GetAvatarUrl(),
                    "https://patreon.com/mikibot"
                    )
                        .SetDescription(
                    e.Locale.GetString("blackjack_draw_description") + "\n" +
                    e.Locale.GetString("miki_blackjack_current_balance", user.Currency)
                    ).ToEmbed()
            }
                                       );

            await cache.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}");
        }
Пример #5
0
        private async Task OnBlackjackWin(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet)
        {
            e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>()
            .RemoveSession(e.Author.Id, e.Channel.Id);

            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(bet * 2, e.Channel);

                    await context.SaveChangesAsync();
                }
            }

            await instanceMessage.EditAsync(new EditMessageArgs
            {
                embed = bm.CreateEmbed(e)
                        .SetAuthor(e.GetResource("miki_blackjack_win_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot")
                        .SetDescription(e.GetResource("miki_blackjack_win_description", bet * 2) + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency))
                        .ToEmbed()
            });
        }
Пример #6
0
        public async Task StartBlackjack(EventContext e, int bet)
        {
            using (var context = new MikiContext())
            {
                User user = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                await user.AddCurrencyAsync(-bet, e.Channel);

                await context.SaveChangesAsync();
            }

            BlackjackManager bm = new BlackjackManager();

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

            Framework.Events.CommandMap map = new Framework.Events.CommandMap();
            SimpleCommandHandler        c   = new SimpleCommandHandler(map);

            c.AddPrefix("");
            c.AddCommand(new CommandEvent("hit")
                         .Default(async(ec) => await OnBlackjackHit(ec, bm, message, bet)));
            c.AddCommand(new CommandEvent("stand")
                         .SetAliases("knock", "stay", "stop")
                         .Default(async(ec) => await OnBlackjackHold(ec, bm, message, bet)));

            e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>().AddSession(
                new CommandSession()
            {
                UserId = e.Author.Id, ChannelId = e.Channel.Id
            }, c, new TimeSpan(1, 0, 0));
        }
Пример #7
0
        private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm)
        {
            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(bm.Bet, e.Channel);

                    await context.SaveChangesAsync();
                }
            }

            await Global.Client.Client._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId,
                                                                   new EditMessageArgs
            {
                embed = bm.CreateEmbed(e)
                        .SetAuthor(
                    e.Locale.GetString("miki_blackjack_draw_title") + " | " + e.Author.Username,
                    e.Author.GetAvatarUrl(),
                    "https://patreon.com/mikibot"
                    )
                        .SetDescription(
                    e.Locale.GetString("blackjack_draw_description") + "\n" +
                    e.Locale.GetString("miki_blackjack_current_balance", user.Currency)
                    ).ToEmbed()
            }
                                                                   );

            await Global.RedisClient.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}");
        }
Пример #8
0
        private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm, IUserMessage instanceMessage,
                                           int bet)
        {
            e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>()
            .RemoveSession(e.Author.Id, e.Channel.Id);

            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(bet, e.Channel);

                    await context.SaveChangesAsync();
                }
            }

            await instanceMessage.ModifyAsync(x =>
            {
                x.Embed = bm.CreateEmbed(e).ToEmbedBuilder()
                          .SetAuthor(e.GetResource("miki_blackjack_draw_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot")
                          .WithDescription(e.GetResource("blackjack_draw_description") + "\n" +
                                           e.GetResource("miki_blackjack_current_balance", user.Currency)).Build();
            });
        }
Пример #9
0
        private async Task OnBlackjackWin(EventContext e, BlackjackManager bm)
        {
            var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient));
            var api   = (IApiClient)e.Services.GetService(typeof(IApiClient));

            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(bm.Bet * 2, e.Channel);

                    await context.SaveChangesAsync();
                }
            }

            await api.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs
            {
                embed = bm.CreateEmbed(e)
                        .SetAuthor(e.Locale.GetString("miki_blackjack_win_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot")
                        .SetDescription(e.Locale.GetString("miki_blackjack_win_description", bm.Bet * 2) + "\n" + e.Locale.GetString("miki_blackjack_new_balance", user.Currency))
                        .ToEmbed()
            });

            await cache.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}");
        }
Пример #10
0
        private async Task OnBlackjackDead(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet)
        {
            await e.commandHandler.RequestDisposeAsync();

            User user;

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

            await bm.CreateEmbed(e)
            .SetTitle(e.GetResource("miki_blackjack_lose_title"))
            .SetDescription(e.GetResource("miki_blackjack_lose_description") + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency))
            .ModifyMessage(instanceMessage);
        }
Пример #11
0
        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));
            }
        }
Пример #12
0
        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));
            }
        }
Пример #13
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));
            }
        }
Пример #14
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);
                });
            }
        }
Пример #15
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));
            }
        }
Пример #16
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()
                });
            }
        }
Пример #17
0
        private async Task OnBlackjackDead(EventContext e, BlackjackManager bm)
        {
            User user;

            using (var context = new MikiContext())
            {
                user = await context.Users.FindAsync(e.Author.Id.ToDbLong());
            }
            await Global.Client.Client._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId,
                                                                   new EditMessageArgs
            {
                embed = bm.CreateEmbed(e)
                        .SetAuthor(
                    e.Locale.GetString("miki_blackjack_lose_title") + " | " + e.Author.Username,
                    (await e.Guild.GetSelfAsync()).GetAvatarUrl(), "https://patreon.com/mikibot"
                    )
                        .SetDescription(e.Locale.GetString("miki_blackjack_lose_description") + "\n" + e.Locale.GetString("miki_blackjack_new_balance", user.Currency)
                                        ).ToEmbed()
            });

            await Global.RedisClient.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}");
        }
Пример #18
0
        private async Task OnBlackjackWin(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet)
        {
            await e.commandHandler.RequestDisposeAsync();

            User user;

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

                if (user != null)
                {
                    await user.AddCurrencyAsync(e.Channel, null, bet * 2);

                    await context.SaveChangesAsync();
                }
            }

            await bm.CreateEmbed(e)
            .SetTitle(e.GetResource("miki_blackjack_win_title"))
            .SetDescription(e.GetResource("miki_blackjack_win_description", bet * 2) + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency))
            .ModifyMessage(instanceMessage);
        }
Пример #19
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);
            }
        }