Skip to content

Commit

Permalink
Merge pull request #295 from Nimryel/dev-testing
Browse files Browse the repository at this point in the history
Optimized recycling code and removed reduntant entries.
  • Loading branch information
DurtyFree committed Aug 2, 2016
2 parents 58fcb22 + d38ac06 commit 4df7374
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 305 deletions.
34 changes: 14 additions & 20 deletions PoGo.PokeMobBot.Logic/Common/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ public enum TranslationString
PokemonSkipped,
ZeroPokeballInv,
CurrentPokeballInv,
CurrentPotionInv,
CheckingForBallsToRecycle,
CheckingForPotionsToRecycle,
CheckingForRevivesToRecycle,
PokeballsToKeepIncorrect,
PotionsToKeepIncorrect,
RevivesToKeepIncorrect,
InvFullTransferring,
InvFullTransferManually,
InvFullPokestopLooting,
Expand Down Expand Up @@ -133,7 +132,6 @@ public enum TranslationString
DisplayHighestMove1Header,
DisplayHighestMove2Header,
UseBerry,
BerriesToKeepIncorrect,
NianticServerUnstable,
OperationCanceled,
PokemonUpgradeSuccess,
Expand Down Expand Up @@ -187,7 +185,7 @@ public class Translation : ITranslation
"{0}\t- CP: {1} IV: {2}% [Best CP: {3} IV: {4}%] (Candies: {5})"),
new KeyValuePair<TranslationString, string>(TranslationString.EventItemRecycled, "{0}x {1}"),
new KeyValuePair<TranslationString, string>(TranslationString.EventPokemonCapture,
"({0}) | {2}, Lvl: {3} | CP: ({4}/{5}) | IV: {6}% | Type: {1} | Chance: {7}% | Dist: {8}m | Used: {9} ({10} left) | XP: {11} | Candy: {12}"),
"({0}) | ({1}) {2} Lvl: {3} CP: ({4}/{5}) IV: {6}% | Chance: {7}% | {8}m dist | with a {9} ({10} left). | {11}xp | {12}"),
new KeyValuePair<TranslationString, string>(TranslationString.EventNoPokeballs,
"No Pokeballs - We missed a {0} with CP {1}"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusAttempt, "{0} Attempt #{1}"),
Expand Down Expand Up @@ -218,14 +216,14 @@ public class Translation : ITranslation
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryPokestop, "POKESTOP"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryFarming, "FARMING"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryRecycling, "RECYCLING"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryPkmn, "CATCH"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryTransfered, "TRANSFER"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryPkmn, "PKMN"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryTransfered, "TRANSFERED"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryEvolved, "EVOLVED"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryBerry, "BERRY"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryEgg, "EGG"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryDebug, "DEBUG"),
new KeyValuePair<TranslationString, string>(TranslationString.LogEntryUpdate, "UPDATE"),
new KeyValuePair<TranslationString, string>(TranslationString.LoggingIn, "Logging in using account {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.LoggingIn, "Logging in using {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.PtcOffline,
"PTC Servers are probably down OR your credentials are wrong. Try google"),
new KeyValuePair<TranslationString, string>(TranslationString.TryingAgainIn,
Expand Down Expand Up @@ -267,7 +265,9 @@ public class Translation : ITranslation
new KeyValuePair<TranslationString, string>(TranslationString.ZeroPokeballInv,
"You have no pokeballs in your inventory, no more Pokemon can be caught!"),
new KeyValuePair<TranslationString, string>(TranslationString.CurrentPokeballInv,
"[Inventory] Pokeballs: {0} | Greatballs: {1} | Ultraballs: {2} | Masterballs: {3}"),
"[Current Inventory] Pokeballs: {0} | Greatballs: {1} | Ultraballs: {2} | Masterballs: {3}"),
new KeyValuePair<TranslationString, string>(TranslationString.CurrentPotionInv,
"[Current Inventory] Potions: {0} | Super Potions: {1} | Hyper Potions: {2} | Max Potions: {3}"),
new KeyValuePair<TranslationString, string>(TranslationString.CheckingForBallsToRecycle,
"Checking for balls to recycle, keeping {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.CheckingForPotionsToRecycle,
Expand All @@ -276,10 +276,6 @@ public class Translation : ITranslation
"Checking for revives to recycle, keeping {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.PokeballsToKeepIncorrect,
"TotalAmountOfPokeballsToKeep is configured incorrectly. The number is smaller than 1."),
new KeyValuePair<TranslationString, string>(TranslationString.PotionsToKeepIncorrect,
"TotalAmountOfPotionsToKeep is configured incorrectly. The number is smaller than 1."),
new KeyValuePair<TranslationString, string>(TranslationString.RevivesToKeepIncorrect,
"TotalAmountOfRevivesToKeep is configured incorrectly. The number is smaller than 1."),
new KeyValuePair<TranslationString, string>(TranslationString.InvFullTransferring,
"Pokemon Inventory is full, transferring Pokemon..."),
new KeyValuePair<TranslationString, string>(TranslationString.InvFullTransferManually,
Expand All @@ -295,12 +291,12 @@ public class Translation : ITranslation
"Pokemon {0} ({1}) renamed from {2} to {3}."),
new KeyValuePair<TranslationString, string>(TranslationString.PokemonIgnoreFilter,
"[Pokemon ignore filter] - Ignoring {0} as defined in settings"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusAttempt, "Attempt"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusError, "Error"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusEscape, "Escape"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusFlee, "Flee"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusMissed, "Missed"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusSuccess, "Success"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusAttempt, "CatchAttempt"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusError, "CatchError"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusEscape, "CatchEscape"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusFlee, "CatchFlee"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusMissed, "CatchMissed"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchStatusSuccess, "CatchSuccess"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchTypeNormal, "Normal"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchTypeLure, "Lure"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchTypeIncense, "Incense"),
Expand Down Expand Up @@ -334,8 +330,6 @@ public class Translation : ITranslation
new KeyValuePair<TranslationString, string>(TranslationString.DisplayHighestMove2Header, "MOVE2"),
new KeyValuePair<TranslationString, string>(TranslationString.UseBerry,
"Using Razzberry. Berries left: {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.BerriesToKeepIncorrect,
"You can not set the amount of Berries to less then 1"),
new KeyValuePair<TranslationString, string>(TranslationString.NianticServerUnstable,
"Niantic Servers unstable, throttling API Calls."),
new KeyValuePair<TranslationString, string>(TranslationString.OperationCanceled,
Expand Down
118 changes: 12 additions & 106 deletions PoGo.PokeMobBot.Logic/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ public async Task<IEnumerable<ItemData>> GetItemsToRecycle(ISession session)
currentAmountOfMasterballs)
});

