Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. import PokeAPI from 'pokemongo-api';
  2.  
  3. const Poke = new PokeAPI();
  4.  
  5. function lerp(a, b, t) {
  6. var len = a.length;
  7. if (b.length != len) return;
  8.  
  9. var x = [];
  10. for (var i = 0; i < len; i++)
  11. x.push(a[i] + t * (b[i] - a[i]));
  12. return x;
  13. }
  14.  
  15. function sleep(ms = 0) {
  16. return new Promise(r => setTimeout(r, ms));
  17. }
  18.  
  19. function distance(lat1, lon1, lat2, lon2, unit) {
  20. var radlat1 = Math.PI * lat1 / 180
  21. var radlat2 = Math.PI * lat2 / 180
  22. var theta = lon1 - lon2
  23. var radtheta = Math.PI * theta / 180
  24. var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  25. dist = Math.acos(dist)
  26. dist = dist * 180 / Math.PI
  27. dist = dist * 60 * 1.1515
  28. if (unit == "K") {
  29. dist = dist * 1.609344
  30. }
  31. if (unit == "N") {
  32. dist = dist * 0.8684
  33. }
  34. return dist
  35. }
  36. async function walkTo(lat, long, Poke,api) {
  37. let movementSpeed = 4 //14 km/h 4m /s
  38. let player = api.player;
  39. let playerCoords = player.coords;
  40. let startPlayerCoords = playerCoords;
  41. console.log(playerCoords);
  42. console.log(lat,long);
  43. let calculatedDistance = distance(startPlayerCoords[0], startPlayerCoords[1], lat, long, "K") * 1000; //TO M
  44. console.log("GOT TO WALK: "+calculatedDistance+" M");
  45. let startTime = new Date().getTime();
  46. console.log("Starting Time"+startTime);
  47. if (calculatedDistance > 40) {
  48. let currentStep = 0;
  49. let stepCount = Math.round(calculatedDistance / 100);
  50. while (currentStep < stepCount) {
  51. console.log("WALKING STEP: "+currentStep/stepCount);
  52. _ = await Poke.GetMapObjects();
  53.  
  54. currentStep += 1;
  55. let newCoords = lerp(startPlayerCoords, [lat, long], calculatedDistance, currentStep / stepCount);
  56. let location = {
  57. type: 'coords',
  58. coords: {
  59. latitude: newCoords[0],
  60. longitude: newCoords[1],
  61. },
  62. };
  63. await api.player.setLocation(location);
  64. playerCoords = api.player.coords;
  65. console.log("waiting: "+((calculatedDistance / movementSpeed) / stepCount)*1000+" MS");
  66. await sleep(((calculatedDistance / movementSpeed) / stepCount)*1000);
  67. }
  68.  
  69. }
  70. let endTime = new Date().getTime();
  71. console.log("ENDTIME"+endTime,"REQUIRED TIME"+(endTime-startTime));
  72. }
  73. async function init() {
  74. //yep, we do need to login..
  75.  
  76. const api = await Poke.login(username, password, location, provider)
  77.  
  78. // just update the profile...
  79. async function loop() {
  80. let player = await Poke.GetPlayer();
  81.  
  82. // get map objects..
  83. let cells = await Poke.GetMapObjects()
  84. await Poke.player.walkAround()
  85. for (let cell of cells) {
  86.  
  87. if (cell.catchable_pokemons.length > 0) {
  88. //we have wild pokemons
  89. console.log({
  90. catchable_pokemons: cell.catchable_pokemons
  91. })
  92. }
  93.  
  94. //wild pokemons
  95. if (cell.wild_pokemons.length > 0) {
  96. console.log({
  97. wild_pokemons: cell.wild_pokemons
  98. })
  99. }
  100.  
  101. //forts
  102. if (cell.forts.length > 0) {
  103. for (fort of cell.forts) {
  104. switch (fort.type) {
  105. case 1: //POKESTOP
  106. console.log("WALKING TO POKESTOP");
  107. let _ = await walkTo(fort.latitude, fort.longitude, Poke,api);
  108. console.log("WALKED TO POKESTOP");
  109. console.log("SEARCHING TO POKESTOP");
  110. let fortResult = await Poke.FortSearch({
  111. fort_id: fort.id,
  112. fort_latitude: fort.latitude,
  113. fort_longitude: fort.longitude
  114. });
  115. console.log("SEARCHED POKESTOP");
  116. console.log(fortResult);
  117. break;
  118. case 0: //GYM
  119. break;
  120.  
  121. }
  122. }
  123.  
  124. }
  125. if (cell.catchable_pokemons.length > 0) {
  126. for (var pokemon of cell.catchable_pokemons) {
  127. try {
  128. let encounterResult = await Poke.EncounterPokemon(pokemon);
  129. let catchPokemonResult = await Poke.CatchPokemon(pokemon);
  130. console.log({
  131. encounterResult: encounterResult,
  132. catchPokemonResult: catchPokemonResult
  133. });
  134.  
  135. } catch (error) {
  136. console.log(error);
  137. }
  138. }
  139. console.log({
  140. catchable_pokemons: cell.catchable_pokemons
  141. })
  142. }
  143.  
  144. //Done...
  145. //TODO: We need to move.. like a human..!
  146. }
  147. }
  148. while(true) {
  149. await sleep(2000);
  150. await loop();
  151. }
  152. }
  153.  
  154. init().catch(console.log)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement