Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge cells together to avoid staying in one cell too long #1061

Merged
merged 5 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def position(self):

def __init__(self, config):
self.config = config
self.fort_timeouts = dict()
self.pokemon_list = json.load(open(os.path.join('data', 'pokemon.json')))
self.item_list = json.load(open(os.path.join('data', 'items.json')))

Expand All @@ -45,8 +46,22 @@ def take_step(self):
def process_cells(self, work_on_forts=True):
location = self.position[0:2]
cells = self.find_close_cells(*location)
Copy link

@lexxxel lexxxel Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stumbled over this PR and I'm not able to test it myself right now, but shouldn't it be a concatenation between the cells from line 44 (new) and line 46 (new) instead of a replacement? Or If not, shouldn't be line 44(new) be erased


# Combine all cells into a single dict of the items we care about.
forts = []
wild_pokemons = []
catchable_pokemons = []
for cell in cells:
self.work_on_cell(cell, location, work_on_forts)
if "forts" in cell and len(cell["forts"]):
forts += cell["forts"]
if "wild_pokemons" in cell and len(cell["wild_pokemons"]):
wild_pokemons += cell["wild_pokemons"]
if "catchable_pokemons" in cell and len(cell["catchable_pokemons"]):
catchable_pokemons += cell["catchable_pokemons"]

# Have the worker treat the whole area as a single cell.
self.work_on_cell({"forts": forts, "wild_pokemons": wild_pokemons,
"catchable_pokemons": catchable_pokemons}, location, work_on_forts)

def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
# we can call the function with no arguments and still get the position and map_cells
Expand Down Expand Up @@ -188,21 +203,21 @@ def work_on_cell(self, cell, position, work_on_forts=1):
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)

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

self.catch_pokemon(cell['catchable_pokemons'][0])
return

if (self.config.mode == "all" or self.config.mode == "poke"
) and 'wild_pokemons' in cell and len(cell['wild_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
cell['wild_pokemons'].sort(
key=
lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude']))
for pokemon in cell['wild_pokemons']:
if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS:
break
self.catch_pokemon(cell['wild_pokemons'][0])
return
if ((self.config.mode == "all" or
self.config.mode == "farm") and work_on_forts):
if 'forts' in cell:
Expand All @@ -212,22 +227,18 @@ def work_on_cell(self, cell, position, work_on_forts=1):
if 'latitude' in fort and 'type' in fort]
gyms = [gym for gym in cell['forts'] if 'gym_points' in gym]

# Remove stops that are still on timeout
forts = filter(lambda x: x["id"] not in self.fort_timeouts, forts)

# Sort all by distance from current pos- eventually this should
# build graph & A* it
forts.sort(key=lambda x: distance(self.position[
0], self.position[1], x['latitude'], x['longitude']))

for fort in forts:
worker = MoveToFortWorker(fort, self)
worker.work()

worker = SeenFortWorker(fort, self)
hack_chain = worker.work()
if hack_chain > 10:
#print('need a rest')
break
if self.config.mode == "poke":
break
if len(forts) > 0:
# Move to and spin the nearest stop.
MoveToFortWorker(forts[0], self).work()
SeenFortWorker(forts[0], self).work()

def _setup_logging(self):
self.log = logging.getLogger(__name__)
Expand Down Expand Up @@ -504,6 +515,10 @@ def _get_pos_by_name(self, location_name):
return (loc.latitude, loc.longitude, loc.altitude)

def heartbeat(self):
# Remove forts that we can now spin again.
self.fort_timeouts = {id: timeout for id ,timeout
in self.fort_timeouts.iteritems()
if timeout >= time.time() * 1000}
self.api.get_player()
self.api.get_hatched_eggs()
self.api.get_inventory()
Expand Down
3 changes: 3 additions & 0 deletions pokemongo_bot/cell_workers/seen_fort_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def work(self):

pokestop_cooldown = spin_details.get(
'cooldown_complete_timestamp_ms')
self.bot.fort_timeouts.update({self.fort["id"]: pokestop_cooldown})
if pokestop_cooldown:
seconds_since_epoch = time.time()
logger.log('PokeStop on cooldown. Time left: ' + str(
Expand All @@ -120,6 +121,7 @@ def work(self):
pokestop_cooldown = spin_details.get(
'cooldown_complete_timestamp_ms')
if pokestop_cooldown:
self.bot.fort_timeouts.update({self.fort["id"]: pokestop_cooldown})
seconds_since_epoch = time.time()
logger.log('PokeStop on cooldown. Time left: ' + str(
format_time((pokestop_cooldown / 1000) -
Expand All @@ -137,6 +139,7 @@ def work(self):
'chain_hack_sequence_number']
else:
logger.log('Possibly searching too often - taking a short rest :)', 'yellow')
self.bot.fort_timeouts[self.fort["id"]] = (time.time() + 300) * 1000 # Don't spin for 5m
return 11
sleep(8)
return 0
Expand Down