Skip to content

Commit

Permalink
Merge pull request #3213 from GrimmGringo/master
Browse files Browse the repository at this point in the history
Removing bugged microsoft expression parser
  • Loading branch information
GrimmGringo committed Aug 8, 2016
2 parents ad46ad9 + a57fc78 commit 402fc38
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2,277 deletions.
2 changes: 1 addition & 1 deletion FeroxRev
103 changes: 25 additions & 78 deletions PoGo.NecroBot.Logic/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using System.Linq.Dynamic;
using DynamicExpression = System.Linq.Dynamic.DynamicExpression;
using PoGo.NecroBot.Logic.Utils;

#endregion

Expand Down Expand Up @@ -95,86 +93,42 @@ private async Task<GetInventoryResponse> GetCachedInventory()
bool keepPokemonsThatCanEvolve = false, bool prioritizeIVoverCp = false
)
{

var myPokemon = await GetPokemons();

var myPokemonList = myPokemon.ToList();
var pokemonToTransfer = new List<PokemonData>();

//ExpressionContext context = new ExpressionContext(this);
//context.Imports.AddType(typeof(Math));
//context.Imports.AddType(typeof(PokemonInfo));
/*
const string additionalExample = @"
or PokemonInfo.GetPokemonCount(allPokemons, p) <= ptf.KeepMinDuplicatePokemon
or PokemonInfo.GetCountOfCpBetterPokemon(allPokemons, p) <= 0
or PokemonInfo.GetCountOfIvBetterPokemon(allPokemons, p) <= 0";
*/
const string keepExpr =
//Keep if :
@"
(
(
p.Cp >= ptf.KeepMinCp
{0}
PokemonInfo.CalculatePokemonPerfection(p) >= ptf.KeepMinIvPercentage
{0}
(
ptf.UseKeepMinLvl ? PokemonInfo.GetLevel(p) >= ptf.KeepMinLvl
: ""{0}"" == ""and""
)
)
{1}
(ptf.Moves.Contains(p.Move1) or ptf.Moves.Contains(p.Move2))
)
";


ExpressionParser.AddPredefinedType(typeof(PokemonInfo));
ExpressionParser.AddPredefinedType(typeof(PokemonData));
ExpressionParser.AddPredefinedType(typeof(List<PokemonMove>));

var p1 = Expression.Parameter(typeof(PokemonData), "p");
var p2 = Expression.Parameter(typeof(TransferFilter), "ptf");
var p3 = Expression.Parameter(typeof (List<PokemonData>), "allPokemons");
var pokemonToTransfer = myPokemonList.Where(p => !pokemonsNotToTransfer.Contains(p.PokemonId) && p.DeployedFortId == string.Empty && p.Favorite == 0).ToList();

try
{
var globalKeepExpression = DynamicExpression.ParseLambda(new[] { p1, p2 , p3}, null,
string.Format(keepExpr, CommonTransferFilter.KeepMinOperator, CommonTransferFilter.MovesOperator));

var globalKeepDelegate = globalKeepExpression.Compile();

pokemonToTransfer = myPokemonList.Where(p => !pokemonsNotToTransfer.Contains(p.PokemonId) && p.DeployedFortId == string.Empty && p.Favorite == 0).ToList();
pokemonToTransfer =
pokemonToTransfer.Where(
p =>
{
var ptf = GetPokemonTransferFilter(p.PokemonId);
if (ptf == CommonTransferFilter)
return !(bool) globalKeepDelegate.DynamicInvoke(p, ptf, myPokemonList);
var localKeepExpression = DynamicExpression.ParseLambda(new[] { p1, p2, p3 }, null,
string.Format(keepExpr, ptf.KeepMinOperator, ptf.MovesOperator));
var localKeepDelegate = localKeepExpression.Compile();
return !(bool)localKeepDelegate.DynamicInvoke(p, ptf, myPokemonList);
var pokemonTransferFilter = GetPokemonTransferFilter(p.PokemonId);
return
!pokemonTransferFilter.MovesOperator.BoolFunc(
pokemonTransferFilter.Moves.Intersect(new[] { p.Move1, p.Move2 }).Any(),
pokemonTransferFilter.KeepMinOperator.BoolFunc(
p.Cp >= pokemonTransferFilter.KeepMinCp,
PokemonInfo.CalculatePokemonPerfection(p) >= pokemonTransferFilter.KeepMinIvPercentage,
pokemonTransferFilter.KeepMinOperator.ReverseBoolFunc(
pokemonTransferFilter.KeepMinOperator.InverseBool(pokemonTransferFilter.UseKeepMinLvl),
PokemonInfo.GetLevel(p) >= pokemonTransferFilter.KeepMinLvl)));
}).ToList();
}
catch(Exception e)
{
catch (Exception e)
{
throw e;
}




var myPokemonSettings = await GetPokemonSettings();
var pokemonSettings = myPokemonSettings.ToList();

var myPokemonFamilies = await GetPokemonFamilies();
var pokemonFamilies = myPokemonFamilies.ToArray();



var results = new List<PokemonData>();

foreach (var pokemonGroupToTransfer in pokemonToTransfer.GroupBy(p => p.PokemonId).ToList())
Expand All @@ -191,7 +145,6 @@ private async Task<GetInventoryResponse> GetCachedInventory()
settings.CandyToEvolve > 0 &&
settings.EvolutionIds.Count != 0)
{
//do not try to fix something that works, goto line 220 for explonation
var possibleCountToEvolve = familyCandy.Candy_ / settings.CandyToEvolve;
amountToKeepInStorage = Math.Max(amountToKeepInStorage, possibleCountToEvolve);

Expand Down Expand Up @@ -512,21 +465,15 @@ public async Task<List<PokemonData>> GetPokemonToUpgrade()
highestPokemonForUpgrade.OrderByDescending(p => p.Cp).ToList();
}

private TransferFilter _commonTransferFilter = null;
public TransferFilter CommonTransferFilter => _commonTransferFilter ??
(_commonTransferFilter =
new TransferFilter(_logicSettings.KeepMinCp, _logicSettings.KeepMinLvl,
_logicSettings.UseKeepMinLvl, _logicSettings.KeepMinIvPercentage,
_logicSettings.KeepMinOperator, _logicSettings.KeepMinDuplicatePokemon));

public TransferFilter GetPokemonTransferFilter(PokemonId pokemon)
{
TransferFilter result;

if (_logicSettings.PokemonsTransferFilter == null || !_logicSettings.PokemonsTransferFilter.TryGetValue(pokemon, out result))
result = CommonTransferFilter;

return result;
if (_logicSettings.PokemonsTransferFilter != null &&
_logicSettings.PokemonsTransferFilter.ContainsKey(pokemon))
{
return _logicSettings.PokemonsTransferFilter[pokemon];
}
return new TransferFilter(_logicSettings.KeepMinCp, _logicSettings.KeepMinLvl, _logicSettings.UseKeepMinLvl, _logicSettings.KeepMinIvPercentage,
_logicSettings.KeepMinOperator, _logicSettings.KeepMinDuplicatePokemon);
}

public async Task<GetInventoryResponse> RefreshCachedInventory()
Expand Down
1 change: 0 additions & 1 deletion PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
<Compile Include="Tasks\UseNearbyPokestopsTask.cs" />
<Compile Include="Utils\DelayingUtils.cs" />
<Compile Include="Utils\DeviceInfoHelper.cs" />
<Compile Include="Utils\Dynamic.cs" />
<Compile Include="Utils\EggWalker.cs" />
<Compile Include="Utils\ErrorHandler.cs" />
<Compile Include="Utils\GPXReader.cs" />
Expand Down
57 changes: 20 additions & 37 deletions PoGo.NecroBot.Logic/PoGoUtils/PokemonInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override string ToString()

public static class PokemonInfo
{
public static int CalculateCp(this PokemonData poke)
public static int CalculateCp(PokemonData poke)
{
return
Math.Max(
Expand All @@ -40,15 +40,15 @@ public static int CalculateCp(this PokemonData poke)
Math.Pow(poke.CpMultiplier + poke.AdditionalCpMultiplier, 2)), 10);
}

public static double CalculateCpMultiplier(this PokemonData poke)
public static double CalculateCpMultiplier(PokemonData poke)
{
var baseStats = GetBaseStats(poke.PokemonId);
return (baseStats.BaseAttack + poke.IndividualAttack)*
Math.Sqrt(baseStats.BaseDefense + poke.IndividualDefense)*
Math.Sqrt(baseStats.BaseStamina + poke.IndividualStamina);
}

public static int CalculateMaxCp(this PokemonData poke)
public static int CalculateMaxCp(PokemonData poke)
{
return
Math.Max(
Expand All @@ -57,8 +57,14 @@ public static int CalculateMaxCp(this PokemonData poke)
Math.Pow(poke.CpMultiplier + poke.AdditionalCpMultiplier, 2)), 10);
}

