Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.98 KB | None | 0 0
  1. #region using directives
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using PoGo.NecroBot.Logic.PoGoUtils;
  9. using PokemonGo.RocketAPI;
  10. using POGOProtos.Data;
  11. using POGOProtos.Data.Player;
  12. using POGOProtos.Enums;
  13. using POGOProtos.Inventory;
  14. using POGOProtos.Inventory.Item;
  15. using POGOProtos.Networking.Responses;
  16. using POGOProtos.Settings.Master;
  17.  
  18. #endregion
  19.  
  20. namespace PoGo.NecroBot.Logic
  21. {
  22. public class Inventory
  23. {
  24. private readonly Client _client;
  25. private readonly LogicClient _logicClient;
  26. private GetInventoryResponse _cachedInventory;
  27. private DateTime _lastRefresh;
  28.  
  29. public Inventory(Client client, LogicClient logicClient)
  30. {
  31. _client = client;
  32. _logicClient = logicClient;
  33. }
  34.  
  35. public async void DeletePokemonFromInvById(ulong id)
  36. {
  37. var inventory = await GetCachedInventory();
  38. var pokemon =
  39. inventory.InventoryDelta.InventoryItems.FirstOrDefault(
  40. i => i.InventoryItemData.PokemonData != null && i.InventoryItemData.PokemonData.Id == id);
  41. if (pokemon != null)
  42. inventory.InventoryDelta.InventoryItems.Remove(pokemon);
  43. }
  44.  
  45. private async Task<GetInventoryResponse> GetCachedInventory()
  46. {
  47. var now = DateTime.UtcNow;
  48.  
  49. if (_lastRefresh.AddSeconds(30).Ticks > now.Ticks)
  50. {
  51. return _cachedInventory;
  52. }
  53. return await RefreshCachedInventory();
  54. }
  55.  
  56. public async Task<IEnumerable<PokemonData>> GetDuplicatePokemonToTransfer(
  57. bool keepPokemonsThatCanEvolve = false, bool prioritizeIVoverCp = false,
  58. IEnumerable<PokemonId> filter = null)
  59. {
  60. var myPokemon = await GetPokemons();
  61.  
  62. var pokemonList =
  63. myPokemon.Where(
  64. p => p.DeployedFortId == string.Empty && p.Favorite == 0 && p.Cp < _logicClient.Settings.KeepMinCp)
  65. .ToList();
  66. if (filter != null)
  67. {
  68. pokemonList = pokemonList.Where(p => !filter.Contains(p.PokemonId)).ToList();
  69. }
  70. if (keepPokemonsThatCanEvolve)
  71. {
  72. var results = new List<PokemonData>();
  73. var pokemonsThatCanBeTransfered = pokemonList.GroupBy(p => p.PokemonId)
  74. .Where(x => x.Count() > _logicClient.Settings.KeepMinDuplicatePokemon).ToList();
  75.  
  76. var myPokemonSettings = await GetPokemonSettings();
  77. var pokemonSettings = myPokemonSettings.ToList();
  78.  
  79. var myPokemonFamilies = await GetPokemonFamilies();
  80. var pokemonFamilies = myPokemonFamilies.ToArray();
  81.  
  82. foreach (var pokemon in pokemonsThatCanBeTransfered)
  83. {
  84. var settings = pokemonSettings.Single(x => x.PokemonId == pokemon.Key);
  85. var familyCandy = pokemonFamilies.Single(x => settings.FamilyId == x.FamilyId);
  86. var amountToSkip = _logicClient.Settings.KeepMinDuplicatePokemon;
  87. if (settings.CandyToEvolve == 0)
  88. {
  89. // Assuming the Pokemon has no evolution
  90. continue;
  91. }
  92. var amountPossible = familyCandy.Candy / settings.CandyToEvolve;
  93.  
  94. if (settings.CandyToEvolve > 0 && amountPossible > amountToSkip)
  95. amountToSkip = amountPossible;
  96.  
  97. if (prioritizeIVoverCp)
  98. {
  99. results.AddRange(pokemonList.Where(x => x.PokemonId == pokemon.Key)
  100. .OrderByDescending(PokemonInfo.CalculatePokemonPerfection)
  101. .ThenBy(n => n.StaminaMax)
  102. .Skip(amountToSkip)
  103. .ToList());
  104. }
  105. else
  106. {
  107. results.AddRange(pokemonList.Where(x => x.PokemonId == pokemon.Key)
  108. .OrderByDescending(x => x.Cp)
  109. .ThenBy(n => n.StaminaMax)
  110. .Skip(amountToSkip)
  111. .ToList());
  112. }
  113. }
  114.  
  115. return results;
  116. }
  117. if (prioritizeIVoverCp)
  118. {
  119. return pokemonList
  120. .GroupBy(p => p.PokemonId)
  121. .Where(x => x.Count() > 0)
  122. .SelectMany(
  123. p =>
  124. p.OrderByDescending(PokemonInfo.CalculatePokemonPerfection)
  125. .ThenBy(n => n.StaminaMax)
  126. .Skip(_logicClient.Settings.KeepMinDuplicatePokemon)
  127. .ToList());
  128. }
  129. return pokemonList
  130. .GroupBy(p => p.PokemonId)
  131. .Where(x => x.Count() > 0)
  132. .SelectMany(
  133. p =>
  134. p.OrderByDescending(x => x.Cp)
  135. .ThenBy(n => n.StaminaMax)
  136. .Skip(_logicClient.Settings.KeepMinDuplicatePokemon)
  137. .ToList());
  138. }
  139.  
  140. public async Task<PokemonData> GetHighestPokemonOfTypeByCp(PokemonData pokemon)
  141. {
  142. var myPokemon = await GetPokemons();
  143. var pokemons = myPokemon.ToList();
  144. return pokemons.Where(x => x.PokemonId == pokemon.PokemonId)
  145. .OrderByDescending(x => x.Cp)
  146. .FirstOrDefault();
  147. }
  148.  
  149. public async Task<PokemonData> GetHighestPokemonOfTypeByIv(PokemonData pokemon)
  150. {
  151. var myPokemon = await GetPokemons();
  152. var pokemons = myPokemon.ToList();
  153. return pokemons.Where(x => x.PokemonId == pokemon.PokemonId)
  154. .OrderByDescending(PokemonInfo.CalculatePokemonPerfection)
  155. .FirstOrDefault();
  156. }
  157.  
  158. public async Task<IEnumerable<PokemonData>> GetHighestsCp(int limit)
  159. {
  160. var myPokemon = await GetPokemons();
  161. var pokemons = myPokemon.ToList();
  162. return pokemons.OrderByDescending(x => x.Cp).ThenBy(n => n.StaminaMax).Take(limit);
  163. }
  164.  
  165. public async Task<IEnumerable<PokemonData>> GetHighestsPerfect(int limit)
  166. {
  167. var myPokemon = await GetPokemons();
  168. var pokemons = myPokemon.ToList();
  169. return pokemons.OrderByDescending(PokemonInfo.CalculatePokemonPerfection).Take(limit);
  170. }
  171.  
  172. public async Task<int> GetItemAmountByType(ItemId type)
  173. {
  174. var pokeballs = await GetItems();
  175. return pokeballs.FirstOrDefault(i => i.ItemId == type)?.Count ?? 0;
  176. }
  177.  
  178. public async Task<IEnumerable<ItemData>> GetItems()
  179. {
  180. var inventory = await GetCachedInventory();
  181. return inventory.InventoryDelta.InventoryItems
  182. .Select(i => i.InventoryItemData?.Item)
  183. .Where(p => p != null);
  184. }
  185.  
  186. public async Task<IEnumerable<ItemData>> GetItemsToRecycle(ISettings settings)
  187. {
  188. var myItems = await GetItems();
  189.  
  190. return myItems
  191. .Where(x => _logicClient.Settings.ItemRecycleFilter.Any(f => f.Key == x.ItemId && x.Count > f.Value))
  192. .Select(
  193. x =>
  194. new ItemData
  195. {
  196. ItemId = x.ItemId,
  197. Count =
  198. x.Count - _logicClient.Settings.ItemRecycleFilter.Single(f => f.Key == x.ItemId).Value,
  199. Unseen = x.Unseen
  200. });
  201. }
  202.  
  203. public async Task<IEnumerable<PlayerStats>> GetPlayerStats()
  204. {
  205. var inventory = await GetCachedInventory();
  206. return inventory.InventoryDelta.InventoryItems
  207. .Select(i => i.InventoryItemData?.PlayerStats)
  208. .Where(p => p != null);
  209. }
  210.  
  211. public async Task<IEnumerable<PokemonFamily>> GetPokemonFamilies()
  212. {
  213. var inventory = await GetCachedInventory();
  214. return
  215. inventory.InventoryDelta.InventoryItems.Select(i => i.InventoryItemData?.PokemonFamily)
  216. .Where(p => p != null && p.FamilyId != PokemonFamilyId.FamilyUnset);
  217. }
  218.  
  219. public async Task<IEnumerable<PokemonData>> GetPokemons()
  220. {
  221. var inventory = await GetCachedInventory();
  222. return
  223. inventory.InventoryDelta.InventoryItems.Select(i => i.InventoryItemData?.PokemonData)
  224. .Where(p => p != null && p.PokemonId > 0);
  225. }
  226.  
  227. public async Task<IEnumerable<PokemonSettings>> GetPokemonSettings()
  228. {
  229. var templates = await _client.Download.GetItemTemplates();
  230. return
  231. templates.ItemTemplates.Select(i => i.PokemonSettings)
  232. .Where(p => p != null && p.FamilyId != PokemonFamilyId.FamilyUnset);
  233. }
  234.  
  235. public async Task<IEnumerable<PokemonData>> GetPokemonToEvolve(IEnumerable<PokemonId> filter = null)
  236. {
  237. var myPokemons = await GetPokemons();
  238. myPokemons = myPokemons.Where(p => p.DeployedFortId == string.Empty).OrderByDescending(p => p.Cp);
  239. //Don't evolve pokemon in gyms
  240. if (filter != null)
  241. {
  242. myPokemons = myPokemons.Where(p => filter.Contains(p.PokemonId));
  243. }
  244. var pokemons = myPokemons.ToList();
  245.  
  246. var myPokemonSettings = await GetPokemonSettings();
  247. var pokemonSettings = myPokemonSettings.ToList();
  248.  
  249. var myPokemonFamilies = await GetPokemonFamilies();
  250. var pokemonFamilies = myPokemonFamilies.ToArray();
  251.  
  252. var pokemonToEvolve = new List<PokemonData>();
  253. foreach (var pokemon in pokemons)
  254. {
  255. var settings = pokemonSettings.Single(x => x.PokemonId == pokemon.PokemonId);
  256. var familyCandy = pokemonFamilies.Single(x => settings.FamilyId == x.FamilyId);
  257.  
  258. //Don't evolve if we can't evolve it
  259. if (settings.EvolutionIds.Count == 0)
  260. continue;
  261.  
  262. var pokemonCandyNeededAlready =
  263. pokemonToEvolve.Count(
  264. p => pokemonSettings.Single(x => x.PokemonId == p.PokemonId).FamilyId == settings.FamilyId)*
  265. settings.CandyToEvolve;
  266.  
  267. if (_logicClient.Settings.EvolveAllPokemonAboveIv)
  268. {
  269. if (PokemonInfo.CalculatePokemonPerfection(pokemon) >= _logicClient.Settings.EvolveAboveIvValue &&
  270. familyCandy.Candy - pokemonCandyNeededAlready > settings.CandyToEvolve)
  271. {
  272. pokemonToEvolve.Add(pokemon);
  273. }
  274. }
  275. else
  276. {
  277. if (familyCandy.Candy - pokemonCandyNeededAlready > settings.CandyToEvolve)
  278. {
  279. pokemonToEvolve.Add(pokemon);
  280. }
  281. }
  282. }
  283.  
  284. return pokemonToEvolve;
  285. }
  286.  
  287. public async Task<GetInventoryResponse> RefreshCachedInventory()
  288. {
  289. var now = DateTime.UtcNow;
  290. var ss = new SemaphoreSlim(10);
  291.  
  292. await ss.WaitAsync();
  293. try
  294. {
  295. _lastRefresh = now;
  296. _cachedInventory = await _client.Inventory.GetInventory();
  297. return _cachedInventory;
  298. }
  299. finally
  300. {
  301. ss.Release();
  302. }
  303. }
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement