Add host privileges that propagates to client

This commit is contained in:
Sofia 2026-08-14 23:07:00 +03:00
parent abc89c1e3e
commit f32a43a826
3 changed files with 45 additions and 25 deletions

View File

@ -353,9 +353,37 @@ impl Game {
}
pub fn set_host_privileges(&mut self, privileges: bool) {
if privileges {
self.set_ready(true);
}
self.host_privileges = privileges;
}
pub fn has_host_privileges(&self) -> bool {
self.is_host || self.host_privileges
}
pub fn set_ready(&mut self, ready: bool) {
if let Some(self_id) = self.self_id {
self.run_deferred(move |s| {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(peer, socket_addr) => {
godot_print!("Sent ready {}", ready);
peer.send_reliable(&socket_addr, Package::SetReady(self_id, ready))
}
PeerKind::Server(peer, _) => {
if let Some(player) = s.find_player_mut(self_id) {
player.data.ready = ready;
}
peer.broadcast_reliable(Package::SetReady(self_id, ready));
}
}
}
});
}
}
pub fn start_playback(&mut self, time_delta: f64, spectated_id: u16) {
if let Some(recorder) = &self.replay_recorder
&& self.self_id.is_some()
@ -454,14 +482,10 @@ impl Game {
recorder.queue_free();
}
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer
&& let PeerKind::Server(peer, _) = peer
&& let Some(self_id) = self.self_id
&& let Some(player) = self.find_player_mut(self_id)
{
player.data.ready = true;
peer.broadcast_reliable(Package::SetReady(player.id(), true));
if self.has_host_privileges() {
self.set_ready(true);
}
for player in &mut self.players {
player.stats = Default::default();
}

View File

@ -102,11 +102,10 @@ impl NetworkManager {
game.bind_mut().set_players(
players.into_iter().map(|p| p.clone().into()).collect(),
);
game.bind_mut().set_self(self_id);
if host_privileges {
game.bind_mut().set_host_privileges(true);
}
game.bind_mut().set_self(self_id);
}
}
Package::SetNick(id, nick) => {

View File

@ -108,10 +108,7 @@ impl IPanel for LobbyPanel {
game.signals().options_changed().connect_other(self, |s| {
s.run_deferred(|s| {
if let Some(game) = Game::singleton() {
s.on_game_opts_update(
&game.bind().opts,
NetworkManager::singleton().bind().is_host(),
);
s.on_game_opts_update(&game.bind().opts, game.bind().has_host_privileges());
}
});
});
@ -314,10 +311,8 @@ impl LobbyPanel {
}
fn update_ready_button(&mut self) {
if let Some(game) = Game::singleton()
&& let Some(peer) = &NetworkManager::singleton().bind().peer
{
if let PeerKind::Server(_, _) = peer
if let Some(game) = Game::singleton() {
if game.bind().has_host_privileges()
&& let Some(button) = &mut self.ready_button
{
let all_ready = game.bind().players.iter().all(|p| p.data.ready);
@ -388,8 +383,8 @@ impl LobbyPanel {
}
pub fn on_ready(&mut self) {
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
peer.is_host()
let is_host = if let Some(game) = &Game::singleton() {
game.bind().has_host_privileges()
} else {
false
};
@ -397,10 +392,8 @@ impl LobbyPanel {
self.is_ready = !self.is_ready;
self.update_ready_button();
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
if let PeerKind::Client(peer, socket_addr) = peer {
peer.send_reliable(&socket_addr, Package::SetReady(0, self.is_ready));
}
if let Some(game) = &mut Game::singleton() {
game.bind_mut().set_ready(self.is_ready);
}
} else {
if let Some(game) = &mut Game::singleton() {
@ -491,7 +484,9 @@ impl LobbyPanel {
GameOptionValue::Boolean(value) => {
let mut checkbox = CheckBox::new_alloc();
checkbox.set_pressed(*value);
checkbox.set_disabled(!NetworkManager::singleton().bind().is_host());
if let Some(game) = &Game::singleton() {
checkbox.set_disabled(!game.bind().has_host_privileges());
}
let checkbox_clone = checkbox.clone();
let option = option.clone();
@ -515,7 +510,9 @@ impl LobbyPanel {
GameOptionValue::Number(value) => {
let mut spinbox = SpinBox::new_alloc();
spinbox.set_value(*value);
spinbox.set_editable(NetworkManager::singleton().bind().is_host());
if let Some(game) = &Game::singleton() {
spinbox.set_editable(game.bind().has_host_privileges());
}
let option = option.clone();
spinbox