summaryrefslogtreecommitdiff
path: root/network/map_server.gd
blob: f4dacc115c9a4e42c39fee709c1c17cf005958f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class_name MapServer


signal received_packet(packet: Packet)
signal logged_in(packet: MapServerLoginSuccessPacket)

var host: String
var port: int = 5121
var stream: PacketPeerStream = PacketPeerStream.new()
var peer: StreamPeerTCP = StreamPeerTCP.new()


@warning_ignore("shadowed_variable")
func _init(host: String, port: int = 5121) -> 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:
			var header = raw_packet.decode_u16(0)
			var packet_type = Constants.PacketDB[header]
			if packet_type.BYTE_LENGTH > 0:
				raw_packet += peer.get_data(packet_type.BYTE_LENGTH - 2)[1]
			else:
				raw_packet += peer.get_data(peer.get_available_bytes())[1]
			var packet = packet_type.from_bytes(raw_packet)
		
			received_packet.emit(packet)


func login(account_id: int, character_id: int, login_id1: int, gender: Constants.Gender):
	var map_server_login_packet = MapServerLoginPacket.new()
	map_server_login_packet.account_id = account_id
	map_server_login_packet.character_id = character_id
	map_server_login_packet.login_id1 = login_id1
	map_server_login_packet.gender = gender
	
	peer.put_data(map_server_login_packet.to_bytes())
	
	peer.get_data(6) # in-between packet
	peer.get_data(4) # in-between packet
	
	var packet = await received_packet
	if packet is MapServerLoginSuccessPacket:
		print(inst_to_dict(packet))
		logged_in.emit(packet)