def get_min_range(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['GetMinRange']) self._server.send(message.encode(), self._player_list[0]) message.decode(self._server.get(self._player_list[0])) try: min_range = int(message.get_body()) except ValueError: return None return min_range
def get_coord(self, dim, player): message = OnlineMessage(self.MESSAGES_TO_PLAYER['GetCoord']) self._server.send(message.encode(), self.actual_player(player)) message.decode(self._server.get(self.actual_player(player))) try: coord = int(message.get_body()) except ValueError: return False if 1 <= coord <= dim: return coord return False
def get_guess(self, min_range, max_range): min_max = [min_range, max_range] message = OnlineMessage(self.MESSAGES_TO_PLAYER['GetGuess'], min_max) self._server.send(message.encode(), self._player_list[0]) message.decode(self._server.get(self._player_list[0])) try: guess = int(message.get_body()) except ValueError: return None if min_range <= guess <= max_range: return guess return None
def send(self, message, player): try: self._player_list[player].conn.sendall(message) ack = OnlineMessage() ack.decode(self.get(player)) if ack.get_header() != 'ACK': raise ValueError except ConnectionResetError: print("Connection terminated") for player in self._player_list.keys(): self.close_connection(player) exit() except ConnectionAbortedError: print("Connection terminated") for player in self._player_list.keys(): self.close_connection(player) exit()
def ask_game(self): players = self.get_player_list() message = OnlineMessage() type_game = None while type_game not in self.GAME_LIST: message = OnlineMessage(self.MESSAGES_TO_USER['AskGame']) self.send(message.encode(), players[0]) message.decode(self.get(players[0])) type_game = message.get_header() if type_game not in self.GAME_LIST: message.set_header(self.MESSAGES_TO_USER['WrongGame']) self.send(message.encode(), players[0]) message.set_header(self.MESSAGES_TO_USER['CorrectGame']) message.set_body(type_game) self.send(message.encode(), players[0]) self._server_FSM.handle_event(self.GAME_LIST[type_game])
def listen(self): data = None try: data = self.sock.recv(512) if data == b'': print("Server closed connection") exit() except ConnectionResetError: print("Breaking communication with the server") exit() except socket.timeout: print("No response") exit() message = OnlineMessage() message.decode(data) ack = OnlineMessage('ACK') self.send(ack) self.parse(message)
def listen(self): data = None try: data = self._sock.recv(512) if data == b'': self._outputCon.server_close_connection() exit() except ConnectionAbortedError: self._outputCon.breaking_connection_server() exit() except ConnectionResetError: self._outputCon.breaking_connection_server() exit() except socket.timeout: self._outputCon.no_response() exit() message = OnlineMessage() message.decode(data) ack = OnlineMessage('ACK') self.send(ack) self.parse(message)