public async Task Trash(int id) { Hero hero = await this.heroService.GetHero(); HeroEquipment itemToRemove = hero.Inventory.Items.Find(i => i.Id == id); HeroAmulet amuletToRemove = hero.Inventory.Amulets.Find(a => a.Id == id); if (itemToRemove != null) { this.context.HeroEquipments.Remove(itemToRemove); } else if (amuletToRemove != null) { this.context.HeroAmulets.Remove(amuletToRemove); typeof(AmuletBag) .GetProperties() .Where(x => x.Name.StartsWith("On") && (int)x.GetValue(hero.AmuletBag) == id) .ToList() .ForEach(x => x.SetValue(hero.AmuletBag, 0)); } else { throw new FarmHeroesException( InventoryExceptionMessages.ItemNotOwnedMessage, InventoryExceptionMessages.ItemNotOwnedInstruction, Redirects.InventoryRedirect); } await this.context.SaveChangesAsync(); this.tempDataDictionaryFactory .GetTempData(this.httpContext.HttpContext) .Add("Alert", $"You trashed the desired item successfully."); }
public async Task <CollectedResourcesViewModel> Collect() { CollectedResourcesViewModel collectedResources = new CollectedResourcesViewModel(); Hero hero = await this.heroService.GetHero(); HeroAmulet heroAmulet = hero.EquippedSet.Amulet; this.CheckIfHeroWorkedInMine(hero); Random random = new Random(); collectedResources.Crystals = random.Next(1, 5); if (heroAmulet?.Name == AmuletNames.CrystalDigger) { double chance = random.Next(0, 100); if (heroAmulet.Bonus >= chance) { collectedResources.Crystals *= 2; collectedResources.AmuletActivated = true; } } hero.Statistics.EarnedInMines += collectedResources.Crystals; await this.resourcePouchService.IncreaseResource(ResourceNames.Crystals, collectedResources.Crystals); await this.chronometerService.NullifyWorkUntil(); await this.statisticsService.UpdateStatistics(hero.Statistics); return(collectedResources); }
public async Task InsertAmulet(HeroAmulet heroAmulet) { Inventory inventory = await this.GetCurrentHeroInventory(); inventory.Amulets.Add(heroAmulet); await this.context.SaveChangesAsync(); }
private void CheckIfAmuletBelongsToHero(Hero hero, HeroAmulet heroAmulet) { if (heroAmulet == null || hero.InventoryId != heroAmulet.InventoryId) { throw new FarmHeroesException( EquipmentExceptionMessages.DoesNotBelongToHeroMessage, EquipmentExceptionMessages.DoesNotBelongToHeroInstruction, Redirects.InventoryRedirect, true); } }
public async Task <AmuletViewModel> EquipAmulet(int id) { Hero hero = await this.heroService.GetHero(); HeroAmulet heroAmulet = await this.GetHeroAmuletById(id); EquippedSet equippedSet = await this.GetEquippedSet(); this.CheckIfAmuletBelongsToHero(hero, heroAmulet); equippedSet.Amulet = heroAmulet; await this.context.SaveChangesAsync(); return(this.mapper.Map <AmuletViewModel>(heroAmulet)); }
public async Task SellAmulet(int id) { ShopAmulet shopAmulet = await this.context.ShopAmulets.FindAsync(id); await this.resourcePouchService.DecreaseResource(ResourceNames.Crystals, shopAmulet.InitialPrice); HeroAmulet heroAmulet = new HeroAmulet { Name = shopAmulet.Name, ImageUrl = shopAmulet.ImageUrl, InitialPrice = shopAmulet.InitialPrice, InitialBonus = shopAmulet.InitialBonus, Bonus = shopAmulet.InitialBonus, }; await this.inventoryService.InsertAmulet(heroAmulet); this.tempDataDictionaryFactory .GetTempData(this.httpContext.HttpContext) .Add("Alert", $"You bought {heroAmulet.Name}."); }
public async Task UpgradeAmulet(int id) { Inventory inventory = await this.inventoryService.GetCurrentHeroInventory(); HeroAmulet heroAmulet = inventory.Amulets.Find(i => i.Id == id); this.CheckIfItemIsOwnedByHero(heroAmulet); this.CheckIfItemIsFullyUpgraded(heroAmulet, HeroAmuletMaximumLevel); int cost = SmithFormulas.CalculateAmuletUpgradeCost(heroAmulet); await this.resourcePouchService.DecreaseResource(ResourceNames.Crystals, cost); heroAmulet.Bonus += heroAmulet.Level == HeroAmuletMaximumLevel - 1 ? heroAmulet.InitialBonus * 101 : heroAmulet.InitialBonus; heroAmulet.Level++; await this.context.SaveChangesAsync(); this.tempDataDictionaryFactory .GetTempData(this.httpContext.HttpContext) .Add("Alert", $"You upgraded {heroAmulet.Name}."); }
public async Task <CollectedResourcesViewModel> Collect() { CollectedResourcesViewModel collectedResources = new CollectedResourcesViewModel(); Hero hero = await this.heroService.GetHero(); this.CheckIfHeroWorkedOnFarm(hero); HeroAmulet heroAmulet = hero.EquippedSet.Amulet; collectedResources.Experience = FarmFormulas.CalculateExperience(hero.Level.CurrentLevel, WorkDurationInHours); collectedResources.Gold = FarmFormulas.CalculateGoldEarned(hero.Level.CurrentLevel, WorkDurationInHours, heroAmulet?.Name == AmuletNames.Laborium ? heroAmulet.Bonus : 0); hero.Statistics.EarnedOnFarm += collectedResources.Gold; await this.levelService.GiveHeroExperience(collectedResources.Experience); await this.resourcePouchService.IncreaseResource(ResourceNames.Gold, collectedResources.Gold); await this.chronometerService.NullifyWorkUntil(); await this.statisticsService.UpdateStatistics(hero.Statistics); Notification notification = new Notification() { ImageUrl = FarmNotificationImageUrl, Title = FarmNotificationTitle, Content = FarmNotificationContent, Gold = collectedResources.Gold, Experience = collectedResources.Experience, Type = NotificationType.Farm, Hero = hero, }; await this.notificationService.AddNotification(notification); return(collectedResources); }
public async Task <int> InitiateMonsterFight(int monsterLevel = 0) { Random random = new Random(); Fight fight = new Fight(); Hero attacker = await this.heroService.GetHero(); this.CheckIfHeroIsWorking(attacker); this.CheckIfHeroCanAttackMonster(attacker); await this.amuletBagService.EquipAmulet("MonsterAttack"); if (monsterLevel == 0) { monsterLevel = random.Next(RandomMonsterMinimumLevel, RandomMonsterMaximumLevel); await this.resourcePouchService.DecreaseResource(ResourceNames.Gold, RandomMonsterGoldCost); } else { await this.resourcePouchService.DecreaseResource(ResourceNames.Crystals, MonsterCrystalCost); } Monster databaseMonster = await this.monsterService.GetMonsterByLevel(monsterLevel); FightMonster monster = await this.monsterService.GenerateFightMonster(databaseMonster); EquippedSet attackerSet = await this.equipmentService.GetEquippedSet(); int attackerAttack = FightFormulas.CalculateAttack(attacker); int attackerDefense = FightFormulas.CalculateDefense(attacker); int attackerMastery = FightFormulas.CalculateMastery(attacker); int attackerDexterity = FightFormulas.CalculateDexterity(attacker); int attackerMass = attacker.Characteristics.Mass; HeroAmulet heroAmulet = attacker.EquippedSet.Amulet; if (heroAmulet?.Name == AmuletNames.Undergrounder) { attackerAttack = (int)(attackerAttack * (1 + (heroAmulet.Bonus / 100))); attackerDefense = (int)(attackerDefense * (1 + (heroAmulet.Bonus / 100))); attackerMastery = (int)(attackerMastery * (1 + (heroAmulet.Bonus / 100))); attackerDexterity = (int)(attackerDexterity * (1 + (heroAmulet.Bonus / 100))); } int?[] attackerHits = new int?[Rounds]; int?[] defenderHits = new int?[Rounds]; HitType[] attackerHitTypes = new HitType[Rounds]; HitType[] defenderHitTypes = new HitType[Rounds]; string winnerName = string.Empty; int goldStolen = 0; for (int i = 0; i < Rounds; i++) { bool isCrit = FightFormulas.IsCrit( attackerMastery, monster.Characteristics.Dexterity, attackerSet.Amulet?.Name == AmuletNames.Criticum ? attackerSet.Amulet.Bonus : 0); bool isDodged = FightFormulas.IsDodged(attackerAttack, monster.Characteristics.Dexterity, 0); int attackerDamage = FightFormulas.CalculateHitDamage( attackerAttack, monster.Characteristics.Defense, isCrit, isDodged, attackerSet.Amulet?.Name == AmuletNames.Fatty ? attackerSet.Amulet.Bonus : 0); monster.Health -= attackerDamage; if (monster.Health < 1) { monster.Health = 1; } attackerHits[i] = attackerDamage; attackerHitTypes[i] = isDodged ? HitType.Dodged : isCrit ? HitType.Critical : HitType.Normal; if (monster.Health == 1) { winnerName = attacker.Name; break; } isCrit = FightFormulas.IsCrit(monster.Characteristics.Attack, attackerDexterity, 0); isDodged = FightFormulas.IsDodged(monster.Characteristics.Attack, attackerDexterity, 0); int defenderDamage = FightFormulas.CalculateHitDamage( monster.Characteristics.Attack, attackerDefense, isCrit, isDodged, 0); await this.healthService.ReduceHealth(defenderDamage, attacker.HealthId); defenderHits[i] = defenderDamage; defenderHitTypes[i] = isDodged ? HitType.Dodged : isCrit ? HitType.Critical : HitType.Normal; if (await this.healthService.CheckIfDead(attacker.HealthId)) { winnerName = monster.Name; break; } } if (string.IsNullOrEmpty(winnerName)) { winnerName = attackerHits.Sum() > defenderHits.Sum() ? attacker.Name : monster.Name; } if (winnerName == attacker.Name) { goldStolen = MonsterFormulas.CalculateReward(databaseMonster, attacker.Level.CurrentLevel); await this.resourcePouchService.IncreaseResource(ResourceNames.Gold, goldStolen, attacker.ResourcePouchId); await this.levelService.GiveHeroExperience(monster.Level, attacker.LevelId); attacker.Statistics.EarnedFromMonsters += goldStolen; attacker.Statistics.MonstersDefeated++; } else if (winnerName == monster.Name) { goldStolen = ResourceFormulas.CalculateStolenGold( attacker.ResourcePouch.Gold, this.GetGoldSafeBonus(attacker)); await this.resourcePouchService.DecreaseResource(ResourceNames.Gold, goldStolen, attacker.ResourcePouchId); } await this.statisticsService.UpdateStatistics(attacker.Statistics); await this.chronometerService.SetCannotAttackMonsterUntil(SecondsUntilNextMonsterAttack, attacker.ChronometerId); #region Map Fight entity fight.WinnerName = winnerName; fight.GoldStolen = goldStolen; fight.ExperienceGained = monster.Level; fight.AttackerDamageDealt = (int)attackerHits.Sum(); fight.DefenderDamageDealt = (int)defenderHits.Sum(); fight.AttackerHealthLeft = attacker.Health.Current; fight.DefenderHealthLeft = monster.Health; fight.AttackerId = attacker.Id; fight.AttackerName = attacker.Name; fight.DefenderName = monster.Name; fight.AttackerLevel = attacker.Level.CurrentLevel; fight.DefenderLevel = monster.Level; fight.AttackerAvatarUrl = attacker.AvatarUrl; fight.DefenderAvatarUrl = monster.AvatarUrl; fight.AttackerAttack = attackerAttack; fight.AttackerDefense = attackerDefense; fight.AttackerMastery = attackerMastery; fight.AttackerMass = attackerMass; fight.AttackerDexterity = attackerDexterity; fight.DefenderAttack = monster.Characteristics.Attack; fight.DefenderDefense = monster.Characteristics.Defense; fight.DefenderMastery = monster.Characteristics.Mastery; fight.DefenderMass = monster.Characteristics.Mass; fight.DefenderDexterity = monster.Characteristics.Dexterity; fight.AttackerHitOne = attackerHits[0]; fight.AttackerHitTwo = attackerHits[1]; fight.AttackerHitThree = attackerHits[2]; fight.AttackerHitFour = attackerHits[3]; fight.AttackerHitFive = attackerHits[4]; fight.DefenderHitOne = defenderHits[0]; fight.DefenderHitTwo = defenderHits[1]; fight.DefenderHitThree = defenderHits[2]; fight.DefenderHitFour = defenderHits[3]; fight.DefenderHitFive = defenderHits[4]; fight.AttackerHitOneType = attackerHitTypes[0]; fight.AttackerHitTwoType = attackerHitTypes[1]; fight.AttackerHitThreeType = attackerHitTypes[2]; fight.AttackerHitFourType = attackerHitTypes[3]; fight.AttackerHitFiveType = attackerHitTypes[4]; fight.DefenderHitOneType = defenderHitTypes[0]; fight.DefenderHitTwoType = defenderHitTypes[1]; fight.DefenderHitThreeType = defenderHitTypes[2]; fight.DefenderHitFourType = defenderHitTypes[3]; fight.DefenderHitFiveType = defenderHitTypes[4]; #endregion Fight fightEntity = this.context.Fights.AddAsync(fight).Result.Entity; attacker.HeroFights.Add(new HeroFight { Fight = fightEntity, Hero = attacker }); await this.context.SaveChangesAsync(); Notification notification = new Notification() { ImageUrl = MonsterFightNotificationImageUrl, Title = FightNotificationTitle, Link = string.Format(FightNotificationLink, fightEntity.Id), Content = string.Format(FightNotificationAttacker, monster.Name), Gold = winnerName == attacker.Name ? goldStolen : goldStolen * -1, Experience = winnerName == attacker.Name ? monster.Level : default(int?), Type = NotificationType.MonsterFight, Hero = attacker, }; await this.notificationService.AddNotification(notification); await this.amuletBagService.EquipAmulet("Idle"); return(fightEntity.Id); }
/// <summary> /// Calculates the upgrade cost of an <see cref="FarmHeroes.Data.Models.HeroModels.HeroAmulet"/>. /// </summary> /// <param name="amulet"> /// A <see cref="FarmHeroes.Data.Models.HeroModels.HeroAmulet"/>, upon which to be calculated the price. /// </param> /// <returns> /// An <see cref="int"/>, the upgrade cost of the <see cref="FarmHeroes.Data.Models.HeroModels.HeroAmulet"/>. /// </returns> public static int CalculateAmuletUpgradeCost(HeroAmulet amulet) => amulet.Level == 99 ? amulet.InitialPrice * 100 : amulet.InitialPrice;
private async Task <HeroAmulet> GetHeroAmuletById(int id) { HeroAmulet heroAmulet = await this.context.HeroAmulets.FindAsync(id); return(heroAmulet); }