add global speak flag

This commit is contained in:
Aubrey 2025-01-02 19:03:58 -06:00
parent f36b46bfdc
commit 71e6ca35c7
No known key found for this signature in database
2 changed files with 39 additions and 21 deletions

View file

@ -29,6 +29,7 @@ pub async fn broadcast_packet(packet: Packet) {
match packet.data { match packet.data {
PacketData::Connect(connect) => { PacketData::Connect(connect) => {
if packet.user_id != 1 {
manager() manager()
.send(PlayerConnected { .send(PlayerConnected {
id: packet.user_id, id: packet.user_id,
@ -37,6 +38,7 @@ pub async fn broadcast_packet(packet: Packet) {
.await .await
.unwrap(); .unwrap();
} }
}
PacketData::Disconnect(..) => { PacketData::Disconnect(..) => {
manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap(); manager().send(PlayerDisconnected { id: packet.user_id }).await.unwrap();
} }
@ -95,7 +97,7 @@ impl PlayerActor {
return; return;
}; };
write_packet(&mut writer, user_id, PacketData::Init(Init { max_players: 8 })) write_packet(&mut writer, user_id, PacketData::Init(Init { max_players: 10 }))
.await .await
.expect("msfrarausfhsdagsdgkog"); .expect("msfrarausfhsdagsdgkog");
@ -107,19 +109,18 @@ impl PlayerActor {
tokio::spawn(scoped(&address.downgrade(), { tokio::spawn(scoped(&address.downgrade(), {
let address = address.downgrade(); let address = address.downgrade();
async move { async move {
// sending packets to the web app or setting up udp
let ip = addr.ip(); let ip = addr.ip();
let mut addr = None; let mut addr = None;
while let Some(message) = receiver.recv().await { while let Some(message) = receiver.recv().await {
match message { match message {
WriteMessage::Data(Packet { user_id, udp, data }) => { WriteMessage::Data(Packet { user_id, udp, data }) => {
// trace!("writing packet {udp:?}, {data:?}");
let res = if udp && addr.is_some() { let res = if udp && addr.is_some() {
async { async {
let Some(addr) = addr else { let Some(addr) = addr else {
trace!("no address set yet"); trace!("no address set yet");
return Ok(()); return Ok(());
}; };
// trace!("sending udp packet: {data:?}");
let mut buf = [0; 256]; let mut buf = [0; 256];
let mut writer = Cursor::new(buf.as_mut_slice()); let mut writer = Cursor::new(buf.as_mut_slice());
@ -154,9 +155,11 @@ impl PlayerActor {
} }
.instrument(span.clone()) .instrument(span.clone())
})); }));
tokio::spawn(scoped( tokio::spawn(scoped(
&address.downgrade(), &address.downgrade(),
{ {
// read packets and send them to the actor
let address = address.downgrade(); let address = address.downgrade();
async move { async move {
info!("connected {user_id}"); info!("connected {user_id}");
@ -177,7 +180,9 @@ impl PlayerActor {
} }
.instrument(span.clone()), .instrument(span.clone()),
)); ));
clients().write().await.insert(user_id, (MessageChannel::new(address), addr)); clients().write().await.insert(user_id, (MessageChannel::new(address), addr));
xtra::run( xtra::run(
mailbox, mailbox,
PlayerActor { PlayerActor {

View file

@ -4,7 +4,7 @@ use packet::{
Event, Event,
Event_variants::{Answer, Candidate, Offer}, Event_variants::{Answer, Candidate, Offer},
}; };
use tokio::io::AsyncWriteExt; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::{error, info, info_span, trace, warn, Instrument}; use tracing::{error, info, info_span, trace, warn, Instrument};
use wtransport::endpoint::IncomingSession; use wtransport::endpoint::IncomingSession;
use xtra::{Actor, Address, Handler, Mailbox}; use xtra::{Actor, Address, Handler, Mailbox};
@ -19,6 +19,7 @@ use super::{
pub struct ProximityPlayer { pub struct ProximityPlayer {
id: UuidString, id: UuidString,
name: String<CLIENT_NAME_SIZE>, name: String<CLIENT_NAME_SIZE>,
global_speak: bool,
send: wtransport::SendStream, send: wtransport::SendStream,
connection: Arc<wtransport::Connection>, connection: Arc<wtransport::Connection>,
} }
@ -37,6 +38,7 @@ impl ProximityPlayer {
recv.read_exact(id.as_mut_bytes()).await.expect("failed to read uuid"); recv.read_exact(id.as_mut_bytes()).await.expect("failed to read uuid");
let mut name = String::new_zeroed(); let mut name = String::new_zeroed();
recv.read_exact(name.as_mut_bytes()).await.expect("failed to read name"); recv.read_exact(name.as_mut_bytes()).await.expect("failed to read name");
let global_speak = recv.read_u8().await.expect("failed to read global speak state") != 0;
let span = info_span!("", %id); let span = info_span!("", %id);
span.in_scope(|| trace!("uuid parsed")); span.in_scope(|| trace!("uuid parsed"));
info!(parent: &span, "connected as {name}"); info!(parent: &span, "connected as {name}");
@ -53,9 +55,10 @@ impl ProximityPlayer {
let listeners = listeners().read().await; let listeners = listeners().read().await;
send.write_u32_le(listeners.len() as u32).await.expect("failed to write peer length"); send.write_u32_le(listeners.len() as u32).await.expect("failed to write peer length");
for (id, listener) in listeners.iter() { for (id, listener) in listeners.iter() {
if let Ok(name) = listener.send(GetName).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");
} }
} }
} }
@ -94,6 +97,7 @@ impl ProximityPlayer {
ProximityPlayer { ProximityPlayer {
id, id,
name, name,
global_speak,
send, send,
connection, connection,
}, },
@ -116,16 +120,18 @@ 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
}, },
}; };
for (id, listener) in listeners().write().await.iter() { for (id, listener) in listeners().write().await.iter() {
if *id != self.id { if *id != self.id {
if let Ok(Ok(name)) = listener if let Ok(Ok((name, global_speak))) = listener
.send(PeerConnectionChanged { .send(PeerConnectionChanged {
id: self.id, id: self.id,
connected: true, connected: true,
name: self.name, name: self.name,
global_speak: self.global_speak,
}) })
.await .await
{ {
@ -133,6 +139,7 @@ impl Actor for ProximityPlayer {
data: packet::PeerConnectionChanged { data: packet::PeerConnectionChanged {
id: *id, id: *id,
name, name,
global_speak,
..changed_event.data ..changed_event.data
}, },
..changed_event ..changed_event
@ -156,6 +163,7 @@ impl Actor for ProximityPlayer {
id: self.id, id: self.id,
connected: false, connected: false,
name: self.name, name: self.name,
global_speak: false,
}) })
.detach() .detach()
.await; .await;
@ -174,12 +182,12 @@ impl Handler<Stop> for ProximityPlayer {
} }
} }
struct GetName; struct GetStartInfo;
impl Handler<GetName> for ProximityPlayer { impl Handler<GetStartInfo> for ProximityPlayer {
type Return = String<CLIENT_NAME_SIZE>; type Return = (String<CLIENT_NAME_SIZE>, bool);
async fn handle(&mut self, _: GetName, _: &mut xtra::Context<Self>) -> Self::Return { async fn handle(&mut self, _: GetStartInfo, _: &mut xtra::Context<Self>) -> Self::Return {
self.name (self.name, self.global_speak)
} }
} }
@ -274,9 +282,10 @@ struct PeerConnectionChanged {
id: UuidString, id: UuidString,
connected: bool, connected: bool,
name: String<CLIENT_NAME_SIZE>, name: String<CLIENT_NAME_SIZE>,
global_speak: bool,
} }
impl Handler<PeerConnectionChanged> for ProximityPlayer { impl Handler<PeerConnectionChanged> for ProximityPlayer {
type Return = Result<String<CLIENT_NAME_SIZE>, ()>; type Return = Result<(String<CLIENT_NAME_SIZE>, bool), ()>;
async fn handle(&mut self, message: PeerConnectionChanged, ctx: &mut xtra::Context<Self>) -> Self::Return { async fn handle(&mut self, message: PeerConnectionChanged, ctx: &mut xtra::Context<Self>) -> Self::Return {
let event = packet::Packet { let event = packet::Packet {
@ -285,6 +294,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
}, },
}; };
@ -294,7 +304,7 @@ impl Handler<PeerConnectionChanged> for ProximityPlayer {
return Err(()); return Err(());
} }
Ok(self.name) Ok((self.name, self.global_speak))
} }
} }
@ -443,6 +453,7 @@ pub mod packet {
pub id: UuidString, pub id: UuidString,
pub connected: bool, pub connected: bool,
pub name: String<CLIENT_NAME_SIZE>, pub name: String<CLIENT_NAME_SIZE>,
pub global_speak: bool,
} }
#[newtype_enum] #[newtype_enum]
@ -462,6 +473,7 @@ pub mod packet {
}, },
TargetChanged { TargetChanged {
pub name: String<CLIENT_NAME_SIZE>, pub name: String<CLIENT_NAME_SIZE>,
pub global_speak: bool,
}, },
} }
@ -524,6 +536,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
}), }),
kind => bail!("invalid kind: {kind}"), kind => bail!("invalid kind: {kind}"),
}; };