-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
big refactor to Stepper class #737
Changes from all commits
5ec8719
502cfd7
b8e1601
bc18472
b2f400d
65c1a28
6ba3c43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,12 @@ | |
import logger | ||
import re | ||
from pgoapi import PGoApi | ||
from pgoapi.utilities import f2i, h2f | ||
from cell_workers import PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker | ||
from cell_workers.utils import distance | ||
from cell_workers.utils import distance, get_cellid, encode | ||
from step_walker import StepWalker | ||
from human_behaviour import sleep | ||
from stepper import Stepper | ||
from spiral_navigator import SpiralNavigator | ||
from geopy.geocoders import GoogleV3 | ||
from math import radians, sqrt, sin, cos, atan2 | ||
from item_list import Item | ||
|
@@ -30,13 +32,45 @@ def __init__(self, config): | |
def start(self): | ||
self._setup_logging() | ||
self._setup_api() | ||
self.stepper = Stepper(self) | ||
self.step_walker = StepWalker(self) | ||
self.navigator = SpiralNavigator(self) | ||
random.seed() | ||
|
||
def take_step(self): | ||
self.stepper.take_step() | ||
|
||
def work_on_cell(self, cell, position, include_fort_on_path): | ||
location = self.navigator.take_step() | ||
cells = self.find_close_cells(*location) | ||
|
||
for cell in cells: | ||
self.work_on_cell(cell, location) | ||
|
||
def find_close_cells(self, lat, lng): | ||
cellid = get_cellid(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() | ||
map_objects = response_dict.get('responses', {}).get('GET_MAP_OBJECTS', {}) | ||
status = map_objects.get('status', None) | ||
|
||
map_cells = [] | ||
if status and status == 1: | ||
map_cells = map_objects['map_cells'] | ||
position = (lat, lng, 0) | ||
map_cells.sort( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be wrong but you dont need to do key= |
||
key=lambda x: distance( | ||
lat, | ||
lng, | ||
x['forts'][0]['latitude'], | ||
x['forts'][0]['longitude']) if x.get('forts', []) else 1e6 | ||
) | ||
return map_cells | ||
|
||
def work_on_cell(self, cell, position): | ||
if self.config.evolve_all: | ||
# Run evolve all once. Flip the bit. | ||
print('[#] Attempting to evolve all pokemons ...') | ||
|
@@ -78,7 +112,7 @@ def work_on_cell(self, cell, position, include_fort_on_path): | |
if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS: | ||
break | ||
if (self.config.mode == "all" or | ||
self.config.mode == "farm") and include_fort_on_path: | ||
self.config.mode == "farm"): | ||
if 'forts' in cell: | ||
# Only include those with a lat/long | ||
forts = [fort | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am i missing something or is this missing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's on the next line:
|
||
|
@@ -90,6 +124,7 @@ def work_on_cell(self, cell, position, include_fort_on_path): | |
# build graph & A* it | ||
forts.sort(key=lambda x: distance(self.position[ | ||
0], self.position[1], x['latitude'], x['longitude'])) | ||
|
||
for fort in forts: | ||
worker = MoveToFortWorker(fort, self) | ||
worker.work() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import json | ||
import time | ||
import pprint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We dont use this import |
||
|
||
from math import ceil | ||
from s2sphere import CellId, LatLng | ||
from google.protobuf.internal import encoder | ||
|
||
from human_behaviour import sleep, random_lat_long_delta | ||
from cell_workers.utils import distance, i2f, format_time, format_dist | ||
|
||
from pgoapi.utilities import f2i, h2f | ||
import logger | ||
|
||
|
||
class SpiralNavigator(object): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
self.api = bot.api | ||
self.config = bot.config | ||
|
||
self.pos = 1 | ||
self.x = 0 | ||
self.y = 0 | ||
self.dx = 0 | ||
self.dy = -1 | ||
self.steplimit = self.config.max_steps | ||
self.steplimit2 = self.steplimit**2 | ||
self.origin_lat = self.bot.position[0] | ||
self.origin_lon = self.bot.position[1] | ||
|
||
def take_step(self): | ||
position = (self.origin_lat, self.origin_lon, 0.0) | ||
|
||
logger.log('[#] Scanning area for objects....') | ||
# logger.log('[#] Scanning area for objects ({} / {})'.format( | ||
# (step + 1), self.steplimit**2)) | ||
if self.config.debug: | ||
logger.log( | ||
'steplimit: {} x: {} y: {} pos: {} dx: {} dy {}'.format( | ||
self.steplimit2, self.x, self.y, self.pos, self.dx, | ||
self.dy)) | ||
# Scan location math | ||
|
||
if -self.steplimit2 / 2 < self.x <= self.steplimit2 / 2 and -self.steplimit2 / 2 < self.y <= self.steplimit2 / 2: | ||
position = (self.x * 0.0025 + self.origin_lat, | ||
self.y * 0.0025 + self.origin_lon, 0) | ||
if self.config.walk > 0: | ||
|
||
dist = distance( | ||
i2f(self.api._position_lat), | ||
i2f(self.api._position_lng), | ||
position[0], | ||
position[1] | ||
) | ||
|
||
logger.log('[#] Walking from ' + str((i2f(self.api._position_lat), i2f( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the format instead of + There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we continue on using the .format convention? |
||
self.api._position_lng))) + " to " + str((str(position[0:2]))) + " " + format_dist(dist, self.config.distance_unit)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we do str twice |
||
self.bot.step_walker.step(self.config.walk, *position[0:2]) | ||
else: | ||
self.api.set_position(*position) | ||
if self.x == self.y or self.x < 0 and self.x == -self.y or self.x > 0 and self.x == 1 - self.y: | ||
(self.dx, self.dy) = (-self.dy, self.dx) | ||
|
||
(self.x, self.y) = (self.x + self.dx, self.y + self.dy) | ||
sleep(10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any context in to why we sleep for so long here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return position[0:2] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import logger | ||
|
||
from cell_workers.utils import distance, i2f, format_time | ||
from human_behaviour import random_lat_long_delta, sleep | ||
from math import ceil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You dont use this import |
||
|
||
|
||
class StepWalker(object): | ||
|
||
def __init__(self, bot): | ||
self.bot = bot | ||
self.api = bot.api | ||
|
||
def step(self, speed, lat, lng): | ||
if self.api._position_lat == lat and self.api._position_lng == lng: | ||
return True | ||
|
||
dLat = (lat - i2f(self.api._position_lat)) | ||
dLng = (lng - i2f(self.api._position_lng)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just change this to |
||
|
||
cLat = i2f(self.api._position_lat) + dLat + random_lat_long_delta() | ||
cLng = i2f(self.api._position_lng) + dLng + random_lat_long_delta() | ||
|
||
self.api.set_position(cLat, cLng, 0) | ||
self.bot.heartbeat() | ||
sleep(1) # sleep one second plus a random delta | ||
# self._work_at_position( | ||
# i2f(self.api._position_lat), i2f(self.api._position_lng), | ||
# alt, False) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just do
map_objects.get('status')
andif status == 1:
the second change isnt needed