Skip to content

Commit

Permalink
Merge pull request #5382 from Anakin5/fix_movers
Browse files Browse the repository at this point in the history
Bug fix on FollowPath, MoveToFort and PolylineGenerator
  • Loading branch information
anakin5 authored Sep 12, 2016
2 parents 917fbaf + 3c1b0f5 commit 8b2eb8c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 36 deletions.
6 changes: 3 additions & 3 deletions pokemongo_bot/cell_workers/follow_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def work(self):
return WorkerResult.SUCCESS

if self.status == STATUS_LOITERING and time.time() < self.loiter_end_time:
return WorkerResult.SUCCESS
return WorkerResult.RUNNING

last_lat, last_lng, last_alt = self.bot.position

Expand Down Expand Up @@ -187,12 +187,12 @@ def work(self):
}
)

if dist <= 1 or (self.bot.config.walk_min > 0 and is_at_destination) or (self.status == STATUS_LOITERING and time.time() >= self.loiter_end_time):
if (self.bot.config.walk_min > 0 and is_at_destination) or (self.status == STATUS_LOITERING and time.time() >= self.loiter_end_time):
if "loiter" in point and self.status != STATUS_LOITERING:
self.logger.info("Loitering for {} seconds...".format(point["loiter"]))
self.status = STATUS_LOITERING
self.loiter_end_time = time.time() + point["loiter"]
return WorkerResult.SUCCESS
return WorkerResult.RUNNING
if (self.ptr + 1) == len(self.points):
if self.path_mode == 'single':
self.status = STATUS_FINISHED
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/move_to_fort.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def work(self):
formatted='Arrived at fort.'
)

return WorkerResult.SUCCESS
return WorkerResult.RUNNING

def _get_nearest_fort_on_lure_way(self, forts):

Expand Down
17 changes: 7 additions & 10 deletions pokemongo_bot/test/polyline_generator_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import unittest, pickle, os
import os
import pickle
import unittest
import requests_mock
from mock import patch, Mock
from pokemongo_bot.walkers.polyline_generator import Polyline
import datetime
import time

ex_orig = (47.1706378, 8.5167405)
ex_dest = (47.1700271, 8.518072999999998)
ex_speed = 2.5
ex_total_distance = 194
ex_resp_directions = 'example_directions.pickle'
ex_resp_elevations = 'example_elevations.pickle'
ex_enc_polyline = 'o_|~Gsl~r@??h@LVDf@LDcBFi@AUEUQg@EKCI?G?GBG@EBEJKNC??'
ex_nr_samples = 78

ex_enc_polyline = 'o_%7C~Gsl~r@??h@LVDf@LDcBFi@AUEUQg@EKCI?G?GBG@EBEJKNC??'
ex_nr_samples = 64


class PolylineTestCase(unittest.TestCase):
Expand All @@ -31,7 +29,7 @@ def setUp(self):
m.get('https://maps.googleapis.com/maps/api/elevation/json?path=enc:{}&samples={}'.format(
ex_enc_polyline, ex_nr_samples
), json=ex_elevations, status_code=200)
self.polyline = Polyline(ex_orig, ex_dest, ex_speed)
self.polyline = Polyline(ex_orig, ex_dest)

def test_first_point(self):
self.assertEqual(self.polyline._points[0], ex_orig)
Expand All @@ -40,12 +38,11 @@ def test_last_point(self):
self.assertEqual(self.polyline._points[-1], ex_dest)

def test_nr_of_elevations_returned(self):
total_seconds = self.polyline.get_total_distance() / self.polyline.speed
total_seconds = self.polyline.get_total_distance() / 3
self.assertAlmostEqual(total_seconds, ex_nr_samples, places=0)

def test_total_distance(self):
self.assertEquals(self.polyline.get_total_distance(), ex_total_distance)

def test_get_last_pos(self):
self.assertEquals(self.polyline.get_last_pos(), self.polyline._last_pos)

23 changes: 7 additions & 16 deletions pokemongo_bot/test/polyline_walker_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import unittest, pickle, os
import datetime, time
import os
import pickle
import unittest

from geographiclib.geodesic import Geodesic
from mock import MagicMock, patch, mock

import requests_mock
from pokemongo_bot.walkers.polyline_generator import Polyline
from pokemongo_bot.walkers.polyline_generator import PolylineObjectHandler
from pokemongo_bot.walkers.polyline_walker import PolylineWalker

Expand All @@ -16,10 +16,8 @@
ex_total_distance = 194
ex_resp_directions = 'example_directions.pickle'
ex_resp_elevations = 'example_elevations.pickle'
ex_enc_polyline = 'o_|~Gsl~r@??h@LVDf@LDcBFi@AUEUQg@EKCI?G?GBG@EBEJKNC??'
ex_nr_samples = 78


ex_enc_polyline = 'o_%7C~Gsl~r@??h@LVDf@LDcBFi@AUEUQg@EKCI?G?GBG@EBEJKNC??'
ex_nr_samples = 64


