diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/src/commands.rs | 9 | ||||
-rw-r--r-- | server/src/server.rs | 15 | ||||
-rw-r--r-- | server/src/state.rs | 40 |
3 files changed, 44 insertions, 20 deletions
diff --git a/server/src/commands.rs b/server/src/commands.rs index 1ffe4cd1..ad5812bd 100644 --- a/server/src/commands.rs +++ b/server/src/commands.rs @@ -18,7 +18,7 @@ use crate::{ entity::{bot::BotDriver, tutorial::Tutorial}, message::TrError, - server::Server, + server::{AnnounceState, Server}, tre, trm, }; use anyhow::Result; @@ -173,11 +173,8 @@ impl Server { .generate_with_book(&spec) .map_err(|e| TrError::Plain(e.to_string()))?; self.load(data, timer.map(Duration::from_secs)); - if !skip_announce { - self.announce_timer = 3.5; - self.packet_out - .push_back(PacketC::Menu(Menu::AnnounceStart)); - self.update_paused(); + if skip_announce { + self.announce_state = AnnounceState::Done } } Command::End => { diff --git a/server/src/server.rs b/server/src/server.rs index 22ad5d23..57b26122 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -47,11 +47,17 @@ pub struct ConnectionData { pub ready: bool, } +pub enum AnnounceState { + Queued, + Running(f32), + Done, +} + pub struct Server { pub tx: Sender<PacketC>, pub connections: HashMap<ConnectionID, ConnectionData>, pub paused: bool, - pub announce_timer: f32, + pub announce_state: AnnounceState, pub game: Game, @@ -326,7 +332,7 @@ impl Server { game: Game::default(), index: DataIndex::load().context("Failed to load data index")?, tx, - announce_timer: 0., + announce_state: AnnounceState::Done, packet_out: VecDeque::new(), connections: HashMap::new(), data: Serverdata::default().into(), @@ -386,6 +392,11 @@ impl Server { }); } self.connections.values_mut().for_each(|c| c.ready = false); + self.announce_state = if self.game.lobby { + AnnounceState::Done + } else { + AnnounceState::Queued + }; self.update_paused(); } diff --git a/server/src/state.rs b/server/src/state.rs index 1d2af247..45e928aa 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -17,30 +17,38 @@ */ use crate::{ message::TrError, - server::{ConnectionData, GameServerExt, Server}, + server::{AnnounceState, ConnectionData, GameServerExt, Server}, tre, trm, ConnectionID, }; use anyhow::Result; -use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID, VERSION}; +use hurrycurry_protocol::{Menu, Message, PacketC, PacketS, PlayerID, VERSION}; use log::{debug, info, trace}; impl Server { pub fn tick_outer(&mut self, dt: f32) -> anyhow::Result<()> { - let should_pause = self.announce_timer > 0.; - if should_pause != self.paused { - self.paused = should_pause; - } if !self.paused { let r = self.tick(dt); if let Some((name, timer)) = r { self.scoreboard.save()?; self.load(self.index.generate_with_book(&name)?, timer); } - } else if self.announce_timer > 0. { - self.announce_timer -= dt; - if self.announce_timer <= 0. { + } + + match &mut self.announce_state { + AnnounceState::Queued if !self.paused => { + self.announce_state = AnnounceState::Running(3.5); + self.packet_out + .push_back(PacketC::Menu(Menu::AnnounceStart)); self.update_paused(); } + AnnounceState::Running(timer) => { + *timer -= dt; + if *timer <= 0. { + self.announce_state = AnnounceState::Done; + self.update_paused(); + } + } + _ => (), } while let Some(p) = self.packet_out.pop_front() { @@ -166,8 +174,12 @@ impl Server { pub fn update_paused(&mut self) { let all_idle = self.connections.values().all(|c| c.idle); - let not_ready = self.connections.values().filter(|c| !c.ready).count(); - let announcing = self.announce_timer > 0.; + let mut not_ready = self.connections.values().filter(|c| !c.ready).count(); + let announcing = matches!(self.announce_state, AnnounceState::Running(_)); + + if self.game.lobby { + not_ready = 0; // not waiting for players in lobby + } let should_pause = all_idle || not_ready > 0 || announcing; @@ -178,7 +190,11 @@ impl Server { s = not_ready.to_string() )) } else if all_idle { - info!("Game paused: All players idle"); + if self.connections.is_empty() { + info!("Game paused: Server empty"); + } else { + info!("Game paused: All players idle"); + } Some(trm!("s.state.paused.all_idle")) } else if announcing { info!("Game paused: Waiting for announcement"); |