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

Fix step walker and move to fort issues #5407

Closed
wants to merge 1 commit into from
Closed
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
109 changes: 46 additions & 63 deletions pokemongo_bot/cell_workers/move_to_fort.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import time
from geopy.distance import great_circle

from pokemongo_bot import inventory
from pokemongo_bot.constants import Constants
from pokemongo_bot.walkers.walker_factory import walker_factory
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.constants import Constants
from utils import distance, format_dist, fort_details
from datetime import datetime, timedelta


class MoveToFort(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.lure_distance = 0
self.wait_log_sent = None
self.destination = None
self.walker = None

self.lure_attraction = self.config.get("lure_attraction", True)
self.lure_max_distance = self.config.get("lure_max_distance", 2000)
self.ignore_item_count = self.config.get("ignore_item_count", False)
self.walker = self.config.get('walker', 'StepWalker')
self.config_walker = self.config.get('walker', 'StepWalker')
self.wait_at_fort = self.config.get('wait_on_lure', False)
self.wait_log_sent = None

def should_run(self):
has_space_for_loot = inventory.Items.has_space_for_loot()
Expand All @@ -38,79 +43,57 @@ def work(self):
if not self.should_run():
return WorkerResult.SUCCESS

nearest_fort = self.get_nearest_fort()
if self.destination is None:
self.destination = self.get_nearest_fort()

if nearest_fort is None:
return WorkerResult.SUCCESS
if self.destination is not None:
self.walker = walker_factory(self.config_walker, self.bot, self.destination['latitude'], self.destination['longitude'])
self.walker.is_arrived = self.walker_is_arrived
Copy link
Contributor

@th3w4y th3w4y Sep 12, 2016

Choose a reason for hiding this comment

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

You are monkeypatching walker is_arrived method? fine by me if documented 👍
But I wonder how clear is this for everybody...
Could you please at least add a comment in the code...!
(Raise you hands whoever does not know what monkey patching is! :) )
http://cosent.nl/en/blog/monkey-patching-good-bad-ugly

else:
return WorkerResult.SUCCESS

lat = nearest_fort['latitude']
lng = nearest_fort['longitude']
fortID = nearest_fort['id']
details = fort_details(self.bot, fortID, lat, lng)
fortID = self.destination['id']
details = fort_details(self.bot, fortID, self.destination['latitude'], self.destination['longitude'])
fort_name = details.get('name', 'Unknown')

unit = self.bot.config.distance_unit # Unit to use when printing formatted distance

dist = distance(
self.bot.position[0],
self.bot.position[1],
lat,
lng
)
noised_dist = distance(
self.bot.noised_position[0],
self.bot.noised_position[1],
lat,
lng
)
if not self.walker.step():
self.wait_log_sent = None

moving = noised_dist > Constants.MAX_DISTANCE_FORT_IS_REACHABLE if self.bot.config.replicate_gps_xy_noise else dist > Constants.MAX_DISTANCE_FORT_IS_REACHABLE
dist = distance(self.bot.position[0], self.bot.position[1], self.destination['latitude'], self.destination['longitude'])
unit = self.bot.config.distance_unit # Unit to use when printing formatted distance

if moving:
self.wait_log_sent = None
fort_event_data = {
'fort_name': u"{}".format(fort_name),
'distance': format_dist(dist, unit),
}
fort_event_data = {'fort_name': u"{}".format(fort_name),
'distance': format_dist(dist, unit)}

if self.is_attracted() > 0:
fort_event_data.update(lure_distance=format_dist(self.lure_distance, unit))
self.emit_event(
'moving_to_lured_fort',
formatted="Moving towards pokestop {fort_name} - {distance} (attraction of lure {lure_distance})",
data=fort_event_data
)

self.emit_event('moving_to_lured_fort',
formatted="Moving towards pokestop {fort_name} - {distance} (attraction of lure {lure_distance})",
data=fort_event_data)
else:
self.emit_event(
'moving_to_fort',
formatted="Moving towards pokestop {fort_name} - {distance}",
data=fort_event_data
)

step_walker = walker_factory(self.walker,
self.bot,
lat,
lng
)
self.emit_event('moving_to_fort',
formatted="Moving towards pokestop {fort_name} - {distance}",
data=fort_event_data)
elif self.destination.get('active_fort_modifier') and self.wait_at_fort:
now = time.time()

if not step_walker.step():
return WorkerResult.RUNNING
if (self.wait_log_sent is None) or (self.wait_log_sent < now - 60):
self.wait_log_sent = now

self.emit_event('arrived_at_fort',
formatted='Waiting near fort %s until lure module expires' % fort_name)
else:
if nearest_fort.get('active_fort_modifier') and self.wait_at_fort:
if self.wait_log_sent == None or self.wait_log_sent < datetime.now() - timedelta(seconds=60):
self.wait_log_sent = datetime.now()
self.emit_event(
'arrived_at_fort',
formatted='Waiting near fort %s until lure module expires' % fort_name
)
else:
self.emit_event(
'arrived_at_fort',
formatted='Arrived at fort.'
)
self.destination = None

self.emit_event('arrived_at_fort',
formatted='Arrived at fort.')

return WorkerResult.RUNNING

def walker_is_arrived(self):
return great_circle(self.bot.position, (self.walker.dest_lat, self.walker.dest_lng)).meters < Constants.MAX_DISTANCE_FORT_IS_REACHABLE

def _get_nearest_fort_on_lure_way(self, forts):

if not self.lure_attraction:
Expand All @@ -122,7 +105,7 @@ def _get_nearest_fort_on_lure_way(self, forts):

if len(lures):
dist_lure_me = distance(self.bot.position[0], self.bot.position[1],
lures[0]['latitude'],lures[0]['longitude'])
lures[0]['latitude'], lures[0]['longitude'])
else:
dist_lure_me = 0

Expand Down
17 changes: 9 additions & 8 deletions pokemongo_bot/walkers/step_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def __init__(self, bot, dest_lat, dest_lng, dest_alt=None, precision=0.5):

def step(self, speed=None):
now = time.time()
t = 1 - min(now - self.last_update, 1)

sleep(1 - min(now - self.last_update, 1))
self.last_update = now
sleep(t)
self.last_update = now + t

if speed is None:
speed = uniform(self.bot.config.walk_min, self.bot.config.walk_max)
Expand All @@ -54,8 +55,8 @@ def is_arrived(self):
return inverse["s12"] <= self.precision + self.epsilon

def get_next_position(self, origin_lat, origin_lng, origin_alt, dest_lat, dest_lng, dest_alt, distance):
inverse = Geodesic.WGS84.Inverse(origin_lat, origin_lng, dest_lat, dest_lng)
total_distance = inverse["s12"]
line = Geodesic.WGS84.InverseLine(origin_lat, origin_lng, dest_lat, dest_lng)
total_distance = line.s13

if total_distance == 0:
total_distance = self.precision or self.epsilon
Expand All @@ -70,11 +71,11 @@ def get_next_position(self, origin_lat, origin_lng, origin_alt, dest_lat, dest_l
self.saved_location = None
travel = min(total_distance, distance)

direct = Geodesic.WGS84.Direct(origin_lat, origin_lng, inverse["azi1"], travel)
next_lat = direct["lat2"]
next_lng = direct["lon2"]
position = line.Position(travel)
next_lat = position["lat2"]
next_lng = position["lon2"]

random_azi = uniform(inverse["azi1"] - 90, inverse["azi1"] + 90)
random_azi = uniform(line.azi1 - 90, line.azi1 + 90)
random_dist = uniform(0.0, self.precision)
direct = Geodesic.WGS84.Direct(next_lat, next_lng, random_azi, random_dist)

Expand Down