mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat(multiplayer): notify clients before server shutdown
This commit is contained in:
@@ -17,6 +17,7 @@ signal player_left(peer_id: int)
|
||||
signal player_state_changed(peer_id: int, team: int, ready: bool)
|
||||
signal rejected(reason: String) # client-side only: the server refused our hello
|
||||
signal welcomed() # client-side only: our hello was accepted
|
||||
signal server_shutdown(reason: String) # client-side notification before planned close
|
||||
|
||||
const TEAM_COUNT := 2
|
||||
const RECONNECT_GRACE_SECONDS := 60.0
|
||||
@@ -215,6 +216,26 @@ func _broadcast_player_left(peer_id: int) -> void:
|
||||
_player_left.rpc_id(other_peer_id, peer_id)
|
||||
|
||||
|
||||
func broadcast_server_shutdown(reason: String) -> void:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
var safe_reason := _sanitize_shutdown_reason(reason)
|
||||
for peer_id in multiplayer.get_peers():
|
||||
_server_shutdown.rpc_id(peer_id, safe_reason)
|
||||
|
||||
|
||||
static func _sanitize_shutdown_reason(raw: String) -> String:
|
||||
var clean := ""
|
||||
for c in raw:
|
||||
var code := c.unicode_at(0)
|
||||
if code >= 0x20 and code != 0x7F:
|
||||
clean += c
|
||||
clean = clean.strip_edges()
|
||||
if clean.length() > 96:
|
||||
clean = clean.substr(0, 96)
|
||||
return clean if not clean.is_empty() else "server_shutdown"
|
||||
|
||||
|
||||
# Balances a new joiner onto whichever team currently has fewer players
|
||||
# (ties go to team 0). Server only.
|
||||
func _pick_balanced_team() -> int:
|
||||
@@ -488,6 +509,11 @@ func _rejected(reason: String) -> void:
|
||||
rejected.emit(reason)
|
||||
|
||||
|
||||
@rpc("authority", "call_remote", "reliable")
|
||||
func _server_shutdown(reason: String) -> void:
|
||||
server_shutdown.emit(_sanitize_shutdown_reason(reason))
|
||||
|
||||
|
||||
@rpc("authority", "call_remote", "reliable")
|
||||
func _player_joined(peer_id: int, player_name: String, team: int, ready: bool) -> void:
|
||||
roster[peer_id] = PlayerInfo.new(peer_id, player_name, team, ready)
|
||||
|
||||
Reference in New Issue
Block a user