def connect_second_player(self): self.connect_player() message = OnlineMessage(self.MESSAGES_TO_USER['JoinGame']) players = self.get_player_list() self.send(message.encode(), players[1]) self._server_FSM.handle_event(Event.PLAYER_CONNECTED)
def parse(self, message): header = message.get_header() data = message.get_body() if header == self.MESSAGES_FROM_SERVER['Welcome']: self._outputCon.welcome() elif header == self.MESSAGES_FROM_SERVER['AskMinRange']: self._outputCon.ask_min_range() elif header == self.MESSAGES_FROM_SERVER['GetMinRange']: min_range = self._inputCon.get_min_range() message = OnlineMessage('', min_range) self.send(message) elif header == self.MESSAGES_FROM_SERVER['WrongMinRange']: self._outputCon.wrong_min_range() elif header == self.MESSAGES_FROM_SERVER['AskMaxRange']: self._outputCon.ask_max_range() elif header == self.MESSAGES_FROM_SERVER['GetMaxRange']: min_range = data max_range = self._inputCon.get_max_range(min_range) message = OnlineMessage('', max_range) self.send(message) elif header == self.MESSAGES_FROM_SERVER['WrongMaxRange']: min_range = data self._outputCon.wrong_max_range(min_range) elif header == self.MESSAGES_FROM_SERVER['AskPlayerGuess']: self._outputCon.ask_player_guess() elif header == self.MESSAGES_FROM_SERVER['GetGuess']: min_range, max_range = data[0], data[1] guess = self._inputCon.get_guess(min_range, max_range) message = OnlineMessage('', guess) self.send(message) elif header == self.MESSAGES_FROM_SERVER['WrongGuess']: min_range, max_range = data[0], data[1] self._outputCon.wrong_guess(min_range, max_range) elif header == self.MESSAGES_FROM_SERVER['Less']: self._outputCon.less() elif header == self.MESSAGES_FROM_SERVER['More']: self._outputCon.more() elif header == self.MESSAGES_FROM_SERVER['CongratulateWin']: self._outputCon.congratulate_win() self.sock.close() exit() self.listen()
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 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 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 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)
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 parse(self, message): header = message.get_header() data = message.get_body() if header == self.MESSAGES_FROM_SERVER['AskGame']: self._outputCon.ask_game() type_game = self._inputCon.get_game_name() message = OnlineMessage(type_game) self.send(message) elif header == self.MESSAGES_FROM_SERVER['WrongGame']: self._outputCon.wrong_game_name() elif header == self.MESSAGES_FROM_SERVER['CorrectGame']: type_game = data if type_game == self.MESSAGES_FROM_SERVER['TicTacToe']: self._outputCon.waiting_for_second_player() self._type_game = type_game return None elif header == self.MESSAGES_FROM_SERVER['JoinGame']: self._type_game = 'TIC' return None self.listen()
def parse(self, message): header = message.get_header() data = message.get_body() if header == self.MESSAGES_FROM_SERVER['YouFirst']: self._outputCon.first(data) elif header == self.MESSAGES_FROM_SERVER['YouSecond']: self._outputCon.second(data) elif header == self.MESSAGES_FROM_SERVER['Welcome']: self._outputCon.welcome() elif header == self.MESSAGES_FROM_SERVER['DrawBoard']: board = data self._outputCon.draw_board(board) elif header == self.MESSAGES_FROM_SERVER['PlayerMove']: self._outputCon.player_move(data) elif header == self.MESSAGES_FROM_SERVER['AskCoord']: self._outputCon.ask_coord(data) elif header == self.MESSAGES_FROM_SERVER['GetCoord']: coord = self._inputCon.get_coord(dim) message = OnlineMessage('', coord) self.send(message) elif header == self.MESSAGES_FROM_SERVER['WrongCoord']: self._outputCon.wrong_coord(dim, data) elif header == self.MESSAGES_FROM_SERVER['WrongMove']: self._outputCon.wrong_move() elif header == self.MESSAGES_FROM_SERVER['CongratulateWinner']: self._outputCon.congratulate_winner(data) self.sock.close() exit() elif header == self.MESSAGES_FROM_SERVER['AnnounceDraw']: self._outputCon.announce_draw() self.sock.close() exit() self.listen()
def ask_player_guess(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['AskPlayerGuess']) self._server.send(message.encode(), self._player_list[0])
def wrong_max_range(self, min_range): message = OnlineMessage(self.MESSAGES_TO_PLAYER['WrongMaxRange'], min_range) self._server.send(message.encode(), self._player_list[0])
def ask_max_range(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['AskMaxRange']) self._server.send(message.encode(), self._player_list[0])
def player_move(self, player): message = OnlineMessage(self.MESSAGES_TO_PLAYER['PlayerMove'], player) self._server.send(message.encode(), self._player_list[0]) self._server.send(message.encode(), self._player_list[1])
def welcome(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['Welcome']) self._server.send(message.encode(), self._player_list[0])
def congratulate_win(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['CongratulateWin']) self._server.send(message.encode(), self._player_list[0])
def draw_board(self, board, dim): message = OnlineMessage(self.MESSAGES_TO_PLAYER['DrawBoard'], board) self._server.send(message.encode(), self._player_list[0]) self._server.send(message.encode(), self._player_list[1])
def wrong_guess(self, min_range, max_range): min_max = [min_range, max_range] message = OnlineMessage(self.MESSAGES_TO_PLAYER['WrongGuess'], min_max) self._server.send(message.encode(), self._player_list[0])
def ask_coord(self, coord, player): message = OnlineMessage(self.MESSAGES_TO_PLAYER['AskCoord'], coord) self._server.send(message.encode(), self.actual_player(player))
def wrong_coord(self, dim, player): message = OnlineMessage(self.MESSAGES_TO_PLAYER['WrongCoord'], dim) self._server.send(message.encode(), self.actual_player(player))
def wrong_move(self, player): message = OnlineMessage(self.MESSAGES_TO_PLAYER['WrongMove']) self._server.send(message.encode(), self.actual_player(player))
def announce_draw(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['AnnounceDraw']) self._server.send(message.encode(), self._player_list[0]) self._server.send(message.encode(), self._player_list[1])
def less(self): message = OnlineMessage(self.MESSAGES_TO_PLAYER['Less']) self._server.send(message.encode(), self._player_list[0])
def first_second(self, player1, player2): message = OnlineMessage(self.MESSAGES_TO_PLAYER['YouFirst'], player1) self._server.send(message.encode(), self.actual_player(player1)) message = OnlineMessage(self.MESSAGES_TO_PLAYER['YouSecond'], player2) self._server.send(message.encode(), self.actual_player(player2))