Exemplo n.º 1
0
 def reconnected(self, connection: GameConnection):
     """Reattaches the user to a connection and cancels the disconnection kick timers."""
     if self.connection:
         create_task_log_errors(self.connection.replaced())
     self.connection = connection
     self._disconnect_kick_timer.cancel()
     self._disconnect_remove_timer.cancel()
Exemplo n.º 2
0
 async def open(self):
     self.local_addr = f"<direct {id(self)}>"
     self.server_config = self.server.config_json()
     self.send_dispatcher_task = create_task_log_errors(
         self._send_dispatcher())
     self.recv_dispatcher_task = create_task_log_errors(
         self._recv_dispatcher())
     await super().open()
Exemplo n.º 3
0
 async def open(self):
     self.connection = await connect(self.url)
     self.local_addr = str(self.connection.local_address)
     # perform handshake
     await self.connection.send({"version": UI_VERSION})
     config_response = json.loads(await self.connection.recv())
     assert "config" in config_response, "connection handshake failed"
     self.server_config = config_response["config"]
     # continue with connection
     await super().open()
     create_task_log_errors(self._recv_dispatcher())
Exemplo n.º 4
0
async def run_server(stop_condition: Future):
    db_connection.init(config.database.file)
    game_server = GameServer()
    await get_event_loop().run_in_executor(None, game_server.load_local_packs)

    if config.debug.enabled and config.debug.bots.count > 0:
        from pyxyzzy.test.bot import run_bots
        create_task_log_errors(run_bots(game_server, stop_condition))

    async with serve(connection_factory(game_server), config.server.host,
                     config.server.port):
        await stop_condition
Exemplo n.º 5
0
 def receive_json_from_server(self, data: dict):
     """Called by subclasses when they receive a JSON message from the server."""
     if self.failed:
         return
     try:
         if "call_id" in data:
             call = self.calls.pop(data["call_id"])
             if data["error"] is not None:
                 call.future.set_exception(
                     GameError(data["error"], data["description"]))
             else:
                 call.future.set_result(data)
         else:
             if "events" in data:
                 for event in data["events"]:
                     self.bot.handle_event(event)
             self.bot.handle_update(data)
     except Exception:
         LOGGER.error(
             "Error handling message from server to bot, closing connection",
             exc_info=True)
         self.failed = True
         create_task_log_errors(self.close())
Exemplo n.º 6
0
    def _perform_action(self, action: Optional[Awaitable]):
        """Performs a new action, canceling the previous one if any."""
        async def run_action():
            try:
                await action
            except GameError:
                LOGGER.error("Performing action failed, quitting bot %s",
                             self,
                             exc_info=True)
                self.quit()
                return
            else:
                # does not occur if we are cancelled while running
                self._action = ("acted", None)

        if self.action_task:
            self.action_task.cancel()
        if action:
            task = create_task_log_errors(run_action())
            self._action = ("acting", task)
        else:
            self._action = ("not_acted", None)
Exemplo n.º 7
0
 def send_message(self, message: dict):
     if self.connection:
         create_task_log_errors(
             self.connection.send_json_to_client(message))
Exemplo n.º 8
0
 def start_bot():
     new_bot = RandomPlayBot(lambda: DirectBotConnection(server, new_bot))
     LOGGER.info("Starting %s", new_bot)
     running_bots.append(new_bot)
     create_task_log_errors(new_bot.connect())
Exemplo n.º 9
0
 def handle_authenticated(self):
     create_task_log_errors(self.join_or_create_game())
Exemplo n.º 10
0
 def quit(self):
     self.finished = True
     self._perform_action(None)
     create_task_log_errors(self.disconnect())