Beispiel #1
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));
            }
        }
Beispiel #2
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));
            }
        }