about summary refs log tree commit diff stats
path: root/Archipelago
diff options
context:
space:
mode:
Diffstat (limited to 'Archipelago')
-rw-r--r--Archipelago/client.gd81
-rw-r--r--Archipelago/settings_buttons.gd2
2 files changed, 81 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 = ""
4var ap_user = "" 4var ap_user = ""
5var ap_pass = "" 5var ap_pass = ""
6 6
7var _client = WebSocketClient.new()
8var _last_state = WebSocketPeer.STATE_CLOSED
9var _should_process = false
10
11var _datapackage_checksum = ""
12var _item_name_to_id = {}
13var _location_name_to_id = {}
14
7 15
8func _init(): 16func _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
26func _ready(): 40func _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
47func _closed(was_clean = false):
48 global._print("Closed, clean: " + was_clean)
49 _should_process = false
50
51
52func _connected(_proto = ""):
53 global._print("Connected!")
54
55
56func _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
81func _process(_delta):
82 if _should_process:
83 _client.poll()
28 84
29 85
30func saveSettings(): 86func 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
105func 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
114func 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
120func requestDatapackage():
121 sendMessage([{"cmd": "GetDataPackage", "games": ["Lingo"]}])
diff --git a/Archipelago/settings_buttons.gd b/Archipelago/settings_buttons.gd index 1ab3788..735f944 100644 --- a/Archipelago/settings_buttons.gd +++ b/Archipelago/settings_buttons.gd
@@ -12,6 +12,8 @@ func _connect_pressed():
12 apclient.ap_pass = self.get_parent().get_node("password_box").text 12 apclient.ap_pass = self.get_parent().get_node("password_box").text
13 apclient.saveSettings() 13 apclient.saveSettings()
14 14
15 apclient.connectToServer()
16
15 17
16func _back_pressed(): 18func _back_pressed():
17 fader._fade_start("main_menu") 19 fader._fade_start("main_menu")