From cc45280174e9c52eb83d85ae9fb06149dfb17f66 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 20 Apr 2023 19:58:46 -0400 Subject: Show error message if failure to connect --- Archipelago/client.gd | 69 +++++++++++++++++++++++++++++++++++++++--- Archipelago/load.gd | 8 +++++ Archipelago/settings_screen.gd | 9 ++++++ archipelago.tscn | 31 +++++++++++-------- 4 files changed, 101 insertions(+), 16 deletions(-) diff --git a/Archipelago/client.gd b/Archipelago/client.gd index 293bf7b..a6a78e2 100644 --- a/Archipelago/client.gd +++ b/Archipelago/client.gd @@ -17,8 +17,8 @@ const kNO_PANEL_SHUFFLE = 0 const kREARRANGE_PANELS = 1 var _client = WebSocketClient.new() -var _last_state = WebSocketPeer.STATE_CLOSED var _should_process = false +var _initiated_disconnect = false var _datapackages = {} var _item_id_to_name = {} # All games @@ -26,6 +26,8 @@ var _location_id_to_name = {} # All games var _item_name_to_id = {} # LINGO only var _location_name_to_id = {} # LINGO only +var _remote_version = {"major": 0, "minor": 0, "build": 0} + const uuid_util = preload("user://maps/Archipelago/vendor/uuid.gd") # TODO: caching per MW/slot, reset between connections @@ -60,6 +62,7 @@ var _last_new_item = -1 var _tower_floors = 0 var _has_colors = ["white"] +signal could_not_connect signal client_connected signal evaluate_solvability @@ -88,16 +91,34 @@ func _init(): func _ready(): _client.connect("connection_closed", self, "_closed") - _client.connect("connection_error", self, "_closed") + _client.connect("connection_failed", self, "_closed") + _client.connect("server_disconnected", self, "_closed") + _client.connect("connection_error", self, "_errored") _client.connect("connection_established", self, "_connected") _client.connect("data_received", self, "_on_data") -func _closed(was_clean = false): - global._print("Closed, clean: " + was_clean) +func _errored(): + global._print("AP connection failed") + _should_process = false + _authenticated = false + + emit_signal( + "could_not_connect", + "Could not connect to Archipelago. Check that your server and port are correct. See the error log for more information." + ) + + +func _closed(): + global._print("Connection closed") _should_process = false _authenticated = false + if not _initiated_disconnect: + emit_signal("could_not_connect", "Disconnected from Archipelago") + + _initiated_disconnect = false + func _connected(_proto = ""): global._print("Connected!") @@ -117,6 +138,7 @@ func _on_data(): if cmd == "RoomInfo": _seed = message["seed_name"] + _remote_version = message["version"] var needed_games = [] for game in message["datapackage_checksums"].keys(): @@ -204,6 +226,42 @@ func _on_data(): emit_signal("client_connected") elif cmd == "ConnectionRefused": + var error_message = "" + for error in message["errors"]: + var submsg = "" + if error == "InvalidSlot": + submsg = "Invalid player name." + elif error == "InvalidGame": + submsg = "The specified player is not playing Lingo." + elif error == "IncompatibleVersion": + submsg = ( + "The Archipelago server is not the correct version for this client. Expected v%d.%d.%d. Found v%d.%d.%d." + % [ + ap_version["major"], + ap_version["minor"], + ap_version["build"], + _remote_version["major"], + _remote_version["minor"], + _remote_version["build"] + ] + ) + elif error == "InvalidPassword": + submsg = "Incorrect password." + elif error == "InvalidItemsHandling": + submsg = "Invalid item handling flag. This is a bug with the client. Please report it to the lingo-archipelago GitHub." + + if submsg != "": + if error_message != "": + error_message += " " + error_message += submsg + + if error_message == "": + error_message = "Unknown error." + + _initiated_disconnect = true + _client.disconnect_from_host() + + emit_signal("could_not_connect", error_message) global._print("Connection to AP refused") global._print(message) @@ -325,9 +383,12 @@ func getSaveFileName(): func connectToServer(): + _initiated_disconnect = false + var url = "ws://" + ap_server var err = _client.connect_to_url(url) if err != OK: + emit_signal("could_not_connect", "Could not connect to Archipelago. Error code: %d." % err) global._print("Could not connect to AP: " + err) return _should_process = true diff --git a/Archipelago/load.gd b/Archipelago/load.gd index 9632d61..a83d055 100644 --- a/Archipelago/load.gd +++ b/Archipelago/load.gd @@ -205,6 +205,9 @@ func _load(): messages.set_name("AP_Messages") self.add_child(messages) + # Hook up the scene to be able to handle connection failures. + apclient.connect("could_not_connect", self, "archipelago_disconnected") + # Proceed with the rest of the load. global._print("Hooked Load End") ._load() @@ -256,3 +259,8 @@ func instantiate_painting(name, scene): new_painting.add_child(mps_inst) old_painting.queue_free() return mps_inst + + +func archipelago_disconnected(reason): + var messages_node = self.get_node("AP_Messages") + messages_node.show_message(reason) diff --git a/Archipelago/settings_screen.gd b/Archipelago/settings_screen.gd index 9c4d59a..0eb68cf 100644 --- a/Archipelago/settings_screen.gd +++ b/Archipelago/settings_screen.gd @@ -26,6 +26,7 @@ func _ready(): installScriptExtension("user://maps/Archipelago/panelEnd.gd") global.get_node("Archipelago").connect("client_connected", self, "connectionSuccessful") + global.get_node("Archipelago").connect("could_not_connect", self, "connectionUnsuccessful") # Populate textboxes with AP settings. self.get_node("Panel/server_box").text = global.get_node("Archipelago").ap_server @@ -62,3 +63,11 @@ func connectionSuccessful(): global.map = "level1" global.save_file = apclient.getSaveFileName() var _discard = get_tree().change_scene("res://scenes/load_screen.tscn") + + +func connectionUnsuccessful(error_message): + var popup = self.get_node("Panel/AcceptDialog") + popup.window_title = "Could not connect to Archipelago" + popup.dialog_text = error_message + popup.popup_exclusive = true + popup.popup_centered() diff --git a/archipelago.tscn b/archipelago.tscn index def9ce4..284b73f 100644 --- a/archipelago.tscn +++ b/archipelago.tscn @@ -73,31 +73,34 @@ text = "BACK" script = ExtResource( 4 ) [node name="credit2" parent="Panel" instance=ExtResource( 1 )] -margin_left = 267.0 -margin_top = 347.0 -margin_right = 857.0 -margin_bottom = 411.0 +margin_left = 75.0 +margin_top = 346.0 +margin_right = 665.0 +margin_bottom = 410.0 custom_fonts/font = ExtResource( 2 ) custom_styles/normal = SubResource( 1 ) text = "SERVER" +align = 2 [node name="credit3" parent="Panel" instance=ExtResource( 1 )] -margin_left = 263.0 -margin_top = 515.0 -margin_right = 853.0 -margin_bottom = 579.0 +margin_left = 75.0 +margin_top = 519.0 +margin_right = 665.0 +margin_bottom = 583.0 custom_fonts/font = ExtResource( 2 ) custom_styles/normal = SubResource( 1 ) text = "PLAYER" +align = 2 [node name="credit4" parent="Panel" instance=ExtResource( 1 )] -margin_left = 199.0 -margin_top = 691.0 -margin_right = 789.0 -margin_bottom = 755.0 +margin_left = 75.0 +margin_top = 704.0 +margin_right = 665.0 +margin_bottom = 768.0 custom_fonts/font = ExtResource( 2 ) custom_styles/normal = SubResource( 1 ) text = "PASSWORD" +align = 2 [node name="server_box" type="LineEdit" parent="Panel"] margin_left = 682.0 @@ -129,5 +132,9 @@ custom_colors/font_color = Color( 0, 0, 0, 1 ) custom_styles/focus = SubResource( 2 ) align = 1 +[node name="AcceptDialog" type="AcceptDialog" parent="Panel"] +margin_right = 83.0 +margin_bottom = 58.0 + [connection signal="pressed" from="Panel/connect_button" to="Panel/connect_button" method="_connect_pressed"] [connection signal="pressed" from="Panel/quit_button" to="Panel/quit_button" method="_back_pressed"] -- cgit 1.4.1