45 lines
984 B
Rust
45 lines
984 B
Rust
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
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))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|