public static double CalculateMaxCpMultiplier(PokemonId pokemonId)
{
var baseStats = GetBaseStats(pokemonId);
return (baseStats.BaseAttack + 15)*Math.Sqrt(baseStats.BaseDefense + 15)*
Math.Sqrt(baseStats.BaseStamina + 15);
}

public static int CalculateMinCp(this PokemonData poke)
public static int CalculateMinCp(PokemonData poke)
{
return
Math.Max(
Expand All @@ -67,33 +73,26 @@ public static int CalculateMinCp(this PokemonData poke)
Math.Pow(poke.CpMultiplier + poke.AdditionalCpMultiplier, 2)), 10);
}

public static double CalculateMinCpMultiplier(this PokemonData poke)
public static double CalculateMinCpMultiplier(PokemonData poke)
{
var baseStats = GetBaseStats(poke.PokemonId);
return baseStats.BaseAttack*Math.Sqrt(baseStats.BaseDefense)*Math.Sqrt(baseStats.BaseStamina);
}

public static double CalculatePokemonPerfection(this PokemonData poke)
public static double CalculatePokemonPerfection(PokemonData poke)
{
if (Math.Abs(poke.CpMultiplier + poke.AdditionalCpMultiplier) <= 0)
return (poke.IndividualAttack + poke.IndividualDefense + poke.IndividualStamina) / 45.0 * 100.0;
return (poke.IndividualAttack + poke.IndividualDefense + poke.IndividualStamina)/45.0*100.0;

//GetBaseStats(poke.PokemonId);
var maxCp = CalculateMaxCpMultiplier(poke.PokemonId);
var minCp = CalculateMinCpMultiplier(poke);
var curCp = CalculateCpMultiplier(poke);

return (curCp - minCp) / (maxCp - minCp) * 100.0;
}

