class_name CharacterServer signal received_packet(packet: Packet) signal logged_in(packet: CharacterServerLoginSuccessPacket) signal requested_character_list(packet: RequestCharacterListSuccessPacket) signal selected_character(packet: CharacterSelectionSuccessPacket) var host: String var port: int = 6121 var stream: PacketPeerStream = PacketPeerStream.new() var peer: StreamPeerTCP = StreamPeerTCP.new() @warning_ignore("shadowed_variable") func _init(host: String, port: int = 6121) -> void: self.host = host self.port = port stream.stream_peer = peer peer.connect_to_host(host, port) peer.poll() ## Emits [signal received_packet]. func listen() -> void: if peer.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED: var raw_packet: PackedByteArray = peer.get_partial_data(2)[1] if raw_packet.size() > 0: raw_packet += peer.get_data(peer.get_available_bytes())[1] var header = raw_packet.decode_u16(0) var packet_type = Constants.PacketDB[header] var packet = packet_type.from_bytes(raw_packet) received_packet.emit(packet) func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Gender): var character_server_login_packet = CharacterServerLoginPacket.new() character_server_login_packet.account_id = account_id character_server_login_packet.login_id1 = login_id1 character_server_login_packet.login_id2 = login_id2 character_server_login_packet.gender = gender peer.put_data(character_server_login_packet.to_bytes()) peer.get_data(4) # in-between packet var packet = await received_packet if packet is CharacterServerLoginSuccessPacket: print(inst_to_dict(packet)) logged_in.emit(packet) func request_character_list(): var request_character_list_packet = RequestCharacterListPacket.new() peer.put_data(request_character_list_packet.to_bytes()) var packet = await received_packet if packet is RequestCharacterListSuccessPacket: print(inst_to_dict(packet), inst_to_dict(packet.character_information[0])) requested_character_list.emit(packet) func select_character(slot: int): var select_character_packet = SelectCharacterPacket.new() select_character_packet.selected_slot = slot peer.put_data(select_character_packet.to_bytes()) var packet = await received_packet if packet is CharacterSelectionSuccessPacket: print(inst_to_dict(packet)) selected_character.emit(packet)