public static Chicken FromDatabaseChicken(DatabaseChicken dbc) { if (dbc == null) { return(null); } return(new Chicken { GuildId = dbc.GuildId, Name = dbc.Name, OwnerId = dbc.UserId, Stats = new ChickenStats { BareStrength = dbc.Strength, BareMaxVitality = dbc.MaxVitality, BareVitality = dbc.Vitality, Upgrades = dbc.DbUpgrades.Select(u => new ChickenUpgrade { Id = u.Id, Modifier = u.DbChickenUpgrade.Modifier, Name = u.DbChickenUpgrade.Name, Price = u.DbChickenUpgrade.Cost, UpgradesStat = u.DbChickenUpgrade.UpgradesStat }).ToList().AsReadOnly() } }); }
public DatabaseChicken ToDatabaseChicken(DatabaseChicken target = null) { if (target is null) { return(new DatabaseChicken { DbUpgrades = this.Stats.Upgrades.Select(u => new DatabaseChickenBoughtUpgrade { GuildId = this.GuildId, Id = u.Id, UserId = this.OwnerId }).ToList(), GuildId = this.GuildId, MaxVitality = this.Stats.BareMaxVitality, Name = this.Name, Strength = this.Stats.BareStrength, UserId = this.OwnerId, Vitality = this.Stats.BareVitality }); } else { target.DbUpgrades = this.Stats.Upgrades.Select(u => new DatabaseChickenBoughtUpgrade { GuildId = this.GuildId, Id = u.Id, UserId = this.OwnerId }).ToList(); target.GuildId = this.GuildId; target.MaxVitality = this.Stats.BareMaxVitality; target.Name = this.Name; target.Strength = this.Stats.BareStrength; target.UserId = this.OwnerId; target.Vitality = this.Stats.BareVitality; return(target); } }
public async Task StrengthAsync(CommandContext ctx) { if (this.Shared.GetEventInChannel(ctx.Channel.Id) is ChickenWar) { throw new CommandFailedException("There is a chicken war running in this channel. No trainings are allowed before the war finishes."); } string result; using (DatabaseContext db = this.Database.CreateContext()) { DatabaseChicken dbc = await db.Chickens.FindAsync((long)ctx.Guild.Id, (long)ctx.User.Id); var chicken = Chicken.FromDatabaseChicken(dbc); if (chicken is null) { throw new CommandFailedException("You do not own a chicken!"); } if (chicken.Stats.TotalVitality < 25) { throw new CommandFailedException($"{ctx.User.Mention}, your chicken is too weak for that action! Heal it using {Formatter.BlockCode("chicken heal")} command."); } long price = chicken.TrainStrengthPrice; if (!await ctx.WaitForBoolReplyAsync($"{ctx.User.Mention}, are you sure you want to train your chicken for {Formatter.Bold($"{price:n0}")} {this.Shared.GetGuildConfig(ctx.Guild.Id).Currency ?? "credits"}?\n\nNote: This action will also weaken the vitality of your chicken by 1.")) { return; } if (!await db.TryDecreaseBankAccountAsync(ctx.User.Id, ctx.Guild.Id, price)) { throw new CommandFailedException($"You do not have enough {this.Shared.GetGuildConfig(ctx.Guild.Id).Currency ?? "credits"} to train a chicken ({price:n0} needed)!"); } if (chicken.TrainStrength()) { result = $"{ctx.User.Mention}'s chicken learned alot from the training. New strength: {chicken.Stats.TotalStrength}"; } else { result = $"{ctx.User.Mention}'s chicken got tired and didn't learn anything. New strength: {chicken.Stats.TotalStrength}"; } chicken.Stats.BareVitality--; dbc.Strength = chicken.Stats.BareStrength; dbc.Vitality--; db.Chickens.Update(dbc); await db.SaveChangesAsync(); } await this.InformAsync(ctx, StaticDiscordEmoji.Chicken, result); }
public static Chicken FromDatabase(DatabaseContextBuilder dbb, ulong gid, string name) { Chicken chicken = null; using (DatabaseContext db = dbb.CreateContext()) { DatabaseChicken dbc = db.Chickens .Include(c => c.DbUpgrades) .ThenInclude(u => u.DbChickenUpgrade) .FirstOrDefault(c => c.GuildId == gid && string.Compare(c.Name, name, true) == 0); chicken = FromDatabaseChicken(dbc); } return(chicken); }
public static Chicken FromDatabase(DatabaseContextBuilder dbb, ulong gid, ulong uid) { Chicken chicken = null; using (DatabaseContext db = dbb.CreateContext()) { DatabaseChicken dbc = db.Chickens .Include(c => c.DbUpgrades) .ThenInclude(u => u.DbChickenUpgrade) .SingleOrDefault(c => c.GuildId == gid && c.UserId == uid); chicken = FromDatabaseChicken(dbc); } return(chicken); }
public async Task HealAsync(CommandContext ctx) { if (this.Shared.GetEventInChannel(ctx.Channel.Id) is ChickenWar) { throw new CommandFailedException("There is a chicken war running in this channel. You are not allowed to heal your chicken before the war finishes."); } using (DatabaseContext db = this.Database.CreateContext()) { DatabaseChicken dbc = await db.Chickens.FindAsync((long)ctx.Guild.Id, (long)ctx.User.Id); if (dbc is null) { throw new CommandFailedException("You do not own a chicken in this guild!"); } dbc.Vitality = (dbc.Vitality + 100) > dbc.MaxVitality ? dbc.MaxVitality : (dbc.Vitality + 100); db.Chickens.Update(dbc); await db.SaveChangesAsync(); } await this.InformAsync(ctx, StaticDiscordEmoji.Chicken, $"{ctx.User.Mention} healed his chicken (+100 to current HP)!"); }
public async Task RenameAsync(CommandContext ctx, [RemainingText, Description("New chicken name.")] string newname) { if (string.IsNullOrWhiteSpace(newname)) { throw new InvalidCommandUsageException("New name for your chicken is missing."); } if (newname.Length < 2 || newname.Length > 30) { throw new InvalidCommandUsageException("Name cannot be shorter than 2 and longer than 30 characters."); } if (!newname.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c))) { throw new InvalidCommandUsageException("Name cannot contain characters that are not letters or digits."); } if (this.Shared.GetEventInChannel(ctx.Channel.Id) is ChickenWar) { throw new CommandFailedException("There is a chicken war running in this channel. No renames are allowed before the war finishes."); } using (DatabaseContext db = this.Database.CreateContext()) { DatabaseChicken dbc = db.Chickens.FirstOrDefault(c => c.GuildId == ctx.Guild.Id && c.UserId == ctx.User.Id); if (dbc is null) { throw new CommandFailedException("You do not own a chicken in this guild!"); } dbc.Name = newname; db.Update(dbc); await db.SaveChangesAsync(); } await this.InformAsync(ctx, StaticDiscordEmoji.Chicken, $"{ctx.User.Mention} renamed his chicken to {Formatter.Bold(newname)}"); }