client: added ConnectionStatus to invoke UI changes when connection status changes
This commit is contained in:
@@ -72,3 +72,49 @@ func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void:
|
||||
"client_name": client_name,
|
||||
"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
|
||||
|
||||
@@ -24,6 +24,16 @@ signal disconnected_from_lobby()
|
||||
|
||||
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:
|
||||
_tcp.poll()
|
||||
_new_status = _tcp.get_status()
|
||||
@@ -31,15 +41,17 @@ func _process(delta: float) -> void:
|
||||
if not _processed_status == _new_status:
|
||||
match _new_status:
|
||||
StreamPeerTCP.Status.STATUS_NONE:
|
||||
pass
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
StreamPeerTCP.Status.STATUS_CONNECTING:
|
||||
pass
|
||||
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
||||
StreamPeerTCP.Status.STATUS_CONNECTED:
|
||||
_thread = Thread.new()
|
||||
_thread.start(_listen_for_data)
|
||||
connected_to_server.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
||||
StreamPeerTCP.Status.STATUS_ERROR:
|
||||
connection_error_occurred.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
|
||||
_processed_status = _new_status
|
||||
|
||||
@@ -59,9 +71,11 @@ func _handle_response(response: String) -> void:
|
||||
"create_lobby":
|
||||
if data["status"] == "ok":
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"join_lobby":
|
||||
if data["status"] == "ok":
|
||||
connected_to_lobby.emit.call_deferred(_lobby_id)
|
||||
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
||||
"client_joined_lobby":
|
||||
client_joined_lobby.emit.call_deferred(data["new_client_name"])
|
||||
|
||||
@@ -79,6 +93,7 @@ func disconnect_from_server() -> void:
|
||||
if _thread:
|
||||
_thread.wait_to_finish()
|
||||
disconnected_from_server.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
||||
|
||||
func create_lobby(client_name: String) -> void:
|
||||
_lobby_id = _generate_code()
|
||||
@@ -102,6 +117,7 @@ func leave_lobby() -> void:
|
||||
_client_name = ""
|
||||
|
||||
disconnected_from_lobby.emit()
|
||||
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
||||
|
||||
func _send_message(message: String) -> void:
|
||||
_tcp.put_data(message.to_utf8_buffer())
|
||||
|
||||
Reference in New Issue
Block a user