Refactor message bus and lobby networking flow

* Add local message bus emission with optional argument expansion
* Improve message type validation and callback dispatch
* Simplify lobby match join/disconnect handling
* Move lobby server networking toward an overridable `ServerNetworking` base class
* Update lobby scene to use the dedicated lobby server script
* Remove obsolete window configuration and unused RPC stubs
This commit is contained in:
2026-08-09 18:36:28 -05:00
parent 5ab970aec5
commit 1eb057fefd
8 changed files with 76 additions and 64 deletions

View File

@@ -11,46 +11,49 @@ 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
# about server talking to clients than clients to server. I.E, reducing
# the number of needed @rpc method decorator bindings.
# the number of needed @rpc method decorator bindings.
@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:
callback.callv(data)
@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:

View File

@@ -21,10 +21,6 @@ Globals="*res://globals/globals.gd"
InstanceLauncher="*res://globals/instance_launcher.gd"
MessageBus="*res://globals/message_bus.gd"
[display]
window/size/always_on_top=true
[rendering]
renderer/rendering_method="gl_compatibility"

View File

@@ -81,6 +81,14 @@ func match_list_remove_entry(match_id: String) -> void:
_delete_from_match_pool(match_id)
func match_list_join_entry(match_id: String) -> void:
if active_match_pool.has(match_id):
push_error("Client Joining Match List Entry Failed. Already consumed. ", match_id)
# TODO: Need to signal to prospective client that match has been taken.
# Server is in the midst of cleanup for it once clients disconnect
# to join generated game server...
return
var match_entry = match_pool.get(match_id)
if not match_entry: return
@@ -100,22 +108,14 @@ func match_list_join_entry(match_id: String) -> void:
client2.partner_id = client1.id
active_match_pool[match_id] = match_entry
# NOTE: Cleanup of match info and client info
# will/should happen on their disconnect...
client_pool.erase(client1.id)
client_pool.erase(client2.id)
# TODO: Need to look into moving this out of here.
# Maybe just have clients drop and internally call on their own?
# Maybe just check here if we can even launch the game server?
_launch_game_server(match_entry, client1, client2)
# NOTE: Needed for hash check to pass between client and server but isn't used by server...
@rpc("authority", "reliable")
func connect_to_game() -> void:
pass
@rpc("authority", "reliable")
func go_back_to_match_screen() -> void:
pass
func server_client_connected(id: int) -> void:
push_warning("Client Connected... ID: ", id)
@@ -134,32 +134,29 @@ func server_client_disconnected(id: int) -> void:
push_warning("Client Disconnected... ID: ", id)
var client1 = client_pool.get(id)
if not client1: return
if not client1: return
var client2 = client_pool.get( client1.partner_id )
var match_entry = _delete_from_match_pool(client1.match_id)
if not match_entry: return
if not client1.match_id.is_empty() and not match_entry.is_empty():
MessageBus.emit_propagation(
"match_list_remove_entry", match_entry.match_id
)
push_warning("Removing Match... Match: ", match_entry)
for client in client_pool.values():
if client.in_match: continue
if client.id == id: continue
MessageBus.emit_local(
"match_list_remove_entry", match_entry.match_id
)
MessageBus.emit_propagation_clients_only.rpc_id(
client.id, "match_list_remove_entry", match_entry.match_id
)
match_pool.erase(client1.match_id)
MessageBus.emit_propagation_clients_only.rpc(
"match_list_remove_entry", match_entry.match_id
)
client_pool.erase(client1.id)
if not client2: return
if active_match_pool.get(client2.match_id):
match_pool.erase(client2.match_id)
var _id = client2.id
client2 = CLIENT_DICT.duplicate_deep()
client2.id = _id
# NOTE: Needed for hash check to pass between client and server but isn't used by server...
@rpc("authority", "reliable")
func connect_to_game() -> void:
pass
@rpc("authority", "reliable")
func go_back_to_match_screen() -> void:
pass

View File

@@ -56,12 +56,8 @@ func _on_match_list_entry_removed(match_id: String) -> void:
match_entry.queue_free()
func _on_match_list_join_entry(match_id: String) -> void:
Globals.lobby_data.match_list_join_entry(match_id)
push_warning("Client Joining Match List Entry... ", match_id)
if Globals.lobby_data.match_pool.has(match_id): return
_on_match_list_entry_removed(match_id)
Globals.lobby_data.match_list_join_entry(match_id)
func _create_match_entry(match_entry: Dictionary) -> void:
var container = MATCH_ENTRY.instantiate()

View File

@@ -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://rb6av5eg4u0" path="res://scenes/ui_controls/lobby_screen/lobby_ui.tscn" id="2_lqt48"]
[ext_resource type="Script" uid="uid://6sfwe1hwgf0g" path="res://scenes/screens/lobby_screen/lobby_rpc.gd" id="3_rurl6"]
[ext_resource type="Script" uid="uid://bvqtgf3vbsoj4" path="res://scripts/server_networking.gd" id="4_glu7g"]
[ext_resource type="Script" uid="uid://crbm3v4td6du6" path="res://scenes/screens/lobby_screen/lobby_server.gd" id="4_glu7g"]
[node name="lobby_screen" type="Control"]
layout_mode = 3

View File

@@ -0,0 +1,19 @@
class_name Lobbyserver extends ServerNetworking
func _ready() -> void:
pass
func _process(_delta: float) -> void:
pass
func server_client_connected(id: int) -> void:
Globals.lobby_data.server_client_connected(id)
func server_client_disconnected(id: int) -> void:
# NOTE: Wait for clients to fully disconnect...
var timer = get_tree().create_timer(1)
await timer.timeout
Globals.lobby_data.server_client_disconnected(id)

View File

@@ -0,0 +1 @@
uid://crbm3v4td6du6

View File

@@ -1,4 +1,4 @@
extends Node
class_name ServerNetworking extends Node
var peer: ENetMultiplayerPeer
@@ -44,8 +44,8 @@ func close_connection() -> void:
peer.close()
peer = null
func server_client_connected(id: int) -> void:
Globals.lobby_data.server_client_connected(id)
func server_client_connected(_id: int) -> void:
assert(false, "This method needs to be overridden...")
func server_client_disconnected(id: int) -> void:
Globals.lobby_data.server_client_disconnected(id)
func server_client_disconnected(_id: int) -> void:
assert(false, "This method needs to be overridden...")