示例#1
0
    def server_on_message(self, message):
        """
        When the server receive a message
        :param message: The message receive
        :return: None
        """
        message = message.split(PARAMETER_SEPARATOR)

        if message[0] == MESSAGE_NEW_TOKEN and len(message) == 3:
            if self.game.current_turn == TokenState.TokenState.Player_1:
                self.syn_client()

            try:
                x = int(message[1])
                y = int(message[2])

                if 0 <= x < self.game.grid_width and \
                        0 <= y < self.game.grid_height:

                    if not self.add_token_with_coord(x, y)[0]:
                        Server.warn("Can't place a token at this coord", "GamePanel")
                        self.syn_client()
                else:
                    Server.warn("X or y is not valid", "GamePanel")
                    self.syn_client()
            except ValueError:
                Server.warn("X or y coord NaN", "GamePanel")
                self.syn_client()

        elif message[0] == MESSAGE_SYN:
            self.syn_client()
示例#2
0
    def client_on_message(self, message):
        """
        When the client receive a message
        :param message: The message receive
        :return: None
        """
        message = message.split(PARAMETER_SEPARATOR)

        if message[0] == MESSAGE_SET_TURN and len(message) == 2:
            self.game.current_turn = TokenState.TokenState(int(message[1]))
            self.update_turn_label()

        elif message[0] == MESSAGE_NEW_TOKEN and len(message) == 3:
            if self.game.current_turn == TokenState.TokenState.Player_2:
                self.syn_client()
                return

            try:
                x = int(message[1])
                y = int(message[2])

                if 0 <= x < self.game.grid_width and \
                        0 <= y < self.game.grid_height:

                    if not self.add_token_with_coord(x, y)[0]:
                        Server.warn("Can't place a token at this coord", "GamePanel")
                        self.syn_client()
                else:
                    Server.warn("X or y is not valid", "GamePanel")
                    self.syn_client()
            except ValueError:
                Server.warn("X or y coord NaN", "GamePanel")
                self.syn_client()

        elif message[0] == MESSAGE_BACK_MENU:
            from UI.ServerGameConfigurationPanel import ServerGameConfigurationPanel
            self.ui.change_panel(ServerGameConfigurationPanel, is_server=False)

        elif message[0] == MESSAGE_RESTART_GAME:
            self.button_restart_command()

        elif message[0] == MESSAGE_SYN and 3 < len(message) < 6:
            if message[1] == SYN_GRID:
                try:
                    current_token = TokenState.TokenState(int(message[2]))
                    if self.game.current_turn != current_token:
                        self.game.current_turn = current_token
                        self.update_turn_label()
                except ValueError:
                    pass

                split = message[3].split(SUB_SEPARATOR)
                if len(split) == self.game.grid_width and len(split[0]) == self.game.grid_height:
                    for x, line in enumerate(split):
                        for y, e in enumerate(line):
                            token = TokenState.TokenState(int(e))
                            if self.game.grid[x][y] != token:
                                self.game.grid[x][y] = token
                                self.update_image(x, y)
            else:
                try:
                    self.game.winner = TokenState.TokenState(int(message[2]))
                except ValueError:
                    pass

                scores = message[3].split(SUB_SEPARATOR)
                if len(scores) == 2:
                    scores[0], scores[1] = int(scores[0]), int(scores[1])
                    self.game.score = scores

                token_coord = message[4].split(SUB_SEPARATOR)  # xy|xy
                if len(token_coord) == 2 and len(token_coord[0]) == 2:
                    for i, c in enumerate(token_coord):
                        for j, xy in enumerate(c):
                            self.game.win_tokens_coord[i][j] = xy

                    self.game.win_tokens_coord = token_coord

                self.update_win()