diff --git a/pokemongo_bot/polyline_stepper.py b/pokemongo_bot/polyline_stepper.py index b6dbd78d44..af701cc0d3 100644 --- a/pokemongo_bot/polyline_stepper.py +++ b/pokemongo_bot/polyline_stepper.py @@ -3,7 +3,7 @@ import logger from human_behaviour import sleep -from polyline_walker import PolylineWalker +from walkers.polyline_walker import PolylineWalker class PolylineStepper(Stepper): diff --git a/pokemongo_bot/polyline_walker/__init__.py b/pokemongo_bot/walkers/__init__.py similarity index 51% rename from pokemongo_bot/polyline_walker/__init__.py rename to pokemongo_bot/walkers/__init__.py index 272b8375d4..95338c20cf 100644 --- a/pokemongo_bot/polyline_walker/__init__.py +++ b/pokemongo_bot/walkers/__init__.py @@ -1 +1,3 @@ +from polyline_generator import Polyline from polyline_walker import PolylineWalker + diff --git a/pokemongo_bot/polyline_walker/polyline_walker.py b/pokemongo_bot/walkers/polyline_generator.py similarity index 77% rename from pokemongo_bot/polyline_walker/polyline_walker.py rename to pokemongo_bot/walkers/polyline_generator.py index 2c5be3daaa..ff50885b98 100644 --- a/pokemongo_bot/polyline_walker/polyline_walker.py +++ b/pokemongo_bot/walkers/polyline_generator.py @@ -1,25 +1,24 @@ import time from itertools import chain from math import ceil - import haversine import polyline import requests -class PolylineWalker(object): +class Polyline(object): def __init__(self, origin, destination, speed): self.DISTANCE_API_URL='https://maps.googleapis.com/maps/api/directions/json?mode=walking' self.origin = origin self.destination = destination + self.URL = '{}&origin={}&destination={}'.format(self.DISTANCE_API_URL, + '{},{}'.format(*self.origin), + '{},{}'.format(*self.destination)) self.polyline_points = [x['polyline']['points'] for x in - requests.get(self.DISTANCE_API_URL+'&origin='+ - self.origin+'&destination='+ - self.destination - ).json()['routes'][0]['legs'][0]['steps']] + requests.get(self.URL).json()['routes'][0]['legs'][0]['steps']] self.speed = float(speed) - self.points = self.get_points(self.polyline_points) + self.points = [self.origin] + self.get_points(self.polyline_points) + [self.destination] self.lat, self.long = self.points[0][0], self.points[0][1] self.polyline = self.combine_polylines(self.points) self._timestamp = time.time() @@ -85,9 +84,15 @@ def get_pos(self): return self.calculate_coord(percentage_walked, *steps_dict[walked_end_step]) def calculate_coord(self, percentage, o, d): - lat = o[0]+ (d[0] -o[0]) * percentage - lon = o[1]+ (d[1] -o[1]) * percentage - return [(lat, lon)] + # If this is the destination then returning as such + if self.points[-1] == d: + return [d] + else: + # intermediary points returned with 5 decimals precision only + # this ensures ~3-50cm ofset from the geometrical point calculated + lat = o[0]+ (d[0] -o[0]) * percentage + lon = o[1]+ (d[1] -o[1]) * percentage + return [(round(lat, 5), round(lon, 5))] def get_total_distance(self): return ceil(sum([haversine.haversine(*x)*1000 for x in self.walk_steps()])) diff --git a/pokemongo_bot/polyline_walker/polyline_tester.py b/pokemongo_bot/walkers/polyline_generator_tester.py similarity index 82% rename from pokemongo_bot/polyline_walker/polyline_tester.py rename to pokemongo_bot/walkers/polyline_generator_tester.py index a8d0554e16..d58265618c 100644 --- a/pokemongo_bot/polyline_walker/polyline_tester.py +++ b/pokemongo_bot/walkers/polyline_generator_tester.py @@ -3,10 +3,10 @@ import haversine import polyline - -from polyline_walker import PolylineWalker - -a = PolylineWalker('Poststrasse+20,Zug,CH', 'Guggiweg+7,Zug,CH', 100) +from math import ceil +from polyline_generator import Polyline +a = Polyline((47.1706378, 8.5167405), (47.1700271, 8.518072999999998), 100) +print(a.points) print('Walking polyline: ', a.polyline) print('Encoded level: ','B'*len(a.points)) print('Initialted with speed: ', a.speed, 'm/s') diff --git a/pokemongo_bot/walkers/polyline_walker.py b/pokemongo_bot/walkers/polyline_walker.py new file mode 100644 index 0000000000..a942195f50 --- /dev/null +++ b/pokemongo_bot/walkers/polyline_walker.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from polyline_generator import Polyline +from math import ceil +from pokemongo_bot.human_behaviour import sleep +from pokemongo_bot.cell_workers.utils import i2f +from pokemongo_bot.step_walker import StepWalker +from pokemongo_bot import logger + +class PolylineWalker(StepWalker): + + def __init__(self, bot, speed, initLat, initLng, destLat, destLng): + super(PolylineWalker, self).__init__(bot, speed, initLat, initLng, destLat, destLng) + self.polyline_walker = Polyline((i2f(self.api._position_lat), i2f(self.api._position_lng)), + (self.destLat, self.destLng), self.speed) + logger.log('[#] {}'.format(self.polyline_walker.URL)) + + def step(self): + self.polyline_walker.unpause() + sleep(1) + self.polyline_walker.pause() + cLat, cLng = self.polyline_walker.get_pos()[0] + self.api.set_position(round(cLat, 5), round(cLng, 5), 0) + self.bot.heartbeat() + if self.destLat == cLat and self.destLng == cLng: + return True