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

61 lines
1.7 KiB
Rust

// Copyright (C) 2025 Emilis Bliūdžius
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use werewolves_proto::error::GameError;
use crate::{
communication::{host::HostComms, player::PlayerIdComms},
runner::Message,
};
pub mod connect;
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
}
#[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))
}
}
}
}