Skip to content

Commit

Permalink
Merge pull request #2054 from redChrillz/AddAlwaysEvolveWithLuckyEggW…
Browse files Browse the repository at this point in the history
…henMinPokemonAmountOccursOption

Add always evolve with lucky egg when min pokemon amount occurs option
  • Loading branch information
BornSupercharged authored Aug 2, 2016
2 parents 81eb425 + 7a05d84 commit 2b985db
Showing 1 changed file with 70 additions and 57 deletions.
127 changes: 70 additions & 57 deletions PoGo.NecroBot.Logic/Tasks/EvolvePokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using PoGo.NecroBot.Logic.Utils;
using POGOProtos.Inventory.Item;
using PoGo.NecroBot.Logic.Common;
using System.Collections.Generic;
using POGOProtos.Data;

#endregion

Expand Down Expand Up @@ -36,11 +38,11 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
{
var totalPokemon = await session.Inventory.GetPokemons();

var pokemonNeededInInventory = session.Profile.PlayerData.MaxPokemonStorage * session.LogicSettings.EvolveKeptPokemonsAtStorageUsagePercentage/100.0f;
var pokemonNeededInInventory = session.Profile.PlayerData.MaxPokemonStorage * session.LogicSettings.EvolveKeptPokemonsAtStorageUsagePercentage / 100.0f;
var needPokemonToStartEvolve = Math.Round(
Math.Max(0,
Math.Max(0,
Math.Min(pokemonNeededInInventory, session.Profile.PlayerData.MaxPokemonStorage)));

var deltaCount = needPokemonToStartEvolve - totalPokemon.Count();

if (deltaCount > 0)
Expand All @@ -50,65 +52,15 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
Message = session.Translation.GetTranslation(TranslationString.WaitingForMorePokemonToEvolve,
pokemonToEvolve.Count, deltaCount, totalPokemon.Count(), needPokemonToStartEvolve, session.LogicSettings.EvolveKeptPokemonsAtStorageUsagePercentage)
});

return;
}
}

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

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

if (session.LogicSettings.UseLuckyEggsWhileEvolving && luckyEgg != null && luckyEgg.Count > 0)
{
if (pokemonToEvolve.Count >= session.LogicSettings.UseLuckyEggsMinPokemonAmount)
{
await UseLuckyEgg(session);
}
else
{
var evolvablePokemon = await session.Inventory.GetPokemons();

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

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

if (deltaPokemonToUseLuckyEgg > availableSpace)
{
var possibleLimitInThisIteration = pokemonToEvolve.Count + availableSpace;

session.EventDispatcher.Send(new NoticeEvent()
{
Message = session.Translation.GetTranslation(TranslationString.UseLuckyEggsMinPokemonAmountTooHigh,
session.LogicSettings.UseLuckyEggsMinPokemonAmount, possibleLimitInThisIteration)
});
}
else
{
session.EventDispatcher.Send(new NoticeEvent()
{
Message = session.Translation.GetTranslation(TranslationString.CatchMorePokemonToUseLuckyEgg,
deltaPokemonToUseLuckyEgg)
});
}
}
}
}

foreach (var pokemon in pokemonToEvolve)
if (await shouldUseLuckyEgg(session, pokemonToEvolve))
{
// no cancellationToken.ThrowIfCancellationRequested here, otherwise the lucky egg would be wasted.
var evolveResponse = await session.Client.Inventory.EvolvePokemon(pokemon.Id);

session.EventDispatcher.Send(new PokemonEvolveEvent
{
Id = pokemon.PokemonId,
Exp = evolveResponse.ExperienceAwarded,
Result = evolveResponse.Result
});
await UseLuckyEgg(session);
}
var end = true;
await evolve(session, pokemonToEvolve);
}
}

Expand All @@ -128,5 +80,66 @@ public static async Task UseLuckyEgg(ISession session)
if (luckyEgg != null) session.EventDispatcher.Send(new UseLuckyEggEvent {Count = luckyEgg.Count});
DelayingUtils.Delay(session.LogicSettings.DelayBetweenPokemonCatch, 2000);
}

private static async Task evolve(ISession session, List<PokemonData> pokemonToEvolve)
{
foreach (var pokemon in pokemonToEvolve)
{
// no cancellationToken.ThrowIfCancellationRequested here, otherwise the lucky egg would be wasted.
var evolveResponse = await session.Client.Inventory.EvolvePokemon(pokemon.Id);

session.EventDispatcher.Send(new PokemonEvolveEvent
{
Id = pokemon.PokemonId,
Exp = evolveResponse.ExperienceAwarded,
Result = evolveResponse.Result
});
}
}

private static async Task<Boolean> shouldUseLuckyEgg(ISession session, List<PokemonData> pokemonToEvolve)
{
var inventoryContent = await session.Inventory.GetItems();

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

if (session.LogicSettings.UseLuckyEggsWhileEvolving && luckyEgg != null && luckyEgg.Count > 0)
{
if (pokemonToEvolve.Count >= session.LogicSettings.UseLuckyEggsMinPokemonAmount)
{
return true;
}
else
{
var evolvablePokemon = await session.Inventory.GetPokemons();

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

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

if (deltaPokemonToUseLuckyEgg > availableSpace)
{
var possibleLimitInThisIteration = pokemonToEvolve.Count + availableSpace;

session.EventDispatcher.Send(new NoticeEvent()
{
Message = session.Translation.GetTranslation(TranslationString.UseLuckyEggsMinPokemonAmountTooHigh,
session.LogicSettings.UseLuckyEggsMinPokemonAmount, possibleLimitInThisIteration)
});
}
else
{
session.EventDispatcher.Send(new NoticeEvent()
{
Message = session.Translation.GetTranslation(TranslationString.CatchMorePokemonToUseLuckyEgg,
deltaPokemonToUseLuckyEgg)
});
}
}
}
return false;
}
}
}

0 comments on commit 2b985db

Please sign in to comment.