From 3436e9c790a1182802ab2ec422a9457b47e2667a Mon Sep 17 00:00:00 2001 From: supercourgette Date: Sun, 21 Aug 2016 16:07:55 +0900 Subject: [PATCH] Number of passage max in the follow path task (#4330) * Correct sort on keep best custom * Some test on follow path * Number lap max in followpath * Sleep at end of follow path * Add restart timer to follow path * Add example for config path * Update wiki for follow path, number of lap * Add login back up after pause * Forget comma in config path example --- configs/config.json.path.example | 7 ++- docs/configuration_files.md | 14 +++++ pokemongo_bot/__init__.py | 16 +++++ pokemongo_bot/cell_workers/follow_path.py | 60 +++++++++++++++---- pokemongo_bot/cell_workers/utils.py | 18 ++++++ .../event_handlers/colored_logging_handler.py | 2 + 6 files changed, 105 insertions(+), 12 deletions(-) diff --git a/configs/config.json.path.example b/configs/config.json.path.example index 7612b5a264..4d354fff1f 100644 --- a/configs/config.json.path.example +++ b/configs/config.json.path.example @@ -143,7 +143,10 @@ "config": { "path_mode": "loop", "path_start_mode": "first", - "path_file": "configs/path.example.json" + "path_file": "configs/path.example.json", + "number_lap": 10, + "timer_restart_min": "00:10:00", + "timer_restart_max": "00:20:00" } } ], @@ -192,4 +195,4 @@ "// Example of keeping the 2 strongest (based on CP) and 3 best (based on IV) Zubat:": {}, "// Zubat": {"keep_best_cp": 2, "keep_best_iv": 3} } -} +} \ No newline at end of file diff --git a/docs/configuration_files.md b/docs/configuration_files.md index 8c8a767441..141fa3e3e5 100644 --- a/docs/configuration_files.md +++ b/docs/configuration_files.md @@ -217,6 +217,20 @@ Setting the `navigator.type` setting to `path` allows you to specify waypoints w An example for a JSON file can be found in `configs/path.example.json`. GPX files can be exported from many online tools, such as gpsies.com.The bot loads the first segment of the first track. +#### Number of Laps + +In the path navigator configuration task, add a maximum of passage above which the bot stop for a time before starting again. *Note that others tasks (such as SleepSchedule or RandomPause) can stop the bot before.* + +- number_lap set-up the number of passage. **To allow for an infinity number of laps, set-up the number at -1**. + +`"number_lap": 10` (will do 10 passages before stopping) +- timer_restart_min is the minimum time the bot stop before starting again (format Hours:Minutes:Seconds). + +`"timer_restart_min": "00:10:00"` (will stop for a minimum of 10 minutes) +- timer_restart_max is the maximum time the bot stop before starting again (format Hours:Minutes:Seconds). + +`"timer_restart_max": "00:20:00"` (will stop for a maximum of 10 minutes) + ## Pokemon Nicknaming A `nickname_template` can be specified for the `NicknamePokemon` task to allow a nickname template to be applied to all pokemon in the user's inventory. For example, a user wanting all their pokemon to have their IV values as their nickname could use a template `{iv_ads}`, which will cause their pokemon to be named something like `13/7/12` (depending on the pokemon's actual IVs). diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index bd0a5ea102..fa0cadfadd 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -165,6 +165,22 @@ def _register_events(self): 'distance_unit' # optional ) ) + self.event_manager.register_event( + 'path_lap_update', + parameters=( + 'number_lap', + 'number_lap_max' + ) + ) + self.event_manager.register_event( + 'path_lap_end', + parameters=( + 'duration', + 'resume' + ) + ) + + self.event_manager.register_event('location_cache_error') self.event_manager.register_event('bot_start') diff --git a/pokemongo_bot/cell_workers/follow_path.py b/pokemongo_bot/cell_workers/follow_path.py index bec447cf6b..afb6d099ce 100644 --- a/pokemongo_bot/cell_workers/follow_path.py +++ b/pokemongo_bot/cell_workers/follow_path.py @@ -9,7 +9,8 @@ from pokemongo_bot.walkers.step_walker import StepWalker from pgoapi.utilities import f2i from random import uniform - +from utils import getSeconds +from datetime import datetime as dt, timedelta class FollowPath(BaseTask): SUPPORTED_TASK_API_VERSION = 1 @@ -28,7 +29,16 @@ def _process_config(self): self.path_file = self.config.get("path_file", None) self.path_mode = self.config.get("path_mode", "linear") self.path_start_mode = self.config.get("path_start_mode", "first") - + self.number_lap_max = self.config.get("number_lap", -1) # if < 0, then the number is inf. + self.timer_restart_min = getSeconds(self.config.get("timer_restart_min", "00:20:00")) + self.timer_restart_max = getSeconds(self.config.get("timer_restart_max", "02:00:00")) + + if self.timer_restart_min > self.timer_restart_max: + raise ValueError('path timer_restart_min is bigger than path timer_restart_max') #TODO there must be a more elegant way to do it... + + #var not related to configs + self.number_lap = 0 + def load_path(self): if self.path_file is None: raise RuntimeError('You need to specify a path file (json or gpx)') @@ -100,6 +110,23 @@ def find_closest_point_idx(self, points): return return_idx + def endLaps(self): + duration = int(uniform(self.timer_restart_min, self.timer_restart_max)) + resume = dt.now() + timedelta(seconds=duration) + + self.emit_event( + 'path_lap_end', + formatted="Great job, lot of calories burned! Taking a break now for {duration}, will resume at {resume}.", + data={ + 'duration': str(timedelta(seconds=duration)), + 'resume': resume.strftime("%H:%M:%S") + } + ) + + self.number_lap = 0 # at the end of the break, start again + sleep(duration) + self.bot.login() + def work(self): last_lat = self.bot.api._position_lat last_lng = self.bot.api._position_lng @@ -132,14 +159,6 @@ def work(self): lng ) - if dist <= 1 or (self.bot.config.walk_min > 0 and is_at_destination): - if (self.ptr + 1) == len(self.points): - self.ptr = 0 - if self.path_mode == 'linear': - self.points = list(reversed(self.points)) - else: - self.ptr += 1 - self.emit_event( 'position_update', formatted="Walking from {last_position} to {current_position}, distance left: ({distance} {distance_unit}) ..", @@ -150,4 +169,25 @@ def work(self): 'distance_unit': 'm' } ) + + if dist <= 1 or (self.bot.config.walk_min > 0 and is_at_destination): + if (self.ptr + 1) == len(self.points): + self.ptr = 0 + if self.path_mode == 'linear': + self.points = list(reversed(self.points)) + if self.number_lap_max >= 0: + self.number_lap+=1 + self.emit_event( + 'path_lap_update', + formatted="number lap : {number_lap} / {number_lap_max}", + data={ + 'number_lap': str(self.number_lap), + 'number_lap_max': str(self.number_lap_max) + } + ) + if self.number_lap >= self.number_lap_max: + self.endLaps() + else: + self.ptr += 1 + return [lat, lng] diff --git a/pokemongo_bot/cell_workers/utils.py b/pokemongo_bot/cell_workers/utils.py index 99fa2b23e2..452176e687 100644 --- a/pokemongo_bot/cell_workers/utils.py +++ b/pokemongo_bot/cell_workers/utils.py @@ -9,6 +9,8 @@ import networkx as nx import numpy as np +from datetime import datetime as dt, timedelta + init() TIME_PERIODS = ( @@ -119,6 +121,22 @@ def format_dist(distance, unit): return dist_to_str(convert(distance, 'm', unit), unit) +def getSeconds(strTime): + ''' + Return the duration in seconds of a time string + :param strTime: string time of format %H:%M:%S + ''' + try: + x = dt.strptime(strTime, '%H:%M:%S') + seconds = int(timedelta(hours=x.hour,minutes=x.minute,seconds=x.second).total_seconds()) + except ValueError: + seconds = 0; + + if seconds < 0: + seconds = 0; + + return seconds + def format_time(seconds): # Return a string displaying the time given as seconds or minutes num, duration = 0, long(round(seconds)) diff --git a/pokemongo_bot/event_handlers/colored_logging_handler.py b/pokemongo_bot/event_handlers/colored_logging_handler.py index 0bdff6d2b8..8fe8559dc7 100644 --- a/pokemongo_bot/event_handlers/colored_logging_handler.py +++ b/pokemongo_bot/event_handlers/colored_logging_handler.py @@ -60,6 +60,7 @@ class ColoredLoggingHandler(EventHandler): 'unknown_spin_result': 'red', 'unset_pokemon_nickname': 'red', 'vip_pokemon': 'red', + 'path_lap_end': 'green', 'log_stats': 'magenta', 'show_inventory': 'magenta', @@ -86,6 +87,7 @@ class ColoredLoggingHandler(EventHandler): 'pokestop_out_of_range': 'white', 'polyline_request': 'white', 'position_update': 'white', + 'path_lap_update': 'white', 'set_start_location': 'white', 'softban_fix': 'white', 'softban_log': 'magenta',