2025-06-28 15:19:04 +02:00
|
|
|
class_name ClientInterface
|
|
|
|
|
extends Control
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
@export var client: WebClientTCP
|
|
|
|
|
|
|
|
|
|
@export var connect_button: Button
|
|
|
|
|
@export var create_lobby_button: Button
|
|
|
|
|
@export var join_lobby_button: Button
|
|
|
|
|
@export var leave_lobby_button: Button
|
|
|
|
|
|
2025-07-04 11:29:25 +02:00
|
|
|
@export var chat_message_input: LineEdit
|
|
|
|
|
@export var send_chat_message_button: Button
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
@export var name_input: LineEdit
|
|
|
|
|
@export var lobby_code_input: LineEdit
|
|
|
|
|
|
2025-06-30 22:07:43 +02:00
|
|
|
@export var output: Label
|
|
|
|
|
|
|
|
|
|
func _write_line(text: String) -> void:
|
|
|
|
|
output.text += text + "\n"
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _on_create_lobby_button_pressed() -> void:
|
|
|
|
|
client.create_lobby(name_input.text.strip_edges())
|
|
|
|
|
|
|
|
|
|
create_lobby_button.disabled = true
|
|
|
|
|
join_lobby_button.disabled = true
|
|
|
|
|
leave_lobby_button.disabled = false
|
|
|
|
|
|
|
|
|
|
name_input.editable = false
|
|
|
|
|
lobby_code_input.editable = false
|
|
|
|
|
|
|
|
|
|
func _on_connect_button_pressed() -> void:
|
|
|
|
|
connect_button.disabled = true
|
|
|
|
|
client.connect_to_server()
|
|
|
|
|
|
|
|
|
|
create_lobby_button.disabled = true
|
|
|
|
|
join_lobby_button.disabled = true
|
|
|
|
|
leave_lobby_button.disabled = false
|
|
|
|
|
|
|
|
|
|
func _on_join_lobby_button_pressed() -> void:
|
|
|
|
|
client.join_lobby(lobby_code_input.text.strip_edges(), name_input.text.strip_edges())
|
|
|
|
|
|
|
|
|
|
func _on_disconnect_lobby_button_pressed() -> void:
|
|
|
|
|
leave_lobby_button.disabled = true
|
|
|
|
|
client.leave_lobby()
|
|
|
|
|
|
2025-07-04 11:29:25 +02:00
|
|
|
func _on_send_message_button_pressed() -> void:
|
|
|
|
|
var msg: String = chat_message_input.text.strip_edges()
|
|
|
|
|
if not msg.is_empty():
|
|
|
|
|
client.send_chat_message(msg)
|
|
|
|
|
chat_message_input.text = ""
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _on_web_client_tcp_connected_to_server() -> void:
|
|
|
|
|
create_lobby_button.disabled = false
|
|
|
|
|
join_lobby_button.disabled = false
|
|
|
|
|
|
|
|
|
|
name_input.editable = true
|
|
|
|
|
lobby_code_input.editable = true
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_connection_error_occurred() -> void:
|
|
|
|
|
connect_button.disabled = false
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_disconnected_from_server() -> void:
|
|
|
|
|
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 = ""
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void:
|
|
|
|
|
lobby_code_input.text = lobby_id
|
2025-06-30 22:07:43 +02:00
|
|
|
_write_line("you connected to lobby {lobby_id}".format({"lobby_id": lobby_id}))
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func _on_web_client_tcp_disconnected_from_lobby() -> void:
|
2025-06-30 22:07:43 +02:00
|
|
|
_write_line("you disconnected from the lobby")
|
|
|
|
|
|
|
|
|
|
func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void:
|
|
|
|
|
_write_line("{client_name} joined the lobby{you}".format({
|
|
|
|
|
"client_name": client_name,
|
|
|
|
|
"you": " (that's you!)" if client_name == name_input.text.strip_edges() else ""
|
|
|
|
|
}))
|
2025-07-02 21:58:19 +02:00
|
|
|
|
2025-07-04 11:29:25 +02:00
|
|
|
func _on_web_client_tcp_received_chat_message(client_name: String, message: String) -> void:
|
|
|
|
|
_write_line("<{client_name}> {message}".format({
|
|
|
|
|
"client_name": client_name,
|
|
|
|
|
"message": message,
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-04 11:41:57 +02:00
|
|
|
func _on_web_client_tcp_client_left_lobby(client_name: String) -> void:
|
|
|
|
|
_write_line("{client_name} left the lobby".format({"client_name": client_name}))
|
|
|
|
|
|
2025-07-02 21:58:19 +02:00
|
|
|
|
|
|
|
|
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 = ""
|
2025-07-04 11:29:25 +02:00
|
|
|
chat_message_input.editable = false
|
|
|
|
|
send_chat_message_button.disabled = true
|
2025-07-02 21:58:19 +02:00
|
|
|
|
|
|
|
|
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 = ""
|
2025-07-04 11:29:25 +02:00
|
|
|
chat_message_input.editable = false
|
|
|
|
|
send_chat_message_button.disabled = true
|
2025-07-02 21:58:19 +02:00
|
|
|
|
|
|
|
|
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 = ""
|
2025-07-04 11:29:25 +02:00
|
|
|
chat_message_input.editable = false
|
|
|
|
|
send_chat_message_button.disabled = true
|
2025-07-02 21:58:19 +02:00
|
|
|
|
|
|
|
|
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
|
2025-07-04 11:29:25 +02:00
|
|
|
chat_message_input.editable = false
|
|
|
|
|
send_chat_message_button.disabled = true
|
2025-07-02 21:58:19 +02:00
|
|
|
|
|
|
|
|
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
|
2025-07-04 11:29:25 +02:00
|
|
|
chat_message_input.editable = true
|
|
|
|
|
send_chat_message_button.disabled = false
|