From 64cb35c8a59357d9f23824233534291450c30152 Mon Sep 17 00:00:00 2001 From: Chris Wild Date: Sat, 30 Jul 2016 13:22:49 +0100 Subject: [PATCH] Location cache check. If start position differs, don't use the cache. --- pokemongo_bot/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index a67d829a80..c75ebbaf8e 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -43,6 +43,7 @@ def __init__(self, config): self.recent_forts = [None] * config.forts_max_circle_size self.tick_count = 0 self.softban = False + self.start_position = None # Make our own copy of the workers for this instance self.workers = [] @@ -164,7 +165,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None): user_data_lastlocation = os.path.join('data', 'last-location-%s.json' % (self.config.username)) try: with open(user_data_lastlocation, 'w') as outfile: - json.dump({'lat': lat, 'lng': lng}, outfile) + json.dump({'lat': lat, 'lng': lng, 'start_position': self.start_position}, outfile) except IOError as e: logger.log('[x] Error while opening location file: %s' % e, 'red') @@ -452,6 +453,7 @@ def _set_starting_position(self): location_str = self.config.location.encode('utf-8') location = (self._get_pos_by_name(location_str.replace(" ", ""))) self.api.set_position(*location) + self.start_position = self.position logger.log('') logger.log(u'Location Found: {}'.format(self.config.location)) logger.log('GeoPosition: {}'.format(self.position)) @@ -469,7 +471,17 @@ def _set_starting_position(self): location_json = json.load(f) location = (location_json['lat'], location_json['lng'], 0.0) - #print(location) + + # If location has been set in config, only use cache if starting position has not differed + if has_position and 'start_position' in location_json: + last_start_position = tuple(location_json.get('start_position', [])) + + # Start position has to have been set on a previous run to do this check + if last_start_position and last_start_position != self.start_position: + logger.log('[x] Last location flag used but with a stale starting location', 'yellow') + logger.log('[x] Using new starting location, {}'.format(self.position)) + return + self.api.set_position(*location) logger.log('') @@ -481,7 +493,6 @@ def _set_starting_position(self): logger.log('') has_position = True - return except Exception: if(has_position == False): sys.exit(