From 42438914e10bb60bfa00c5d30b8e7320acd835f2 Mon Sep 17 00:00:00 2001 From: Sofia Date: Fri, 14 Aug 2026 23:40:37 +0300 Subject: [PATCH] Make finishing game possible from client with host privileges --- rust/src/game/mod.rs | 4 ++++ rust/src/net/protocol.rs | 14 +++++++++++++- rust/src/player/local_player.rs | 9 ++++++++- rust/src/ui/hud.rs | 6 ++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/rust/src/game/mod.rs b/rust/src/game/mod.rs index 59acb6b..2a5d3d4 100644 --- a/rust/src/game/mod.rs +++ b/rust/src/game/mod.rs @@ -497,6 +497,10 @@ impl Game { } pub fn finish_game(&mut self) { + if !self.game_started { + return; + } + if let Some(recorder) = &mut self.replay_recorder { recorder.bind_mut().stop_recording(); } diff --git a/rust/src/net/protocol.rs b/rust/src/net/protocol.rs index 57d6a99..e520049 100644 --- a/rust/src/net/protocol.rs +++ b/rust/src/net/protocol.rs @@ -1,6 +1,6 @@ use std::net::SocketAddr; -use godot::obj::Singleton; +use godot::{global::godot_print, obj::Singleton}; use teanet::PeerMessage; use crate::{ @@ -595,6 +595,18 @@ impl NetworkManager { } } } + Package::FinishGame => { + if let Some(game) = &mut Game::singleton() { + let player = + game.bind().find_player_by_addr(&conn.address).cloned(); + if let Some(player) = player + && player.has_host_privileges + { + godot_print!("Finishing game"); + game.bind_mut().finish_game(); + } + } + } _ => {} }, }, diff --git a/rust/src/player/local_player.rs b/rust/src/player/local_player.rs index b39fe8e..b54f991 100644 --- a/rust/src/player/local_player.rs +++ b/rust/src/player/local_player.rs @@ -871,7 +871,14 @@ impl LocalPlayer { #[func] pub fn exit_to_lobby(&mut self) { if let Some(game) = &mut Game::singleton() { - game.bind_mut().finish_game(); + if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer { + match peer { + PeerKind::Client(peer, socket_addr) => { + peer.send_reliable(socket_addr, Package::FinishGame); + } + PeerKind::Server(..) => game.bind_mut().finish_game(), + } + } } } diff --git a/rust/src/ui/hud.rs b/rust/src/ui/hud.rs index 3535e87..e3a8809 100644 --- a/rust/src/ui/hud.rs +++ b/rust/src/ui/hud.rs @@ -43,8 +43,10 @@ pub struct Hud { impl IControl for Hud { fn ready(&mut self) { self.run_deferred(|s| { - if let Some(exit_to_lobby) = &mut s.exit_to_lobby { - exit_to_lobby.set_visible(NetworkManager::singleton().bind().is_host()); + if let Some(exit_to_lobby) = &mut s.exit_to_lobby + && let Some(game) = &Game::singleton() + { + exit_to_lobby.set_visible(game.bind().has_host_privileges()); } });