werewolves/werewolves-server/src/communication/mod.rs

46 lines
1005 B
Rust
Raw Normal View History

use werewolves_proto::error::GameError;
use crate::{
communication::{host::HostComms, player::PlayerIdComms},
runner::Message,
};
pub mod host;
pub mod lobby;
pub mod player;
pub struct Comms {
host: HostComms,
player: PlayerIdComms,
}
impl Comms {
pub const fn new(host: HostComms, player: PlayerIdComms) -> Self {
Self { host, player }
}
pub const fn host(&mut self) -> &mut HostComms {
&mut self.host
}
2025-10-03 00:00:39 +01:00
#[allow(unused)]
pub const fn player(&mut self) -> &mut PlayerIdComms {
&mut self.player
}
pub async fn message(&mut self) -> Result<Message, GameError> {
tokio::select! {
msg = self.host.recv() => {
match msg {
Some(msg) => Ok(Message::Host(msg)),
None => Err(GameError::HostChannelClosed),
}
}
Ok(msg) = self.player.recv() => {
Ok(Message::Client(msg))
}
}
}
}