diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2025-09-25 12:09:50 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2025-09-25 12:09:50 -0400 |
commit | 3f53502a5907ed1982d28a392c54331f0c1c2c42 (patch) | |
tree | 1dd087464d0fba1c35feaab0cee357fca6f2763c /client/Archipelago/client.gd | |
parent | fb220e1c75e72a536c19aa1283f905850a91cf44 (diff) | |
download | lingo2-archipelago-3f53502a5907ed1982d28a392c54331f0c1c2c42.tar.gz lingo2-archipelago-3f53502a5907ed1982d28a392c54331f0c1c2c42.tar.bz2 lingo2-archipelago-3f53502a5907ed1982d28a392c54331f0c1c2c42.zip |
Move the client into the apworld
Only works on source right now, not as an apworld.
Diffstat (limited to 'client/Archipelago/client.gd')
-rw-r--r-- | client/Archipelago/client.gd | 417 |
1 files changed, 0 insertions, 417 deletions
diff --git a/client/Archipelago/client.gd b/client/Archipelago/client.gd deleted file mode 100644 index 843647d..0000000 --- a/client/Archipelago/client.gd +++ /dev/null | |||
@@ -1,417 +0,0 @@ | |||
1 | extends Node | ||
2 | |||
3 | const ap_version = {"major": 0, "minor": 6, "build": 3, "class": "Version"} | ||
4 | |||
5 | var SCRIPT_uuid | ||
6 | |||
7 | var _ws = WebSocketPeer.new() | ||
8 | var _should_process = false | ||
9 | var _initiated_disconnect = false | ||
10 | var _try_wss = false | ||
11 | var _has_connected = false | ||
12 | |||
13 | var _datapackages = {} | ||
14 | var _pending_packages = [] | ||
15 | var _item_id_to_name = {} # All games | ||
16 | var _location_id_to_name = {} # All games | ||
17 | var _item_name_to_id = {} # Lingo 2 only | ||
18 | var _location_name_to_id = {} # Lingo 2 only | ||
19 | |||
20 | var _remote_version = {"major": 0, "minor": 0, "build": 0} | ||
21 | var _gen_version = {"major": 0, "minor": 0, "build": 0} | ||
22 | |||
23 | var ap_server = "" | ||
24 | var ap_user = "" | ||
25 | var ap_pass = "" | ||
26 | |||
27 | var _authenticated = false | ||
28 | var _seed = "" | ||
29 | var _team = 0 | ||
30 | var _slot = 0 | ||
31 | var _players = [] | ||
32 | var _player_name_by_slot = {} | ||
33 | var _game_by_player = {} | ||
34 | var _checked_locations = [] | ||
35 | var _received_indexes = [] | ||
36 | var _received_items = {} | ||
37 | var _slot_data = {} | ||
38 | |||
39 | signal could_not_connect | ||
40 | signal connect_status | ||
41 | signal client_connected(slot_data) | ||
42 | signal item_received(item_id, index, player, flags, amount) | ||
43 | signal message_received(message) | ||
44 | signal location_scout_received(item_id, location_id, player, flags) | ||
45 | |||
46 | |||
47 | func _init(): | ||
48 | set_process_mode(Node.PROCESS_MODE_ALWAYS) | ||
49 | |||
50 | _ws.inbound_buffer_size = 8388608 | ||
51 | |||
52 | global._print("Instantiated APClient") | ||
53 | |||
54 | # Read AP datapackages from file, if there are any | ||
55 | if FileAccess.file_exists("user://ap_datapackages"): | ||
56 | var file = FileAccess.open("user://ap_datapackages", FileAccess.READ) | ||
57 | var data = file.get_var(true) | ||
58 | file.close() | ||
59 | |||
60 | if typeof(data) != TYPE_DICTIONARY: | ||
61 | global._print("AP datapackages file is corrupted") | ||
62 | data = {} | ||
63 | |||
64 | _datapackages = data | ||
65 | |||
66 | processDatapackages() | ||
67 | |||
68 | |||
69 | func _ready(): | ||
70 | pass | ||
71 | #_ws.connect("connection_closed", _closed) | ||
72 | #_ws.connect("connection_failed", _closed) | ||
73 | #_ws.connect("server_disconnected", _closed) | ||
74 | #_ws.connect("connection_error", _errored) | ||
75 | #_ws.connect("connection_established", _connected) | ||
76 | |||
77 | |||
78 | func _reset_state(): | ||
79 | _should_process = false | ||
80 | _authenticated = false | ||
81 | _try_wss = false | ||
82 | _has_connected = false | ||
83 | _received_items = {} | ||
84 | _received_indexes = [] | ||
85 | |||
86 | |||
87 | func _errored(): | ||
88 | if _try_wss: | ||
89 | global._print("Could not connect to AP with ws://, now trying wss://") | ||
90 | connectToServer(ap_server, ap_user, ap_pass) | ||
91 | else: | ||
92 | global._print("AP connection failed") | ||
93 | _reset_state() | ||
94 | |||
95 | emit_signal( | ||
96 | "could_not_connect", | ||
97 | "Could not connect to Archipelago. Check that your server and port are correct. See the error log for more information." | ||
98 | ) | ||
99 | |||
100 | |||
101 | func _closed(_was_clean = true): | ||
102 | global._print("Connection closed") | ||
103 | _reset_state() | ||
104 | |||
105 | if not _initiated_disconnect: | ||
106 | emit_signal("could_not_connect", "Disconnected from Archipelago") | ||
107 | |||
108 | _initiated_disconnect = false | ||
109 | |||
110 | |||
111 | func _connected(_proto = ""): | ||
112 | global._print("Connected!") | ||
113 | _try_wss = false | ||
114 | |||
115 | |||
116 | func disconnect_from_ap(): | ||
117 | _initiated_disconnect = true | ||
118 | _ws.close() | ||
119 | |||
120 | |||
121 | func _process(_delta): | ||
122 | if _should_process: | ||
123 | _ws.poll() | ||
124 | |||
125 | var state = _ws.get_ready_state() | ||
126 | if state == WebSocketPeer.STATE_OPEN: | ||
127 | if not _has_connected: | ||
128 | _has_connected = true | ||
129 | |||
130 | _connected() | ||
131 | |||
132 | while _ws.get_available_packet_count(): | ||
133 | var packet = _ws.get_packet() | ||
134 | global._print("Got data from server: " + packet.get_string_from_utf8()) | ||
135 | var json = JSON.new() | ||
136 | var jserror = json.parse(packet.get_string_from_utf8()) | ||
137 | if jserror != OK: | ||
138 | global._print("Error parsing packet from AP: " + jserror.error_string) | ||
139 | return | ||
140 | |||
141 | for message in json.data: | ||
142 | var cmd = message["cmd"] | ||
143 | global._print("Received command: " + cmd) | ||
144 | |||
145 | if cmd == "RoomInfo": | ||
146 | _seed = message["seed_name"] | ||
147 | _remote_version = message["version"] | ||
148 | _gen_version = message["generator_version"] | ||
149 | |||
150 | var needed_games = [] | ||
151 | for game in message["datapackage_checksums"].keys(): | ||
152 | if ( | ||
153 | !_datapackages.has(game) | ||
154 | or ( | ||
155 | _datapackages[game]["checksum"] | ||
156 | != message["datapackage_checksums"][game] | ||
157 | ) | ||
158 | ): | ||
159 | needed_games.append(game) | ||
160 | |||
161 | if !needed_games.is_empty(): | ||
162 | _pending_packages = needed_games | ||
163 | var cur_needed = _pending_packages.pop_front() | ||
164 | requestDatapackages([cur_needed]) | ||
165 | else: | ||
166 | connectToRoom() | ||
167 | |||
168 | elif cmd == "DataPackage": | ||
169 | for game in message["data"]["games"].keys(): | ||
170 | _datapackages[game] = message["data"]["games"][game] | ||
171 | saveDatapackages() | ||
172 | |||
173 | if !_pending_packages.is_empty(): | ||
174 | var cur_needed = _pending_packages.pop_front() | ||
175 | requestDatapackages([cur_needed]) | ||
176 | else: | ||
177 | processDatapackages() | ||
178 | connectToRoom() | ||
179 | |||
180 | elif cmd == "Connected": | ||
181 | _authenticated = true | ||
182 | _team = message["team"] | ||
183 | _slot = message["slot"] | ||
184 | _players = message["players"] | ||
185 | _checked_locations = message["checked_locations"] | ||
186 | _slot_data = message["slot_data"] | ||
187 | |||
188 | for player in _players: | ||
189 | _player_name_by_slot[player["slot"]] = player["alias"] | ||
190 | _game_by_player[player["slot"]] = message["slot_info"][str( | ||
191 | player["slot"] | ||
192 | )]["game"] | ||
193 | |||
194 | emit_signal("client_connected", _slot_data) | ||
195 | |||
196 | elif cmd == "ConnectionRefused": | ||
197 | var error_message = "" | ||
198 | for error in message["errors"]: | ||
199 | var submsg = "" | ||
200 | if error == "InvalidSlot": | ||
201 | submsg = "Invalid player name." | ||
202 | elif error == "InvalidGame": | ||
203 | submsg = "The specified player is not playing Lingo." | ||
204 | elif error == "IncompatibleVersion": | ||
205 | submsg = ( | ||
206 | "The Archipelago server is not the correct version for this client. Expected v%d.%d.%d. Found v%d.%d.%d." | ||
207 | % [ | ||
208 | ap_version["major"], | ||
209 | ap_version["minor"], | ||
210 | ap_version["build"], | ||
211 | _remote_version["major"], | ||
212 | _remote_version["minor"], | ||
213 | _remote_version["build"] | ||
214 | ] | ||
215 | ) | ||
216 | elif error == "InvalidPassword": | ||
217 | submsg = "Incorrect password." | ||
218 | elif error == "InvalidItemsHandling": | ||
219 | submsg = "Invalid item handling flag. This is a bug with the client." | ||
220 | |||
221 | if submsg != "": | ||
222 | if error_message != "": | ||
223 | error_message += " " | ||
224 | error_message += submsg | ||
225 | |||
226 | if error_message == "": | ||
227 | error_message = "Unknown error." | ||
228 | |||
229 | _initiated_disconnect = true | ||
230 | _ws.close() | ||
231 | |||
232 | emit_signal("could_not_connect", error_message) | ||
233 | global._print("Connection to AP refused") | ||
234 | global._print(message) | ||
235 | |||
236 | elif cmd == "ReceivedItems": | ||
237 | var i = 0 | ||
238 | for item in message["items"]: | ||
239 | var index = int(message["index"] + i) | ||
240 | i += 1 | ||
241 | |||
242 | if _received_indexes.has(index): | ||
243 | # Do not re-process items. | ||
244 | continue | ||
245 | |||
246 | _received_indexes.append(index) | ||
247 | |||
248 | var item_id = int(item["item"]) | ||
249 | _received_items[item_id] = _received_items.get(item_id, 0) + 1 | ||
250 | |||
251 | emit_signal( | ||
252 | "item_received", | ||
253 | item_id, | ||
254 | index, | ||
255 | int(item["player"]), | ||
256 | int(item["flags"]), | ||
257 | _received_items[item_id] | ||
258 | ) | ||
259 | |||
260 | elif cmd == "PrintJSON": | ||
261 | emit_signal("message_received", message) | ||
262 | |||
263 | elif cmd == "LocationInfo": | ||
264 | for loc in message["locations"]: | ||
265 | emit_signal( | ||
266 | "location_scout_received", | ||
267 | int(loc["item"]), | ||
268 | int(loc["location"]), | ||
269 | int(loc["player"]), | ||
270 | int(loc["flags"]) | ||
271 | ) | ||
272 | |||
273 | elif state == WebSocketPeer.STATE_CLOSED: | ||
274 | if _has_connected: | ||
275 | _closed() | ||
276 | else: | ||
277 | _errored() | ||
278 | |||
279 | |||
280 | func saveDatapackages(): | ||
281 | # Save the AP datapackages to disk. | ||
282 | var file = FileAccess.open("user://ap_datapackages", FileAccess.WRITE) | ||
283 | file.store_var(_datapackages, true) | ||
284 | file.close() | ||
285 | |||
286 | |||
287 | func connectToServer(server, un, pw): | ||
288 | ap_server = server | ||
289 | ap_user = un | ||
290 | ap_pass = pw | ||
291 | |||
292 | _initiated_disconnect = false | ||
293 | |||
294 | var url = "" | ||
295 | if ap_server.begins_with("ws://") or ap_server.begins_with("wss://"): | ||
296 | url = ap_server | ||
297 | _try_wss = false | ||
298 | elif _try_wss: | ||
299 | url = "wss://" + ap_server | ||
300 | _try_wss = false | ||
301 | else: | ||
302 | url = "ws://" + ap_server | ||
303 | _try_wss = true | ||
304 | |||
305 | var err = _ws.connect_to_url(url) | ||
306 | if err != OK: | ||
307 | emit_signal( | ||
308 | "could_not_connect", | ||
309 | ( | ||
310 | "Could not connect to Archipelago. Check that your server and port are correct. See the error log for more information. Error code: %d." | ||
311 | % err | ||
312 | ) | ||
313 | ) | ||
314 | global._print("Could not connect to AP: %d" % err) | ||
315 | return | ||
316 | _should_process = true | ||
317 | |||
318 | emit_signal("connect_status", "Connecting...") | ||
319 | |||
320 | |||
321 | func sendMessage(msg): | ||
322 | var payload = JSON.stringify(msg) | ||
323 | _ws.send_text(payload) | ||
324 | |||
325 | |||
326 | func requestDatapackages(games): | ||
327 | emit_signal("connect_status", "Downloading %s data package..." % games[0]) | ||
328 | |||
329 | sendMessage([{"cmd": "GetDataPackage", "games": games}]) | ||
330 | |||
331 | |||
332 | func processDatapackages(): | ||
333 | _item_id_to_name = {} | ||
334 | _location_id_to_name = {} | ||
335 | for game in _datapackages.keys(): | ||
336 | var package = _datapackages[game] | ||
337 | |||
338 | _item_id_to_name[game] = {} | ||
339 | for item_name in package["item_name_to_id"].keys(): | ||
340 | _item_id_to_name[game][int(package["item_name_to_id"][item_name])] = item_name | ||
341 | |||
342 | _location_id_to_name[game] = {} | ||
343 | for location_name in package["location_name_to_id"].keys(): | ||
344 | _location_id_to_name[game][int(package["location_name_to_id"][location_name])] = location_name | ||
345 | |||
346 | if _datapackages.has("Lingo 2"): | ||
347 | _item_name_to_id = _datapackages["Lingo 2"]["item_name_to_id"] | ||
348 | _location_name_to_id = _datapackages["Lingo 2"]["location_name_to_id"] | ||
349 | |||
350 | |||
351 | func connectToRoom(): | ||
352 | emit_signal("connect_status", "Authenticating...") | ||
353 | |||
354 | sendMessage( | ||
355 | [ | ||
356 | { | ||
357 | "cmd": "Connect", | ||
358 | "password": ap_pass, | ||
359 | "game": "Lingo 2", | ||
360 | "name": ap_user, | ||
361 | "uuid": SCRIPT_uuid.v4(), | ||
362 | "version": ap_version, | ||
363 | "items_handling": 0b111, # always receive our items | ||
364 | "tags": [], | ||
365 | "slot_data": true | ||
366 | } | ||
367 | ] | ||
368 | ) | ||
369 | |||
370 | |||
371 | func sendConnectUpdate(tags): | ||
372 | sendMessage([{"cmd": "ConnectUpdate", "tags": tags}]) | ||
373 | |||
374 | |||
375 | func requestSync(): | ||
376 | sendMessage([{"cmd": "Sync"}]) | ||
377 | |||
378 | |||
379 | func sendLocation(loc_id): | ||
380 | sendMessage([{"cmd": "LocationChecks", "locations": [loc_id]}]) | ||
381 | |||
382 | |||
383 | func sendLocations(loc_ids): | ||
384 | sendMessage([{"cmd": "LocationChecks", "locations": loc_ids}]) | ||
385 | |||
386 | |||
387 | func setValue(key, value, operation = "replace"): | ||
388 | sendMessage( | ||
389 | [ | ||
390 | { | ||
391 | "cmd": "Set", | ||
392 | "key": "Lingo2_%d_%s" % [_slot, key], | ||
393 | "want_reply": false, | ||
394 | "operations": [{"operation": operation, "value": value}] | ||
395 | } | ||
396 | ] | ||
397 | ) | ||
398 | |||
399 | |||
400 | func say(textdata): | ||
401 | sendMessage([{"cmd": "Say", "text": textdata}]) | ||
402 | |||
403 | |||
404 | func completedGoal(): | ||
405 | sendMessage([{"cmd": "StatusUpdate", "status": 30}]) # CLIENT_GOAL | ||
406 | |||
407 | |||
408 | func scoutLocations(loc_ids): | ||
409 | sendMessage([{"cmd": "LocationScouts", "locations": loc_ids}]) | ||
410 | |||
411 | |||
412 | func hasItem(item_id): | ||
413 | return _received_items.has(item_id) | ||
414 | |||
415 | |||
416 | func getItemAmount(item_id): | ||
417 | return _received_items.get(item_id, 0) | ||