diff --git a/game_client.gd b/game_client.gd index ba781ec..30198b8 100644 --- a/game_client.gd +++ b/game_client.gd @@ -10,11 +10,18 @@ func setup_signals() -> void: MessageBus.subscribe("game_wait_for_connection", _wait_for_connection) MessageBus.subscribe("game_close_connection", _wait_close_connection) + MessageBus.subscribe("game_peer_disconnected", _on_game_peer_disconnected) func _start_client(address: String = "127.0.0.1", port: int = 8080) -> void: if peer: return start_client(address, port) +func _on_game_peer_disconnected() -> void: + close_connection() + + Globals.multiplayer_data.reset() + Globals.lobby_data.go_back_to_match_screen() + MessageBus.clear_voided_listeners() func client_connected_to_server() -> void: push_warning("Connected to server!") diff --git a/game_server.gd b/game_server.gd index 3ba4aee..44eb100 100644 --- a/game_server.gd +++ b/game_server.gd @@ -6,6 +6,26 @@ func _ready() -> void: func setup_signals() -> void: - #MessageBus.subscribe("game_wait_for_connection", _wait_for_connection) - #MessageBus.subscribe("game_close_connection", _wait_close_connection) pass + +func server_client_connected(id: int) -> void: + push_warning("Client Connected... ID: ", id) + + if not Globals.game_data.client1: + Globals.game_data.client1 = id + elif not Globals.game_data.client2: + Globals.game_data.client2 = id + + if Globals.game_data.client1 and Globals.game_data.client2: + Globals.multiplayer_data.init_match() + + # TODO: Add watch match feature... + +func server_client_disconnected(id: int) -> void: + push_warning("Client Disconnected... ID: ", id) + + MessageBus.emit_propagation_clients_only.rpc("game_peer_disconnected", null) + + await get_tree().create_timer(5.0).timeout + # NOTE: Quits game server instance. + get_tree().quit() diff --git a/globals/message_bus.gd b/globals/message_bus.gd index 5518fae..6a6eac9 100644 --- a/globals/message_bus.gd +++ b/globals/message_bus.gd @@ -11,6 +11,14 @@ func subscribe(id: String, callback: Callable) -> void: message_types.get(id).append(callback) +func clear_voided_listeners() -> void: + for mtype in message_types.keys(): + var mlisteners: Array = message_types.get(mtype) + for mlistener in mlisteners: + if mlistener.is_valid(): continue + mlisteners.erase(mlistener) + + func emit_local(id: String, data: Variant, expand: bool = false) -> void: emit_propagation(id, data, expand) diff --git a/scenes/data_bridge/multiplayer_data.gd b/scenes/data_bridge/multiplayer_data.gd index 15bd02e..f43feaa 100644 --- a/scenes/data_bridge/multiplayer_data.gd +++ b/scenes/data_bridge/multiplayer_data.gd @@ -23,8 +23,17 @@ func set_active_player_id(id: int) -> void: current_turn_player_id = id +func reset() -> void: + multiplayer_synchronizer = MultiplayerSynchronizer.new() + synchronizer_config = SceneReplicationConfig.new() + current_turn_player_id = -1 -func init_match(): + is_multiplayer_active = true + multiplayer_synchronizer.name = "multiplayer_synchronizer" + multiplayer_synchronizer.replication_config = synchronizer_config + multiplayer_synchronizer.root_path = "/root/billiards" + +func init_match() -> void: var match_entry = Dictionary() match_entry.set("type", Globals.game_data.game_mode) diff --git a/scenes/screens/lobby_screen/lobby_base.gd b/scenes/screens/lobby_screen/lobby_base.gd index 5c8381c..81635d4 100644 --- a/scenes/screens/lobby_screen/lobby_base.gd +++ b/scenes/screens/lobby_screen/lobby_base.gd @@ -28,7 +28,11 @@ func _on_home_bttn_pressed() -> void: ) func _on_host_connect_bttn_pressed() -> void: - client.start_client(host_address_input.text, int(host_port_range.value)) + Globals.game_data.game_address = host_address_input.text + Globals.game_data.game_port = int(host_port_range.value) + client.start_client( + Globals.game_data.game_address, Globals.game_data.game_port + ) func _on_host_disconnect_bttn_pressed() -> void: match_create_vbox.visible = false diff --git a/scenes/ui_controls/lobby_screen/lobby_ui.tscn b/scenes/ui_controls/lobby_screen/lobby_ui.tscn index eb68fa9..4c9dd4e 100644 --- a/scenes/ui_controls/lobby_screen/lobby_ui.tscn +++ b/scenes/ui_controls/lobby_screen/lobby_ui.tscn @@ -55,7 +55,7 @@ size_flags_horizontal = 3 text = "127.0.0.1" placeholder_text = "Server Address: (Eg. 0.0.0.0)" alignment = 1 -max_length = 12 +max_length = 15 emoji_menu_enabled = false clear_button_enabled = true caret_blink = true @@ -65,7 +65,7 @@ layout_mode = 2 min_value = 1.0 max_value = 65535.0 page = 10.0 -value = 31315.0 +value = 8080.0 rounded = true alignment = 1 diff --git a/scripts/client_networking.gd b/scripts/client_networking.gd index 9717dfe..fbd6e96 100644 --- a/scripts/client_networking.gd +++ b/scripts/client_networking.gd @@ -8,6 +8,7 @@ func start_client(address: String = "127.0.0.1", port: int = 8080) -> void: if peer: return push_warning("Client Starting... Address: ", address, " Port: ", port) + # TODO: Add throbber or other indication of connection attempt. peer = ENetMultiplayerPeer.new() peer.create_client(address, port) diff --git a/scripts/server_networking.gd b/scripts/server_networking.gd index a7beb31..df23922 100644 --- a/scripts/server_networking.gd +++ b/scripts/server_networking.gd @@ -40,30 +40,8 @@ func close_connection() -> void: peer.close() peer = null -func server_client_connected(id: int) -> void: - push_warning("Client Connected... ID: ", id) +func server_client_connected(_id: int) -> void: + assert(false, "This method needs to be overridden...") - if not Globals.game_data.client1: - Globals.game_data.client1 = id - elif not Globals.game_data.client2: - Globals.game_data.client2 = id - - if Globals.game_data.client1 and Globals.game_data.client2: - Globals.multiplayer_data.init_match() - - # TODO: Add watch match feature... - -func server_client_disconnected(id: int) -> void: - push_warning("Client Disconnected... ID: ", id) - - # TODO: Fix call structure - #if id == Globals.game_data.client1: - #LobbyNetworking.rpc_id( - #Globals.game_data.client1, - #"go_back_to_match_screen" - #) - #elif id == Globals.game_data.client2: - #LobbyNetworking.rpc_id( - #Globals.game_data.client2, - #"go_back_to_match_screen" - #) +func server_client_disconnected(_id: int) -> void: + assert(false, "This method needs to be overridden...")