#![allow(dead_code)]
use crossbeam::channel::{self, Receiver, Sender, TryRecvError};
use crate::sc2::PlayerResult;
pub enum FromSupervisor {
Quit,
}
pub enum ToSupervisor {}
pub fn create_channels(count: usize) -> (Receiver<ToGame>, Vec<ChannelToPlayer>, Vec<ChannelToGame>) {
let mut to_player_channels = Vec::new();
let mut to_game_channels = Vec::new();
let (tx_to_game, rx_game) = channel::unbounded();
for player_index in 0..count {
let (tx, rx) = channel::unbounded();
to_player_channels.push(ChannelToPlayer { tx });
to_game_channels.push(ChannelToGame {
player_index,
tx: tx_to_game.clone(),
rx,
});
}
(rx_game, to_player_channels, to_game_channels)
}
pub struct ChannelToGame {
player_index: usize,
tx: Sender<ToGame>,
rx: Receiver<ToPlayer>,
}
impl ChannelToGame {
pub fn send(&mut self, content: ToGameContent) {
self.tx
.send(ToGame {
player_index: self.player_index,
content,
})
.expect("Unable to send to the game");
}
pub fn recv(&mut self) -> Option<ToPlayer> {
match self.rx.try_recv() {
Ok(msg) => Some(msg),
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => panic!("Disconnected"),
}
}
}
#[derive(Debug, Clone)]
pub struct ToGame {
pub player_index: usize,
pub content: ToGameContent,
}
#[derive(Debug, Clone)]
pub enum ToGameContent {
GameOver(Vec<PlayerResult>),
LeftGame,
QuitBeforeLeave,
SC2UnexpectedConnectionClose,
UnexpectedConnectionClose,
}
#[derive(Clone)]
pub struct ChannelToPlayer {
tx: Sender<ToPlayer>,
}
impl ChannelToPlayer {
pub fn send(&mut self, content: ToPlayer) {
self.tx.send(content).expect("Unable to send to the game");
}
}
#[derive(Debug, Clone)]
pub enum ToPlayer {
Quit,
}