Skip to content

Commit

Permalink
Request meta cell data once every 5 seconds (#2171)
Browse files Browse the repository at this point in the history
This solve the Niantic "scan for pokemon" throttling without
making the bot very slow.
  • Loading branch information
matheussampaio authored and elicwhite committed Aug 1, 2016
1 parent c4b913c commit a9fd95b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
* kbinani
* MFizz
* NamPNQ
* matheussampaio
1 change: 1 addition & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"type": "FollowSpiral"
}
],
"map_object_cache_time": 5,
"max_steps": 5,
"forts": {
"avoid_circles": true,
Expand Down
1 change: 1 addition & 0 deletions configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
}
}
],
"map_object_cache_time": 5,
"max_steps": 5,
"forts": {
"avoid_circles": true,
Expand Down
1 change: 1 addition & 0 deletions configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"type": "FollowSpiral"
}
],
"map_object_cache_time": 5,
"max_steps": 5,
"forts": {
"avoid_circles": true,
Expand Down
13 changes: 13 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ def init_config():
type=float,
default=1.0
)
add_config(
parser,
load,
long_flag="--map_object_cache_time",
help="Amount of seconds to keep the map object in cache (bypass Niantic throttling)",
type=float,
default=5.0
)

# Start to parse other attrs
config = parser.parse_args()
Expand All @@ -338,6 +346,11 @@ def init_config():
config.action_wait_min = load.get('action_wait_min', 1)
config.raw_tasks = load.get('tasks', [])
config.vips = load.get('vips',{})
config.map_object_cache_time = load.get('map_object_cache_time')

This comment has been minimized.

Copy link
@Shoh

Shoh Aug 1, 2016

Contributor

I think you should add default here as well?

config.map_object_cache_time = load.get('map_object_cache_time', 5.0)

Otherwise it won't default and an error will reported to the user stating it's out of range.


if config.map_object_cache_time < 0.0:
parser.error("--map_object_cache_time is out of range! (should be >= 0.0)")
return None

if len(config.raw_tasks) == 0:
logging.error("No tasks are configured. Did you mean to configure some behaviors? Read https://github.com/PokemonGoF/PokemonGo-Bot/wiki/Configuration-files#configuring-tasks for more information")
Expand Down
35 changes: 20 additions & 15 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self, config):
self.tick_count = 0
self.softban = False
self.start_position = None
self.last_map_object = None
self.last_time_map_object = 0

# Make our own copy of the workers for this instance
self.workers = []
Expand Down Expand Up @@ -121,13 +123,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
if cells == []:
cellid = get_cell_ids(lat, lng)
timestamp = [0, ] * len(cellid)
self.api.get_map_objects(
latitude=f2i(lat),
longitude=f2i(lng),
since_timestamp_ms=timestamp,
cell_id=cellid
)
response_dict = self.api.call()
response_dict = self.get_map_objects(lat, lng, timestamp, cellid)
map_objects = response_dict.get(
'responses', {}
).get('GET_MAP_OBJECTS', {})
Expand Down Expand Up @@ -182,14 +178,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
def find_close_cells(self, lat, lng):
cellid = get_cell_ids(lat, lng)
timestamp = [0, ] * len(cellid)

self.api.get_map_objects(
latitude=f2i(lat),
longitude=f2i(lng),
since_timestamp_ms=timestamp,
cell_id=cellid
)
response_dict = self.api.call()
response_dict = self.get_map_objects(lat, lng, timestamp, cellid)
map_objects = response_dict.get(
'responses', {}
).get('GET_MAP_OBJECTS', {})
Expand Down Expand Up @@ -625,3 +614,19 @@ def get_forts(self, order_by_distance=False):
))

return forts

def get_map_objects(self, lat, lng, timestamp, cellid):
if time.time() - self.last_time_map_object < self.config.map_object_cache_time:
return self.last_map_object

self.api.get_map_objects(
latitude=f2i(lat),
longitude=f2i(lng),
since_timestamp_ms=timestamp,
cell_id=cellid
)

self.last_map_object = self.api.call()
self.last_time_map_object = time.time()

return self.last_map_object
4 changes: 2 additions & 2 deletions pokemongo_bot/cell_workers/catch_visible_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def work(self):
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)

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

if 'wild_pokemons' in self.bot.cell and len(self.bot.cell['wild_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['wild_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']))
return self.catch_pokemon(self.bot.cell['wild_pokemons'][0])
return self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0))

def catch_pokemon(self, pokemon):
worker = PokemonCatchWorker(pokemon, self.bot)
Expand Down

0 comments on commit a9fd95b

Please sign in to comment.