feat: initial game logic

This commit is contained in:
2025-11-16 15:13:13 -05:00
Unverified
parent be4e18323a
commit 41b8906f86
4 changed files with 186 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::vec;
use rand::Rng;
use tokio::sync::mpsc::error::SendError;
use tokio::sync::mpsc::UnboundedSender;
use tokio_tungstenite::tungstenite::Message;
@@ -11,27 +13,29 @@ pub enum Role {
Player,
}
#[derive(PartialEq)]
#[derive(PartialEq, Clone)]
pub enum Color {
Red,
Blue,
None,
}
pub struct Client {
pub struct Client<'a> {
pub client_id: String,
pub role: Role,
pub connection: UnboundedSender<Message>,
pub username: Option<String>,
pub current_match: Option<&'a Match<'static>>,
}
impl Client {
pub fn new(client_id: String, role: Role, connection: UnboundedSender<Message>) -> Client {
impl Client<'static> {
pub fn new(client_id: String, role: Role, connection: UnboundedSender<Message>) -> Client<'static> {
Client {
client_id,
role,
connection,
username: None,
current_match: None,
}
}
@@ -40,33 +44,39 @@ impl Client {
}
}
#[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) -> AI {
AI { username: username.to_string(), color, ready: false }
pub fn new(username: &str, color: Color, addr: String) -> AI {
AI { username: username.to_string(), color, ready: false, addr }
}
}
pub struct Match {
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,
}
impl Match {
pub fn new(player1: AI, player2: AI) -> Match {
Match { board: Vec::new(), viewers: Vec::new(), player1, player2 }
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() };
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>,
pub matches: HashMap<u32, Match<'static>>,
pub ready_players: Vec<AI>,
}