diff options
Diffstat (limited to 'Archipelago/client.gd')
-rw-r--r-- | Archipelago/client.gd | 81 |
1 files changed, 79 insertions, 2 deletions
diff --git a/Archipelago/client.gd b/Archipelago/client.gd index 1d2affd..489ec70 100644 --- a/Archipelago/client.gd +++ b/Archipelago/client.gd | |||
@@ -4,6 +4,14 @@ var ap_server = "" | |||
4 | var ap_user = "" | 4 | var ap_user = "" |
5 | var ap_pass = "" | 5 | var ap_pass = "" |
6 | 6 | ||
7 | var _client = WebSocketClient.new() | ||
8 | var _last_state = WebSocketPeer.STATE_CLOSED | ||
9 | var _should_process = false | ||
10 | |||
11 | var _datapackage_checksum = "" | ||
12 | var _item_name_to_id = {} | ||
13 | var _location_name_to_id = {} | ||
14 | |||
7 | 15 | ||
8 | func _init(): | 16 | func _init(): |
9 | global._print("Instantiated APClient") | 17 | global._print("Instantiated APClient") |
@@ -21,10 +29,58 @@ func _init(): | |||
21 | ap_user = data[1] | 29 | ap_user = data[1] |
22 | if data.size() > 2: | 30 | if data.size() > 2: |
23 | ap_pass = data[2] | 31 | ap_pass = data[2] |
32 | if data.size() > 3: | ||
33 | _datapackage_checksum = data[3] | ||
34 | if data.size() > 4: | ||
35 | _item_name_to_id = data[4] | ||
36 | if data.size() > 5: | ||
37 | _location_name_to_id = data[5] | ||
24 | 38 | ||
25 | 39 | ||
26 | func _ready(): | 40 | func _ready(): |
27 | pass | 41 | _client.connect("connection_closed", self, "_closed") |
42 | _client.connect("connection_error", self, "_closed") | ||
43 | _client.connect("connection_established", self, "_connected") | ||
44 | _client.connect("data_received", self, "_on_data") | ||
45 | |||
46 | |||
47 | func _closed(was_clean = false): | ||
48 | global._print("Closed, clean: " + was_clean) | ||
49 | _should_process = false | ||
50 | |||
51 | |||
52 | func _connected(_proto = ""): | ||
53 | global._print("Connected!") | ||
54 | |||
55 | |||
56 | func _on_data(): | ||
57 | var packet = _client.get_peer(1).get_packet() | ||
58 | global._print("Got data from server: " + packet.get_string_from_utf8()) | ||
59 | var data = JSON.parse(packet.get_string_from_utf8()) | ||
60 | if data.error != OK: | ||
61 | global._print("Error parsing packet from AP: " + data.error_string) | ||
62 | return | ||
63 | |||
64 | for message in data.result: | ||
65 | var cmd = message["cmd"] | ||
66 | global._print("Received command: " + cmd) | ||
67 | |||
68 | if cmd == "RoomInfo": | ||
69 | if message["datapackage_checksums"].has("Lingo"): | ||
70 | if _datapackage_checksum != message["datapackage_checksums"]["Lingo"]: | ||
71 | requestDatapackage() | ||
72 | elif cmd == "DataPackage": | ||
73 | if message["data"]["games"].has("Lingo"): | ||
74 | var lingo_pkg = message["data"]["games"]["Lingo"] | ||
75 | _datapackage_checksum = lingo_pkg["checksum"] | ||
76 | _item_name_to_id = lingo_pkg["item_name_to_id"] | ||
77 | _location_name_to_id = lingo_pkg["location_name_to_id"] | ||
78 | saveSettings() | ||
79 | |||
80 | |||
81 | func _process(_delta): | ||
82 | if _should_process: | ||
83 | _client.poll() | ||
28 | 84 | ||
29 | 85 | ||
30 | func saveSettings(): | 86 | func saveSettings(): |
@@ -39,6 +95,27 @@ func saveSettings(): | |||
39 | var file = File.new() | 95 | var file = File.new() |
40 | file.open("user://settings/archipelago", File.WRITE) | 96 | file.open("user://settings/archipelago", File.WRITE) |
41 | 97 | ||
42 | var data = [ap_server, ap_user, ap_pass] | 98 | var data = [ |
99 | ap_server, ap_user, ap_pass, _datapackage_checksum, _item_name_to_id, _location_name_to_id | ||
100 | ] | ||
43 | file.store_var(data, true) | 101 | file.store_var(data, true) |
44 | file.close() | 102 | file.close() |
103 | |||
104 | |||
105 | func connectToServer(): | ||
106 | var url = "ws://" + ap_server | ||
107 | var err = _client.connect_to_url(url) | ||
108 | if err != OK: | ||
109 | global._print("Could not connect to AP: " + err) | ||
110 | return | ||
111 | _should_process = true | ||
112 | |||
113 | |||
114 | func sendMessage(msg): | ||
115 | var payload = JSON.print(msg) | ||
116 | _client.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) | ||
117 | _client.get_peer(1).put_packet(payload.to_utf8()) | ||
118 | |||
119 | |||
120 | func requestDatapackage(): | ||
121 | sendMessage([{"cmd": "GetDataPackage", "games": ["Lingo"]}]) | ||