Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snipe task: prevent incorrect player position setting; Evolve task, fixed never evolve case; Null reference exception in SnipeScanForPokemon fix #1852

Merged
merged 6 commits into from
Aug 1, 2016
5 changes: 1 addition & 4 deletions PoGo.NecroBot.CLI/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ private static void HandleEvent(DisplayHighestsPokemonEvent displayHighestsPokem

private static void HandleEvent(EvolveCountEvent evolveCountEvent, ISession session )
{
Logger.Write(session.Translation.GetTranslation(TranslationString.PkmPotentialEvolveCount, evolveCountEvent.Evolves) +
( session.LogicSettings.UseLuckyEggsWhileEvolving ?
$" | {session.LogicSettings.UseLuckyEggsMinPokemonAmount} required for mass evolving"
: "" ), LogLevel.Update, ConsoleColor.White );
Logger.Write(session.Translation.GetTranslation(TranslationString.PkmPotentialEvolveCount, evolveCountEvent.Evolves), LogLevel.Update, ConsoleColor.White);
}

private static void HandleEvent(UpdateEvent updateEvent, ISession session)
Expand Down
48 changes: 40 additions & 8 deletions PoGo.NecroBot.Logic/Tasks/EvolvePokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,30 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
if (session.LogicSettings.KeepPokemonsThatCanEvolve)
{
var myPokemons = await session.Inventory.GetPokemons();
if (session.Profile.PlayerData.MaxPokemonStorage * session.LogicSettings.EvolveKeptPokemonsAtStorageUsagePercentage > myPokemons.Count())
var needPokemonToStartEvolve = Math.Max(0,
Math.Min(session.Profile.PlayerData.MaxPokemonStorage*
session.LogicSettings.EvolveKeptPokemonsAtStorageUsagePercentage,
session.Profile.PlayerData.MaxPokemonStorage));

var deltaCount = needPokemonToStartEvolve - myPokemons.Count();

if (deltaCount > 0)
{
session.EventDispatcher.Send(new NoticeEvent()
{
Message = $"Not enough Pokemons to start evlove. Waiting for {deltaCount} " +
$"more ({ pokemonToEvolve.Count}/{deltaCount + pokemonToEvolve.Count})"
});

return;
}
}

var inventoryContent = await session.Inventory.GetItems();

var luckyEggs = inventoryContent.Where(p => p.ItemId == ItemId.ItemLuckyEgg);
var luckyEgg = luckyEggs.FirstOrDefault();

//maybe there can be a warning message as an else condition of luckyEgg checks, like;
//"There is no Lucky Egg, so, your UseLuckyEggsMinPokemonAmount setting bypassed."
if (session.LogicSettings.UseLuckyEggsWhileEvolving && luckyEgg != null && luckyEgg.Count > 0)
{
if (pokemonToEvolve.Count >= session.LogicSettings.UseLuckyEggsMinPokemonAmount)
Expand All @@ -53,12 +66,31 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
}
else
{
// Wait until we have enough pokemon
session.EventDispatcher.Send(new NoticeEvent()
var myPokemons = await session.Inventory.GetPokemons();

var deltaPokemonsToUseLuckyEgg = session.LogicSettings.UseLuckyEggsMinPokemonAmount -
pokemonToEvolve.Count;

var availableSpace = session.Profile.PlayerData.MaxPokemonStorage - myPokemons.Count();

if (deltaPokemonsToUseLuckyEgg > availableSpace)
{
Message = $"Not enough Pokemons to trigger a lucky egg. Waiting for {session.LogicSettings.UseLuckyEggsMinPokemonAmount - pokemonToEvolve.Count} more ({ pokemonToEvolve.Count}/{session.LogicSettings.UseLuckyEggsMinPokemonAmount})"
});
return;
var possibleLimitInThisIteration = pokemonToEvolve.Count + availableSpace;

session.EventDispatcher.Send(new NoticeEvent()
{
Message = $"Use lucky egg impossible with UseLuckyEggsMinPokemonAmount = {session.LogicSettings.UseLuckyEggsMinPokemonAmount} " +
Environment.NewLine +
$"set value <= {possibleLimitInThisIteration} instead"
});
}
else
{
session.EventDispatcher.Send(new NoticeEvent()
{
Message = $"Not enough Pokemons to trigger a lucky egg. Were needed {deltaPokemonsToUseLuckyEgg} more"
});
}
}
}

Expand Down
42 changes: 24 additions & 18 deletions PoGo.NecroBot.Logic/Tasks/SnipePokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using PoGo.NecroBot.Logic.State;
using POGOProtos.Enums;
using POGOProtos.Inventory.Item;
using POGOProtos.Map.Pokemon;
using POGOProtos.Networking.Responses;

#endregion
Expand Down Expand Up @@ -207,7 +208,7 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
cancellationToken);
}
}
else if (scanResult.Status.Contains("fail"))
else if (!string.IsNullOrEmpty(scanResult.Status) && scanResult.Status.Contains("fail"))
{
session.EventDispatcher.Send(new SnipeEvent
{
Expand Down Expand Up @@ -237,25 +238,30 @@ private static async Task Snipe(ISession session, IEnumerable<PokemonId> pokemon

session.EventDispatcher.Send(new SnipeModeEvent {Active = true});

await
session.Client.Player.UpdatePlayerLocation(latitude,
longitude, session.Client.CurrentAltitude);
List<MapPokemon> catchablePokemon;
try
{
await
session.Client.Player.UpdatePlayerLocation(latitude, longitude, session.Client.CurrentAltitude);

session.EventDispatcher.Send(new UpdatePositionEvent
session.EventDispatcher.Send(new UpdatePositionEvent
{
Longitude = longitude,
Latitude = latitude
});

var mapObjects = session.Client.Map.GetMapObjects().Result;
catchablePokemon =
mapObjects.MapCells.SelectMany(q => q.CatchablePokemons)
.Where(q => pokemonIds.Contains(q.PokemonId))
.OrderByDescending(pokemon => PokemonInfo.CalculateMaxCpMultiplier(pokemon.PokemonId))
.ToList();
}
finally
{
Longitude = longitude,
Latitude = latitude
});

var mapObjects = session.Client.Map.GetMapObjects().Result;
var catchablePokemon =
mapObjects.MapCells.SelectMany(q => q.CatchablePokemons)
.Where(q => pokemonIds.Contains(q.PokemonId))
.OrderByDescending(pokemon => PokemonInfo.CalculateMaxCpMultiplier(pokemon.PokemonId))
.ToList();

await session.Client.Player.UpdatePlayerLocation(CurrentLatitude, CurrentLongitude,
session.Client.CurrentAltitude);
await
session.Client.Player.UpdatePlayerLocation(CurrentLatitude, CurrentLongitude, session.Client.CurrentAltitude);
}

foreach (var pokemon in catchablePokemon)
{
Expand Down