2025-06-23 09:48:28 +01:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 00:00:39 +01:00
|
|
|
#[allow(unused)]
|
2025-06-23 09:48:28 +01:00
|
|
|
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<Message, GameError> {
|
|
|
|
|
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())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|