diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-11-29 21:54:32 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-11-29 21:54:32 +0100 |
commit | 313afffce7a823c30acf4853b083f9ca5132e879 (patch) | |
tree | 5a7370392ebd39314612166b35e6f53e67441c0b /network/character_server.gd | |
parent | e8aa9f50886b66d525307a58d71036e6fd3d4aa1 (diff) |
next commit
Diffstat (limited to 'network/character_server.gd')
-rw-r--r-- | network/character_server.gd | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/network/character_server.gd b/network/character_server.gd index e710639..f629283 100644 --- a/network/character_server.gd +++ b/network/character_server.gd @@ -1,6 +1,11 @@ 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() @@ -17,7 +22,20 @@ func _init(host: String, port: int = 6121) -> void: peer.poll() -func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Gender) -> CharacterServerLoginSuccessPacket: +## 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 @@ -26,29 +44,31 @@ func login(account_id: int, login_id1: int, login_id2: int, gender: Constants.Ge peer.put_data(character_server_login_packet.to_bytes()) - var success = CharacterServerLoginSuccessPacket.from_bytes_via_peer(peer) - print(inst_to_dict(success)) + peer.get_data(4) # in-between packet - return success + var packet = await received_packet + if packet is CharacterServerLoginSuccessPacket: + print(inst_to_dict(packet)) + logged_in.emit(packet) -func request_character_list() -> RequestCharacterListSuccessPacket: +func request_character_list(): var request_character_list_packet = RequestCharacterListPacket.new() peer.put_data(request_character_list_packet.to_bytes()) - var success = RequestCharacterListSuccessPacket.from_bytes_via_peer(peer) - print(inst_to_dict(success)) - - return success + 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) -> CharacterSelectionSuccessPacket: +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 success = CharacterSelectionSuccessPacket.from_bytes_via_peer(peer) - print(inst_to_dict(success)) - - return success + var packet = await received_packet + if packet is CharacterSelectionSuccessPacket: + print(inst_to_dict(packet)) + selected_character.emit(packet) |