Ejemplo n.º 1
0
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [RemainingText, Description("Three numbers.")] params int[] numbers)
            {
                if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Shared.GetEventInChannel(ctx.Channel.Id) is LotteryGame)
                    {
                        await this.JoinAsync(ctx, numbers);
                    }
                    else
                    {
                        throw new CommandFailedException("Another event is already running in the current channel.");
                    }
                    return;
                }

                var game = new LotteryGame(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Shared.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await this.InformAsync(ctx, StaticDiscordEmoji.Clock1, $"The Lottery game will start in 30s or when there are 10 participants. Use command {Formatter.InlineCode("casino lottery")} to join the pool.");

                    await this.JoinAsync(ctx, numbers);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        await game.RunAsync();

                        if (game.Winners.Any())
                        {
                            await this.InformAsync(ctx, StaticDiscordEmoji.MoneyBag, $"Winnings:\n\n{string.Join(", ", game.Winners.Select(w => $"{w.User.Mention} : {w.WinAmount}"))}");

                            using (DatabaseContext db = this.Database.CreateContext()) {
                                foreach (LotteryParticipant winner in game.Winners)
                                {
                                    await db.ModifyBankAccountAsync(ctx.User.Id, ctx.Guild.Id, v => v + winner.WinAmount);
                                }
                                await db.SaveChangesAsync();
                            }
                        }
                        else
                        {
                            await this.InformAsync(ctx, StaticDiscordEmoji.MoneyBag, "Better luck next time!");
                        }
                    }
                    else
                    {
                        if (game.IsParticipating(ctx.User))
                        {
                            using (DatabaseContext db = this.Database.CreateContext()) {
                                await db.ModifyBankAccountAsync(ctx.User.Id, ctx.Guild.Id, v => v + LotteryGame.TicketPrice);

                                await db.SaveChangesAsync();
                            }
                        }
                        await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, "Not enough users joined the Blackjack game.");
                    }
                } finally {
                    this.Shared.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
Ejemplo n.º 2
0
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [RemainingText, Description("desc-gamble-numbers-3")] params int[] numbers)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Service.GetEventInChannel(ctx.Channel.Id) is LotteryGame)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    return;
                }

                string currency = ctx.Services.GetRequiredService <GuildConfigService>().GetCachedConfig(ctx.Guild.Id).Currency;
                var    game     = new LotteryGame(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-casino-lottery-start",
                                           LotteryGame.MaxParticipants, LotteryGame.TicketPrice, currency
                                           );

                    await this.JoinAsync(ctx, numbers);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    BankAccountService bas = ctx.Services.GetRequiredService <BankAccountService>();
                    if (game.ParticipantCount > 1)
                    {
                        await game.RunAsync(this.Localization);

                        if (game.Winners.Any())
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Cards.Suits[0], "fmt-winnings",
                                                   game.Winners.Select(w => $"{w.User.Mention}: {w.WinAmount:n0} {currency}").JoinWith()
                                                   );

                            foreach (LotteryGame.Participant winner in game.Winners)
                            {
                                await bas.IncreaseBankAccountAsync(ctx.Guild.Id, winner.Id, winner.WinAmount);
                            }
                        }
                        else
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Cards.Suits[0], "str-casino-lottery-lose");
                        }
                    }
                    else
                    {
                        if (game.IsParticipating(ctx.User))
                        {
                            await bas.IncreaseBankAccountAsync(ctx.Guild.Id, ctx.User.Id, LotteryGame.TicketPrice);
                        }
                        await ctx.ImpInfoAsync(this.ModuleColor, Emojis.AlarmClock, "str-casino-lottery-none");
                    }
                } finally {
                    this.Service.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }