def test_handle_event_call_connect_first_player_for_start_server_state_start_play_event( self): self.server.connect_first_player = Mock() fsm = ServerTransitionFSM(self.server) fsm.handle_event(Event.START_PLAY) self.assertTrue(fsm.state == State.CONNECT_FIRST_PLAYER) self.server.connect_first_player.assert_called()
def __init__(self): self._server_FSM = ServerTransitionFSM(self) self.TCP_IP = '127.0.0.1' self.TCP_PORT = 5005 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind((self.TCP_IP, self.TCP_PORT)) self._player_list = {}
def test_handle_event_call_disconnect_players_for_play_tic_tac_toe_state_game_finished_event( self): self.server.disconnect_players = Mock() fsm = ServerTransitionFSM(self.server) fsm.state = State.PLAY_TIC_TAC_TOE fsm.handle_event(Event.GAME_FINISHED) self.assertTrue(fsm.state == State.START_SERVER) self.server.disconnect_players.assert_called()
def test_handle_event_call_start_tic_tac_toe_game_for_connect_second_player_state_player_connected_event( self): self.server.start_tic_tac_toe_game = Mock() fsm = ServerTransitionFSM(self.server) fsm.state = State.CONNECT_SECOND_PLAYER fsm.handle_event(Event.PLAYER_CONNECTED) self.assertTrue(fsm.state == State.PLAY_TIC_TAC_TOE) self.server.start_tic_tac_toe_game.assert_called()
def test_handle_event_call_connect_second_player_for_ask_player_game_state_player_choose_tic_tac_toe_event( self): self.server.connect_second_player = Mock() fsm = ServerTransitionFSM(self.server) fsm.state = State.ASK_PLAYER_GAME fsm.handle_event(Event.PLAYER_CHOOSE_TIC_TAC_TOE) self.assertTrue(fsm.state == State.CONNECT_SECOND_PLAYER) self.server.connect_second_player.assert_called()
def test_handle_event_call_start_more_less_game_for_ask_player_game_state_player_choose_more_less_event( self): self.server.start_more_less_game = Mock() fsm = ServerTransitionFSM(self.server) fsm.state = State.ASK_PLAYER_GAME fsm.handle_event(Event.PLAYER_CHOOSE_MORE_LESS) self.assertTrue(fsm.state == State.PLAY_MORE_LESS) self.server.start_more_less_game.assert_called()
def test_handle_event_call_ask_game_for_connect_first_player_state_player_connected_event( self): self.server.ask_game = Mock() fsm = ServerTransitionFSM(self.server) fsm.state = State.CONNECT_FIRST_PLAYER fsm.handle_event(Event.PLAYER_CONNECTED) self.assertTrue(fsm.state == State.ASK_PLAYER_GAME) self.server.ask_game.assert_called()
class Server: MESSAGES_TO_USER = {'AskGame': 'AG', 'WrongGame': 'WG', 'CorrectGame': 'CG', 'JoinGame': 'JG'} GAME_LIST = {'ML': Event.PLAYER_CHOOSE_MORE_LESS, 'TIC': Event.PLAYER_CHOOSE_TIC_TAC_TOE} def __init__(self): self._server_FSM = ServerTransitionFSM(self) self.TCP_IP = '127.0.0.1' self.TCP_PORT = 5005 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind((self.TCP_IP, self.TCP_PORT)) self._player_list = {} def start_server(self): self._server_FSM.handle_event(Event.START_PLAY) def connect_first_player(self): self.connect_player() self._server_FSM.handle_event(Event.PLAYER_CONNECTED) 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 connect_player(self): self.sock.listen(0) number_of_players = len(self._player_list) while len(self._player_list) == number_of_players: print("wait for client") conn, address = self.sock.accept() self._player_list["player" + str(number_of_players+1)] =\ Players(address, 'Player' + str(number_of_players), conn) 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 start_more_less_game(self): game = MoreLessGame(self) game.start_game() self._server_FSM.handle_event(Event.GAME_FINISHED) def start_tic_tac_toe_game(self): game = TicTacToeGame(self) game.start_game() self._server_FSM.handle_event(Event.GAME_FINISHED) def disconnect_players(self): for player in self._player_list.keys(): self.close_connection(player) self._player_list.clear() self._server_FSM.handle_event(Event.START_PLAY) def close_connection(self, player): self._player_list[player].conn.close() def get_player_list(self): return list(self._player_list.keys()) 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 get(self, player): message = None try: message = self._player_list[player].conn.recv(512) 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() return message