diff --git a/components/client_interface.gd b/components/client_interface.gd index 6d8ad57..67fa5ed 100644 --- a/components/client_interface.gd +++ b/components/client_interface.gd @@ -2,6 +2,7 @@ class_name ClientInterface extends Control @export var client: WebClientTCP +@export var game_client: GameClient @export var connect_button: Button @export var create_lobby_button: Button @@ -21,24 +22,14 @@ func _write_line(text: String) -> void: 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 + game_client.is_host = true 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()) + game_client.is_host = false func _on_disconnect_lobby_button_pressed() -> void: leave_lobby_button.disabled = true @@ -51,26 +42,12 @@ func _on_send_message_button_pressed() -> void: chat_message_input.text = "" 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 - _write_line("You are connected to the server") 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 = "" - _write_line("You were disconnected from the server") func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void: @@ -95,7 +72,6 @@ func _on_web_client_tcp_received_chat_message(client_name: String, message: Stri func _on_web_client_tcp_client_left_lobby(client_name: String) -> void: _write_line("{client_name} left the lobby".format({"client_name": client_name})) - func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void: match new_status: WebClientTCP.ConnectionStatus.DISCONNECTED: diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index 17883b7..eb87841 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -60,7 +60,6 @@ func _process(_delta: float) -> void: connection_status_changed.emit(ConnectionStatus.DISCONNECTED) _processed_status = _new_status - func _listen_for_data() -> void: while not _stop_thread: diff --git a/game.tscn b/game.tscn index 58ccc59..efcea2a 100644 --- a/game.tscn +++ b/game.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=3 uid="uid://gqrdpjslr4np"] +[gd_scene load_steps=5 format=3 uid="uid://gqrdpjslr4np"] [ext_resource type="Script" uid="uid://p5gvnyhjqe6o" path="res://components/web_client_tcp.gd" id="1_feb5d"] [ext_resource type="Script" uid="uid://c0cqkatyo4uj1" path="res://components/client_interface.gd" id="2_e2o6t"] +[ext_resource type="Script" uid="uid://utt8atbh7b2a" path="res://game_client.gd" id="3_feb5d"] [sub_resource type="SystemFont" id="SystemFont_80nbo"] font_names = PackedStringArray("IBM Plex Mono") @@ -12,7 +13,7 @@ font_weight = 500 [node name="WebClientTCP" type="Node" parent="."] script = ExtResource("1_feb5d") -[node name="ClientInterface" type="ColorRect" parent="." node_paths=PackedStringArray("client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "chat_message_input", "send_chat_message_button", "name_input", "lobby_code_input", "output")] +[node name="ClientInterface" type="ColorRect" parent="." node_paths=PackedStringArray("client", "game_client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "chat_message_input", "send_chat_message_button", "name_input", "lobby_code_input", "output")] anchors_preset = 9 anchor_bottom = 1.0 offset_right = 362.0 @@ -20,6 +21,7 @@ grow_vertical = 2 color = Color(0.26, 0.2236, 0.230273, 1) script = ExtResource("2_e2o6t") client = NodePath("../WebClientTCP") +game_client = NodePath("../GameClient") connect_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/ConnectButton") create_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/CreateLobbyButton") join_lobby_button = NodePath("MarginContainer/VBoxContainer/VBoxContainer/JoinLobbyButton") @@ -112,6 +114,10 @@ size_flags_horizontal = 3 layout_mode = 2 text = "Send" +[node name="GameClient" type="Node" parent="." node_paths=PackedStringArray("web_client")] +script = ExtResource("3_feb5d") +web_client = NodePath("../WebClientTCP") + [connection signal="client_joined_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_client_joined_lobby"] [connection signal="client_left_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_client_left_lobby"] [connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] diff --git a/game_client.gd b/game_client.gd new file mode 100644 index 0000000..82475af --- /dev/null +++ b/game_client.gd @@ -0,0 +1,9 @@ +class_name GameClient +extends Node + +@export var web_client: WebClientTCP + +var is_host: bool = false + +func start_game() -> void: + web_client.start_game() diff --git a/game_client.gd.uid b/game_client.gd.uid new file mode 100644 index 0000000..354eefc --- /dev/null +++ b/game_client.gd.uid @@ -0,0 +1 @@ +uid://utt8atbh7b2a diff --git a/server_tcp.py b/server_tcp.py index 5cd6d66..45c017f 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -115,8 +115,6 @@ class LobbyManager: "client_name": client_name, }), ) - if self.get_lobby_player_count(lobby_id) == 0: - self.delete_lobby(lobby_id) print(f"Deleted client {client_id} from lobby {lobby_id}") except KeyError: print(f"Client with ID {client_id} in lobby {lobby_id} not found, could not be deleted") @@ -150,7 +148,7 @@ class ClientConnection: # Listens to a connected TCP client. def listen_client(self) -> None: try: - print(f"connected to {self.address[0]}:{self.address[1]}") + print(f"Connected to {self.address[0]}:{self.address[1]}") received_data = "" while True: @@ -165,6 +163,8 @@ class ClientConnection: break except IndexError: print("Client force-disconnected") + except ConnectionResetError: + print("Client force-disconnected") finally: self.connection.close() print(f"Closing connection to {self.address[0]}:{self.address[1]}") @@ -230,8 +230,6 @@ class ClientConnection: if host_client_id == data_object["client_id"]: # client is host; close lobby - lobby_clients = self.lobby_manager.get_lobby(lobby_id).connected_clients.copy() - # for client_id in lobby_clients: self.lobby_manager.leave_lobby(lobby_id, data_object["client_id"]) self.lobby_manager.broadcast_message( lobby_id, @@ -239,8 +237,8 @@ class ClientConnection: "response": "leave_lobby_request", }) ) - # self.lobby_manager.leave_lobby(lobby_id, client_id) - # self.lobby_manager.delete_lobby(lobby_id) + if self.lobby_manager.get_lobby_player_count(lobby_id) == 0: + self.lobby_manager.delete_lobby(lobby_id) else: # client is not host; client leaves only self.lobby_manager.leave_lobby(lobby_id, data_object["client_id"])