Esempio n. 1
0
    def game_information_spectator(game_id, nickname_white, nickname_black, game_time, piece_list):
        code = 0x0E

        packet = bytearray()
        packet.append(code)
        packet.append(game_id)
        packet.append(PacketFormatter.from_seconds(game_time))
        packet.extend(PacketFormatter.from_string(nickname_white))
        packet.extend(PacketFormatter.from_string(nickname_black))
        packet.extend(PacketFormatter.from_piece_list(piece_list))

        return packet
Esempio n. 2
0
    def game_information(game_id, color, opponent_nickname):
        code = 0x04

        packet = bytearray(struct.pack("3B", code, game_id, color))
        packet.extend(PacketFormatter.from_string(opponent_nickname))

        return packet
Esempio n. 3
0
    def message(message):
        code = 0x0D

        packet = bytearray().append(code)
        packet.extend(PacketFormatter.from_string(message))

        return packet
    def handle(self, data, conn):
        packet_type, packet_code, packet_data = PacketFormatter.process_packet(
            data)

        if packet_type == AUTHENTICATION_TYPE:
            if packet_code == AuthenticationProtocol.request_codes[
                    "authentication"]:
                username, password = None, None

                try:
                    username, password = AuthenticationProtocol.on_authentication(
                        packet_data)
                except InvalidPacket:
                    conn.send(GeneralProtocol.invalid_packet())
                    self.server.close_connection(conn)
                    return

                logging.info(packet_data)
                logging.info("Username: {} ; Password: {}".format(
                    username, password))

                if self.connect(username, password, conn):
                    self.server.selector.unregister(conn)
                    self.server.selector.register(
                        fileobj=conn,
                        events=selectors.EVENT_READ,
                        data=self.server.general_handler.on_read)

                    arguments = (
                        self.server.client_list[conn.fileno()].user_id,
                    )  # Tuple needed
                    _thread.start_new_thread(self.send_connection_signal,
                                             arguments)
Esempio n. 5
0
    def game_over_spectator(winner_nickname):
        code = 0x0C

        packet = bytearray()
        packet.append(code)
        packet.extend(PacketFormatter.from_string(winner_nickname))

        return packet
Esempio n. 6
0
    def game_list_request(data):
        in_progress = data[0]
        nickname = None

        if data[1] > 0:
            nickname = PacketFormatter.to_string(data[1:])

        return in_progress, nickname
Esempio n. 7
0
    def king_in_check(king_id, piece_id_list):
        code = 0x09

        packet = bytearray()
        packet.append(code)
        packet.append(king_id)
        packet.extend(PacketFormatter.from_list(piece_id_list))

        return packet
Esempio n. 8
0
    def move_piece(piece_id, position):
        code = 0x07

        packet = bytearray()
        packet.append(code)
        packet.append(piece_id)
        packet.append(PacketFormatter.from_position(position))

        return packet
Esempio n. 9
0
    def authentication_response(success, nickname=None):
        code = 0x01

        if success:
            packet = bytearray(struct.pack("B?", code, success))
            packet.extend(PacketFormatter.from_string(nickname))
        else:
            packet = bytearray(struct.pack("?", success))

        return packet
Esempio n. 10
0
    def handle(self, data, conn):
        client = self.server.client_list[conn.fileno()]
        user = self.user_manager.get_user_by_id(client.user_id)

        packet_type, packet_code, packet_data = PacketFormatter.process_packet(data)

        if packet_type == GENERAL_TYPE:
            if packet_code == GeneralProtocol.request_codes["friend_list"]:
                online_option = GeneralProtocol.friend_list_request(packet_data)
                friend_list = list()

                if online_option == 0x00:  # Offline users
                    friend_list = self.user_manager.get_friend_list(user.username, connected=False)
                elif online_option == 0x01:  # Online users
                    friend_list = self.user_manager.get_friend_list(user.username, connected=True)
                elif online_option == 0x02:  # Both
                    self.user_manager.get_friend_list(user.username)

                conn.send(GeneralProtocol.friend_list(friend_list))
            elif packet_code == GeneralProtocol.request_codes["game_list_progress"]:
                try:
                    in_progress, nickname = GeneralProtocol.game_list_request(packet_data)
                except InvalidPacket:
                    conn.send(GeneralProtocol.invalid_packet())
                else:
                    self.game_manager.game_list(in_progress, nickname)
            elif packet_code == GeneralProtocol.request_codes["create_game"]:
                try:
                    password = GeneralProtocol.create_game_request(packet_data)
                except InvalidPacket:
                    conn.send(GeneralProtocol.invalid_packet())
                else:


            else:
                conn.send(GeneralProtocol.invalid_packet())
Esempio n. 11
0
    def invalid_packet():
        code = 0x01

        return PacketFormatter.format_response_packet(GENERAL_TYPE, code)
Esempio n. 12
0
    def on_authentication(data):
        username, password = PacketFormatter.to_string(data, 2)

        return username, password
Esempio n. 13
0
    def server_full():
        response_code = 0x06

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code)
Esempio n. 14
0
    def server_not_available():
        response_code = 0x05

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code)
Esempio n. 15
0
    def banned():
        response_code = 0x04

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code)
Esempio n. 16
0
    def already_connected():
        response_code = 0x03

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code)
Esempio n. 17
0
    def bad_credentials():
        response_code = 0x02

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code)
Esempio n. 18
0
    def success(nickname):
        response_code = 0x01
        data = PacketFormatter.from_string(nickname)

        return PacketFormatter.format_response_packet(AUTHENTICATION_TYPE, response_code, data)
Esempio n. 19
0
    def disconnection_signal(user_id):
        code = 0x05
        data = bytearray(struct.pack("Q", user_id))

        return PacketFormatter.format_response_packet(GENERAL_TYPE, code, data)
Esempio n. 20
0
    def game_list(game_list):
        code = 0x03
        data = bytearray(zlib.compress(pickle.dumps(game_list)))

        return PacketFormatter.format_response_packet(GENERAL_TYPE, code, data)
Esempio n. 21
0
    def user_not_found():
        code = 0x06

        return PacketFormatter.format_response_packet(GENERAL_TYPE, code)
Esempio n. 22
0
    def create_game_request(data):
        # TODO: game_name?
        password = PacketFormatter.to_string(data)

        return password
Esempio n. 23
0
    def private_message_request(data):
        nickname, message = PacketFormatter.to_string(data, 2)

        return nickname, message