public async Task ExtendBonus(string bonusName) { ShopBonus shopBonus = await this.GetShopBonus(bonusName); if (shopBonus == null || shopBonus.CrystalsPrice <= 0) { throw new FarmHeroesException( "Such option doesn't exist", "You probably tried to extend a non-existing bonus.", "/Hero"); } Hero hero = await this.heroService.GetHero(); HeroBonus heroBonus = hero.Inventory.Bonuses.SingleOrDefault(b => b.Name == shopBonus.Name); if (heroBonus == null) { hero.Inventory.Bonuses.Add(this.GenerateHeroBonus(shopBonus)); } else { heroBonus.ActiveUntil = shopBonus.IsPermanent ? DateTime.MaxValue : heroBonus.ActiveUntil < DateTime.UtcNow ? DateTime.UtcNow.AddDays(shopBonus.Days) : heroBonus.ActiveUntil.AddDays(shopBonus.Days); } await this.resourcePouchService.DecreaseResource(ResourceNames.Crystals, shopBonus.CrystalsPrice); await this.context.SaveChangesAsync(); this.tempDataDictionaryFactory .GetTempData(this.httpContext.HttpContext) .Add("Alert", $"You extended your rent on {shopBonus.Name}."); }
private double GetGoldSafeBonus(Hero hero) { HeroBonus safe = hero.Inventory.Bonuses.SingleOrDefault(b => b.Name == BonusNames.GoldSafe); return(safe != null && safe.ActiveUntil > DateTime.UtcNow ? safe.Bonus : 0); }