public async Task SellAsync(CommandContext ctx) { if (!ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar _)) { throw new CommandFailedException(ctx, "cmd-err-chicken-war"); } Chicken?chicken = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = ctx.User; long price = chicken.SellPrice; string currency = ctx.Services.GetRequiredService <GuildConfigService>().GetCachedConfig(ctx.Guild.Id).Currency; if (!await ctx.WaitForBoolReplyAsync("q-chicken-sell", args: new object[] { ctx.User.Mention, price, currency })) { return; } await this.Service.RemoveAsync(chicken); await ctx.Services.GetRequiredService <BankAccountService>().IncreaseBankAccountAsync(ctx.Guild.Id, ctx.User.Id, price); await ctx.InfoAsync(this.ModuleColor, Emojis.Chicken, "fmt-chicken-sell", ctx.User.Mention, chicken.Name, price); }
private async Task TryJoinInternalAsync(CommandContext ctx, int?teamId = null, string?teamName = null) { if (!ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar? war) || war is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-war-none"); } Chicken?chicken = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = ctx.User; if (chicken.Stats.TotalVitality < Chicken.MinVitalityToFight) { throw new CommandFailedException(ctx, "cmd-err-chicken-weak", ctx.User.Mention); } if (war.IsRunning) { throw new CommandFailedException(ctx, "cmd-err-chicken-war-started"); } if (!war.IsParticipating(ctx.User)) { throw new CommandFailedException(ctx, "cmd-err-chicken-war-dup"); } if (teamId is { })
public async Task InfoAsync(CommandContext ctx, [Description("desc-member")] DiscordMember?member = null) { member ??= ctx.Member; Chicken?chicken = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = member; await ctx.RespondWithLocalizedEmbedAsync(emb => { emb.WithTitle($"{Emojis.Chicken} {chicken.Name}"); emb.WithColor(this.ModuleColor); emb.AddLocalizedTitleField("str-owner", chicken.Owner?.Mention ?? chicken.UserId.ToString(), inline: true); emb.AddLocalizedTitleField("str-value", $"{chicken.SellPrice:n0}", inline: true); emb.AddLocalizedTitleField("str-stats", chicken.Stats.ToString(), inline: true); if (chicken.Stats.Upgrades?.Any() ?? false) { emb.AddField("str-upgrades", chicken.Stats.Upgrades.Select(u => u.Upgrade.Name).JoinWith(", "), inline: true); } emb.WithLocalizedFooter("str-chickens", chicken.Owner?.AvatarUrl); }); }
private async Task <Chicken> TryJoinInternalAsync(CommandContext ctx, bool team2 = true) { if (!ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar? ambush) || ambush is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-ambush-none"); } Chicken?chicken = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = ctx.User; if (chicken.Stats.TotalVitality < Chicken.MinVitalityToFight) { throw new CommandFailedException(ctx, "cmd-err-chicken-weak", ctx.User.Mention); } if (ambush.IsRunning) { throw new CommandFailedException(ctx, "cmd-err-chicken-ambush-started"); } if (!ambush.AddParticipant(chicken, ctx.User, team2: team2)) { throw new CommandFailedException(ctx, "cmd-err-chicken-ambush-dup"); } return(chicken); }
public async Task ExecuteGroupAsync(CommandContext ctx, [Description("desc-chicken-upgrade-ids")] params int[] ids) { if (ids is null || !ids.Any()) { throw new CommandFailedException(ctx, "cmd-err-chicken-upg-ids-none"); } if (ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar _)) { throw new CommandFailedException(ctx, "cmd-err-chicken-war"); } Chicken?chicken = await ctx.Services.GetRequiredService <ChickenService>().GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = ctx.User; if (chicken.Stats.Upgrades?.Any(u => ids.Contains(u.Id)) ?? false) { throw new CommandFailedException(ctx, "cmd-err-chicken-upg-dup"); } IReadOnlyList <ChickenUpgrade> upgrades = await this.Service.GetAsync(); var toBuy = upgrades.Where(u => ids.Contains(u.Id)).ToList(); CachedGuildConfig gcfg = ctx.Services.GetRequiredService <GuildConfigService>().GetCachedConfig(ctx.Guild.Id); long totalCost = toBuy.Sum(u => u.Cost); string upgradeNames = toBuy.Select(u => u.Name).JoinWith(", "); if (!await ctx.WaitForBoolReplyAsync("q-chicken-upg", args: new object[] { ctx.User.Mention, totalCost, gcfg.Currency, upgradeNames })) { return; } if (!await ctx.Services.GetRequiredService <BankAccountService>().TryDecreaseBankAccountAsync(ctx.Guild.Id, ctx.User.Id, totalCost)) { throw new CommandFailedException(ctx, "cmd-err-funds", gcfg.Currency, totalCost); } await ctx.Services.GetRequiredService <ChickenBoughtUpgradeService>().AddAsync( toBuy.Select(u => new ChickenBoughtUpgrade { Id = u.Id, GuildId = chicken.GuildId, UserId = chicken.UserId }) ); int addedStr = toBuy.Where(u => u.UpgradesStat == ChickenStatUpgrade.Str).Sum(u => u.Modifier); int addedVit = toBuy.Where(u => u.UpgradesStat == ChickenStatUpgrade.MaxVit).Sum(u => u.Modifier); await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Chicken, "fmt-chicken-upg", ctx.User.Mention, chicken.Name, toBuy.Count, addedStr, addedVit); }
public async Task <Chicken?> GetCompleteAsync(ulong gid, ulong uid) { Chicken?chicken = null; using (TheGodfatherDbContext db = this.dbb.CreateContext()) { chicken = await db.Chickens .Include(c => c.Upgrades) .ThenInclude(u => u.Upgrade) .FirstOrDefaultAsync(c => c.GuildIdDb == (long)gid && c.UserIdDb == (long)uid); } return(chicken); }
public Chicken?GetByName(ulong gid, string name) { using TheGodfatherDbContext db = this.dbb.CreateContext(); Chicken?chicken = db.Chickens .Include(c => c.Upgrades) .ThenInclude(u => u.Upgrade) .Where(c => c.GuildIdDb == (long)gid) .ToList() .FirstOrDefault(c => string.Compare(c.Name, name, true) == 0); return(chicken); }
public static async Task <Chicken?> GetAndSetOwnerAsync(this ChickenService service, DiscordClient client, ulong gid, ulong uid) { Chicken?chicken = await service.GetCompleteAsync(gid, uid); if (chicken is null) { return(null); } bool success = await service.SetOwnerAsync(chicken, client); return(success ? chicken : null); }
public async Task <bool> RenameAsync(ulong gid, ulong uid, string name) { Chicken?chicken = await this.GetAsync(gid, uid); if (chicken is null) { return(false); } using (TheGodfatherDbContext db = this.dbb.CreateContext()) { chicken.Name = name; db.Chickens.Update(chicken); await db.SaveChangesAsync(); } return(true); }
public async Task VitalityAsync(CommandContext ctx) { Chicken?chicken = await this.PreTrainCheckAsync(ctx, "VIT"); if (chicken is null) { return; } bool success = chicken.TrainVitality(); chicken.Stats.BareVitality--; await this.Service.UpdateAsync(chicken); await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Chicken, success? "fmt-chicken-train-succ" : "fmt-chicken-train-fail", ctx.User.Mention, "VIT", chicken.Stats.TotalMaxVitality ); }
public async Task <bool> HealAsync(ulong gid, ulong uid, int amount) { Chicken?chicken = await this.GetAsync(gid, uid); if (chicken is null) { return(false); } using (TheGodfatherDbContext db = this.dbb.CreateContext()) { chicken.Vitality = (chicken.Vitality + amount) > chicken.BareMaxVitality ? chicken.BareMaxVitality : chicken.Vitality + amount; db.Chickens.Update(chicken); await db.SaveChangesAsync(); } return(true); }
public async Task InfoAsync(CommandContext ctx, [Description("desc-chicken-name")] string chickenName) { Chicken?chicken = this.Service.GetByName(ctx.Guild.Id, chickenName); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-name-404"); } try { DiscordMember member = await ctx.Guild.GetMemberAsync(chicken.UserId); await this.InfoAsync(ctx, member); } catch (NotFoundException) { await this.Service.RemoveAsync(chicken); throw new CommandFailedException(ctx, "cmd-err-chicken-owner"); } }
private async Task <Chicken?> PreTrainCheckAsync(CommandContext ctx, string stat) { if (ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar _)) { throw new CommandFailedException(ctx, "cmd-err-chicken-war"); } Chicken?chicken = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (chicken is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } chicken.Owner = ctx.User; if (chicken.Stats.TotalVitality < Chicken.MinVitalityToFight) { throw new CommandFailedException(ctx, "cmd-err-chicken-weak", ctx.User.Mention); } CachedGuildConfig gcfg = ctx.Services.GetRequiredService <GuildConfigService>().GetCachedConfig(ctx.Guild.Id); long price = stat switch { "STR" => chicken.TrainStrengthPrice, "VIT" => chicken.TrainVitalityPrice, _ => throw new CommandFailedException(ctx), }; if (!await ctx.WaitForBoolReplyAsync("q-chicken-train", args: new object[] { ctx.User.Mention, stat, price, gcfg.Currency })) { return(null); } if (!await ctx.Services.GetRequiredService <BankAccountService>().TryDecreaseBankAccountAsync(ctx.Guild.Id, ctx.User.Id, price)) { throw new CommandFailedException(ctx, "cmd-err-funds", gcfg.Currency, price); } return(chicken); }
public async Task FightAsync(CommandContext ctx, [Description("desc-member")] DiscordMember member) { if (ctx.Services.GetRequiredService <ChannelEventService>().IsEventRunningInChannel(ctx.Channel.Id, out ChickenWar _)) { throw new CommandFailedException(ctx, "cmd-err-chicken-war"); } if (member == ctx.User) { throw new CommandFailedException(ctx, "cmd-err-chicken-self"); } Chicken?c1 = await this.Service.GetCompleteAsync(ctx.Guild.Id, ctx.User.Id); if (c1 is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-none"); } c1.Owner = ctx.User; if (c1.Stats.TotalVitality < Chicken.MinVitalityToFight) { throw new CommandFailedException(ctx, "cmd-err-chicken-weak", ctx.User.Mention); } Chicken?c2 = await this.Service.GetCompleteAsync(ctx.Guild.Id, member.Id); if (c2 is null) { throw new CommandFailedException(ctx, "cmd-err-chicken-404", member.Mention); } c2.Owner = member; if (c1.IsTooStrongFor(c2)) { throw new CommandFailedException(ctx, "cmd-err-chicken-strdiff", Chicken.MaxFightStrDiff); } var res = ChickenFightResult.Fight(c1, c2); await this.Service.UpdateAsync(res); await ctx.Services.GetRequiredService <BankAccountService>().IncreaseBankAccountAsync(ctx.Guild.Id, res.Winner.UserId, res.Reward); await ctx.RespondWithLocalizedEmbedAsync(emb => { emb.WithColor(this.ModuleColor); var desc = new StringBuilder(); desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-h", Emojis.Chicken, c1.Name, c1.Stats.ToShortString(), Emojis.DuelSwords, c2.Name, c2.Stats.ToShortString(), Emojis.Chicken )).AppendLine(); desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-w", Emojis.Trophy, res.Winner.Name)).AppendLine(); desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-gain", res.Winner.Name, res.StrGain)); if (res.IsLoserDead) { desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-d", res.Loser.Name)); } else { desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-loss", res.Loser.Name, ChickenFightResult.VitLoss)); } desc.AppendLine(); string currency = ctx.Services.GetRequiredService <GuildConfigService>().GetCachedConfig(ctx.Guild.Id).Currency; desc.AppendLine(this.Localization.GetString(ctx.Guild.Id, "fmt-chicken-fight-rew", res.Winner.Owner?.Mention, res.Reward, currency)); emb.WithDescription(desc); }); }
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); } }