Set host privileges to new player after old host leaves

This commit is contained in:
Sofia 2026-08-15 00:11:14 +03:00
parent 42438914e1
commit 97ee080970
4 changed files with 90 additions and 14 deletions

View File

@ -341,6 +341,8 @@ impl Game {
pub fn on_pickup_ball(player_id: u16); pub fn on_pickup_ball(player_id: u16);
#[signal] #[signal]
pub fn on_drop_ball(); pub fn on_drop_ball();
#[signal]
pub fn on_host_privileges_changed();
pub fn singleton() -> Option<Gd<Game>> { pub fn singleton() -> Option<Gd<Game>> {
if GameManager::singleton().get_children().len() > 0 { if GameManager::singleton().get_children().len() > 0 {
@ -352,11 +354,15 @@ impl Game {
} }
} }
pub fn set_host_privileges(&mut self, privileges: bool) { pub fn set_host_privileges(&mut self, player_id: u16, privileges: bool) {
if privileges { if self.self_id.map(|s| s == player_id).unwrap_or(false) {
self.set_ready(true); self.set_ready(privileges);
self.host_privileges = privileges;
} }
self.host_privileges = privileges; if let Some(player) = self.find_player_mut(player_id) {
player.data.has_host_privileges = privileges;
}
self.signals().on_host_privileges_changed().emit();
} }
pub fn has_host_privileges(&self) -> bool { pub fn has_host_privileges(&self) -> bool {
@ -705,6 +711,7 @@ impl Game {
ping: 0, ping: 0,
team: 0, team: 0,
ready: false, ready: false,
has_host_privileges: host_privileges,
}, },
stats: Default::default(), stats: Default::default(),
killing_spree: 0, killing_spree: 0,
@ -712,7 +719,6 @@ impl Game {
character: None, character: None,
last_held_weapon: WeaponType::Raygun, last_held_weapon: WeaponType::Raygun,
last_position: Vector3::ZERO, last_position: Vector3::ZERO,
has_host_privileges: host_privileges,
}); });
self.run_deferred(move |s| s.signals().on_new_player().emit(id)); self.run_deferred(move |s| s.signals().on_new_player().emit(id));
@ -790,6 +796,25 @@ impl Game {
} }
self.players.retain(|p| p.id() != id); self.players.retain(|p| p.id() != id);
self.run_deferred(move |s| s.signals().on_player_disconnected().emit(id)); self.run_deferred(move |s| s.signals().on_player_disconnected().emit(id));
self.run_deferred(|s| {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
&& let PeerKind::Server(peer, _) = peer
{
let players_with_host = s
.players
.iter()
.filter(|p| p.data.has_host_privileges)
.count();
if players_with_host == 0
&& let Some(player) = s.players.iter_mut().next()
{
player.data.has_host_privileges = true;
peer.broadcast_reliable(Package::SetHostPrivileges(player.id(), true));
s.signals().on_host_privileges_changed().emit();
}
}
});
} }
pub fn update_player_nick(&mut self, player_id: u16, nick: String) { pub fn update_player_nick(&mut self, player_id: u16, nick: String) {
@ -1700,7 +1725,6 @@ pub struct Player {
pub killing_spree_cd: f64, pub killing_spree_cd: f64,
pub last_held_weapon: WeaponType, pub last_held_weapon: WeaponType,
pub last_position: Vector3, pub last_position: Vector3,
pub has_host_privileges: bool,
} }
impl Player { impl Player {
@ -1874,6 +1898,7 @@ pub struct NetPlayer {
pub name: String, pub name: String,
pub ping: u128, pub ping: u128,
pub team: u8, pub team: u8,
pub has_host_privileges: bool,
} }
impl From<NetPlayer> for Player { impl From<NetPlayer> for Player {
@ -1887,7 +1912,6 @@ impl From<NetPlayer> for Player {
killing_spree_cd: 0., killing_spree_cd: 0.,
last_held_weapon: WeaponType::Raygun, last_held_weapon: WeaponType::Raygun,
last_position: Vector3::ZERO, last_position: Vector3::ZERO,
has_host_privileges: false,
} }
} }
} }

View File

