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-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); } }