about summary refs log tree commit diff stats
path: root/Archipelago/tracker.gd
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2024-12-17 16:01:19 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2024-12-17 16:01:19 -0500
commit1bdfb112b659a6e4447ae8ed02c452bf12d8700a (patch)
tree0285f67a9600b83616edb69d209e3ebaf47cbae8 /Archipelago/tracker.gd
parent45fa76505dd00bdd6665bae476277371fb1252a2 (diff)
downloadlingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.tar.gz
lingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.tar.bz2
lingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.zip
Add position tracking via IPC
Diffstat (limited to 'Archipelago/tracker.gd')
-rw-r--r--Archipelago/tracker.gd84
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 @@
1extends Node
2
3var autotracker_port = 41253
4
5var _server = WebSocketServer.new()
6var _peers = []
7
8
9func _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
17func _process(_delta):
18 _server.poll()
19
20
21func _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
33func _connection_dropped(id, _was_clean_close):
34 _peers.erase(id)
35
36
37func _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
63func _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
79func has_connection():
80 return _peers.size() > 0
81
82
83func update_position(x, z):
84 _broadcast({"cmd": "UpdatePosition", "position": {"x": x, "z": z}})