From 7d2549a650e5017ec2f86ed1da0ee66bd8b05609 Mon Sep 17 00:00:00 2001 From: Aubrey Taylor Date: Sat, 4 Jan 2025 02:00:17 -0600 Subject: [PATCH] implement target change propagation --- src/player.rs | 3 ++- src/server/prox.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/player.rs b/src/player.rs index f2ca721..0cc6763 100644 --- a/src/player.rs +++ b/src/player.rs @@ -40,7 +40,8 @@ pub async fn broadcast_packet(packet: Packet) { } } PacketData::Disconnect(..) => { - manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap(); + if packet.user_id != 1 { + manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap();} } PacketData::Player(player) => { let _ = manager() diff --git a/src/server/prox.rs b/src/server/prox.rs index 9305d0f..9f76bb9 100644 --- a/src/server/prox.rs +++ b/src/server/prox.rs @@ -59,7 +59,10 @@ impl ProximityPlayer { if let Ok((name, global_speak)) = listener.send(GetStartInfo).await { send.write_all(id.as_bytes()).await.expect("failed to write peer id"); send.write_all(name.as_bytes()).await.expect("failed to write peer name"); - send.write_all(&[if global_speak { 1 } else { 0 }]).await.expect("failed to write peer global speaking status"); + send + .write_all(&[if global_speak { 1 } else { 0 }]) + .await + .expect("failed to write peer global speaking status"); } } } @@ -121,11 +124,11 @@ impl Actor for ProximityPlayer { id: FromZeros::new_zeroed(), connected: true, name: FromZeros::new_zeroed(), - global_speak: false + global_speak: false, }, }; - for (id, listener) in listeners().write().await.iter() { + for (id, listener) in listeners().read().await.iter() { if *id != self.id { if let Ok(Ok((name, global_speak))) = listener .send(PeerConnectionChanged { @@ -156,7 +159,7 @@ impl Actor for ProximityPlayer { } async fn stopped(self) -> Self::Stop { listeners().write().await.remove(&self.id); - for listener in listeners().write().await.iter() { + for listener in listeners().read().await.iter() { if *listener.0 != self.id { let _ = listener .1 @@ -241,6 +244,24 @@ impl Handler for ProximityPlayer { } Event::TargetChanged(changed) => { self.name = changed.name; + self.global_speak = changed.global_speak; + + for (id, listener) in listeners().read().await.iter() { + if *id != self.id { + if let Err(error) = listener + .send(PeerConnectionChanged { + id: self.id, + connected: true, + name: self.name, + global_speak: self.global_speak, + }) + .detach() + .await + { + warn!("listener {id} is dead: {error}") + } + } + } } } } @@ -295,7 +316,7 @@ impl Handler for ProximityPlayer { id: message.id, connected: message.connected, name: message.name, - global_speak: message.global_speak + global_speak: message.global_speak, }, }; @@ -537,7 +558,7 @@ pub mod packet { }), 3 => Self::TargetChanged(TargetChanged { name: read_fixed_string(recv).await?, - global_speak: recv.read_u8().await? != 0 + global_speak: recv.read_u8().await? != 0, }), kind => bail!("invalid kind: {kind}"), };