| 5 | from pokemongo_bot import inventory |
| 6 | |
| 7 | class WebsocketRemoteControl(object): |
| 8 | |
| 9 | |
| 10 | def __init__(self, bot): |
| 11 | self.bot = bot |
| 12 | self.host, port_str = self.bot.config.websocket_server_url.split(':') |
| 13 | self.port = int(port_str) |
| 14 | self.sio = SocketIO(self.host, self.port) |
| 15 | self.sio.on( |
| 16 | 'bot:process_request:{}'.format(self.bot.config.username), |
| 17 | self.on_remote_command |
| 18 | ) |
| 19 | self.thread = threading.Thread(target=self.process_messages) |
| 20 | self.logger = logging.getLogger(type(self).__name__) |
| 21 | |
| 22 | def start(self): |
| 23 | self.thread.start() |
| 24 | return self |
| 25 | |
| 26 | def process_messages(self): |
| 27 | self.sio.wait() |
| 28 | |
| 29 | def on_remote_command(self, command): |
| 30 | name = command['name'] |
| 31 | command_handler = getattr(self, name, None) |
| 32 | if not command_handler or not callable(command_handler): |
| 33 | self.sio.emit( |
| 34 | 'bot:send_reply', |
| 35 | { |
| 36 | 'response': '', |
| 37 | 'command': 'command_not_found', |
| 38 | 'account': self.bot.config.username |
| 39 | } |
| 40 | ) |
| 41 | return |
| 42 | if 'args' in command: |
| 43 | command_handler(*args) |
| 44 | return |
| 45 | command_handler() |
| 46 | |
| 47 | def get_player_info(self): |
| 48 | try: |
| 49 | self.sio.emit( |
| 50 | 'bot:send_reply', |
| 51 | { |
| 52 | 'result': {'inventory': inventory.jsonify_inventory(), 'player': self.bot._player}, |
| 53 | 'command': 'get_player_info', |
| 54 | 'account': self.bot.config.username |
| 55 | } |
| 56 | ) |
| 57 | except Exception as e: |
| 58 | self.logger.error(e) |