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

Egg incubation #726

Merged
merged 2 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PoGo.NecroBot.CLI/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public void HandleEvent(ItemRecycledEvent evt, Context ctx)
Logger.Write($"{evt.Count}x {evt.Id}", LogLevel.Recycling);
}

public void HandleEvent(EggIncubatorStatusEvent evt, Context ctx)
{
if (evt.WasAddedNow)
Logger.Write($"Putting egg in incubator: {evt.KmRemaining:0.00}km left");
else
Logger.Write($"Incubator status update: {evt.KmRemaining:0.00}km left");
}

public void HandleEvent(FortUsedEvent evt, Context ctx)
{
Logger.Write($"XP: {evt.Exp}, Gems: {evt.Gems}, Items: {evt.Items}", LogLevel.Pokestop);
Expand Down
2 changes: 2 additions & 0 deletions PoGo.NecroBot.CLI/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void Save(string fullPath)
public int MaxTravelDistanceInMeters = 1000;
public bool PrioritizeIvOverCp = true;
public bool TransferDuplicatePokemon = true;
public bool UseEggIncubators = true;
public bool UseGpxPathing = false;
public bool UseLuckyEggsWhileEvolving = false;
public bool UsePokemonToNotCatchFilter = false;
Expand Down Expand Up @@ -286,6 +287,7 @@ public LogicSettings(GlobalSettings settings)
public bool EvolveAllPokemonWithEnoughCandy => _settings.EvolveAllPokemonWithEnoughCandy;
public bool KeepPokemonsThatCanEvolve => _settings.KeepPokemonsThatCanEvolve;
public bool TransferDuplicatePokemon => _settings.TransferDuplicatePokemon;
public bool UseEggIncubators => _settings.UseEggIncubators;
public int DelayBetweenPokemonCatch => _settings.DelayBetweenPokemonCatch;
public bool UsePokemonToNotCatchFilter => _settings.UsePokemonToNotCatchFilter;
public int KeepMinDuplicatePokemon => _settings.KeepMinDuplicatePokemon;
Expand Down
12 changes: 12 additions & 0 deletions PoGo.NecroBot.Logic/Event/EggIncubatorStatusEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace PoGo.NecroBot.Logic.Event
{
public class EggIncubatorStatusEvent : IEvent
{
public string IncubatorId;
public bool WasAddedNow;
public ulong PokemonId;
public double KmToWalk;
public double KmRemaining;
public double KmWalked => KmToWalk - KmRemaining;
}
}
1 change: 1 addition & 0 deletions PoGo.NecroBot.Logic/ILogicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface ILogicSettings
bool EvolveAllPokemonWithEnoughCandy { get; }
bool KeepPokemonsThatCanEvolve { get; }
bool TransferDuplicatePokemon { get; }
bool UseEggIncubators { get; }
int DelayBetweenPokemonCatch { get; }
bool UsePokemonToNotCatchFilter { get; }
int KeepMinDuplicatePokemon { get; }
Expand Down
18 changes: 18 additions & 0 deletions PoGo.NecroBot.Logic/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ public async Task<IEnumerable<PokemonData>> GetHighestsPerfect(int limit)
return pokemons.OrderByDescending(PokemonInfo.CalculatePokemonPerfection).Take(limit);
}

public async Task<IEnumerable<PokemonData>> GetEggs()
{
var inventory = await GetCachedInventory();
return
inventory.InventoryDelta.InventoryItems.Select(i => i.InventoryItemData?.PokemonData)
.Where(p => p != null && p.IsEgg);
}

public async Task<IEnumerable<EggIncubator>> GetEggIncubators()
{
var inventory = await GetCachedInventory();
return
inventory.InventoryDelta.InventoryItems
.Where(x => x.InventoryItemData.EggIncubators != null)
.SelectMany(i => i.InventoryItemData.EggIncubators.EggIncubator)
.Where(i => i != null);
}

