@@ -30,6 +30,7 @@ def position(self):
30
30
31
31
def __init__ (self , config ):
32
32
self .config = config
33
+ self .fort_timeouts = dict ()
33
34
self .pokemon_list = json .load (open (os .path .join ('data' , 'pokemon.json' )))
34
35
self .item_list = json .load (open (os .path .join ('data' , 'items.json' )))
35
36
self .metrics = Metrics (self )
@@ -47,8 +48,22 @@ def take_step(self):
47
48
def process_cells (self , work_on_forts = True ):
48
49
location = self .position [0 :2 ]
49
50
cells = self .find_close_cells (* location )
51
+
52
+ # Combine all cells into a single dict of the items we care about.
53
+ forts = []
54
+ wild_pokemons = []
55
+ catchable_pokemons = []
50
56
for cell in cells :
51
- self .work_on_cell (cell , location , work_on_forts )
57
+ if "forts" in cell and len (cell ["forts" ]):
58
+ forts += cell ["forts" ]
59
+ if "wild_pokemons" in cell and len (cell ["wild_pokemons" ]):
60
+ wild_pokemons += cell ["wild_pokemons" ]
61
+ if "catchable_pokemons" in cell and len (cell ["catchable_pokemons" ]):
62
+ catchable_pokemons += cell ["catchable_pokemons" ]
63
+
64
+ # Have the worker treat the whole area as a single cell.
65
+ self .work_on_cell ({"forts" : forts , "wild_pokemons" : wild_pokemons ,
66
+ "catchable_pokemons" : catchable_pokemons }, location , work_on_forts )
52
67
53
68
def update_web_location (self , cells = [], lat = None , lng = None , alt = None ):
54
69
# we can call the function with no arguments and still get the position and map_cells
@@ -190,21 +205,21 @@ def work_on_cell(self, cell, position, work_on_forts=1):
190
205
with open (user_web_catchable , 'w' ) as outfile :
191
206
json .dump (pokemon , outfile )
192
207
193
- if self .catch_pokemon (pokemon ) == PokemonCatchWorker .NO_POKEBALLS :
194
- break
195
208
with open (user_web_catchable , 'w' ) as outfile :
196
209
json .dump ({}, outfile )
197
210
211
+ self .catch_pokemon (cell ['catchable_pokemons' ][0 ])
212
+ return
213
+
198
214
if (self .config .mode == "all" or self .config .mode == "poke"
199
215
) and 'wild_pokemons' in cell and len (cell ['wild_pokemons' ]) > 0 :
200
216
# Sort all by distance from current pos- eventually this should
201
217
# build graph & A* it
202
218
cell ['wild_pokemons' ].sort (
203
219
key =
204
220
lambda x : distance (self .position [0 ], self .position [1 ], x ['latitude' ], x ['longitude' ]))
205
- for pokemon in cell ['wild_pokemons' ]:
206
- if self .catch_pokemon (pokemon ) == PokemonCatchWorker .NO_POKEBALLS :
207
- break
221
+ self .catch_pokemon (cell ['wild_pokemons' ][0 ])
222
+ return
208
223
if ((self .config .mode == "all" or
209
224
self .config .mode == "farm" ) and work_on_forts ):
210
225
if 'forts' in cell :
@@ -214,22 +229,18 @@ def work_on_cell(self, cell, position, work_on_forts=1):
214
229
if 'latitude' in fort and 'type' in fort ]
215
230
gyms = [gym for gym in cell ['forts' ] if 'gym_points' in gym ]
216
231
232
+ # Remove stops that are still on timeout
233
+ forts = filter (lambda x : x ["id" ] not in self .fort_timeouts , forts )
234
+
217
235
# Sort all by distance from current pos- eventually this should
218
236
# build graph & A* it
219
237
forts .sort (key = lambda x : distance (self .position [
220
238
0 ], self .position [1 ], x ['latitude' ], x ['longitude' ]))
221
239
222
- for fort in forts :
223
- worker = MoveToFortWorker (fort , self )
224
- worker .work ()
225
-
226
- worker = SeenFortWorker (fort , self )
227
- hack_chain = worker .work ()
228
- if hack_chain > 10 :
229
- #print('need a rest')
230
- break
231
- if self .config .mode == "poke" :
232
- break
240
+ if len (forts ) > 0 :
241
+ # Move to and spin the nearest stop.
242
+ MoveToFortWorker (forts [0 ], self ).work ()
243
+ SeenFortWorker (forts [0 ], self ).work ()
233
244
234
245
def _setup_logging (self ):
235
246
self .log = logging .getLogger (__name__ )
@@ -506,6 +517,10 @@ def _get_pos_by_name(self, location_name):
506
517
return (loc .latitude , loc .longitude , loc .altitude )
507
518
508
519
def heartbeat (self ):
520
+ # Remove forts that we can now spin again.
521
+ self .fort_timeouts = {id : timeout for id ,timeout
522
+ in self .fort_timeouts .iteritems ()
523
+ if timeout >= time .time () * 1000 }
509
524
self .api .get_player ()
510
525
self .api .get_hatched_eggs ()
511
526
self .api .get_inventory ()
0 commit comments