Skip to content

Commit

Permalink
- debug improvements for MoveToMap (#4860)
Browse files Browse the repository at this point in the history
- fix for Telegram to accept "@username" as "master", too, along with numeric  IDs
  • Loading branch information
DBa2016 authored and mjmadsen committed Aug 28, 2016
1 parent 1b90f3f commit a01f4dd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
8 changes: 8 additions & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def _register_events(self):
self.event_manager.register_event('set_start_location')
self.event_manager.register_event('load_cached_location')
self.event_manager.register_event('location_cache_ignored')

self.event_manager.register_event('debug')

# ignore candy above threshold
self.event_manager.register_event(
Expand Down Expand Up @@ -592,6 +594,12 @@ def _register_events(self):
'moving_to_pokemon_throught_fort',
parameters=('fort_name', 'distance','poke_name','poke_dist')
)
self.event_manager.register_event(
'move_to_map_pokemon',
parameters=('message')
)



# cached recent_forts
self.event_manager.register_event('loaded_cached_forts')
Expand Down
25 changes: 19 additions & 6 deletions pokemongo_bot/cell_workers/move_to_map_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Moves a trainer to a Pokemon.
Events:
move_to_map_pokemon
When a generic message is logged
Returns:
message: Log message.
move_to_map_pokemon_fail
When the worker fails.
Returns:
Expand Down Expand Up @@ -83,7 +88,7 @@
SNIPE_MAX_IN_CHAIN = 2

# Don't call sniper every time in workers
SNIPE_SKIP_IN_ROUND = 30
SNIPE_SKIP_IN_ROUND = 5

DEBUG_ON = False

Expand Down Expand Up @@ -130,7 +135,13 @@ def get_pokemon_from_social(self):
pokemon['is_vip'] = pokemon['name'] in self.bot.config.vips

if pokemon['name'] not in self.config['catch']:
if DEBUG_ON:
self._emit_failure("Not catching {}".format(pokemon['name']))
continue
else:
if DEBUG_ON:
self._emit_log("Catching {}".format(pokemon['name']))


if self.was_caught(pokemon):
continue
Expand All @@ -144,7 +155,7 @@ def get_pokemon_from_social(self):
pokemon['longitude'],
)

if pokemon['dist'] > self.config['max_distance'] or not self.config['snipe']:
if pokemon['dist'] > self.config['max_distance'] and not self.config['snipe']:

This comment has been minimized.

Copy link
@Jasperrr91

Jasperrr91 Aug 30, 2016

Contributor

God damnit, stop messing with this logic. This is the third time someone breaks it.

continue

# pokemon not reachable with mean walking speed (by config)
Expand Down Expand Up @@ -300,14 +311,16 @@ def work(self):

if (pokeballs_quantity + superballs_quantity + ultraballs_quantity) < self.min_ball:
if DEBUG_ON:
print 'no enough balls'
self._emit_log("Not enough balls to start sniping (have {}, {} needed)".format(pokeballs_quantity + superballs_quantity + ultraballs_quantity, self.min_ball))
return WorkerResult.SUCCESS

self.dump_caught_pokemon()
if self.bot.config.enable_social:
if self.config['snipe']:
self.by_pass_times = self.by_pass_times + 1
if self.by_pass_times < SNIPE_SKIP_IN_ROUND:
if DEBUG_ON:
self._emit_log("Skipping pass {}".format(self.by_pass_times))
return WorkerResult.SUCCESS
self.by_pass_times = 0
pokemon_list = self.get_pokemon_from_social()
Expand All @@ -323,12 +336,12 @@ def work(self):

if len(pokemon_list) < 1:
if DEBUG_ON:
print 'No enough pokemon in list to snip'
self._emit_log("No pokemons in list to snipe")
return WorkerResult.SUCCESS

pokemon = pokemon_list[0]
if DEBUG_ON:
print 'How many pokemon in list: {}'.format(len(pokemon_list))
self._emit_log('How many pokemon in list: {}'.format(len(pokemon_list)))
if self.config['snipe']:
if self.snipe_high_prio_only:
count = 0
Expand All @@ -342,7 +355,7 @@ def work(self):
time.sleep(SNIPE_SLEEP_SEC*5)
else:
if DEBUG_ON:
print 'this pokemon is not good enough to snip {}'.format(pokemon)
self._emit_log('this pokemon is not good enough to snipe {}'.format(pokemon))
return WorkerResult.SUCCESS
else:
return self.snipe(pokemon)
Expand Down
17 changes: 16 additions & 1 deletion pokemongo_bot/cell_workers/telegram_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.event_handlers import TelegramHandler

from pprint import pprint
import re

class TelegramTask(BaseTask):
SUPPORTED_TASK_API_VERSION = 1
update_id = None
Expand Down Expand Up @@ -37,8 +41,19 @@ def work(self):
self.update_id = update.update_id+1
if update.message:
self.logger.info("message from {} ({}): {}".format(update.message.from_user.username, update.message.from_user.id, update.message.text))
if self.config.get('master',None) and self.config.get('master',None)<>update.message.from_user.id:
if self.config.get('master',None) and self.config.get('master',None) not in [update.message.from_user.id, "@{}".format(update.message.from_user.username)]:
self.emit_event(
'debug',
formatted="Master wrong: expecting {}, got {}({})".format(self.config.get('master',None), update.message.from_user.username, update.message.from_user.id))
continue
else:
if not re.match(r'^[0-9]+$', "{}".format(self.config['master'])): # master was not numeric...
self.config['master'] = update.message.chat_id
idx = (i for i,v in enumerate(self.bot.event_manager._handlers) if type(v) is TelegramHandler).next()
self.bot.event_manager._handlers[idx] = TelegramHandler(self.tbot,self.config['master'], self.config.get('alert_catch'))



if update.message.text == "/info":
stats = self._get_player_stats()
if stats:
Expand Down
17 changes: 13 additions & 4 deletions pokemongo_bot/event_handlers/telegram_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from pokemongo_bot.event_manager import EventHandler
import thread
import re

DEBUG_ON = False

Expand All @@ -9,15 +10,23 @@ def __init__(self, tbot,master,pokemons):
self.tbot = tbot
self.master=master
self.pokemons=pokemons
self.whoami="TelegramHandler"

def handle_event(self, event, sender, level, formatted_msg, data):
if self.master:
if not re.match(r'^[0-9]+$', str(self.master)):

This comment has been minimized.

Copy link
@askovpen

askovpen Aug 28, 2016

Contributor

self.master can be '@username' or 12345. why this rules?

This comment has been minimized.

Copy link
@askovpen

askovpen Aug 28, 2016

Contributor

https://pythonhosted.org/python-telegram-bot/telegram.html#telegram.Bot.send_message

chat_id (str) – Unique identifier for the target chat or username of the target channel (in the format @channelusername).

return
master = self.master

if event == 'level_up':
self.tbot.sendMessage(chat_id=self.master, parse_mode='Markdown', text="level up ({})".format(data["current_level"]))
msg = "level up ({})".format(data["current_level"])
elif event == 'pokemon_caught':
if data["pokemon"] in self.pokemons or self.pokemons[0]=="all":
self.tbot.sendMessage(chat_id=self.master, parse_mode='Markdown',
text="Caught {} CP: {}, IV: {}".format(data["pokemon"],data["cp"],data["iv"])
)
msg = "Caught {} CP: {}, IV: {}".format(data["pokemon"],data["cp"],data["iv"])
else:
return
else:
return
self.tbot.sendMessage(chat_id=master, parse_mode='Markdown', text=msg)


0 comments on commit a01f4dd

Please sign in to comment.