public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [Description("Team 1 name.")] string team1 = null,
                                                [Description("Team 2 name.")] string team2 = null)
            {
                if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException("Another event is already running in the current channel.");
                }

                var war = new ChickenWar(ctx.Client.GetInteractivity(), ctx.Channel, team1, team2);

                this.Shared.RegisterEventInChannel(war, ctx.Channel.Id);
                try {
                    await this.InformAsync(ctx, StaticDiscordEmoji.Clock1, $"The war will start in 1 minute. Use command {Formatter.InlineCode("chicken war join <teamname>")} to make your chicken join the war.");

                    await Task.Delay(TimeSpan.FromMinutes(1));

                    if (war.Team1.Any() && war.Team2.Any())
                    {
                        await war.RunAsync();

                        var sb = new StringBuilder();

                        foreach (Chicken chicken in war.Team1Won ? war.Team1 : war.Team2)
                        {
                            chicken.Stats.BareStrength += war.Gain;
                            chicken.Stats.BareVitality -= 10;
                            await this.Database.ModifyChickenAsync(chicken, ctx.Guild.Id);

                            await this.Database.IncreaseBankAccountBalanceAsync(chicken.OwnerId, ctx.Guild.Id, 100000);

                            sb.AppendLine($"{Formatter.Bold(chicken.Name)} gained {war.Gain} STR and lost 10 HP!");
                        }

                        foreach (Chicken chicken in war.Team1Won ? war.Team2 : war.Team1)
                        {
                            chicken.Stats.BareVitality -= 50;
                            if (chicken.Stats.TotalVitality > 0)
                            {
                                await this.Database.ModifyChickenAsync(chicken, ctx.Guild.Id);

                                sb.AppendLine($"{Formatter.Bold(chicken.Name)} lost 25 HP!");
                            }
                            else
                            {
                                await this.Database.RemoveChickenAsync(chicken.OwnerId, ctx.Guild.Id);

                                sb.AppendLine($"{Formatter.Bold(chicken.Name)} died!");
                            }
                        }

                        await this.InformAsync(ctx, StaticDiscordEmoji.Chicken, $"{Formatter.Bold(war.Team1Won ? war.Team1Name : war.Team2Name)} won the war!\n\nEach chicken owner in the won party gains 100000 {this.Shared.GetGuildConfig(ctx.Guild.Id).Currency ?? "credits"}.\n\n{sb.ToString()}");
                    }
                    else
                    {
                        await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, "Not enough chickens joined the war (need atleast one in each team).");
                    }
                } finally {
                    this.Shared.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [Description("Whose chicken to ambush?")] DiscordMember member)
            {
                if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Shared.GetEventInChannel(ctx.Channel.Id) is ChickenWar)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException("Another event is already running in the current channel.");
                    }
                    return;
                }

                Chicken ambushed = await this.Database.GetChickenAsync(member.Id, ctx.Guild.Id);

                if (ambushed == null)
                {
                    throw new CommandFailedException("Given user does not have a chicken in this guild!");
                }

                Chicken ambusher = await this.Database.GetChickenAsync(ctx.User.Id, ctx.Guild.Id);

                if (ambusher == null)
                {
                    throw new CommandFailedException("You do not own a chicken!");
                }

                if (ambusher.Stats.TotalStrength > ambushed.Stats.TotalStrength)
                {
                    throw new CommandFailedException("You cannot start an ambush against a weaker chicken!");
                }

                var ambush = new ChickenWar(ctx.Client.GetInteractivity(), ctx.Channel, "Ambushed chickens", "Evil ambushers");

                this.Shared.RegisterEventInChannel(ambush, ctx.Channel.Id);
                try {
                    ambush.AddParticipant(ambushed, member, team1: true);
                    await this.JoinAsync(ctx);

                    await this.InformAsync(ctx, StaticDiscordEmoji.Clock1, $"The ambush will start in 1 minute. Use command {Formatter.InlineCode("chicken ambush")} to make your chicken join the ambush, or {Formatter.InlineCode("chicken ambush help")} to help the ambushed chicken.");

                    await Task.Delay(TimeSpan.FromMinutes(1));

                    if (ambush.Team2.Any())
                    {
                        await ambush.RunAsync();

                        var sb = new StringBuilder();

                        foreach (Chicken chicken in ambush.Team1Won ? ambush.Team1 : ambush.Team2)
                        {
                            chicken.Stats.BareStrength += 5;
                            chicken.Stats.BareVitality -= 10;
                            await this.Database.ModifyChickenAsync(chicken, ctx.Guild.Id);

                            sb.AppendLine($"{Formatter.Bold(chicken.Name)} gained 5 STR and lost 10 HP!");
                        }

                        foreach (Chicken chicken in ambush.Team1Won ? ambush.Team2 : ambush.Team1)
                        {
                            chicken.Stats.BareVitality -= 50;
                            if (chicken.Stats.TotalVitality > 0)
                            {
                                await this.Database.ModifyChickenAsync(chicken, ctx.Guild.Id);

                                sb.AppendLine($"{Formatter.Bold(chicken.Name)} lost 50 HP!");
                            }
                            else
                            {
                                await this.Database.RemoveChickenAsync(chicken.OwnerId, ctx.Guild.Id);

                                sb.AppendLine($"{Formatter.Bold(chicken.Name)} died!");
                            }
                        }

                        await this.InformAsync(ctx, StaticDiscordEmoji.Chicken, $"{Formatter.Bold(ambush.Team1Won ? ambush.Team1Name : ambush.Team2Name)} won!\n\n{sb.ToString()}");
                    }
                } finally {
                    this.Shared.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [Description("desc-team1-name")] string?team1 = null,
                                                [Description("desc-team2-name")] string?team2 = null)
            {
                ChannelEventService evs = ctx.Services.GetRequiredService <ChannelEventService>();

                if (evs.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                }

                var war = new ChickenWar(ctx.Client.GetInteractivity(), ctx.Channel, team1, team2);

                evs.RegisterEventInChannel(war, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-chicken-war-start");

                    await Task.Delay(TimeSpan.FromMinutes(1));

                    if (war.Team1.Any() && war.Team2.Any())
                    {
                        await war.RunAsync(this.Localization);

                        ChickenFightResult?res = war.Result;
                        if (res is null)
                        {
                            return;
                        }

                        var sb   = new StringBuilder();
                        int gain = (int)Math.Floor((double)res.StrGain / war.WinningTeam.Count);
                        BankAccountService bas = ctx.Services.GetRequiredService <BankAccountService>();
                        foreach (Chicken chicken in war.WinningTeam)
                        {
                            chicken.Stats.BareStrength += gain;
                            chicken.Stats.BareVitality -= 20;
                            sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-gain-loss", chicken.Name, gain, 10));
                            await bas.IncreaseBankAccountAsync(chicken.GuildId, chicken.UserId, res.Reward);
                        }
                        await this.Service.UpdateAsync(war.WinningTeam);

                        foreach (Chicken chicken in war.LosingTeam)
                        {
                            chicken.Stats.BareVitality -= 50;
                            if (chicken.Stats.TotalVitality > 0)
                            {
                                sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-d", chicken.Name));
                            }
                            else
                            {
                                sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-loss", chicken.Name, ChickenFightResult.VitLoss));
                            }
                        }
                        await this.Service.RemoveAsync(war.LosingTeam.Where(c => c.Stats.TotalVitality <= 0));

                        await this.Service.UpdateAsync(war.LosingTeam.Where(c => c.Stats.TotalVitality > 0));

                        await ctx.RespondWithLocalizedEmbedAsync(emb => {
                            emb.WithLocalizedTitle("fmt-chicken-war-won", Emojis.Chicken, war.Team1Won ? war.Team1Name : war.Team2Name);
                            emb.WithDescription(sb.ToString());
                            emb.WithLocalizedFooter("fmt-chicken-war-rew", null, res.Reward);
                            emb.WithColor(this.ModuleColor);
                        });
                    }
                    else
                    {
                        await ctx.FailAsync(Emojis.AlarmClock, "str-chicken-war-none");
                    }
                } finally {
                    evs.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
Beispiel #4
0
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [Description("desc-member")] DiscordMember member)
            {
                ChannelEventService evs = ctx.Services.GetRequiredService <ChannelEventService>();

                if (evs.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (evs.GetEventInChannel <ChickenWar>(ctx.Channel.Id) is null)
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    await this.JoinAsync(ctx);

                    return;
                }

                if (member == ctx.User)
                {
                    throw new CommandFailedException(ctx, "cmd-err-chicken-self");
                }

                Chicken?ambusher = await this.Service.GetAsync(member.Id, ctx.User.Id);

                if (ambusher is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-chicken-none");
                }
                ambusher.Owner = ctx.User;

                Chicken?ambushed = await this.Service.GetAndSetOwnerAsync(ctx.Client, ctx.Guild.Id, member.Id);

                if (ambushed is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-chicken-404", member.Mention);
                }
                ambushed.Owner = member;

                if (ambusher.IsTooStrongFor(ambushed))
                {
                    throw new CommandFailedException(ctx, "cmd-err-chicken-strdiff", Chicken.MaxFightStrDiff);
                }

                var ambush = new ChickenWar(ctx.Client.GetInteractivity(), ctx.Channel, "Ambushed chickens", "Evil ambushers");

                evs.RegisterEventInChannel(ambush, ctx.Channel.Id);
                try {
                    ambush.AddParticipant(ambushed, member, team1: true);
                    await this.JoinAsync(ctx);

                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-chicken-ambush-start");

                    await Task.Delay(TimeSpan.FromMinutes(1));

                    if (ambush.Team2.Any())
                    {
                        await ambush.RunAsync(this.Localization);

                        ChickenFightResult?res = ambush.Result;
                        if (res is null)
                        {
                            return;
                        }

                        var sb   = new StringBuilder();
                        int gain = (int)Math.Floor((double)res.StrGain / ambush.WinningTeam.Count);
                        foreach (Chicken chicken in ambush.WinningTeam)
                        {
                            chicken.Stats.BareStrength += gain;
                            chicken.Stats.BareVitality -= 10;
                            sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-gain-loss", chicken.Name, gain, 10));
                        }
                        await this.Service.UpdateAsync(ambush.WinningTeam);

                        foreach (Chicken chicken in ambush.LosingTeam)
                        {
                            chicken.Stats.BareVitality -= 50;
                            if (chicken.Stats.TotalVitality > 0)
                            {
                                sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-d", chicken.Name));
                            }
                            else
                            {
                                sb.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-loss", chicken.Name, ChickenFightResult.VitLoss));
                            }
                        }
                        await this.Service.RemoveAsync(ambush.LosingTeam.Where(c => c.Stats.TotalVitality <= 0));

                        await this.Service.UpdateAsync(ambush.LosingTeam.Where(c => c.Stats.TotalVitality > 0));

                        await ctx.RespondWithLocalizedEmbedAsync(emb => {
                            emb.WithLocalizedTitle("fmt-chicken-war-won", Emojis.Chicken, ambush.Team1Won ? ambush.Team1Name : ambush.Team2Name);
                            emb.WithDescription(sb.ToString());
                            emb.WithColor(this.ModuleColor);
                        });
                    }
                } finally {
                    evs.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }