Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from __future__ import absolute_import
  4.  
  5. from pokemongo_bot import inventory
  6. from pokemongo_bot.constants import Constants
  7. from pokemongo_bot.walkers.walker_factory import walker_factory
  8. from pokemongo_bot.worker_result import WorkerResult
  9. from pokemongo_bot.base_task import BaseTask
  10. from .utils import distance, format_dist, fort_details
  11. from datetime import datetime, timedelta
  12.  
  13. class MoveToFort(BaseTask):
  14. SUPPORTED_TASK_API_VERSION = 1
  15.  
  16. def initialize(self):
  17. self.lure_distance = 0
  18. self.lure_attraction = self.config.get("lure_attraction", True)
  19. self.lure_max_distance = self.config.get("lure_max_distance", 2000)
  20. self.ignore_item_count = self.config.get("ignore_item_count", False)
  21. self.fort_ids = []
  22. self.walker = self.config.get('walker', 'StepWalker')
  23. self.wait_at_fort = self.config.get('wait_on_lure', False)
  24. self.wait_log_sent = None
  25.  
  26. def should_run(self):
  27. has_space_for_loot = inventory.Items.has_space_for_loot()
  28. if not has_space_for_loot and not self.ignore_item_count:
  29. self.emit_event(
  30. 'inventory_full',
  31. formatted="Inventory is full. You might want to change your config to recycle more items if this message appears consistently."
  32. )
  33. return has_space_for_loot or self.ignore_item_count or self.bot.softban
  34.  
  35. def is_attracted(self):
  36. return (self.lure_distance > 0)
  37.  
  38. def work(self):
  39. if not self.should_run():
  40. return WorkerResult.SUCCESS
  41.  
  42. nearest_fort = self.get_nearest_fort()
  43.  
  44. if nearest_fort is None:
  45. return WorkerResult.SUCCESS
  46.  
  47. lat = nearest_fort['latitude']
  48. lng = nearest_fort['longitude']
  49. fortID = nearest_fort['id']
  50. details = fort_details(self.bot, fortID, lat, lng)
  51. fort_name = details.get('name', 'Unknown')
  52.  
  53. unit = self.bot.config.distance_unit # Unit to use when printing formatted distance
  54.  
  55. dist = distance(
  56. self.bot.position[0],
  57. self.bot.position[1],
  58. lat,
  59. lng
  60. )
  61. noised_dist = distance(
  62. self.bot.noised_position[0],
  63. self.bot.noised_position[1],
  64. lat,
  65. lng
  66. )
  67.  
  68. moving = noised_dist > Constants.MAX_DISTANCE_FORT_IS_REACHABLE if self.bot.config.replicate_gps_xy_noise else dist > Constants.MAX_DISTANCE_FORT_IS_REACHABLE
  69.  
  70. if moving:
  71. self.wait_log_sent = None
  72. fort_event_data = {
  73. 'fort_name': u"{}".format(fort_name),
  74. 'distance': format_dist(dist, unit),
  75. }
  76.  
  77. if self.is_attracted() > 0:
  78. fort_event_data.update(lure_distance=format_dist(self.lure_distance, unit))
  79. self.emit_event(
  80. 'moving_to_lured_fort',
  81. formatted="Moving toward PokeStop {fort_name} - {distance} (attraction of lure {lure_distance})",
  82. data=fort_event_data
  83. )
  84. else:
  85. self.emit_event(
  86. 'moving_to_fort',
  87. formatted="Moving toward PokeStop {fort_name} - {distance}",
  88. data=fort_event_data
  89. )
  90.  
  91. step_walker = walker_factory(self.walker,
  92. self.bot,
  93. lat,
  94. lng
  95. )
  96.  
  97. if not step_walker.step():
  98. return WorkerResult.RUNNING
  99. else:
  100. if not self.bot.catch_disabled and nearest_fort.get('active_fort_modifier') and self.wait_at_fort:
  101. if self.wait_log_sent == None or self.wait_log_sent < datetime.now() - timedelta(seconds=60):
  102. self.wait_log_sent = datetime.now()
  103. self.emit_event(
  104. 'arrived_at_fort',
  105. formatted='Waiting near PokeStop %s until lure module expires' % fort_name
  106. )
  107. else:
  108. self.emit_event(
  109. 'arrived_at_fort',
  110. formatted='Arrived at PokeStop {fort_name}.'
  111. )
  112.  
  113. return WorkerResult.RUNNING
  114.  
  115. def _get_nearest_fort_on_lure_way(self, forts):
  116. if not self.lure_attraction:
  117. return None, 0
  118.  
  119. lures = filter(lambda x: True if x.get('lure_info', None) != None else False, forts)
  120. if not self.bot.catch_disabled and self.wait_at_fort:
  121. lures = filter(lambda x: x.get('active_fort_modifier', False), forts)
  122.  
  123. if len(lures):
  124. dist_lure_me = distance(self.bot.position[0], self.bot.position[1],
  125. lures[0]['latitude'],lures[0]['longitude'])
  126. else:
  127. dist_lure_me = 0
  128.  
  129. if dist_lure_me > 0 and dist_lure_me < self.lure_max_distance:
  130.  
  131. self.lure_distance = dist_lure_me
  132.  
  133. for fort in forts:
  134. dist_lure_fort = distance(
  135. fort['latitude'],
  136. fort['longitude'],
  137. lures[0]['latitude'],
  138. lures[0]['longitude'])
  139. dist_fort_me = distance(
  140. fort['latitude'],
  141. fort['longitude'],
  142. self.bot.position[0],
  143. self.bot.position[1])
  144.  
  145. if dist_lure_fort < dist_lure_me and dist_lure_me > dist_fort_me:
  146. return fort, dist_lure_me
  147.  
  148. if dist_fort_me > dist_lure_me:
  149. break
  150.  
  151. return lures[0], dist_lure_me
  152.  
  153. else:
  154. return None, 0
  155.  
  156. def get_nearest_fort(self):
  157. nearest_fort = []
  158. forts = self.bot.get_forts(order_by_distance=True)
  159. # Remove stops that are still on timeout
  160. forts = filter(
  161. lambda x: x["id"] not in self.bot.fort_timeouts or (
  162. x.get('active_fort_modifier', False) and self.wait_at_fort and not self.bot.catch_disabled
  163. ),
  164. forts
  165. )
  166.  
  167. next_attracted_pts, lure_distance = self._get_nearest_fort_on_lure_way(forts)
  168.  
  169. # Remove all forts which were spun in the last ticks to avoid circles if set
  170. if self.bot.config.forts_avoid_circles or not self.wait_at_fort or self.bot.catch_disabled:
  171. forts = filter(lambda x: x["id"] not in self.bot.recent_forts, forts)
  172.  
  173. self.lure_distance = lure_distance
  174.  
  175. if (lure_distance > 0):
  176. return next_attracted_pts
  177.  
  178. if len(forts) >= 3:
  179. # Get ID of fort, store it. Check index 0 & index 2. Both must not be same
  180. nearest_fort = forts[0]
  181.  
  182. if len(self.fort_ids) < 3:
  183. self.fort_ids.extend(nearest_fort['id'])
  184. else:
  185. #this will always be len of 3, compare index 1 and nearest_fort
  186. if self.fort_ids[1] == nearest_fort['id'] and self.fort_ids[0] == self.fort_ids[2]:
  187. self.fort_ids.pop(0)
  188. # take the next nearest, assuming bot is bouncing between index 0 and 1
  189. nearest_fort = forts[2]
  190. self.fort_ids.extend(nearest_fort['id'])
  191. else:
  192. self.fort_ids.pop(0)
  193. self.fort_ids.extend(nearest_fort['id'])
  194.  
  195. return nearest_fort
  196. else:
  197. return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement