diff options
Diffstat (limited to 'server/src/state.rs')
-rw-r--r-- | server/src/state.rs | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/server/src/state.rs b/server/src/state.rs index 3492e6e7..82540fd6 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -20,11 +20,12 @@ use anyhow::{anyhow, bail, Result}; use clap::{Parser, ValueEnum}; use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID}; use log::{debug, trace}; -use std::time::Duration; +use std::{collections::VecDeque, time::Duration}; use tokio::sync::broadcast::Sender; pub struct State { index: DataIndex, + packet_out: VecDeque<PacketC>, tx: Sender<PacketC>, pub game: Game, } @@ -74,24 +75,37 @@ impl State { let mut index = DataIndex::default(); index.reload()?; + let mut packet_out = VecDeque::new(); let mut game = Game::new(); - game.load(index.generate("lobby-none".to_string()).await?, None); + game.load( + index.generate("lobby-none".to_string()).await?, + None, + &mut packet_out, + ); - Ok(Self { game, index, tx }) + Ok(Self { + game, + index, + tx, + packet_out, + }) } pub async fn tick(&mut self, dt: f32) -> anyhow::Result<()> { - if self.game.tick(dt) { - self.game - .load(self.index.generate("lobby-none".to_string()).await?, None); + if self.game.tick(dt, &mut self.packet_out) { + self.game.load( + self.index.generate("lobby-none".to_string()).await?, + None, + &mut self.packet_out, + ); } - while let Some(p) = self.game.packet_out.pop_front() { + while let Some(p) = self.packet_out.pop_front() { if matches!(p, PacketC::UpdateMap { .. } | PacketC::Position { .. }) { trace!("-> {p:?}"); } else { debug!("-> {p:?}"); } - let _ = self.tx.send(p); + self.tx.send(p).unwrap(); } Ok(()) } @@ -113,15 +127,19 @@ impl State { } _ => (), } - self.game.packet_in(player, packet, &mut replies)?; + self.game + .packet_in(player, packet, &mut replies, &mut self.packet_out)?; if self.game.players.is_empty() && !self.game.lobby { self.tx .send(PacketC::ServerMessage { text: "Game was aborted automatically due to a lack of players".to_string(), }) .ok(); - self.game - .load(self.index.generate("lobby-none".to_string()).await?, None); + self.game.load( + self.index.generate("lobby-none".to_string()).await?, + None, + &mut self.packet_out, + ); } Ok(replies) } @@ -143,7 +161,8 @@ impl State { match command { Command::Start { spec, timer } => { let data = self.index.generate(spec).await?; - self.game.load(data, Some(Duration::from_secs(timer))); + self.game + .load(data, Some(Duration::from_secs(timer)), &mut self.packet_out); } Command::End => { self.tx @@ -158,8 +177,11 @@ impl State { ), }) .ok(); - self.game - .load(self.index.generate("lobby-none".to_string()).await?, None); + self.game.load( + self.index.generate("lobby-none".to_string()).await?, + None, + &mut self.packet_out, + ); } Command::Reload => { if self.game.count_chefs() > 1 { @@ -168,6 +190,7 @@ impl State { self.game.load( self.index.generate(self.game.data.spec.to_string()).await?, None, + &mut self.packet_out, ); } Command::ReloadIndex => { |