Skip to content

Commit db5b719

Browse files
bstpierrsolderzzc
authored andcommitted
Merge cells together to avoid staying in one cell too long (#1061)
* Merge cells together to avoid staying in one cell too long This should help mitigate the issue where the bot travels to a stop that is farther than a nearby one because the nearer one is in another cell. I also release control back to the make loop after catching any pokemon. * PR Feedback fixes + Add concatenation of nearby cells rather than override. ~ Actually call the SeenFortWorker rather than just reference it. * Don't make work a property I seem to have made it one at some point, somehow… Go PyCharm! * Add check to ensure there are available gyms
1 parent c2022d5 commit db5b719

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

pokemongo_bot/__init__.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def position(self):
3030

3131
def __init__(self, config):
3232
self.config = config
33+
self.fort_timeouts = dict()
3334
self.pokemon_list = json.load(open(os.path.join('data', 'pokemon.json')))
3435
self.item_list = json.load(open(os.path.join('data', 'items.json')))
3536
self.metrics = Metrics(self)
@@ -47,8 +48,22 @@ def take_step(self):
4748
def process_cells(self, work_on_forts=True):
4849
location = self.position[0:2]
4950
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 = []
5056
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)
5267

5368
def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
5469
# 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):
190205
with open(user_web_catchable, 'w') as outfile:
191206
json.dump(pokemon, outfile)
192207

193-
if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS:
194-
break
195208
with open(user_web_catchable, 'w') as outfile:
196209
json.dump({}, outfile)
197210

211+
self.catch_pokemon(cell['catchable_pokemons'][0])
212+
return
213+
198214
if (self.config.mode == "all" or self.config.mode == "poke"
199215
) and 'wild_pokemons' in cell and len(cell['wild_pokemons']) > 0:
200216
# Sort all by distance from current pos- eventually this should
201217
# build graph & A* it
202218
cell['wild_pokemons'].sort(
203219
key=
204220
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
208223
if ((self.config.mode == "all" or
209224
self.config.mode == "farm") and work_on_forts):
210225
if 'forts' in cell:
@@ -214,22 +229,18 @@ def work_on_cell(self, cell, position, work_on_forts=1):
214229
if 'latitude' in fort and 'type' in fort]
215230
gyms = [gym for gym in cell['forts'] if 'gym_points' in gym]
216231

232+
# Remove stops that are still on timeout
233+
forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts)
234+
217235
# Sort all by distance from current pos- eventually this should
218236
# build graph & A* it
219237
forts.sort(key=lambda x: distance(self.position[
220238
0], self.position[1], x['latitude'], x['longitude']))
221239

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()
233244

234245
def _setup_logging(self):
235246
self.log = logging.getLogger(__name__)
@@ -506,6 +517,10 @@ def _get_pos_by_name(self, location_name):
506517
return (loc.latitude, loc.longitude, loc.altitude)
507518

508519
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}
509524
self.api.get_player()
510525
self.api.get_hatched_eggs()
511526
self.api.get_inventory()

pokemongo_bot/cell_workers/seen_fort_worker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def work(self):
9999

100100
pokestop_cooldown = spin_details.get(
101101
'cooldown_complete_timestamp_ms')
102+
self.bot.fort_timeouts.update({self.fort["id"]: pokestop_cooldown})
102103
if pokestop_cooldown:
103104
seconds_since_epoch = time.time()
104105
logger.log('PokeStop on cooldown. Time left: ' + str(
@@ -120,6 +121,7 @@ def work(self):
120121
pokestop_cooldown = spin_details.get(
121122
'cooldown_complete_timestamp_ms')
122123
if pokestop_cooldown:
124+
self.bot.fort_timeouts.update({self.fort["id"]: pokestop_cooldown})
123125
seconds_since_epoch = time.time()
124126
logger.log('PokeStop on cooldown. Time left: ' + str(
125127
format_time((pokestop_cooldown / 1000) -
@@ -137,6 +139,7 @@ def work(self):
137139
'chain_hack_sequence_number']
138140
else:
139141
logger.log('Possibly searching too often - taking a short rest :)', 'yellow')
142+
self.bot.fort_timeouts[self.fort["id"]] = (time.time() + 300) * 1000 # Don't spin for 5m
140143
return 11
141144
sleep(8)
142145
return 0

0 commit comments

Comments
 (0)