feat: demo mode for testing AIs

This commit is contained in:
2025-11-16 20:40:15 -05:00
Unverified
parent ca37c3475c
commit 067f2dbf02
3 changed files with 206 additions and 75 deletions

View File

@@ -1,10 +1,12 @@
mod types; mod types;
mod random_ai;
use crate::types::{Color, Match}; use crate::types::{Color, Match};
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use rand::Rng;
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::error::SendError; use tokio::sync::mpsc::error::SendError;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
@@ -12,10 +14,11 @@ use tokio::sync::RwLock;
use tokio_tungstenite::{accept_async, tungstenite::Message}; use tokio_tungstenite::{accept_async, tungstenite::Message};
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use types::Client; use types::Client;
use crate::random_ai::random_move;
type Clients = Arc<RwLock<HashMap<SocketAddr, Client<'static>>>>; type Clients = Arc<RwLock<HashMap<SocketAddr, Arc<RwLock<Client>>>>>;
type Observers = Arc<RwLock<HashMap<SocketAddr, UnboundedSender<Message>>>>; type Observers = Arc<RwLock<HashMap<SocketAddr, UnboundedSender<Message>>>>;
type Matches = Arc<RwLock<HashMap<u32, Match<'static>>>>; type Matches = Arc<RwLock<HashMap<u32, Arc<RwLock<Match>>>>>;
#[tokio::main] #[tokio::main]
@@ -23,6 +26,9 @@ async fn main() -> Result<(), anyhow::Error> {
// Initialize logging // Initialize logging
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let args: Vec<String> = std::env::args().collect();
let demo_mode = args.get(1).is_some() && args.get(1).unwrap() == "demo";
let addr = "0.0.0.0:8080"; let addr = "0.0.0.0:8080";
let listener = TcpListener::bind(&addr).await?; let listener = TcpListener::bind(&addr).await?;
info!("WebSocket server listening on: {}", addr); info!("WebSocket server listening on: {}", addr);
@@ -38,6 +44,7 @@ async fn main() -> Result<(), anyhow::Error> {
clients.clone(), clients.clone(),
observers.clone(), observers.clone(),
matches.clone(), matches.clone(),
demo_mode
)); ));
} }
@@ -50,6 +57,7 @@ async fn handle_connection(
clients: Clients, clients: Clients,
observers: Observers, observers: Observers,
matches: Matches, matches: Matches,
demo_mode: bool,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
info!("New WebSocket connection from: {}", addr); info!("New WebSocket connection from: {}", addr);
@@ -58,12 +66,7 @@ async fn handle_connection(
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
// Store the client // Store the client
{ observers.write().await.insert(addr, tx.clone());
observers
.write()
.await
.insert(addr, tx.clone());
}
// Spawn task to handle outgoing messages // Spawn task to handle outgoing messages
let mut send_task = tokio::spawn(async move { let mut send_task = tokio::spawn(async move {
@@ -80,11 +83,17 @@ async fn handle_connection(
Ok(Message::Text(text)) => { Ok(Message::Text(text)) => {
info!("Received text from {}: {}", addr, text); info!("Received text from {}: {}", addr, text);
if text.starts_with("CONNECT") { if text.starts_with("CONNECT:") {
let requested_username = text.split(":").collect::<Vec<&str>>()[1].to_string(); let requested_username = text.split(":").collect::<Vec<&str>>()[1].to_string();
if requested_username.is_empty() {
let _ = send(&tx, &format!("ERROR:INVALID:ID:{}", requested_username));
continue;
}
let mut is_taken = false; let mut is_taken = false;
for client in clients.read().await.values() { for client in clients.read().await.values() {
if requested_username == client.username { if requested_username == client.read().await.username {
let _ = send(&tx, &format!("ERROR:INVALID:ID:{}", requested_username)); let _ = send(&tx, &format!("ERROR:INVALID:ID:{}", requested_username));
is_taken = true; is_taken = true;
break; break;
@@ -95,45 +104,75 @@ async fn handle_connection(
// not taken // not taken
observers.write().await.remove(&addr); observers.write().await.remove(&addr);
clients.write().await.insert(addr.to_string().parse()?, Client::new(requested_username, tx.clone())); clients
.write().await
.insert(
addr.to_string().parse()?,
Arc::new(RwLock::new(Client::new(requested_username, tx.clone(), addr.to_string().parse()?)))
);
let _ = send(&tx, "CONNECT:ACK"); let _ = send(&tx, "CONNECT:ACK");
} }
else if text.starts_with("READY") { else if text == "READY" {
if clients.read().await.get(&addr).is_none() { if clients.read().await.get(&addr).is_none() {
let _ = send(&tx, "ERROR:INVALID"); let _ = send(&tx, "ERROR:INVALID");
continue; continue;
} }
if clients.read().await.get(&addr).unwrap().ready { if clients.read().await.get(&addr).unwrap().read().await.ready {
let _ = send(&tx, "ERROR:INVALID"); let _ = send(&tx, "ERROR:INVALID");
continue; continue;
} }
clients.write().await.get_mut(&addr).unwrap().ready = true; clients.write().await.get_mut(&addr).unwrap().write().await.ready = true;
let _ = send(&tx,"READY:ACK"); let _ = send(&tx,"READY:ACK");
if demo_mode {
let match_id: u32 = rand::rng().random_range(100000..=999999);
let new_match = Arc::new(RwLock::new(Match::new(match_id, addr.to_string().parse()?, addr.to_string().parse()?)));
matches.write().await.insert(match_id, new_match.clone());
clients.write().await.get_mut(&addr).unwrap().write().await.ready = false;
clients
.write().await
.get_mut(&addr).unwrap()
.write().await.current_match = Some(match_id);
clients
.write().await
.get_mut(&addr).unwrap()
.write().await.color = Color::Red;
let _ = send(&tx, "GAME:START:1");
}
} }
else if text.starts_with("PLAY") { else if text.starts_with("PLAY:") {
let read = clients.read().await; let clients_guard = clients.read().await;
let client = read.get(&addr); let client_option = clients_guard.get(&addr);
// Check if client is valid // Check if client is valid
if client.is_none() || client.unwrap().current_match.is_none() { if client_option.is_none() || client_option.unwrap().read().await.current_match.is_none() {
let _ = send(&tx, "ERROR:INVALID:MOVE"); let _ = send(&tx, "ERROR:INVALID:MOVE");
continue; continue;
} }
let current_match = client.unwrap().current_match.unwrap(); let client = client_option.unwrap().read().await;
let current_player = if client.unwrap().username == current_match.player1.username
{ current_match.player1 } else { current_match.player2 }; let matches_guard = matches.read().await;
let opponent = if client.unwrap().username == current_match.player1.username let current_match = matches_guard.get(&client.current_match.unwrap()).unwrap().read().await;
{ current_match.player2 } else { current_match.player1 };
let opponent = {
let result =
if addr == current_match.player1
{ clients_guard.get(&current_match.player2).unwrap().read().await }
else
{ clients_guard.get(&current_match.player1).unwrap().read().await };
result
};
// Check if it's their move // Check if it's their move
if (current_match.ledger.is_empty() && if (current_match.ledger.is_empty() &&
current_match.first.username != client.unwrap().username) || current_match.first != addr) ||
(client.unwrap().current_match.unwrap().ledger.last().unwrap().0.username == client.unwrap().username) (current_match.ledger.last().is_some() && current_match.ledger.last().unwrap().0 == client.color)
{ {
let _ = send(&tx, "ERROR:INVALID:MOVE"); let _ = send(&tx, "ERROR:INVALID:MOVE");
continue; continue;
@@ -141,6 +180,12 @@ async fn handle_connection(
let column_parse = text.split(":").collect::<Vec<&str>>()[1].parse::<usize>(); let column_parse = text.split(":").collect::<Vec<&str>>()[1].parse::<usize>();
drop(current_match);
drop(matches_guard);
let mut matches_guard = matches.write().await;
let mut current_match = matches_guard.get_mut(&client.current_match.unwrap()).unwrap().write().await;
// Check if valid move // Check if valid move
if let Ok(column) = column_parse { if let Ok(column) = column_parse {
if current_match.board[column][4] != Color::None { if current_match.board[column][4] != Color::None {
@@ -149,21 +194,19 @@ async fn handle_connection(
} }
// Place it // Place it
for i in 0..current_match.board[column].len() { current_match.place_token(client.color.clone(), column)
if current_match.board[column][i] == Color::None {
matches
.write()
.await
.get_mut(&current_match.id).unwrap()
.board[column][i] = current_player.color.clone();
break;
}
}
} else { } else {
let _ = send(&tx, "ERROR:INVALID:MOVE"); let _ = send(&tx, "ERROR:INVALID:MOVE");
continue; continue;
} }
// broadcast the move to viewers
broadcast_message(
&current_match.viewers,
&observers,
&format!("GAME:MOVE:{}:{}", client.username, column_parse.clone()?))
.await;
// Check game end conditions // Check game end conditions
let (winner, filled) = { let (winner, filled) = {
let mut result = (Color::None, false); let mut result = (Color::None, false);
@@ -171,31 +214,32 @@ async fn handle_connection(
let mut any_empty = true; let mut any_empty = true;
for x in 0..6 { for x in 0..6 {
for y in 0..5 { for y in 0..5 {
let color = &current_match.board[x][y]; let color = current_match.board[x][y].clone();
let mut horizontal_end = true; let mut horizontal_end = true;
let mut vertical_end = true; let mut vertical_end = true;
let mut diagonal_end = true; let mut diagonal_end = true;
if any_empty && *color == Color::None { if any_empty && color == Color::None {
any_empty = false; any_empty = false;
} }
for i in 0..4 { for i in 0..4 {
if x + 3 < 6 && current_match.board[x + i][y] != *color { if x + i < 6 && current_match.board[x + i][y] != color && horizontal_end {
horizontal_end = false; horizontal_end = false;
} }
if y + 3 < 5 && current_match.board[x + i][y] != *color { if y + i < 5 && current_match.board[x][y + i] != color && vertical_end{
vertical_end = false; vertical_end = false;
} }
if x + 3 < 6 && y + 3 < 5 && current_match.board[x + i][y + i] != *color { if x + i < 6 && y + i < 5 && current_match.board[x + i][y + i] != color && diagonal_end {
diagonal_end = false; diagonal_end = false;
} }
} }
if horizontal_end || vertical_end || diagonal_end { result = (color.clone(), false) } if horizontal_end || vertical_end || diagonal_end { result = (color.clone(), false); break; }
} }
if result.0 != Color::None { break; }
} }
if any_empty && result.0 == Color::None { result.1 = true; } if any_empty && result.0 == Color::None { result.1 = true; }
@@ -204,29 +248,89 @@ async fn handle_connection(
}; };
if winner != Color::None { if winner != Color::None {
if winner == current_player.color { if winner == client.color {
let _ = send(&tx, "GAME:WINS"); let _ = send(&tx, "GAME:WINS");
let _ = send(&opponent.connection, "GAME:LOSS"); if !demo_mode {
let _ = send(&opponent.connection, "GAME:LOSS");
}
broadcast_message(&current_match.viewers, &observers, &format!("GAME:WIN:{}", client.username)).await;
} else { } else {
let _ = send(&tx, "GAME:LOSS"); let _ = send(&tx, "GAME:LOSS");
let _ = send(&opponent.connection, "GAME:WINS"); if !demo_mode {
let _ = send(&opponent.connection, "GAME:WINS");
}
broadcast_message(&current_match.viewers, &observers, &format!("GAME:WIN:{}", opponent.username)).await;
} }
} }
else if filled { else if filled {
let _ = send(&tx, "GAME:DRAW"); let _ = send(&tx, "GAME:DRAW");
let _ = send(&opponent.connection, "GAME:DRAW"); if !demo_mode {
} let _ = send(&opponent.connection, "GAME:DRAW");
else { }
let _ = send(&opponent.connection, &format!("OPPONENT:{}", column_parse?)); broadcast_message(&current_match.viewers, &observers, "GAME:DRAW").await;
}
// remove match from matchmaker
if winner != Color::None || filled {
let opponent_addr = opponent.addr;
let current_match_id = current_match.id;
drop(client);
drop(opponent);
drop(current_match);
drop(clients_guard);
let mut clients_guard = clients.write().await;
let mut client = clients_guard.get_mut(&addr).unwrap().write().await;
client.current_match = None;
drop(client);
let mut opponent = clients_guard.get_mut(&opponent_addr).unwrap().write().await;
if !demo_mode {
opponent.current_match = None;
}
matches_guard.remove(&current_match_id).unwrap();
continue;
}
if !demo_mode{
// TODO: delay/autoplay/continue behavior
let _ = send(&opponent.connection, &format!("OPPONENT:{}", column_parse.clone()?));
} else {
let random_move = random_move(&current_match.board);
current_match.place_token(Color::Blue, random_move);
let _ = send(&tx, &format!("OPPONENT:{}", random_move));
} }
// TODO: remove match from matchmaker
// TODO: broadcast moves to viewers
} }
else if text == "GAME:LIST" {
todo!()
}
else if text.starts_with("GAME:WATCH:") {
todo!()
}
else if text.starts_with("ADMIN:AUTH:") {
todo!()
}
else if text.starts_with("ADMIN:KICK:") {
todo!()
}
else if text == "GAME:TERMINATE" {
todo!()
}
else if text.starts_with("GAME:AUTOPLAY:") {
todo!()
}
else if text == "GAME:CONTINUE" {
todo!()
}
else { else {
let _ = send(&tx, "GAME:UNKNOWN"); let _ = send(&tx, "ERROR:UNKNOWN");
} }
} }
Ok(Message::Close(_)) => { Ok(Message::Close(_)) => {
@@ -234,7 +338,7 @@ async fn handle_connection(
break; break;
} }
Ok(_) => { Ok(_) => {
let _ = send(&tx, "GAME:UNKNOWN"); let _ = send(&tx, "ERROR:UNKNOWN");
} }
Err(e) => { Err(e) => {
error!("WebSocket error for {}: {}", addr, e); error!("WebSocket error for {}: {}", addr, e);
@@ -255,19 +359,18 @@ async fn handle_connection(
Ok(()) Ok(())
} }
async fn broadcast_message(observers: &Observers, msg: &str) { async fn broadcast_message(addrs: &Vec<SocketAddr>, observers: &Observers, msg: &str) {
let observers = observers.read().await; for addr in addrs {
for (_, tx) in observers.iter() { let _ = send(observers.read().await.get(addr).unwrap(), msg);
let _ = send(tx, msg);
} }
} }
async fn watch(matches: &Matches, new_match_id: u32, addr: SocketAddr) { async fn watch(matches: &Matches, new_match_id: u32, addr: SocketAddr) {
for a_match in &mut matches.write().await.values_mut() { for a_match in &mut matches.write().await.values_mut() {
let mut found = false; let mut found = false;
for i in 0..a_match.viewers.len() { for i in 0..a_match.write().await.viewers.len() {
if a_match.viewers[i] == addr { if a_match.write().await.viewers[i] == addr {
a_match.viewers.remove(i); a_match.write().await.viewers.remove(i);
found = true; found = true;
break; break;
} }
@@ -276,7 +379,11 @@ async fn watch(matches: &Matches, new_match_id: u32, addr: SocketAddr) {
if found { break; } if found { break; }
} }
matches.write().await.get_mut(&new_match_id).unwrap().viewers.push(addr); matches
.write().await
.get_mut(&new_match_id).unwrap()
.write().await
.viewers.push(addr);
} }
fn send(tx: &UnboundedSender<Message>, text: &str) -> Result<(), SendError<Message>> { fn send(tx: &UnboundedSender<Message>, text: &str) -> Result<(), SendError<Message>> {

11
src/random_ai.rs Normal file
View File

@@ -0,0 +1,11 @@
use rand::Rng;
use crate::types::{Client, Color};
pub fn random_move(board: &[Vec<Color>]) -> usize {
let mut random = rand::rng().random_range(0..6);
while board[random][4] != Color::None {
random = rand::rng().random_range(0..6);
}
random
}

View File

@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::vec; use std::vec;
use rand::Rng; use rand::Rng;
@@ -13,22 +12,27 @@ pub enum Color {
None, None,
} }
pub struct Client<'a> { #[derive(Clone)]
pub struct Client {
pub username: String, pub username: String,
pub connection: UnboundedSender<Message>, pub connection: UnboundedSender<Message>,
pub ready: bool, pub ready: bool,
pub color: Color, pub color: Color,
pub current_match: Option<&'a Match<'static>>, pub current_match: Option<u32>,
pub demo: bool,
pub addr: SocketAddr,
} }
impl Client<'static> { impl Client {
pub fn new(username: String, connection: UnboundedSender<Message>) -> Client<'static> { pub fn new(username: String, connection: UnboundedSender<Message>, addr: SocketAddr) -> Client {
Client { Client {
username, username,
connection, connection,
ready: false, ready: false,
color: Color::None, color: Color::None,
current_match: None, current_match: None,
demo: false,
addr,
} }
} }
@@ -37,19 +41,28 @@ impl Client<'static> {
} }
} }
pub struct Match<'a> { pub struct Match {
pub id: u32, pub id: u32,
pub board: Vec<Vec<Color>>, pub board: Vec<Vec<Color>>,
pub viewers: Vec<SocketAddr>, pub viewers: Vec<SocketAddr>,
pub ledger: Vec<(&'a Client<'a>, usize)>, pub ledger: Vec<(Color, usize)>,
pub first: &'a Client<'a>, pub first: SocketAddr,
pub player1: &'a Client<'a>, pub player1: SocketAddr,
pub player2: &'a Client<'a>, pub player2: SocketAddr,
} }
impl<'a> Match<'a> { impl Match {
pub fn new(id: u32, player1: &'a Client, player2: &'a Client) -> Match<'a> { pub fn new(id: u32, player1: SocketAddr, player2: SocketAddr) -> Match {
let first = if rand::rng().random_range(0..=1) == 0 { player1 } else { player2 }; 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 } Match { id, board: vec![vec![Color::None; 5]; 6], viewers: Vec::new(), ledger: Vec::new(), first, player1, player2 }
} }
pub fn place_token(&mut self, color: Color, column: usize) {
for i in 0..5 {
if self.board[column][i] == Color::None {
self.board[column][i] = color;
break;
}
}
}
} }