class TestPolylineWalker(unittest.TestCase):
Expand Down Expand Up @@ -55,7 +53,7 @@ def hearbeat():
m.get("https://maps.googleapis.com/maps/api/elevation/json?path=enc:{}&samples={}".format(
ex_enc_polyline, ex_nr_samples
), json=ex_elevations, status_code=200)
self.polyline = PolylineObjectHandler.cached_polyline(ex_orig, ex_dest, ex_speed)
self.polyline = PolylineObjectHandler.cached_polyline(ex_orig, ex_dest)

self.bot.position = [ex_orig[0], ex_orig[1], self.polyline.get_alt(ex_orig)]

Expand All @@ -66,12 +64,11 @@ def tearDown(self):
def test_polyline_fetched(self):
self.assertEqual(self.polyline._points[0], ex_orig)
self.assertEqual(self.polyline._points[-1], ex_dest)
total_seconds = self.polyline.get_total_distance() / self.polyline.speed
total_seconds = self.polyline.get_total_distance() / 3
self.assertAlmostEqual(total_seconds, ex_nr_samples, places=0)
self.assertEquals(self.polyline.get_total_distance(), ex_total_distance)
self.assertEquals(self.polyline.get_last_pos(), self.polyline._last_pos)


def test_one_small_speed(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
Expand Down Expand Up @@ -103,7 +100,6 @@ def run_step(mock_random):
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min


def test_one_small_speed_big_precision(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
Expand Down Expand Up @@ -135,7 +131,6 @@ def run_step(mock_random):
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min


def test_intermediary_speed(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
Expand Down Expand Up @@ -203,8 +198,6 @@ def test_bigger_then_total_speed(self):
walk_min = self.bot.config.walk_min
speed = 300
precision = 0.0
dlat = 47.1700271
dlng = 8.518072999999998

self.bot.config.walk_max = speed
self.bot.config.walk_min = speed
Expand Down Expand Up @@ -234,8 +227,6 @@ def test_bigger_then_total_speed_big_precision_offset(self):
walk_min = self.bot.config.walk_min
speed = 300
precision = 2.5
dlat = 47.1700271
dlng = 8.518072999999998

self.bot.config.walk_max = speed
self.bot.config.walk_min = speed
Expand Down
9 changes: 4 additions & 5 deletions pokemongo_bot/walkers/polyline_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PolylineObjectHandler:
_run = False

@staticmethod
def cached_polyline(origin, destination, speed, google_map_api_key=None):
def cached_polyline(origin, destination, google_map_api_key=None):
'''
Google API has limits, so we can't generate new Polyline at every tick...
'''
Expand All @@ -49,7 +49,7 @@ def cached_polyline(origin, destination, speed, google_map_api_key=None):
PolylineObjectHandler._run = True
PolylineObjectHandler._instability = 20 # next N moves use same cache

PolylineObjectHandler._cache = Polyline(origin, destination, speed, google_map_api_key)
PolylineObjectHandler._cache = Polyline(origin, destination, google_map_api_key)
else:
# valid cache found
PolylineObjectHandler._instability -= 1
Expand All @@ -59,8 +59,7 @@ def cached_polyline(origin, destination, speed, google_map_api_key=None):


class Polyline(object):
def __init__(self, origin, destination, speed, google_map_api_key=None):
self.speed = float(speed)
def __init__(self, origin, destination, google_map_api_key=None):
self.origin = origin
self.destination = tuple(destination)
self.DIRECTIONS_API_URL = 'https://maps.googleapis.com/maps/api/directions/json?mode=walking'
Expand Down Expand Up @@ -94,7 +93,7 @@ def __init__(self, origin, destination, speed, google_map_api_key=None):
self._step_keys = sorted(self._step_dict.keys())
self._last_step = 0

self._nr_samples = int(min(self.get_total_distance() / self.speed + 1, 512))
self._nr_samples = int(max(min(self.get_total_distance() / 3, 512), 2))
self.ELEVATION_API_URL = 'https://maps.googleapis.com/maps/api/elevation/json?path=enc:'
self.ELEVATION_URL = '{}{}&samples={}'.format(self.ELEVATION_API_URL,
self._polyline, self._nr_samples)
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/walkers/polyline_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class PolylineWalker(StepWalker):
def get_next_position(self, origin_lat, origin_lng, origin_alt, dest_lat, dest_lng, dest_alt, distance):
polyline = PolylineObjectHandler.cached_polyline((self.bot.position[0], self.bot.position[1]), (dest_lat, dest_lng), distance, google_map_api_key=self.bot.config.gmapkey)
polyline = PolylineObjectHandler.cached_polyline((self.bot.position[0], self.bot.position[1]), (dest_lat, dest_lng), google_map_api_key=self.bot.config.gmapkey)

while True:
_, (dest_lat, dest_lng) = polyline._step_dict[polyline._step_keys[polyline._last_step]]
Expand Down

0 comments on commit 8b2eb8c

Please sign in to comment.