Send input state out to clients every tick

This commit is contained in:
Tangent Wantwight 2020-06-08 20:09:31 -04:00
parent a16bd95c6a
commit 9fcbc42a41
1 changed files with 27 additions and 1 deletions

View File

@ -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) {