var currentAmountOfPotions = await GetItemAmountByType(ItemId.ItemPotion);
var currentAmountOfSuperPotions = await GetItemAmountByType(ItemId.ItemSuperPotion);
var currentAmountOfHyperPotions = await GetItemAmountByType(ItemId.ItemHyperPotion);
var currentAmountOfMaxPotions= await GetItemAmountByType(ItemId.ItemMaxPotion);

session.EventDispatcher.Send(new NoticeEvent()
{
Message = session.Translation.GetTranslation(TranslationString.CurrentPotionInv,
currentAmountOfPotions, currentAmountOfSuperPotions, currentAmountOfHyperPotions,
currentAmountOfMaxPotions)
});

var otherItemsToRecycle = myItems
.Where(x => _logicSettings.ItemRecycleFilter.Any(f => f.Key == x.ItemId && x.Count > f.Value))
.Select(
Expand All @@ -270,9 +282,7 @@ public async Task<IEnumerable<ItemData>> GetItemsToRecycle(ISession session)
Count = x.Count - _logicSettings.ItemRecycleFilter.Single(f => f.Key == x.ItemId).Value,
Unseen = x.Unseen
});

itemsToRecycle.AddRange(otherItemsToRecycle);

return itemsToRecycle;
}

Expand All @@ -290,42 +300,6 @@ public async Task<IEnumerable<PlayerStats>> GetPlayerStats()
.Where(p => p != null);
}

private List<ItemData> GetPokeballsToRecycle(ISession session, IReadOnlyList<ItemData> myItems)
{
var amountOfPokeballsToKeep = _logicSettings.TotalAmountOfPokeballsToKeep;
if (amountOfPokeballsToKeep < 1)
{
session.EventDispatcher.Send(new ErrorEvent()
{
Message = session.Translation.GetTranslation(TranslationString.PokeballsToKeepIncorrect)
});
return new List<ItemData>();
}

var allPokeballs = myItems.Where(s => _pokeballs.Contains(s.ItemId)).ToList();
allPokeballs.Sort((ball1, ball2) => ((int)ball1.ItemId).CompareTo((int)ball2.ItemId));

return TakeAmountOfItems(allPokeballs, amountOfPokeballsToKeep).ToList();
}