@ -368,6 +368,7 @@ pub enum Package {
SelectMap(u8), SelectMap(u8),
GameStarted, GameStarted,
MapLoaded, MapLoaded,
SetHostPrivileges(u16, bool),
// Game packages // Game packages
SpawnPlayer(u16, NetTransform), SpawnPlayer(u16, NetTransform),

View File

@ -104,10 +104,16 @@ impl NetworkManager {
); );
game.bind_mut().set_self(self_id); game.bind_mut().set_self(self_id);
if host_privileges { if host_privileges {
game.bind_mut().set_host_privileges(true); game.bind_mut().set_host_privileges(self_id, true);
} }
} }
} }
Package::SetHostPrivileges(player_id, host_privileges) => {
if let Some(game) = &mut Game::singleton() {
game.bind_mut()
.set_host_privileges(player_id, host_privileges);
}
}
Package::SetNick(id, nick) => { Package::SetNick(id, nick) => {
if let Some(game) = &mut Game::singleton() { if let Some(game) = &mut Game::singleton() {
game.bind_mut().update_player_nick(id, nick); game.bind_mut().update_player_nick(id, nick);
@ -501,7 +507,7 @@ impl NetworkManager {
if let Some(player) = player { if let Some(player) = player {
if game.bind().opts.is_true(GameOption::AllowAnyTeam) if game.bind().opts.is_true(GameOption::AllowAnyTeam)
|| player.id() == player_id || player.id() == player_id
|| player.has_host_privileges || player.data.has_host_privileges
{ {
game.bind_mut().try_set_player_team(player_id, team); game.bind_mut().try_set_player_team(player_id, team);
} }
@ -515,7 +521,7 @@ impl NetworkManager {
if game.bind().opts.is_true(GameOption::AllowAnyMap) { if game.bind().opts.is_true(GameOption::AllowAnyMap) {
game.bind_mut().change_map_selection(map_idx, false); game.bind_mut().change_map_selection(map_idx, false);
} else if let Some(player) = player } else if let Some(player) = player
&& player.has_host_privileges && player.data.has_host_privileges
{ {
game.bind_mut().change_map_selection(map_idx, false); game.bind_mut().change_map_selection(map_idx, false);
} }
@ -567,7 +573,7 @@ impl NetworkManager {
let player = let player =
game.bind().find_player_by_addr(&conn.address).cloned(); game.bind().find_player_by_addr(&conn.address).cloned();
if let Some(player) = player if let Some(player) = player
&& player.has_host_privileges && player.data.has_host_privileges
{ {
game.bind_mut().start_game(); game.bind_mut().start_game();
} }
@ -578,7 +584,7 @@ impl NetworkManager {
let player = let player =
game.bind().find_player_by_addr(&conn.address).cloned(); game.bind().find_player_by_addr(&conn.address).cloned();
if let Some(player) = player if let Some(player) = player
&& player.has_host_privileges && player.data.has_host_privileges
{ {
game.bind_mut().change_option(key, value, false); game.bind_mut().change_option(key, value, false);
} }
@ -589,7 +595,7 @@ impl NetworkManager {
let player = let player =
game.bind().find_player_by_addr(&conn.address).cloned(); game.bind().find_player_by_addr(&conn.address).cloned();
if let Some(player) = player if let Some(player) = player
&& player.has_host_privileges && player.data.has_host_privileges
{ {
game.bind_mut().change_team_option(team, key, value, false); game.bind_mut().change_team_option(team, key, value, false);
} }
@ -600,7 +606,7 @@ impl NetworkManager {
let player = let player =
game.bind().find_player_by_addr(&conn.address).cloned(); game.bind().find_player_by_addr(&conn.address).cloned();
if let Some(player) = player if let Some(player) = player
&& player.has_host_privileges && player.data.has_host_privileges
{ {
godot_print!("Finishing game"); godot_print!("Finishing game");
game.bind_mut().finish_game(); game.bind_mut().finish_game();

View File

@ -130,6 +130,16 @@ impl IPanel for LobbyPanel {
}); });
}); });
game.signals()
.on_host_privileges_changed()
.connect_other(self, |s| {
s.run_deferred(|s| {
if let Some(game) = Game::singleton() {
s.set_options_disabled(!game.bind().has_host_privileges());
}
});
});
let is_host = if let Some(game) = &Game::singleton() { let is_host = if let Some(game) = &Game::singleton() {
game.bind().has_host_privileges() game.bind().has_host_privileges()
} else { } else {
@ -262,6 +272,26 @@ impl LobbyPanel {
NetworkManager::singleton().bind_mut().disconnect(); NetworkManager::singleton().bind_mut().disconnect();
} }
fn set_options_disabled(&mut self, disabled: bool) {
if let Some(game) = &Game::singleton() {
for (option, value) in &game.bind().opts.values {
if let Some(control) = self.option_nodes.get(option) {
set_control_disabled(control.clone(), value.clone(), disabled);
}
}
for (team_id, opts) in game.bind().opts.teams.iter().enumerate() {
if let Some(team_controls) = self.team_option_nodes.get(&(team_id as u8)) {
for (option, value) in &opts.values {
if let Some(control) = team_controls.get(option) {
set_control_disabled(control.clone(), value.clone(), disabled);
}
}
}
}
self.on_game_opts_update(&game.bind().opts, game.bind().has_host_privileges());
}
}
fn use_alt_colors_changed(&mut self, value: bool) { fn use_alt_colors_changed(&mut self, value: bool) {
if let Some(game) = &mut Game::singleton() { if let Some(game) = &mut Game::singleton() {
game.bind_mut().private_opts.use_alternate_team_colors = value; game.bind_mut().private_opts.use_alternate_team_colors = value;
@ -578,3 +608,18 @@ fn update_control(control: Gd<Control>, value: GameOptionValue) {
} }
} }
} }
fn set_control_disabled(control: Gd<Control>, value: GameOptionValue, disabled: bool) {
match value {
GameOptionValue::Boolean(_) => {
if let Ok(mut checkbox) = control.try_cast::<CheckBox>() {
checkbox.set_disabled(disabled);
}
}
GameOptionValue::Number(_) => {
if let Ok(mut spinbox) = control.try_cast::<SpinBox>() {
spinbox.set_editable(!disabled);
}
}
}
}