From e24ca78a081a9b41ab32a7e9e512ee2b004f4464 Mon Sep 17 00:00:00 2001 From: simplyphp Date: Mon, 1 Aug 2016 20:52:40 -0500 Subject: [PATCH] Rewrote most of the upgrade code to actually work properly. --- PoGo.NecroBot.Logic/ILogicSettings.cs | 1 + PoGo.NecroBot.Logic/Inventory.cs | 37 ++++++-- PoGo.NecroBot.Logic/Settings.cs | 5 +- .../Tasks/DisplayPokemonStatsTask.cs | 1 - .../Tasks/LevelUpPokemonTask.cs | 89 ++++++------------- 5 files changed, 62 insertions(+), 71 deletions(-) diff --git a/PoGo.NecroBot.Logic/ILogicSettings.cs b/PoGo.NecroBot.Logic/ILogicSettings.cs index f424c4d81..84b5d1fd9 100644 --- a/PoGo.NecroBot.Logic/ILogicSettings.cs +++ b/PoGo.NecroBot.Logic/ILogicSettings.cs @@ -102,6 +102,7 @@ public interface ILogicSettings float UseBerriesMinIv { get; } double UseBerriesBelowCatchProbability { get; } string UseBerriesOperator { get; } + string UpgradePokemonMinimumStatsOperator { get; } int MaxTravelDistanceInMeters { get; } bool UseGpxPathing { get; } string GpxFile { get; } diff --git a/PoGo.NecroBot.Logic/Inventory.cs b/PoGo.NecroBot.Logic/Inventory.cs index a331f1448..4435cf26d 100644 --- a/PoGo.NecroBot.Logic/Inventory.cs +++ b/PoGo.NecroBot.Logic/Inventory.cs @@ -349,25 +349,25 @@ public async Task> GetPokemonSettings() public async Task> GetPokemonToEvolve(IEnumerable filter = null) { - var myPokemons = await GetPokemons(); - myPokemons = myPokemons.Where(p => p.DeployedFortId == string.Empty).OrderByDescending(p => p.Cp); + var myPokemon = await GetPokemons(); + myPokemon = myPokemon.Where(p => p.DeployedFortId == string.Empty).OrderByDescending(p => p.Cp); //Don't evolve pokemon in gyms IEnumerable pokemonIds = filter as PokemonId[] ?? filter.ToArray(); if (pokemonIds.Any()) { - myPokemons = - myPokemons.Where( + myPokemon = + myPokemon.Where( p => (_logicSettings.EvolveAllPokemonWithEnoughCandy && pokemonIds.Contains(p.PokemonId)) || (_logicSettings.EvolveAllPokemonAboveIv && (PokemonInfo.CalculatePokemonPerfection(p) >= _logicSettings.EvolveAboveIvValue))); } else if (_logicSettings.EvolveAllPokemonAboveIv) { - myPokemons = - myPokemons.Where( + myPokemon = + myPokemon.Where( p => PokemonInfo.CalculatePokemonPerfection(p) >= _logicSettings.EvolveAboveIvValue); } - var pokemons = myPokemons.ToList(); + var pokemons = myPokemon.ToList(); var myPokemonSettings = await GetPokemonSettings(); var pokemonSettings = myPokemonSettings.ToList(); @@ -399,6 +399,29 @@ public async Task> GetPokemonToEvolve(IEnumerable> GetPokemonToUpgrade() + { + var upgradePokemon = new List(); + + if (!_logicSettings.AutomaticallyLevelUpPokemon) + return upgradePokemon; + + var myPokemon = await GetPokemons(); + myPokemon = myPokemon.Where(p => p.DeployedFortId == string.Empty); + + IEnumerable highestPokemonForUpgrade = (_logicSettings.UpgradePokemonMinimumStatsOperator.ToLower().Equals("and")) ? + myPokemon.Where( + p => (p.Cp >= _logicSettings.UpgradePokemonCpMinimum && + PokemonInfo.CalculatePokemonPerfection(p) >= _logicSettings.UpgradePokemonIvMinimum)).OrderByDescending(p => p.Cp).ToList() : + myPokemon.Where( + p => (p.Cp >= _logicSettings.UpgradePokemonCpMinimum || + PokemonInfo.CalculatePokemonPerfection(p) >= _logicSettings.UpgradePokemonIvMinimum)).OrderByDescending(p => p.Cp).ToList(); + + return upgradePokemon = (_logicSettings.LevelUpByCPorIv.ToLower().Equals("iv")) ? + highestPokemonForUpgrade.OrderByDescending(PokemonInfo.CalculatePokemonPerfection).Take(_logicSettings.AmountOfTimesToUpgradeLoop).ToList() : + highestPokemonForUpgrade.Take(_logicSettings.AmountOfTimesToUpgradeLoop).ToList(); + } + public TransferFilter GetPokemonTransferFilter(PokemonId pokemon) { if (_logicSettings.PokemonsTransferFilter != null && diff --git a/PoGo.NecroBot.Logic/Settings.cs b/PoGo.NecroBot.Logic/Settings.cs index c1ee482f9..8441d87c2 100644 --- a/PoGo.NecroBot.Logic/Settings.cs +++ b/PoGo.NecroBot.Logic/Settings.cs @@ -133,6 +133,8 @@ public class GlobalSettings public float UpgradePokemonCpMinimum; [DefaultValue(95)] public float UpgradePokemonIvMinimum; + [DefaultValue("and")] + public string UpgradePokemonMinimumStatsOperator; //position [DefaultValue(false)] public bool DisableHumanWalking; @@ -702,9 +704,10 @@ public LogicSettings(GlobalSettings settings) public float UseBerriesMinIv => _settings.UseBerriesMinIv; public double UseBerriesBelowCatchProbability => _settings.UseBerriesBelowCatchProbability; public string UseBerriesOperator => _settings.UseBerriesOperator; - + public float UpgradePokemonIvMinimum => _settings.UpgradePokemonIvMinimum; public float UpgradePokemonCpMinimum => _settings.UpgradePokemonCpMinimum; + public string UpgradePokemonMinimumStatsOperator => _settings.UpgradePokemonMinimumStatsOperator; public double WalkingSpeedInKilometerPerHour => _settings.WalkingSpeedInKilometerPerHour; public bool EvolveAllPokemonWithEnoughCandy => _settings.EvolveAllPokemonWithEnoughCandy; public bool KeepPokemonsThatCanEvolve => _settings.KeepPokemonsThatCanEvolve; diff --git a/PoGo.NecroBot.Logic/Tasks/DisplayPokemonStatsTask.cs b/PoGo.NecroBot.Logic/Tasks/DisplayPokemonStatsTask.cs index f7fe4001d..48110910d 100644 --- a/PoGo.NecroBot.Logic/Tasks/DisplayPokemonStatsTask.cs +++ b/PoGo.NecroBot.Logic/Tasks/DisplayPokemonStatsTask.cs @@ -17,7 +17,6 @@ public class DisplayPokemonStatsTask { public static List PokemonId = new List(); - public static List PokemonIdcp = new List(); public static async Task Execute(ISession session) diff --git a/PoGo.NecroBot.Logic/Tasks/LevelUpPokemonTask.cs b/PoGo.NecroBot.Logic/Tasks/LevelUpPokemonTask.cs index 09d83720c..514164ab8 100644 --- a/PoGo.NecroBot.Logic/Tasks/LevelUpPokemonTask.cs +++ b/PoGo.NecroBot.Logic/Tasks/LevelUpPokemonTask.cs @@ -5,6 +5,8 @@ using System.Threading.Tasks; using PoGo.NecroBot.Logic.Logging; using PoGo.NecroBot.Logic.State; +using System.Collections.Generic; +using POGOProtos.Enums; #endregion @@ -15,74 +17,37 @@ internal class LevelUpPokemonTask public static async Task Execute(ISession session, CancellationToken cancellationToken) { if (await session.Inventory.GetStarDust() <= session.LogicSettings.GetMinStarDustForLevelUp) - { return; - } - if (session.LogicSettings.LevelUpByCPorIv.ToLower().Contains("iv") && DisplayPokemonStatsTask.PokemonId.Count != 0) - { - for (int i = 0; i < session.LogicSettings.AmountOfTimesToUpgradeLoop; i++) - { - var rand = new Random(); - var randomNumber = rand.Next(0, DisplayPokemonStatsTask.PokemonId.Count - 1); - var upgradeResult = - await session.Inventory.UpgradePokemon(DisplayPokemonStatsTask.PokemonId[randomNumber]); - if (upgradeResult.Result.ToString().ToLower().Contains("success")) - { - Logger.Write("Pokemon Upgraded:" + upgradeResult.UpgradedPokemon.PokemonId + ":" + - upgradeResult.UpgradedPokemon.Cp); - } - else if (upgradeResult.Result.ToString().ToLower().Contains("insufficient")) - { - Logger.Write("Pokemon Upgrade Failed Not Enough Resources"); - break; - } - else if (upgradeResult.Result.ToString().Contains("ErrorUpgradeNotAvailable")) - { - Logger.Write("Pokemon Is At Max Level For Your Level"); - break; - } - else - { - Logger.Write( - "Pokemon Upgrade Failed Unknown Error"); - break; - } - } + var upgradablePokemon = await session.Inventory.GetPokemonToUpgrade(); + if (upgradablePokemon.Count == 0) + return; - } - else if (session.LogicSettings.LevelUpByCPorIv.ToLower().Contains("cp") && DisplayPokemonStatsTask.PokemonIdcp.Count != 0) + foreach (var pokemon in upgradablePokemon) { - for (int i = 0; i < session.LogicSettings.AmountOfTimesToUpgradeLoop; i++) + var upgradeResult = await session.Inventory.UpgradePokemon(pokemon.Id); + if (upgradeResult.Result.ToString().ToLower().Contains("success")) { - var rand = new Random(); - var randomNumber = rand.Next(0, DisplayPokemonStatsTask.PokemonIdcp.Count - 1); - var upgradeResult = - await session.Inventory.UpgradePokemon(DisplayPokemonStatsTask.PokemonIdcp[randomNumber]); - if (upgradeResult.Result.ToString().ToLower().Contains("success")) - { - Logger.Write("Pokemon Upgraded:" + upgradeResult.UpgradedPokemon.PokemonId + ":" + - upgradeResult.UpgradedPokemon.Cp); - } - else if (upgradeResult.Result.ToString().ToLower().Contains("insufficient")) - { - Logger.Write("Pokemon Upgrade Failed Not Enough Resources"); - break; - } - else if (upgradeResult.Result.ToString().Contains("ErrorUpgradeNotAvailable")) - { - Logger.Write("Pokemon Is At Max Level For Your Level"); - break; - } - else - { - Logger.Write( - "Pokemon Upgrade Failed Unknown Error, Pokemon Could Be Max Level For Your Level The Pokemon That Caused Issue Was:" + - upgradeResult.UpgradedPokemon.PokemonId); - break; - } + Logger.Write("Pokemon Upgraded:" + upgradeResult.UpgradedPokemon.PokemonId + ":" + + upgradeResult.UpgradedPokemon.Cp); + } + else if (upgradeResult.Result.ToString().ToLower().Contains("insufficient")) + { + Logger.Write("Pokemon Upgrade Failed Not Enough Resources"); + break; + } + else if (upgradeResult.Result.ToString().Contains("ErrorUpgradeNotAvailable")) + { + Logger.Write("Pokemon Is At Max Level For Your Level"); + break; + } + else + { + Logger.Write( + "Pokemon Upgrade Failed Unknown Error, Pokemon Could Be Max Level For Your Level The Pokemon That Caused Issue Was:" + + upgradeResult.UpgradedPokemon.PokemonId); + break; } - } } }