Skip to content

Commit

Permalink
SleepSchedule location handling (#5294)
Browse files Browse the repository at this point in the history
* Use bot functions to handle wake location

* Made wake_up_at_location work on bot start

* Update alt handling

* Update event message

* Fix tests

* Update configuration_files.md
  • Loading branch information
cmezh authored and solderzzc committed Sep 8, 2016
1 parent 815862a commit 770ef92
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ Simulates the user going to sleep every day for some time, the sleep time and th
- enable_reminder: (true | false) enables/disables sleep reminder. Default: false
- reminder_interval: (interval) reminder interval in seconds. Default: 600

- entries: [{}] SleepSchedule entries. Default: []
- enabled: (true | false) see above
- time: (HH:MM) local time that the bot should sleep
- duration: (HH:MM) the duration of sleep
- time_random_offset: (HH:MM) random offset of time that the sleep will start, for this example the possible start times are 11:30-12:30 and 16:45-18:45. Default: 01:00
- duration_random_offset: (HH:MM) random offset of duration of sleep, for this example the possible durations are 5:00-6:00 and 2:30-3:30. Default: 00:30
- wake_up_at_location: (lat, long | lat, long, alt | "") the location at which the bot wake up *Note that an empty string ("") will not change the location*.
- wake_up_at_location: (label | lat, long | lat, long, alt | "") the location at which the bot wake up. You can use location "label" set in favorite_location config. Default: "". *Note that an empty string ("") will not change the location*.


## Configuring Tasks
Expand Down
36 changes: 36 additions & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self, db, config):
self.recent_forts = [None] * config.forts_max_circle_size
self.tick_count = 0
self.softban = False
self.wake_location = None
self.start_position = None
self.last_map_object = None
self.last_time_map_object = 0
Expand Down Expand Up @@ -1107,6 +1108,41 @@ def _set_starting_position(self):
# TODO: Add unit tests
return

if self.wake_location:
msg = "Wake up location found: {location} {position}"
self.event_manager.emit(
'location_found',
sender=self,
level='info',
formatted=msg,
data={
'location': self.wake_location['raw'],
'position': self.wake_location['coord']
}
)

self.api.set_position(*self.wake_location['coord'])

self.event_manager.emit(
'position_update',
sender=self,
level='info',
formatted="Now at {current_position}",
data={
'current_position': self.position,
'last_position': '',
'distance': '',
'distance_unit': ''
}
)

self.start_position = self.position

has_position = True

return


if self.config.location:
location_str = self.config.location
location = self.get_pos_by_name(location_str.replace(" ", ""))
Expand Down
46 changes: 34 additions & 12 deletions pokemongo_bot/sleep_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,32 @@ def work(self):
self._schedule_next_sleep()
if wake_up_at_location:
if hasattr(self.bot, 'api'): # Check if api is already initialized
self.bot.api.set_position(wake_up_at_location[0],wake_up_at_location[1],wake_up_at_location[2])
msg = "Wake up location found: {location} {position}"
self.bot.event_manager.emit(
'location_found',
sender=self,
level='info',
formatted=msg,
data={
'location': wake_up_at_location['raw'],
'position': wake_up_at_location['coord']
}
)

self.bot.api.set_position(*wake_up_at_location['coord'])

self.bot.event_manager.emit(
'position_update',
sender=self,
level='info',
formatted="Now at {current_position}",
data={
'current_position': self.bot.position,
'last_position': '',
'distance': '',
'distance_unit': ''
}
)
else:
self.bot.wake_location = wake_up_at_location
if hasattr(self.bot, 'api'): self.bot.login() # Same here
Expand Down Expand Up @@ -129,18 +154,15 @@ def testkey(entry, key, offset=False, defval=''):
timedelta(
hours=raw_duration_random_offset.hour, minutes=raw_duration_random_offset.minute).total_seconds())

raw_wake_up_at_location = entry['wake_up_at_location'] if 'wake_up_at_location' in entry else ''
raw_wake_up_at_location = entry['wake_up_at_location'] if 'wake_up_at_location' in entry else None
if raw_wake_up_at_location:
try:
wake_up_at_location = raw_wake_up_at_location.split(',',2)
lat=float(wake_up_at_location[0])
lng=float(wake_up_at_location[1])
if len(wake_up_at_location) == 3:
alt=float(wake_up_at_location[2])
else:
alt = uniform(self.bot.config.alt_min, self.bot.config.alt_max)
prepared['wake_up_at_location'] = [lat, lng, alt]
except ValueError:
wake_up_at_location = self.bot.get_pos_by_name(raw_wake_up_at_location)
lat = float(wake_up_at_location[0])
lng = float(wake_up_at_location[1])
alt = float(wake_up_at_location[2]) if wake_up_at_location[2] else uniform(self.bot.config.alt_min, self.bot.config.alt_max)
prepared['wake_up_at_location'] = { 'raw': raw_wake_up_at_location, 'coord': (lat, lng, alt) }
except:
index = config.index(entry)
self.bot.warning('SleepSchedule: error parsing wake_up_at_location in entry %d' % index)

Expand Down Expand Up @@ -208,7 +230,7 @@ def _get_next_sleep_schedule(self):
prev_day_time = next_time - timedelta(days=1)
prev_day_end = next_end - timedelta(days=1)

location = self.entries[index]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[index] else ''
location = self.entries[index]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[index] else None

diff = next_time - now

Expand Down
1 change: 1 addition & 0 deletions tests/location_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def setUp(self):
username='Foobar',
)
self.bot.alt = 8
self.bot.wake_location = None
self.bot.logger = MagicMock(return_value="")
self.bot.updateConfig(config)

Expand Down

0 comments on commit 770ef92

Please sign in to comment.