def removePlayer(playerId: int): code = requests.post("http://localhost:5000/post/removeplayer/{}".format( playerId)).status_code if (code != 200): logger.error( "Unable to remove player with id {} from queue. They might be unable to play." .format(playerId))
def addGame(playerId: int): code = requests.post( "http://localhost:5000/post/addgame/{}".format(playerId)).status_code if (code != 200): logger.error( "Unable to find a user with id {}. Game is not counted.".format( playerId))
async def connectPlayer(self, player: Player): while len(self.__players) < 2: await asyncio.sleep(1) try: await player.ping() except ConnectionClosedOK: logger.info("{} has disconnected.".format(player.getAddress())) return except ConnectionClosedError as e: logger.error( "{}: Connection with {} has been terminated. Reason: {}". format(e.code, player.getAddress(), e.reason)) return await self.__start()
async def getBase(player: Player): player.clearMessages() await player.sendMessage("GET_BASE") for _ in range(5): msg = player.getMessageIfReceived() if msg == "OK": return await player.ping() await asyncio.sleep(1) logger.error("{} received a GET_BASE signal but didn't respond!".format(player.getAddress())) await player.sendMessageSafe("KICKED:NO_RESPONSE") await player.disconnect() raise PlayerKickedException(player, "No response")
async def __start(self): if self.__started: # Player 1 comes here and starts the game, await self.__keepConnected( ) # Player 2 goes to __keepConnected() to avoid doing all the tasks twice. self.__started = True logger.info("The game in room #{} has started.".format(str(self.__ID))) player1, player2 = self.__players game = Game(self.__ID) try: await game.start(player1, player2) except ConnectionClosedOK as e: logger.info("{} has disconnected.".format(e.player.getAddress())) await self.__onConnectionClose(e.player) # Ignore these errors. except PlayerKickedException as e: logger.info("{} has been kicked. Reason: {}.".format( e.player.getAddress(), e.reason)) await self.__onConnectionClose(e.player) except ConnectionClosedError as e: logger.error( "{}: Connection with {} has beed terminated. Reason: {}". format(e.code, e.player, e.reason)) # Ignore this error. await self.__onConnectionError() winner = game.getWinner() if (winner == 1): await self.__gameOver(player1, player2) elif (winner == 2): await self.__gameOver(player2, player1) elif (winner == 3): await self.__onDraw() else: await self.__onConnectionError( ) # Theoretically it will never happen.
async def __kick(player): logger.error("{} stopped responding during base getting!".format( player.getAddress())) await player.sendMessageSafe("KICKED:NO_RESPONSE") await player.disconnect() raise PlayerKickedException(player, "No response")