misc: refactor types
This commit is contained in:
77
src/types.rs
77
src/types.rs
@@ -6,13 +6,6 @@ use tokio::sync::mpsc::error::SendError;
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Role {
|
||||
Admin,
|
||||
Observer,
|
||||
Player,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub enum Color {
|
||||
Red,
|
||||
@@ -21,20 +14,20 @@ pub enum Color {
|
||||
}
|
||||
|
||||
pub struct Client<'a> {
|
||||
pub client_id: String,
|
||||
pub role: Role,
|
||||
pub username: String,
|
||||
pub connection: UnboundedSender<Message>,
|
||||
pub username: Option<String>,
|
||||
pub ready: bool,
|
||||
pub color: Color,
|
||||
pub current_match: Option<&'a Match<'static>>,
|
||||
}
|
||||
|
||||
impl Client<'static> {
|
||||
pub fn new(client_id: String, role: Role, connection: UnboundedSender<Message>) -> Client<'static> {
|
||||
pub fn new(username: String, connection: UnboundedSender<Message>) -> Client<'static> {
|
||||
Client {
|
||||
client_id,
|
||||
role,
|
||||
username,
|
||||
connection,
|
||||
username: None,
|
||||
ready: false,
|
||||
color: Color::None,
|
||||
current_match: None,
|
||||
}
|
||||
}
|
||||
@@ -44,61 +37,19 @@ impl Client<'static> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AI {
|
||||
pub username: String,
|
||||
pub color: Color,
|
||||
pub ready: bool,
|
||||
pub addr: String,
|
||||
}
|
||||
|
||||
impl AI {
|
||||
pub fn new(username: &str, color: Color, addr: String) -> AI {
|
||||
AI { username: username.to_string(), color, ready: false, addr }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Match<'a> {
|
||||
pub id: u32,
|
||||
pub board: Vec<Vec<Color>>,
|
||||
pub viewers: Vec<SocketAddr>,
|
||||
pub ledger: Vec<(&'a AI, usize)>,
|
||||
pub first: AI,
|
||||
pub player1: AI,
|
||||
pub player2: AI,
|
||||
pub ledger: Vec<(&'a Client<'a>, usize)>,
|
||||
pub first: &'a Client<'a>,
|
||||
pub player1: &'a Client<'a>,
|
||||
pub player2: &'a Client<'a>,
|
||||
}
|
||||
|
||||
impl Match<'static> {
|
||||
pub fn new(id: u32, player1: AI, player2: AI) -> Match<'static> {
|
||||
let first = if rand::rng().random_range(0..=1) == 0 { player1.clone() } else { player2.clone() };
|
||||
impl<'a> Match<'a> {
|
||||
pub fn new(id: u32, player1: &'a Client, player2: &'a Client) -> Match<'a> {
|
||||
let first = if rand::rng().random_range(0..=1) == 0 { player1 } else { player2 };
|
||||
Match { id, board: vec![vec![Color::None; 5]; 6], viewers: Vec::new(), ledger: Vec::new(), first, player1, player2 }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MatchMaker {
|
||||
pub matches: HashMap<u32, Match<'static>>,
|
||||
pub ready_players: Vec<AI>,
|
||||
}
|
||||
|
||||
impl MatchMaker {
|
||||
pub fn new() -> MatchMaker {
|
||||
MatchMaker { matches: HashMap::new(), ready_players: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn watch(&mut self, viewer: SocketAddr, match_id: u32) {
|
||||
for aMatch in &mut self.matches.values_mut() {
|
||||
let mut found = false;
|
||||
for i in 0..aMatch.viewers.len() {
|
||||
if aMatch.viewers[i] == viewer {
|
||||
aMatch.viewers.remove(i);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if found { break; }
|
||||
}
|
||||
|
||||
self.matches.get_mut(&match_id).unwrap().viewers.push(viewer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user