public async Task<int> GetItemAmountByType(ItemId type)
{
var pokeballs = await GetItems();
Expand Down
2 changes: 2 additions & 0 deletions PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Event\ProfileEvent.cs" />
<Compile Include="Event\TransferPokemonEvent.cs" />
<Compile Include="Event\UseBerryEvent.cs" />
<Compile Include="Event\EggIncubatorStatusEvent.cs" />
<Compile Include="Inventory.cs" />
<Compile Include="Logging\Logger.cs" />
<Compile Include="LogicClient.cs" />
Expand Down Expand Up @@ -95,6 +96,7 @@
<Compile Include="Event\UseLuckyEggEvent.cs" />
<Compile Include="State\VersionCheckState.cs" />
<Compile Include="Event\WarnEvent.cs" />
<Compile Include="Tasks\UseIncubatorsTask.cs" />
<Compile Include="Tasks\UseNearbyPokestopsTask.cs" />
<Compile Include="Utils\GPXReader.cs" />
<Compile Include="Logging\ILogger.cs" />
Expand Down
5 changes: 5 additions & 0 deletions PoGo.NecroBot.Logic/State/FarmState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public async Task<IState> Execute(Context ctx, StateMachine machine)

await RecycleItemsTask.Execute(ctx, machine);

if (ctx.LogicSettings.UseEggIncubators)
{
await UseIncubatorsTask.Execute(ctx, machine);
}

if (ctx.LogicSettings.UseGpxPathing)
{
await FarmPokestopsGpxTask.Execute(ctx, machine);
Expand Down
72 changes: 72 additions & 0 deletions PoGo.NecroBot.Logic/Tasks/UseIncubatorsTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Linq;
using System.Threading.Tasks;
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.State;
using POGOProtos.Inventory.Item;

namespace PoGo.NecroBot.Logic.Tasks
{
class UseIncubatorsTask
{
public static async Task Execute(Context ctx, StateMachine machine)
{
// Refresh inventory so that the player stats are fresh
await ctx.Inventory.RefreshCachedInventory();

var playerStats = (await ctx.Inventory.GetPlayerStats()).FirstOrDefault();
if (playerStats == null)
return;

var kmWalked = playerStats.KmWalked;

var incubators = (await ctx.Inventory.GetEggIncubators())
.Where(x => x.UsesRemaining > 0 || x.ItemId == ItemId.ItemIncubatorBasicUnlimited)
.OrderByDescending(x => x.ItemId == ItemId.ItemIncubatorBasicUnlimited)
.ToList();

var unusedEggs = (await ctx.Inventory.GetEggs())
.Where(x => string.IsNullOrEmpty(x.EggIncubatorId))
.OrderBy(x => x.EggKmWalkedTarget - x.EggKmWalkedStart)
.ToList();

foreach (var incubator in incubators)
{
if (incubator.PokemonId == 0)
{
// Unlimited incubators prefer short eggs, limited incubators prefer long eggs
var egg = incubator.ItemId == ItemId.ItemIncubatorBasicUnlimited
? unusedEggs.FirstOrDefault()
: unusedEggs.LastOrDefault();

if (egg == null)
continue;

var response = await ctx.Client.Inventory.UseItemEggIncubator(incubator.Id, egg.Id);
unusedEggs.Remove(egg);

machine.Fire(new EggIncubatorStatusEvent
{
IncubatorId = incubator.Id,
WasAddedNow = true,
PokemonId = egg.Id,
KmToWalk = egg.EggKmWalkedTarget,
KmRemaining = response.EggIncubator.TargetKmWalked - kmWalked
});

await Task.Delay(500);
}
else
{
machine.Fire(new EggIncubatorStatusEvent
{
IncubatorId = incubator.Id,
PokemonId = incubator.PokemonId,
KmToWalk = incubator.TargetKmWalked - incubator.StartKmWalked,
KmRemaining = incubator.TargetKmWalked - kmWalked
});
}
}
}
}
}