Guest User

Untitled

a guest
Jul 20th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Google.Protobuf;
  8. using PokemonGo.RocketAPI.Enums;
  9. using PokemonGo.RocketAPI.Extensions;
  10. using PokemonGo.RocketAPI.GeneratedCode;
  11. using PokemonGo.RocketAPI.Helpers;
  12. using static PokemonGo.RocketAPI.GeneratedCode.InventoryResponse.Types;
  13. using static PokemonGo.RocketAPI.GeneratedCode.InventoryResponse.Types.PokemonProto.Types;
  14.  
  15. namespace PokemonGo.RocketAPI.Console
  16. {
  17.     class Program
  18.     {
  19.         private static double last_lat;
  20.         private static double last_long;
  21.         static void Main(string[] args)
  22.         {
  23.  
  24.             Task.Run(() => Execute());
  25.             System.Console.ReadLine();
  26.         }
  27.  
  28.         static async void Execute()
  29.         {
  30.  
  31.             last_lat = Settings.DefaultLatitude;
  32.             last_long = Settings.DefaultLongitude;
  33.             var client = new Client(last_lat, last_long);
  34.             if (Settings.DefaultLatitude == 0 && Settings.DefaultLongitude == 0)
  35.             {
  36.                 System.Console.WriteLine("You need to change Latitude and Longitude in the Settings.cs first (before you can use this script).\nThis Window will be closed in 10 Seconds!");
  37.                 await Task.Delay(10000);
  38.                 System.Environment.Exit(1);
  39.             }
  40.             if (Settings.UsePTC)
  41.             {
  42.                 await client.LoginPtc(Settings.PtcUsername, Settings.PtcPassword);
  43.             }
  44.             else
  45.             {
  46.                 System.Console.WriteLine("Remember that Google only works for 30 Minutes then you need to restart the program.");
  47.                 await client.LoginGoogle();
  48.             }
  49.             while (true)
  50.             {
  51.                 var serverResponse = await client.GetServer();
  52.                 System.Console.WriteLine("Server Fetched");
  53.                 var profile = await client.GetProfile();
  54.                 System.Console.WriteLine("Profile Fetched");
  55.                 var settings = await client.GetSettings();
  56.                 System.Console.WriteLine("Settings Fetched");
  57.                 var mapObjects = await client.GetMapObjects();
  58.                 System.Console.WriteLine("Objects Fetched");
  59.                 var inventory = await client.GetInventory();
  60.                 System.Console.WriteLine("Inventory Fetched");
  61.                 var pokemons = inventory.Payload[0].Bag.Items.Select(i => i.Item?.Pokemon).Where(p => p != null && p?.PokemonType != PokemonProto.Types.PokemonIds.PokemonUnset);
  62.            
  63.                 System.Console.WriteLine("Starting up! Don't forget to thank the people who contributed to this project. Pokebot V5.0");
  64.                 await Task.Delay(5000);
  65.  
  66.  
  67.  
  68.                 try
  69.                 {
  70.                     System.Console.WriteLine("||Farm Started||");
  71.                     await ExecuteFarmingPokestopsAndPokemons(client);
  72.                     System.Console.WriteLine("Unexpected stop? Restarting in 20 seconds.");
  73.                     await Task.Delay(20000);
  74.                 }
  75.                 catch (TaskCanceledException tce) { System.Console.WriteLine("Task Canceled Exception - Restarting"); Execute(); }
  76.                 catch (UriFormatException ufe) { System.Console.WriteLine("System URI Format Exception - Restarting"); Execute(); }
  77.                 catch (ArgumentOutOfRangeException aore) { System.Console.WriteLine("ArgumentOutOfRangeException - Restarting"); Execute(); }
  78.                 //await ExecuteCatchAllNearbyPokemons(client);
  79.  
  80.             }
  81.         }
  82.  
  83.         private static async Task ExecuteFarmingPokestopsAndPokemons(Client client)
  84.         {
  85.             var mapObjects = await client.GetMapObjects();
  86.  
  87.             var pokeStops = mapObjects.Payload[0].Profile.SelectMany(i => i.Fort).Where(i => i.FortType == (int)MiscEnums.FortType.CHECKPOINT && i.CooldownCompleteMs < DateTime.UtcNow.ToUnixTime());
  88.  
  89.             foreach (var pokeStop in pokeStops)
  90.             {
  91.                 var update = await client.UpdatePlayerLocation(pokeStop.Latitude, pokeStop.Longitude);
  92.                 var fortInfo = await client.GetFort(pokeStop.FortId, pokeStop.Latitude, pokeStop.Longitude);
  93.                 var fortSearch = await client.SearchFort(pokeStop.FortId, pokeStop.Latitude, pokeStop.Longitude);
  94.                 var bag = fortSearch.Payload[0];
  95.                 last_lat = pokeStop.Latitude;
  96.                 last_long = pokeStop.Longitude;
  97.  
  98.                 System.Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] Farmed XP: {bag.XpAwarded}, Gems: { bag.GemsAwarded}, Eggs: {bag.EggPokemon} Items: {GetFriendlyItemsString(bag.Items)}");
  99.  
  100.                 await ExecuteCatchAllNearbyPokemons(client);
  101.  
  102.                 await Task.Delay(10000); // Delay for Pokestop Jumps
  103.             }
  104.         }
  105.  
  106.         private static async Task ExecuteCatchAllNearbyPokemons(Client client)
  107.         {
  108.             var mapObjects = await client.GetMapObjects();
  109.  
  110.             var pokemons = mapObjects.Payload[0].Profile.SelectMany(i => i.MapPokemon);
  111.  
  112.             foreach (var pokemon in pokemons)
  113.             {
  114.                 var update = await client.UpdatePlayerLocation(pokemon.Latitude, pokemon.Longitude);
  115.                 var encounterPokemonRespone = await client.EncounterPokemon(pokemon.EncounterId, pokemon.SpawnpointId);
  116.  
  117.                 CatchPokemonResponse caughtPokemonResponse;
  118.                 do
  119.                 {
  120.                     caughtPokemonResponse = await client.CatchPokemon(pokemon.EncounterId, pokemon.SpawnpointId, pokemon.Latitude, pokemon.Longitude);
  121.                 }
  122.                 while (caughtPokemonResponse.Payload[0].Status == 2);
  123.  
  124.                 System.Console.WriteLine(caughtPokemonResponse.Payload[0].Status == 1 ? $"[{DateTime.Now.ToString("HH:mm:ss")}] We caught a {GetFriendlyPokemonName(pokemon.PokedexTypeId)}" : $"[{DateTime.Now.ToString("HH:mm:ss")}] {GetFriendlyPokemonName(pokemon.PokedexTypeId)} got away..");
  125.                 await TransferAllButStrongestUnwantedPokemon(client);
  126.                 await Task.Delay(5000); // Delay for Catching Pokemon
  127.             }
  128.         }
  129.         private static async Task TransferAllGivenPokemons(Client client, IEnumerable<PokemonProto> unwantedPokemons)
  130.         {
  131.             foreach (var pokemon in unwantedPokemons)
  132.             {
  133.                 var transferPokemonResponse = await client.TransferPokemon(pokemon.Id);
  134.  
  135.                 /*
  136.                 ReleasePokemonOutProto.Status {
  137.                     UNSET = 0;
  138.                     SUCCESS = 1;
  139.                     POKEMON_DEPLOYED = 2;
  140.                     FAILED = 3;
  141.                     ERROR_POKEMON_IS_EGG = 4;
  142.                 }*/
  143.  
  144.                 if (transferPokemonResponse.Status == 1)
  145.                 {
  146.                     System.Console.WriteLine($"transfered another {pokemon.PokemonType} to Professor.");
  147.                 }
  148.                 else
  149.                 {
  150.                     var status = transferPokemonResponse.Status;
  151.  
  152.                     System.Console.WriteLine($"Somehow failed to grind {pokemon.PokemonType}. " +
  153.                                              $"ReleasePokemonOutProto.Status was {status}");
  154.                 }
  155.  
  156.                 await Task.Delay(3000);
  157.             }
  158.         }
  159.  
  160.         private static async Task EvolveAllGivenPokemons(Client client, IEnumerable<PokemonProto> pokemonToEvolve)
  161.         {
  162.             foreach (var pokemon in pokemonToEvolve)
  163.             {
  164.                 /*
  165.                 enum Holoholo.Rpc.Types.EvolvePokemonOutProto.Result {
  166.                     UNSET = 0;
  167.                     SUCCESS = 1;
  168.                     FAILED_POKEMON_MISSING = 2;
  169.                     FAILED_INSUFFICIENT_RESOURCES = 3;
  170.                     FAILED_POKEMON_CANNOT_EVOLVE = 4;
  171.                     FAILED_POKEMON_IS_DEPLOYED = 5;
  172.                 }
  173.                 }*/
  174.  
  175.                 var countOfEvolvedUnits = 0;
  176.                 var xpCount = 0;
  177.  
  178.                 EvolvePokemonOutProto evolvePokemonOutProto;
  179.                 do
  180.                 {
  181.                     evolvePokemonOutProto = await client.EvolvePokemon(pokemon.Id);
  182.  
  183.                     if (evolvePokemonOutProto.Result == 1)
  184.                     {
  185.                         System.Console.WriteLine($"Evolved {pokemon.PokemonType} successfully for {evolvePokemonOutProto.ExpAwarded}xp");
  186.  
  187.                         countOfEvolvedUnits++;
  188.                         xpCount += evolvePokemonOutProto.ExpAwarded;
  189.                     }
  190.                     else
  191.                     {
  192.                         var result = evolvePokemonOutProto.Result;
  193.  
  194.                         System.Console.WriteLine($"Failed to evolve {pokemon.PokemonType}. " +
  195.                                                  $"EvolvePokemonOutProto.Result was {result}");
  196.  
  197.                         System.Console.WriteLine($"Due to above error, stopping evolving {pokemon.PokemonType}");
  198.                     }
  199.                 }
  200.                 while (evolvePokemonOutProto.Result == 1);
  201.  
  202.                 System.Console.WriteLine($"Evolved {countOfEvolvedUnits} pieces of {pokemon.PokemonType} for {xpCount}xp");
  203.  
  204.                 await Task.Delay(3000);
  205.             }
  206.         }
  207.  
  208.         private static async Task TransferAllButStrongestUnwantedPokemon(Client client)
  209.         {
  210.            
  211.             // Below are the pokemon types that we are throwing away.
  212.             var unwantedPokemonTypes = new[]
  213.             {
  214.                 PokemonIds.V0016PokemonPidgey,
  215.                 PokemonIds.V0019PokemonRattata,
  216.                 PokemonIds.V0013PokemonWeedle,
  217.                 PokemonIds.V0041PokemonZubat,
  218.                 PokemonIds.V0010PokemonCaterpie,
  219.                 PokemonIds.V0017PokemonPidgeotto,
  220.                 PokemonIds.V0029PokemonNidoran,
  221.                 PokemonIds.V0046PokemonParas,
  222.                 PokemonIds.V0048PokemonVenonat,
  223.                 PokemonIds.V0054PokemonPsyduck,
  224.                 PokemonIds.V0060PokemonPoliwag,
  225.                 PokemonIds.V0079PokemonSlowpoke,
  226.                 PokemonIds.V0096PokemonDrowzee,
  227.                 PokemonIds.V0092PokemonGastly,
  228.                 PokemonIds.V0118PokemonGoldeen,
  229.                 PokemonIds.V0120PokemonStaryu,
  230.                 PokemonIds.V0129PokemonMagikarp,
  231.                 PokemonIds.V0100PokemonVoltorb,
  232.                 PokemonIds.V0081PokemonMagnemite,
  233.                 PokemonIds.V0021PokemonSpearow
  234.  
  235.             };
  236.  
  237.             var inventory = await client.GetInventory();
  238.             var pokemons = inventory.Payload[0].Bag
  239.                                 .Items
  240.                                 .Select(i => i.Item?.Pokemon)
  241.                                 .Where(p => p != null && p?.PokemonType != PokemonIds.PokemonUnset)
  242.                                 .ToArray();
  243.  
  244.             foreach (var unwantedPokemonType in unwantedPokemonTypes)
  245.             {
  246.                 var pokemonOfDesiredType = pokemons.Where(p => p.PokemonType == unwantedPokemonType)
  247.                                                    .OrderByDescending(p => p.Cp)
  248.                                                    .ToList();
  249.  
  250.                 var unwantedPokemon = pokemonOfDesiredType.Skip(1) // keep the strongest one for potential battle-evolving
  251.                                                           .ToList();
  252.  
  253.                
  254.                 await TransferAllGivenPokemons(client, unwantedPokemon);
  255.             }
  256.  
  257.            
  258.         }
  259.  
  260.         private static string GetFriendlyPokemonName(MapObjectsResponse.Types.Payload.Types.PokemonIds id)
  261.         {
  262.             var name = Enum.GetName(typeof(PokemonProto.Types.PokemonIds), id);
  263.             return name?.Substring(name.IndexOf("Pokemon") + 7);
  264.         }
  265.  
  266.         private static string GetFriendlyItemsString(IEnumerable<FortSearchResponse.Types.Item> items)
  267.         {
  268.             var enumerable = items as IList<FortSearchResponse.Types.Item> ?? items.ToList();
  269.  
  270.             if (!enumerable.Any())
  271.                 return string.Empty;
  272.  
  273.             return
  274.                 enumerable.GroupBy(i => (MiscEnums.Item)i.Item_)
  275.                           .Select(kvp => new { ItemName = kvp.Key.ToString(), Amount = kvp.Sum(x => x.ItemCount) })
  276.                           .Select(y => $"{y.Amount} x {y.ItemName}")
  277.                           .Aggregate((a, b) => $"{a}, {b}");
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment