Пример #1
0
            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);
                }
            }
Пример #2
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);
                }
            }