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

View file

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