client: added ConnectionStatus to invoke UI changes when connection status changes

This commit is contained in:
2025-07-02 21:58:19 +02:00
parent d128f39e6a
commit f084a599b7
3 changed files with 65 additions and 2 deletions
+46
View File
@@ -72,3 +72,49 @@ func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void:
"client_name": client_name, "client_name": client_name,
"you": " (that's you!)" if client_name == name_input.text.strip_edges() else "" "you": " (that's you!)" if client_name == name_input.text.strip_edges() else ""
})) }))
func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void:
match new_status:
WebClientTCP.ConnectionStatus.DISCONNECTED:
connect_button.disabled = false
create_lobby_button.disabled = true
join_lobby_button.disabled = true
leave_lobby_button.disabled = true
name_input.editable = true
lobby_code_input.editable = false
lobby_code_input.text = ""
WebClientTCP.ConnectionStatus.ATTEMPTING_CONNECTION:
connect_button.disabled = true
create_lobby_button.disabled = true
join_lobby_button.disabled = true
leave_lobby_button.disabled = true
name_input.editable = true
lobby_code_input.editable = false
lobby_code_input.text = ""
WebClientTCP.ConnectionStatus.LOBBYLESS:
connect_button.disabled = true
create_lobby_button.disabled = false
join_lobby_button.disabled = false
leave_lobby_button.disabled = true
name_input.editable = true
lobby_code_input.editable = true
lobby_code_input.text = ""
WebClientTCP.ConnectionStatus.ATTEMPTING_JOIN:
connect_button.disabled = true
create_lobby_button.disabled = true
join_lobby_button.disabled = true
leave_lobby_button.disabled = true
name_input.editable = false
lobby_code_input.editable = false
WebClientTCP.ConnectionStatus.JOINED:
connect_button.disabled = true
create_lobby_button.disabled = true
join_lobby_button.disabled = true
leave_lobby_button.disabled = false
name_input.editable = false
lobby_code_input.editable = false
+18 -2
View File
@@ -24,6 +24,16 @@ signal disconnected_from_lobby()
signal client_joined_lobby(client_name: String) signal client_joined_lobby(client_name: String)
signal connection_status_changed(new_status: ConnectionStatus)
enum ConnectionStatus {
DISCONNECTED,
ATTEMPTING_CONNECTION,
LOBBYLESS, # == connected
ATTEMPTING_JOIN,
JOINED,
}
func _process(delta: float) -> void: func _process(delta: float) -> void:
_tcp.poll() _tcp.poll()
_new_status = _tcp.get_status() _new_status = _tcp.get_status()
@@ -31,15 +41,17 @@ func _process(delta: float) -> void:
if not _processed_status == _new_status: if not _processed_status == _new_status:
match _new_status: match _new_status:
StreamPeerTCP.Status.STATUS_NONE: StreamPeerTCP.Status.STATUS_NONE:
pass connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
StreamPeerTCP.Status.STATUS_CONNECTING: StreamPeerTCP.Status.STATUS_CONNECTING:
pass connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
StreamPeerTCP.Status.STATUS_CONNECTED: StreamPeerTCP.Status.STATUS_CONNECTED:
_thread = Thread.new() _thread = Thread.new()
_thread.start(_listen_for_data) _thread.start(_listen_for_data)
connected_to_server.emit() connected_to_server.emit()
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
StreamPeerTCP.Status.STATUS_ERROR: StreamPeerTCP.Status.STATUS_ERROR:
connection_error_occurred.emit() connection_error_occurred.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
_processed_status = _new_status _processed_status = _new_status
@@ -59,9 +71,11 @@ func _handle_response(response: String) -> void:
"create_lobby": "create_lobby":
if data["status"] == "ok": if data["status"] == "ok":
connected_to_lobby.emit.call_deferred(_lobby_id) connected_to_lobby.emit.call_deferred(_lobby_id)
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
"join_lobby": "join_lobby":
if data["status"] == "ok": if data["status"] == "ok":
connected_to_lobby.emit.call_deferred(_lobby_id) connected_to_lobby.emit.call_deferred(_lobby_id)
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
"client_joined_lobby": "client_joined_lobby":
client_joined_lobby.emit.call_deferred(data["new_client_name"]) client_joined_lobby.emit.call_deferred(data["new_client_name"])
@@ -79,6 +93,7 @@ func disconnect_from_server() -> void:
if _thread: if _thread:
_thread.wait_to_finish() _thread.wait_to_finish()
disconnected_from_server.emit() disconnected_from_server.emit()
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
func create_lobby(client_name: String) -> void: func create_lobby(client_name: String) -> void:
_lobby_id = _generate_code() _lobby_id = _generate_code()
@@ -102,6 +117,7 @@ func leave_lobby() -> void:
_client_name = "" _client_name = ""
disconnected_from_lobby.emit() disconnected_from_lobby.emit()
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
func _send_message(message: String) -> void: func _send_message(message: String) -> void:
_tcp.put_data(message.to_utf8_buffer()) _tcp.put_data(message.to_utf8_buffer())
+1
View File
@@ -88,6 +88,7 @@ autowrap_mode = 2
[connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"]
[connection signal="connected_to_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connected_to_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_server"]
[connection signal="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] [connection signal="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"]
[connection signal="connection_status_changed" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_status_changed"]
[connection signal="disconnected_from_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_lobby"] [connection signal="disconnected_from_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_lobby"]
[connection signal="disconnected_from_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_server"] [connection signal="disconnected_from_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_server"]
[connection signal="pressed" from="ClientInterface/HBoxContainer/VBoxContainer/ConnectButton" to="ClientInterface" method="_on_connect_button_pressed"] [connection signal="pressed" from="ClientInterface/HBoxContainer/VBoxContainer/ConnectButton" to="ClientInterface" method="_on_connect_button_pressed"]