From 9fcbc42a4189eed27025e6858a83567e77e72d4c Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Mon, 8 Jun 2020 20:09:31 -0400 Subject: [PATCH] Send input state out to clients every tick --- src/net/server.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/net/server.rs b/src/net/server.rs index 161972f..cef3c0f 100644 --- a/src/net/server.rs +++ b/src/net/server.rs @@ -96,7 +96,33 @@ impl Server { } fn tick(&mut self, tick: Instant) { - trace!("Tick {:?}", tick) + trace!("Tick {:?}", tick); + + let total_input = self + .players + .iter() + .map(|player| match player { + Some(PlayerState { input, .. }) => input.clone(), + None => json!([]), + }) + .collect(); + + self.broadcast(ServerMessage::Input { total_input }); + } + + fn broadcast(&mut self, msg: ServerMessage) { + // iterate by index instead of iterator, because we need to call + // remove_player(&mut self) in the error case + for slot in 0..self.players.len() { + if let Some(ref mut player) = self.players[slot] { + // don't poll ready; we give the channel enough buffer that an overflow indicates + // the client has fallen hopelessly behind. + if player.sender.start_send(msg.clone()).is_err() { + info!("Client#{} fell behind", slot); + self.remove_player(slot); + } + } + } } pub fn remove_player(&mut self, player_id: PlayerId) {