use tokio::sync::broadcast::Receiver; use werewolves_proto::{error::GameError, player::PlayerId}; use crate::{communication::Comms, runner::Message}; use super::{HostComms, player::PlayerIdComms}; pub struct LobbyComms { comms: Comms, connect_recv: Receiver<(PlayerId, bool)>, } impl LobbyComms { pub fn new(comms: Comms, connect_recv: Receiver<(PlayerId, bool)>) -> Self { Self { comms, connect_recv, } } pub fn into_inner(self) -> (Comms, Receiver<(PlayerId, bool)>) { (self.comms, self.connect_recv) } #[allow(unused)] pub const fn player(&mut self) -> &mut PlayerIdComms { self.comms.player() } pub const fn host(&mut self) -> &mut HostComms { self.comms.host() } pub async fn next_message(&mut self) -> Result { tokio::select! { r = self.comms.message() => { r } r = self.connect_recv.recv() => { match r { Ok((player_id, true)) => Ok(Message::Connect(player_id)), Ok((player_id, false)) => Ok(Message::Disconnect(player_id)), Err(err) => Err(GameError::GenericError(err.to_string())), } } } } }