diff options
Diffstat (limited to 'Archipelago/tracker.gd')
-rw-r--r-- | Archipelago/tracker.gd | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Archipelago/tracker.gd b/Archipelago/tracker.gd new file mode 100644 index 0000000..798721d --- /dev/null +++ b/Archipelago/tracker.gd | |||
@@ -0,0 +1,84 @@ | |||
1 | extends Node | ||
2 | |||
3 | var autotracker_port = 41253 | ||
4 | |||
5 | var _server = WebSocketServer.new() | ||
6 | var _peers = [] | ||
7 | |||
8 | |||
9 | func _ready(): | ||
10 | _server.bind_ip = "127.0.0.1" | ||
11 | _server.connect("client_connected", self, "_connection_established") | ||
12 | _server.connect("client_disconnected", self, "_connection_dropped") | ||
13 | _server.connect("data_received", self, "_data_received") | ||
14 | _server.listen(autotracker_port) | ||
15 | |||
16 | |||
17 | func _process(_delta): | ||
18 | _server.poll() | ||
19 | |||
20 | |||
21 | func _connection_established(id, _protocol): | ||
22 | _peers.append(id) | ||
23 | |||
24 | var apclient = global.get_node("Archipelago") | ||
25 | |||
26 | var msg = {"cmd": "Connect", "slot": {"server": apclient.ap_server, "player": apclient.ap_user}} | ||
27 | |||
28 | var peer = _server.get_peer(id) | ||
29 | peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) | ||
30 | peer.put_packet(JSON.print(msg).to_utf8()) | ||
31 | |||
32 | |||
33 | func _connection_dropped(id, _was_clean_close): | ||
34 | _peers.erase(id) | ||
35 | |||
36 | |||
37 | func _data_received(id): | ||
38 | var peer = _server.get_peer(id) | ||
39 | peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) | ||
40 | |||
41 | var packet_text = peer.get_packet().get_string_from_utf8() | ||
42 | global._print("Got data from tracker: " + packet_text) | ||
43 | var msg = JSON.parse(packet_text) | ||
44 | if msg.error != OK: | ||
45 | global._print("Error parsing packet from Tracker: " + msg.error_string) | ||
46 | return | ||
47 | |||
48 | var apclient = global.get_node("Archipelago") | ||
49 | |||
50 | if msg["cmd"] == "Sync": | ||
51 | var resp = {} | ||
52 | |||
53 | if apclient.track_player: | ||
54 | var player = get_tree().get_root().get_node("Spatial/player") | ||
55 | resp = { | ||
56 | "cmd": "UpdatePosition", | ||
57 | "position": {"x": int(player._oldpos_fine.x), "z": int(player._oldpos_fine.z)} | ||
58 | } | ||
59 | |||
60 | peer.put_packet(JSON.print(resp).to_utf8()) | ||
61 | |||
62 | |||
63 | func _broadcast(msg): | ||
64 | var to_remove = [] | ||
65 | var serialized = JSON.print(msg).to_utf8() | ||
66 | |||
67 | for peer_id in _peers: | ||
68 | if _server.has_peer(peer_id): | ||
69 | var peer = _server.get_peer(peer_id) | ||
70 | peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) | ||
71 | peer.put_packet(serialized) | ||
72 | else: | ||
73 | to_remove.append(peer_id) | ||
74 | |||
75 | for peer_id in to_remove: | ||
76 | _peers.erase(peer_id) | ||
77 | |||
78 | |||
79 | func has_connection(): | ||
80 | return _peers.size() > 0 | ||
81 | |||
82 | |||
83 | func update_position(x, z): | ||
84 | _broadcast({"cmd": "UpdatePosition", "position": {"x": x, "z": z}}) | ||