public static double CalculateMaxCpMultiplier(this PokemonId pokemonId)
{
var baseStats = GetBaseStats(pokemonId);
return (baseStats.BaseAttack + 15) * Math.Sqrt(baseStats.BaseDefense + 15) *
Math.Sqrt(baseStats.BaseStamina + 15);
return (curCp - minCp)/(maxCp - minCp)*100.0;
}

public static BaseStats GetBaseStats(this PokemonId id)
public static BaseStats GetBaseStats(PokemonId id)
{
switch ((int) id)
{
Expand Down Expand Up @@ -404,7 +403,7 @@ public static BaseStats GetBaseStats(this PokemonId id)
}
}

public static double GetLevel(this PokemonData poke)
public static double GetLevel(PokemonData poke)
{
switch ((int) ((poke.CpMultiplier + poke.AdditionalCpMultiplier)*1000.0))
{
Expand Down Expand Up @@ -572,45 +571,29 @@ public static double GetLevel(this PokemonData poke)
}
}

public static PokemonMove GetPokemonMove1(this PokemonData poke)
public static PokemonMove GetPokemonMove1(PokemonData poke)
{
var move1 = poke.Move1;
return move1;
}

public static PokemonMove GetPokemonMove2(this PokemonData poke)
public static PokemonMove GetPokemonMove2(PokemonData poke)
{
var move2 = poke.Move2;
return move2;
}

public static int GetCandy(this PokemonData pokemon, List<Candy> PokemonFamilies, IEnumerable<PokemonSettings> PokemonSettings)
public static int GetCandy(PokemonData pokemon, List<Candy> PokemonFamilies, IEnumerable<PokemonSettings> PokemonSettings)
{
var setting = PokemonSettings.FirstOrDefault(q => pokemon != null && q.PokemonId.Equals(pokemon.PokemonId));
var family = PokemonFamilies.FirstOrDefault(q => setting != null && q.FamilyId.Equals(setting.FamilyId));

return family.Candy_;
}

public static int GetPowerUpLevel(this PokemonData poke)
public static int GetPowerUpLevel(PokemonData poke)
{
return (int) (GetLevel(poke)*2.0);
}

public static int GetPokemonCount(this List<PokemonData> pokemonDatas, PokemonData pokemon)
{
return pokemonDatas.Count(data => data.PokemonId == pokemon.PokemonId);
}

public static int GetCountOfCpBetterPokemon(this List<PokemonData> pokemonDatas, PokemonData pokemon)
{
return pokemonDatas.Where(data => data.PokemonId == pokemon.PokemonId).Count(data => data.Cp > pokemon.Cp);
}

public static int GetCountOfIvBetterPokemon(this List<PokemonData> pokemonDatas, PokemonData pokemon)
{
return pokemonDatas.Where(data => data.PokemonId == pokemon.PokemonId).Count(data => data.CalculatePokemonPerfection() > pokemon.CalculatePokemonPerfection());
}

}
}

0 comments on commit 402fc38

Please sign in to comment.