implement target change propagation

This commit is contained in:
Aubrey 2025-01-04 02:00:17 -06:00
parent 4f75b3c156
commit 7d2549a650
No known key found for this signature in database
2 changed files with 29 additions and 7 deletions

View file

@ -40,7 +40,8 @@ pub async fn broadcast_packet(packet: Packet) {
} }
} }
PacketData::Disconnect(..) => { 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) => { PacketData::Player(player) => {
let _ = manager() let _ = manager()

View file

@ -59,7 +59,10 @@ impl ProximityPlayer {
if let Ok((name, global_speak)) = listener.send(GetStartInfo).await { 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(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(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(), id: FromZeros::new_zeroed(),
connected: true, connected: true,
name: FromZeros::new_zeroed(), 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 *id != self.id {
if let Ok(Ok((name, global_speak))) = listener if let Ok(Ok((name, global_speak))) = listener
.send(PeerConnectionChanged { .send(PeerConnectionChanged {
@ -156,7 +159,7 @@ impl Actor for ProximityPlayer {
} }
async fn stopped(self) -> Self::Stop { async fn stopped(self) -> Self::Stop {
listeners().write().await.remove(&self.id); 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 { if *listener.0 != self.id {
let _ = listener let _ = listener
.1 .1
@ -241,6 +244,24 @@ impl Handler<Event> for ProximityPlayer {
} }
Event::TargetChanged(changed) => { Event::TargetChanged(changed) => {
self.name = changed.name; 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<PeerConnectionChanged> for ProximityPlayer {
id: message.id, id: message.id,
connected: message.connected, connected: message.connected,
name: message.name, name: message.name,
global_speak: message.global_speak global_speak: message.global_speak,
}, },
}; };
@ -537,7 +558,7 @@ pub mod packet {
}), }),
3 => Self::TargetChanged(TargetChanged { 3 => Self::TargetChanged(TargetChanged {
name: read_fixed_string(recv).await?, 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}"), kind => bail!("invalid kind: {kind}"),
}; };