refactor: decouple game networking from lobby client
* Replace direct client/server networking references with GameClient/GameServer scenes * Refactor message bus to support local emission and argument expansion * Move lobby connection lifecycle handling to message bus events * Remove multiplayer connection wait helpers and direct lobby client coupling * Rename Server to ServerNetworking and update lobby client references * Update scene resources and export configuration
This commit is contained in:
24
billiards.gd
24
billiards.gd
@@ -1,8 +1,8 @@
|
||||
class_name Billiards extends Node
|
||||
|
||||
|
||||
@onready var server_net: Node = $server_net
|
||||
@onready var client_net: ClientNetworking = $client_net
|
||||
@onready var game_server: GameServer = $game_server
|
||||
@onready var game_client: GameClient = $game_client
|
||||
|
||||
|
||||
#
|
||||
@@ -76,18 +76,17 @@ func _set_game_mode(mode: String) -> void:
|
||||
Globals.game_data.GameModeType.SNOOKER
|
||||
|
||||
|
||||
|
||||
|
||||
func server_(arg: String) -> void:
|
||||
var server_port = arg.split("_")[1]
|
||||
|
||||
server_net.start_server("0.0.0.0", int(server_port))
|
||||
game_server.start_server("0.0.0.0", int(server_port))
|
||||
|
||||
func host_(arg: String) -> void:
|
||||
var game_mode = arg.split("_")[1]
|
||||
var match_name = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
Globals.multiplayer_data.wait_for_connection()
|
||||
while not $scene/lobby_screen.client.lobby_fully_connected:
|
||||
await get_tree().process_frame
|
||||
|
||||
$scene/lobby_screen.find_child(game_mode, true, false).emit_signal("pressed")
|
||||
$scene/lobby_screen.find_child("new_match_name_input", true, false).text = match_name
|
||||
@@ -99,13 +98,14 @@ func join_server(_arg: String) -> void:
|
||||
|
||||
Globals.multiplayer_data.set_multiplayer_active()
|
||||
|
||||
# TODO: Need to check if true still...
|
||||
# NOTE: Need to load lobby in order for signals to get bound
|
||||
# which then game server calls on its load to do handoff
|
||||
# which then game server calls on its load to do handoff
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/screens/lobby_screen/lobby.tscn"
|
||||
)
|
||||
|
||||
client_net.start_client(server_address, int(server_port))
|
||||
game_client.start_client(server_address, int(server_port))
|
||||
|
||||
func client(_arg: String) -> void:
|
||||
Globals.game_state.set_game_scene(
|
||||
@@ -118,17 +118,13 @@ func game(_arg: String) -> void:
|
||||
var game_mode = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
_set_game_mode(game_mode)
|
||||
Globals.multiplayer_data.set_multiplayer_active()
|
||||
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/game.tscn"
|
||||
)
|
||||
Globals.multiplayer_data.set_multiplayer_active()
|
||||
Globals.game_state.set_game_scene( "res://scenes/game.tscn" )
|
||||
|
||||
func join(_arg: String) -> void:
|
||||
var _match_id = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
Globals.multiplayer_data.wait_for_connection()
|
||||
|
||||
var button = $scene/lobby_screen.find_child("join_bttn", true, false)
|
||||
while not button:
|
||||
await get_tree().process_frame
|
||||
|
||||
@@ -2,18 +2,16 @@
|
||||
|
||||
[ext_resource type="Script" uid="uid://dbca7qndk7coq" path="res://billiards.gd" id="1_bhs8v"]
|
||||
[ext_resource type="PackedScene" uid="uid://dy51wny4x53f4" path="res://scenes/data_bridge/bridge.tscn" id="1_xa5nf"]
|
||||
[ext_resource type="Script" uid="uid://ci57tiuqsx1jp" path="res://scripts/client_networking.gd" id="3_hkpkv"]
|
||||
[ext_resource type="Script" uid="uid://bgnr36m8qvbx3" path="res://scripts/server_networking.gd" id="4_cni66"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbvv2msu13vr2" path="res://game_client.tscn" id="3_hkpkv"]
|
||||
[ext_resource type="PackedScene" uid="uid://bss61m8hfvab3" path="res://game_server.tscn" id="4_cni66"]
|
||||
|
||||
[node name="billiards" type="Node"]
|
||||
script = ExtResource("1_bhs8v")
|
||||
|
||||
[node name="bridge" parent="." instance=ExtResource("1_xa5nf")]
|
||||
|
||||
[node name="client_net" type="Node" parent="."]
|
||||
script = ExtResource("3_hkpkv")
|
||||
[node name="game_client" parent="." instance=ExtResource("3_hkpkv")]
|
||||
|
||||
[node name="server_net" type="Node" parent="."]
|
||||
script = ExtResource("4_cni66")
|
||||
[node name="game_server" parent="." instance=ExtResource("4_cni66")]
|
||||
|
||||
[node name="scene" type="Node" parent="."]
|
||||
|
||||
@@ -54,7 +54,7 @@ dedicated_server=false
|
||||
custom_features=""
|
||||
export_filter="all_resources"
|
||||
include_filter=""
|
||||
exclude_filter="s"
|
||||
exclude_filter=".godot/*"
|
||||
export_path="../../builds/desktop/billiards/billiards.x86_64"
|
||||
patches=PackedStringArray()
|
||||
encryption_include_filters=""
|
||||
|
||||
35
game_client.gd
Normal file
35
game_client.gd
Normal file
@@ -0,0 +1,35 @@
|
||||
class_name GameClient extends ClientNetworking
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
setup_signals()
|
||||
|
||||
|
||||
func setup_signals() -> void:
|
||||
MessageBus.subscribe("game_start_client", _start_client)
|
||||
|
||||
MessageBus.subscribe("game_wait_for_connection", _wait_for_connection)
|
||||
MessageBus.subscribe("game_close_connection", _wait_close_connection)
|
||||
|
||||
func _start_client(address: String = "127.0.0.1", port: int = 8080) -> void:
|
||||
if peer: return
|
||||
start_client(address, port)
|
||||
|
||||
|
||||
func client_connected_to_server() -> void:
|
||||
push_warning("Connected to server!")
|
||||
|
||||
func close_connection() -> void:
|
||||
if not peer: return
|
||||
|
||||
push_warning("Disconnected from, server...")
|
||||
|
||||
unset_client()
|
||||
|
||||
func client_disconnected_from_server() -> void:
|
||||
push_warning("Disconnected from, server...")
|
||||
|
||||
func client_connection_failed_to_server() -> void:
|
||||
push_warning("Connection failed!")
|
||||
|
||||
unset_client()
|
||||
1
game_client.gd.uid
Normal file
1
game_client.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cjtu1d1j35r32
|
||||
6
game_client.tscn
Normal file
6
game_client.tscn
Normal file
@@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://cbvv2msu13vr2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cjtu1d1j35r32" path="res://game_client.gd" id="1_tea1p"]
|
||||
|
||||
[node name="client_net" type="Node"]
|
||||
script = ExtResource("1_tea1p")
|
||||
11
game_server.gd
Normal file
11
game_server.gd
Normal file
@@ -0,0 +1,11 @@
|
||||
class_name GameServer extends ServerNetworking
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
setup_signals()
|
||||
|
||||
|
||||
func setup_signals() -> void:
|
||||
#MessageBus.subscribe("game_wait_for_connection", _wait_for_connection)
|
||||
#MessageBus.subscribe("game_close_connection", _wait_close_connection)
|
||||
pass
|
||||
1
game_server.gd.uid
Normal file
1
game_server.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bndylf4rwtlkn
|
||||
6
game_server.tscn
Normal file
6
game_server.tscn
Normal file
@@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://bss61m8hfvab3"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bndylf4rwtlkn" path="res://game_server.gd" id="1_3xapr"]
|
||||
|
||||
[node name="server_net" type="Node"]
|
||||
script = ExtResource("1_3xapr")
|
||||
@@ -11,8 +11,8 @@ func subscribe(id: String, callback: Callable) -> void:
|
||||
message_types.get(id).append(callback)
|
||||
|
||||
|
||||
func emit(id: String, data: Variant) -> void:
|
||||
emit_propagation(id, data)
|
||||
func emit_local(id: String, data: Variant, expand: bool = false) -> void:
|
||||
emit_propagation(id, data, expand)
|
||||
|
||||
|
||||
# TODO: Need to actually validate client request. Also, message bus is more
|
||||
@@ -21,22 +21,23 @@ func emit(id: String, data: Variant) -> void:
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func emit_request(id: String, data: Variant, expand: bool = false) -> void:
|
||||
if not multiplayer.is_server(): return
|
||||
|
||||
var listeners: Array = message_types.get(id)
|
||||
if not listeners:
|
||||
push_error("'%s' not an existing message type to send...", [id])
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
emit_propagation(id, data, expand)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func emit_propagation(id: String, data: Variant, expand: bool = false) -> void:
|
||||
var listeners: Array = message_types.get(id)
|
||||
if not listeners:
|
||||
push_error("'%s' not an existing message type to send...", [id])
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
for callback: Callable in listeners:
|
||||
for callback: Callable in message_types.get(id):
|
||||
if data == null:
|
||||
callback.call()
|
||||
continue
|
||||
|
||||
if not expand:
|
||||
callback.call(data)
|
||||
else:
|
||||
@@ -44,12 +45,15 @@ func emit_propagation(id: String, data: Variant, expand: bool = false) -> void:
|
||||
|
||||
@rpc("authority", "call_remote", "reliable")
|
||||
func emit_propagation_clients_only(id: String, data: Variant, expand: bool = false) -> void:
|
||||
var listeners: Array = message_types.get(id)
|
||||
if not listeners:
|
||||
push_error("'%s' not an existing message type to send...", [id])
|
||||
if not message_types.has(id):
|
||||
push_error("'%s' not an existing message type to handle...", [id])
|
||||
return
|
||||
|
||||
for callback: Callable in listeners:
|
||||
for callback: Callable in message_types.get(id):
|
||||
if data == null:
|
||||
callback.call()
|
||||
continue
|
||||
|
||||
if not expand:
|
||||
callback.call(data)
|
||||
else:
|
||||
|
||||
@@ -72,7 +72,3 @@ jolt_physics_3d/simulation/velocity_steps=20
|
||||
jolt_physics_3d/simulation/position_steps=20
|
||||
3d/simulation/position_steps=10
|
||||
3d/simulation/velocity_steps=10
|
||||
|
||||
[rendering]
|
||||
|
||||
textures/vram_compression/import_etc2_astc=true
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
class_name LobbyData extends Node
|
||||
|
||||
|
||||
@onready var lobby_ui: Node
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
Globals.lobby_data = self
|
||||
|
||||
@@ -11,15 +8,16 @@ func _init() -> void:
|
||||
@rpc("authority", "reliable")
|
||||
func connect_to_game() -> void:
|
||||
# NOTE: Leave the lobby
|
||||
lobby_ui.client.close_connection()
|
||||
|
||||
Globals.multiplayer_data.wait_for_connection_close()
|
||||
MessageBus.emit_local("lobby_close_connection", null)
|
||||
|
||||
# NOTE: Join target game server
|
||||
# TODO: Don't use libby client... Use a created game client.
|
||||
lobby_ui.client.start_client(
|
||||
Globals.game_data.game_address,
|
||||
Globals.game_data.game_port
|
||||
MessageBus.emit_local(
|
||||
"game_start_client",
|
||||
[
|
||||
Globals.game_data.game_address,
|
||||
Globals.game_data.game_port
|
||||
],
|
||||
true
|
||||
)
|
||||
|
||||
@rpc("authority", "reliable")
|
||||
|
||||
@@ -44,17 +44,6 @@ func init_match():
|
||||
true
|
||||
)
|
||||
|
||||
func wait_for_connection() -> void:
|
||||
while not Globals.lobby_data.lobby_ui.client.peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
while Globals.lobby_data.lobby_ui.client.peer.get_connection_status() != 2:
|
||||
await get_tree().process_frame
|
||||
|
||||
func wait_for_connection_close() -> void:
|
||||
while Globals.lobby_data.lobby_ui.client.peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
|
||||
func set_multiplayer_inactive() -> void:
|
||||
is_multiplayer_active = false
|
||||
|
||||
@@ -2,17 +2,18 @@ class_name Lobby extends LobbyBase
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
Globals.lobby_data.lobby_ui = self
|
||||
|
||||
setup_signals()
|
||||
|
||||
func _exit_tree() -> void:
|
||||
# TODO: Maybe no longer needed...
|
||||
if Globals.multiplayer_data.is_multiplayer_active: return
|
||||
client._do_wait_close_connection()
|
||||
|
||||
if client.peer:
|
||||
client.close_connection()
|
||||
|
||||
func setup_signals() -> void:
|
||||
MessageBus.subscribe("lobby_wait_for_connection", client._do_wait_for_connection)
|
||||
MessageBus.subscribe("lobby_close_connection", client._do_wait_close_connection)
|
||||
|
||||
MessageBus.subscribe("receive_match_list", _on_receive_match_list)
|
||||
MessageBus.subscribe("match_list_activate_entry", _on_match_list_entry_activated)
|
||||
MessageBus.subscribe("match_list_add_entry", _on_match_list_entry_added)
|
||||
@@ -23,7 +24,7 @@ func setup_signals() -> void:
|
||||
func _server_host_started() -> void:
|
||||
host_connect_bttn.visible = false
|
||||
|
||||
# NOTE: 'matches_list' can be empty but so long as we get the call from the
|
||||
# NOTE: 'matches_list' can be empty; so long as we get the call from the
|
||||
# server we know we are connected and thus hide respective UI elements.
|
||||
func _on_receive_match_list(matches_list: Array) -> void:
|
||||
push_warning("Client Joined Host: Full Match List Recieved...\n", matches_list)
|
||||
@@ -36,6 +37,8 @@ func _on_receive_match_list(matches_list: Array) -> void:
|
||||
for match_entry in matches_list:
|
||||
_create_match_entry(match_entry)
|
||||
|
||||
client.lobby_fully_connected = true
|
||||
|
||||
func _on_match_list_entry_activated(match_id: String) -> void:
|
||||
for match_entry in match_list.get_children():
|
||||
match_entry.join_bttn.visible = false
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
[ext_resource type="Script" uid="uid://dk7afnf7ol7dy" path="res://scenes/screens/lobby_screen/lobby.gd" id="1_hhiik"]
|
||||
[ext_resource type="PackedScene" uid="uid://dg7pgj6i5pk8p" path="res://scenes/ui_controls/lobby_screen/lobby_ui.tscn" id="2_lqt48"]
|
||||
[ext_resource type="Script" uid="uid://sihv0kwbcr33" path="res://scenes/screens/lobby_screen/lobby_rpc.gd" id="3_rurl6"]
|
||||
[ext_resource type="Script" uid="uid://ci57tiuqsx1jp" path="res://scripts/client_networking.gd" id="4_glu7g"]
|
||||
[ext_resource type="Script" uid="uid://dv7j35h2ngiq7" path="res://scenes/screens/lobby_screen/lobby_client.gd" id="4_glu7g"]
|
||||
|
||||
[node name="lobby" type="Node"]
|
||||
script = ExtResource("1_hhiik")
|
||||
|
||||
@@ -16,7 +16,7 @@ const MATCH_ENTRY = preload("res://scenes/screens/lobb
|
||||
@onready var new_match_name_input: LineEdit = $ui/body/right_body/create_match_hbox/new_match_name_input
|
||||
|
||||
@onready var rpc_signals: Node = $rpc_signals
|
||||
@onready var client: Node = $client
|
||||
@onready var client: LobbyClient = $client
|
||||
|
||||
var active_mode: String = "snooker"
|
||||
var active_match: HBoxContainer = null
|
||||
@@ -52,7 +52,8 @@ func _on_match_create_bttn_pressed() -> void:
|
||||
"type": active_mode
|
||||
}
|
||||
|
||||
MessageBus.emit_request.rpc_id(1, "match_list_add_entry", match_entry)
|
||||
#MessageBus.emit_request.rpc_id(1, "match_list_add_entry", match_entry)
|
||||
MessageBus.emit_request.rpc("match_list_add_entry", match_entry)
|
||||
|
||||
func _on_mode_bttn_pressed(button: Button) -> void:
|
||||
active_mode = button.text.remove_chars("-").to_lower()
|
||||
|
||||
35
scenes/screens/lobby_screen/lobby_client.gd
Normal file
35
scenes/screens/lobby_screen/lobby_client.gd
Normal file
@@ -0,0 +1,35 @@
|
||||
class_name LobbyClient extends ClientNetworking
|
||||
|
||||
|
||||
var lobby_fully_connected: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
pass
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _do_wait_for_connection() -> void:
|
||||
_wait_for_connection()
|
||||
|
||||
while not lobby_fully_connected:
|
||||
await get_tree().process_frame
|
||||
|
||||
func _do_wait_close_connection():
|
||||
_wait_close_connection()
|
||||
|
||||
lobby_fully_connected = false
|
||||
|
||||
|
||||
func client_connected_to_server() -> void:
|
||||
push_warning("Connected to server!")
|
||||
|
||||
|
||||
func client_disconnected_from_server() -> void:
|
||||
push_warning("Disconnected from, server...")
|
||||
|
||||
func client_connection_failed_to_server() -> void:
|
||||
push_warning("Connection failed!")
|
||||
|
||||
unset_client()
|
||||
1
scenes/screens/lobby_screen/lobby_client.gd.uid
Normal file
1
scenes/screens/lobby_screen/lobby_client.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dv7j35h2ngiq7
|
||||
@@ -17,8 +17,31 @@ func start_client(address: String = "127.0.0.1", port: int = 8080) -> void:
|
||||
multiplayer.server_disconnected.connect(client_disconnected_from_server)
|
||||
multiplayer.connection_failed.connect(client_connection_failed_to_server)
|
||||
|
||||
|
||||
func _wait_for_connection() -> void:
|
||||
while not peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
while peer.get_connection_status() != 2:
|
||||
await get_tree().process_frame
|
||||
|
||||
func _wait_close_connection():
|
||||
close_connection()
|
||||
|
||||
while peer:
|
||||
await get_tree().process_frame
|
||||
|
||||
|
||||
func client_connected_to_server() -> void:
|
||||
push_warning("Connected to server!")
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
func client_disconnected_from_server() -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
|
||||
func client_connection_failed_to_server() -> void:
|
||||
assert(false, "This method needs to be overridden...")
|
||||
|
||||
|
||||
func close_connection() -> void:
|
||||
if not peer: return
|
||||
@@ -27,13 +50,6 @@ func close_connection() -> void:
|
||||
|
||||
unset_client()
|
||||
|
||||
func client_disconnected_from_server() -> void:
|
||||
push_warning("Disconnected from, server...")
|
||||
|
||||
func client_connection_failed_to_server() -> void:
|
||||
push_warning("Connection failed!")
|
||||
|
||||
unset_client()
|
||||
|
||||
func unset_client() -> void:
|
||||
if not peer: return
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class_name Server extends Node
|
||||
class_name ServerNetworking extends Node
|
||||
|
||||
|
||||
var peer: ENetMultiplayerPeer
|
||||
|
||||
Reference in New Issue
Block a user