update player_id in sent-out SetState messages

This commit is contained in:
Tangent Wantwight 2020-06-08 22:08:36 -04:00
parent 9fcbc42a41
commit 334ecc6eaf

View file

@ -115,9 +115,10 @@ impl Server {
// remove_player(&mut self) in the error case // remove_player(&mut self) in the error case
for slot in 0..self.players.len() { for slot in 0..self.players.len() {
if let Some(ref mut player) = self.players[slot] { if let Some(ref mut player) = self.players[slot] {
let msg_for_player = Server::postprocess_message(slot, &msg);
// don't poll ready; we give the channel enough buffer that an overflow indicates // don't poll ready; we give the channel enough buffer that an overflow indicates
// the client has fallen hopelessly behind. // the client has fallen hopelessly behind.
if player.sender.start_send(msg.clone()).is_err() { if player.sender.start_send(msg_for_player).is_err() {
info!("Client#{} fell behind", slot); info!("Client#{} fell behind", slot);
self.remove_player(slot); self.remove_player(slot);
} }
@ -125,6 +126,21 @@ impl Server {
} }
} }
/// given a reference to a ServerMessage, do any per-player "customization" that
// may be called for, returning an owned instance
fn postprocess_message(player: PlayerId, msg: &ServerMessage) -> ServerMessage {
match msg {
ServerMessage::SetState {
player_id: _,
state,
} => ServerMessage::SetState {
player_id: Some(player),
state: state.clone(),
},
msg => msg.clone(),
}
}
pub fn remove_player(&mut self, player_id: PlayerId) { pub fn remove_player(&mut self, player_id: PlayerId) {
if player_id < self.players.len() && self.players[player_id].is_some() { if player_id < self.players.len() && self.players[player_id].is_some() {
self.players[player_id] = None; self.players[player_id] = None;