private List<ItemData> GetBerriesToRecycle(ISession session, IReadOnlyList<ItemData> myItems)
{
var amountOfBerriesToKeep = _logicSettings.TotalAmountOfBerriesToKeep;
if (amountOfBerriesToKeep < 1)
{
session.EventDispatcher.Send(new ErrorEvent
{
Message = session.Translation.GetTranslation(TranslationString.BerriesToKeepIncorrect)
});
return new List<ItemData>();
}

var allPokeballs = myItems.Where(s => _pokeballs.Contains(s.ItemId)).ToList();
allPokeballs.Sort((ball1, ball2) => ((int)ball1.ItemId).CompareTo((int)ball2.ItemId));

return TakeAmountOfItems(allPokeballs, amountOfBerriesToKeep).ToList();
}

public async Task<int> GetPokedexCount()
{
var hfgds = await _client.Inventory.GetInventory();
Expand Down Expand Up @@ -437,42 +411,6 @@ public TransferFilter GetPokemonTransferFilter(PokemonId pokemon)
_logicSettings.KeepMinDuplicatePokemon);
}

private List<ItemData> GetPotionsToRecycle(ISession session, IReadOnlyList<ItemData> myItems)
{
var amountOfPotionsToKeep = _logicSettings.TotalAmountOfPotionsToKeep;
if (amountOfPotionsToKeep < 1)
{
session.EventDispatcher.Send(new ErrorEvent
{
Message = session.Translation.GetTranslation(TranslationString.PotionsToKeepIncorrect)
});
return new List<ItemData>();
}

var allPotions = myItems.Where(s => _potions.Contains(s.ItemId)).ToList();
allPotions.Sort((i1, i2) => ((int)i1.ItemId).CompareTo((int)i2.ItemId));

return TakeAmountOfItems(allPotions, amountOfPotionsToKeep).ToList();
}

private List<ItemData> GetRevivesToRecycle(ISession session, IReadOnlyList<ItemData> myItems)
{
var amountOfRevivesToKeep = _logicSettings.TotalAmountOfRevivesToKeep;
if (amountOfRevivesToKeep < 1)
{
session.EventDispatcher.Send(new ErrorEvent
{
Message = session.Translation.GetTranslation(TranslationString.RevivesToKeepIncorrect)
});
return new List<ItemData>();
}

var allRevives = myItems.Where(s => _revives.Contains(s.ItemId)).ToList();
allRevives.Sort((i1, i2) => ((int)i1.ItemId).CompareTo((int)i2.ItemId));

return TakeAmountOfItems(allRevives, amountOfRevivesToKeep).ToList();
}

public async Task<GetInventoryResponse> RefreshCachedInventory()
{
var now = DateTime.UtcNow;
Expand All @@ -491,38 +429,6 @@ public async Task<GetInventoryResponse> RefreshCachedInventory()
}
}

private IEnumerable<ItemData> TakeAmountOfItems(IReadOnlyList<ItemData> items, int ammountToLeave)
{
var itemsAvailable = 0;
foreach (var item in items)
{
itemsAvailable += item.Count;
}

var itemsToRemove = itemsAvailable - ammountToLeave;

foreach (var item in items)
{
if (itemsToRemove > 0 && item.Count > 0)
{
if (item.Count < itemsToRemove)
{
// Recycle all of this type
itemsToRemove -= item.Count;
yield return item;
}
else
{
// Recycle remaining amount
var count = itemsToRemove;
itemsToRemove = 0;
yield return new ItemData { ItemId = item.ItemId, Count = count };
}
}
}
}


public async Task<UpgradePokemonResponse> UpgradePokemon(ulong pokemonid)
{
var upgradeResult = await _client.Inventory.UpgradePokemon(pokemonid);
Expand Down
5 changes: 0 additions & 5 deletions PoGo.PokeMobBot.Logic/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,6 @@ public class GlobalSettings
new KeyValuePair<ItemId, int>(ItemId.ItemXAttack, 100),
new KeyValuePair<ItemId, int>(ItemId.ItemXDefense, 100),
new KeyValuePair<ItemId, int>(ItemId.ItemXMiracle, 100),
new KeyValuePair<ItemId, int>(ItemId.ItemRazzBerry, 50),
new KeyValuePair<ItemId, int>(ItemId.ItemBlukBerry, 10),
new KeyValuePair<ItemId, int>(ItemId.ItemNanabBerry, 10),
new KeyValuePair<ItemId, int>(ItemId.ItemWeparBerry, 30),
new KeyValuePair<ItemId, int>(ItemId.ItemPinapBerry, 30),
new KeyValuePair<ItemId, int>(ItemId.ItemSpecialCamera, 100),
new KeyValuePair<ItemId, int>(ItemId.ItemIncubatorBasicUnlimited, 100),
new KeyValuePair<ItemId, int>(ItemId.ItemIncubatorBasic, 100),
Expand Down

0 comments on commit 4df7374

Please sign in to comment.