Compare commits
14 Commits
Engine/api
...
Server/log
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fdd7fd65c | ||
|
|
90a59305ac | ||
| 5ccca3ab8e | |||
| f2adc14ce2 | |||
| 7ed9ae3799 | |||
| 622bef963f | |||
|
|
890fab38cb | ||
| 6fc1b6c3fd | |||
| a985182c99 | |||
| 3b78a8e925 | |||
| ab369f179c | |||
| be6a86bebf | |||
| 5336aadf97 | |||
| 8d559f4b11 |
Binary file not shown.
@@ -1,6 +1,9 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
pub enum GameEnd {
|
pub enum GameEnd {
|
||||||
WhiteWon(String),
|
WhiteWon(String),
|
||||||
BlackWon(String),
|
BlackWon(String),
|
||||||
Draw(String)
|
Draw(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1257
server/Cargo.lock
generated
Normal file
1257
server/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,8 @@ uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
|||||||
anyhow = "1.0.100"
|
anyhow = "1.0.100"
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
engine = {path = "../engine/"}
|
engine = {path = "../engine/"}
|
||||||
|
log = {version = "0.4.28"}
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|||||||
@@ -1,17 +1,27 @@
|
|||||||
|
use engine::gameend::GameEnd;
|
||||||
|
use engine::{boardsquare::BoardSquare, chessmove::ChessMove, piecetype::PieceType};
|
||||||
use futures_util::{SinkExt, StreamExt};
|
use futures_util::{SinkExt, StreamExt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct Step {
|
||||||
|
from: String,
|
||||||
|
to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
enum ClientMessage {
|
enum ClientMessage {
|
||||||
Join { username: String },
|
Join { username: String },
|
||||||
FindMatch,
|
FindMatch,
|
||||||
Move { from: String, to: String },
|
Move { step: ChessMove, fen: String },
|
||||||
Resign,
|
Resign,
|
||||||
Chat { text: String },
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -25,6 +35,21 @@ struct ServerMessage {
|
|||||||
reason: Option<String>,
|
reason: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: GameEnd,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Knightly Chess Client");
|
println!("Knightly Chess Client");
|
||||||
@@ -60,15 +85,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
println!("\nServer: {}", text);
|
println!("\nServer: {}", text);
|
||||||
|
|
||||||
// Try to parse as structured message
|
// Try to parse as structured message
|
||||||
if let Ok(parsed) = serde_json::from_str::<ServerMessage>(text) {
|
if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
match parsed.message_type.as_str() {
|
match parsed {
|
||||||
"welcome" => {
|
ServerMessage2::MatchFound {
|
||||||
if let Some(player_id) = parsed.player_id {
|
match_id,
|
||||||
println!("Welcome! Your player ID: {}", player_id);
|
color,
|
||||||
}
|
opponent_name,
|
||||||
|
} => {
|
||||||
|
println!(
|
||||||
|
"opponent: {}, match_id: {}, color: {}",
|
||||||
|
opponent_name, match_id, color
|
||||||
|
);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("cucc: {:?}", parsed);
|
println!("cucc");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,11 +158,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
"move" => {
|
"move" => {
|
||||||
if parts.len() >= 3 {
|
if parts.len() >= 3 {
|
||||||
let from = parts[1].to_string();
|
//let from = parts[1].to_string();
|
||||||
let to = parts[2].to_string();
|
//let to = parts[2].to_string();
|
||||||
let message = ClientMessage::Move { from, to };
|
let fen = parts[1].to_string();
|
||||||
|
|
||||||
|
let step = ChessMove::quiet(
|
||||||
|
engine::piecetype::PieceType::WhiteBishop,
|
||||||
|
BoardSquare::new(),
|
||||||
|
BoardSquare { x: 1, y: 1 },
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
let message = ClientMessage::Move { step, fen };
|
||||||
send_message(&mut write, &message).await?;
|
send_message(&mut write, &message).await?;
|
||||||
println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
//println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
||||||
} else {
|
} else {
|
||||||
println!("Usage: move <from> <to> (e.g., move e2 e4)");
|
println!("Usage: move <from> <to> (e.g., move e2 e4)");
|
||||||
}
|
}
|
||||||
@@ -154,6 +193,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
"help" => {
|
"help" => {
|
||||||
print_help();
|
print_help();
|
||||||
}
|
}
|
||||||
|
"requestmoves" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let fen = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::RequestLegalMoves { fen };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!(
|
println!(
|
||||||
"Unknown command: {}. Type 'help' for available commands.",
|
"Unknown command: {}. Type 'help' for available commands.",
|
||||||
@@ -191,5 +237,6 @@ fn print_help() {
|
|||||||
println!(" resign - Resign from current game");
|
println!(" resign - Resign from current game");
|
||||||
println!(" help - Show this help");
|
println!(" help - Show this help");
|
||||||
println!(" quit - Exit the client");
|
println!(" quit - Exit the client");
|
||||||
|
println!(" requestmoves - Request the legal moves");
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
use crate::connection::ClientEvent::*;
|
use crate::connection::ClientEvent::*;
|
||||||
use engine::get_available_moves;
|
use crate::matchmaking;
|
||||||
|
use engine::chessmove::ChessMove;
|
||||||
|
use engine::gameend::GameEnd::{self, *};
|
||||||
|
use engine::{get_available_moves, is_game_over};
|
||||||
use futures_util::{SinkExt, StreamExt};
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use log::{error, info, warn};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::char::from_u32_unchecked;
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
@@ -15,41 +20,73 @@ pub type ConnectionMap = Arc<Mutex<HashMap<Uuid, PlayerConnection>>>;
|
|||||||
pub type MatchMap = Arc<Mutex<HashMap<Uuid, GameMatch>>>;
|
pub type MatchMap = Arc<Mutex<HashMap<Uuid, GameMatch>>>;
|
||||||
pub type WaitingQueue = Arc<Mutex<VecDeque<Uuid>>>;
|
pub type WaitingQueue = Arc<Mutex<VecDeque<Uuid>>>;
|
||||||
|
|
||||||
|
pub async fn clean_up_match(matches: &MatchMap, match_id: &Uuid) {
|
||||||
|
matches.lock().await.remove(&match_id);
|
||||||
|
}
|
||||||
|
|
||||||
// Helper functions to create new instances
|
// Helper functions to create new instances
|
||||||
pub fn new_connection_map() -> ConnectionMap {
|
pub fn new_connection_map() -> ConnectionMap {
|
||||||
|
warn!("Created new connection map");
|
||||||
Arc::new(Mutex::new(HashMap::new()))
|
Arc::new(Mutex::new(HashMap::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_match_map() -> MatchMap {
|
pub fn new_match_map() -> MatchMap {
|
||||||
|
warn!("Created new match map");
|
||||||
Arc::new(Mutex::new(HashMap::new()))
|
Arc::new(Mutex::new(HashMap::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_waiting_queue() -> WaitingQueue {
|
pub fn new_waiting_queue() -> WaitingQueue {
|
||||||
|
warn!("Created new waiting queue");
|
||||||
Arc::new(Mutex::new(VecDeque::new()))
|
Arc::new(Mutex::new(VecDeque::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct Step {
|
pub struct Step {
|
||||||
pub from: String,
|
pub from: String,
|
||||||
pub to: String,
|
pub to: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
/*#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct ServerMessage {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
message_type: String,
|
||||||
|
player_id: Option<Uuid>,
|
||||||
|
match_id: Option<Uuid>,
|
||||||
|
opponent: Option<Uuid>,
|
||||||
|
color: Option<String>,
|
||||||
|
reason: Option<String>,
|
||||||
|
response: Option<String>,
|
||||||
|
}*/
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: GameEnd,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
enum ClientEvent {
|
enum ClientEvent {
|
||||||
Join { username: String },
|
Join { username: String },
|
||||||
FindMatch,
|
FindMatch,
|
||||||
Move { from: String, to: String },
|
Move { step: ChessMove },
|
||||||
Resign,
|
Resign,
|
||||||
Chat { text: String },
|
Chat { text: String },
|
||||||
RequestLegalMoves { fen: String },
|
RequestLegalMoves { fen: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct EventResponse {
|
|
||||||
pub response: Result<(), String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PlayerConnection {
|
pub struct PlayerConnection {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
@@ -64,23 +101,24 @@ pub struct GameMatch {
|
|||||||
pub player_white: Uuid,
|
pub player_white: Uuid,
|
||||||
pub player_black: Uuid,
|
pub player_black: Uuid,
|
||||||
pub board_state: String,
|
pub board_state: String,
|
||||||
pub move_history: Vec<String>,
|
pub move_history: Vec<Step>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message sending utilities
|
// Message sending utilities
|
||||||
pub async fn send_message_to_player(
|
pub async fn send_message_to_player_connection(
|
||||||
connections: &ConnectionMap,
|
connection: Option<&mut PlayerConnection>,
|
||||||
player_id: Uuid,
|
|
||||||
message: &str,
|
message: &str,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), tokio_tungstenite::tungstenite::Error> {
|
||||||
let mut connections_lock = connections.lock().await;
|
match connection {
|
||||||
if let Some(connection) = connections_lock.get_mut(&player_id) {
|
Some(connection) => {
|
||||||
connection
|
info!("sending message to: {}", connection.id);
|
||||||
.tx
|
connection.tx.send(Message::Text(message.to_string())).await
|
||||||
.send(Message::Text(message.to_string()))
|
}
|
||||||
.await?;
|
None => {
|
||||||
|
error!("No connection provided");
|
||||||
|
Err(tokio_tungstenite::tungstenite::Error::ConnectionClosed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn broadcast_to_all(connections: &ConnectionMap, message: &str) {
|
pub async fn broadcast_to_all(connections: &ConnectionMap, message: &str) {
|
||||||
@@ -89,7 +127,7 @@ pub async fn broadcast_to_all(connections: &ConnectionMap, message: &str) {
|
|||||||
|
|
||||||
for (id, connection) in connections_lock.iter_mut() {
|
for (id, connection) in connections_lock.iter_mut() {
|
||||||
if let Err(e) = connection.tx.send(Message::Text(message.to_string())).await {
|
if let Err(e) = connection.tx.send(Message::Text(message.to_string())).await {
|
||||||
eprintln!("Failed to send to {}: {}", id, e);
|
error!("Failed to send to {}: {}", id, e);
|
||||||
dead_connections.push(*id);
|
dead_connections.push(*id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,10 +144,19 @@ pub async fn broadcast_to_match(
|
|||||||
match_id: Uuid,
|
match_id: Uuid,
|
||||||
message: &str,
|
message: &str,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
info!("Broadcasting data to match: {}", &match_id);
|
||||||
let matches_lock = matches.lock().await;
|
let matches_lock = matches.lock().await;
|
||||||
if let Some(game_match) = matches_lock.get(&match_id) {
|
if let Some(game_match) = matches_lock.get(&match_id) {
|
||||||
send_message_to_player(connections, game_match.player_white, message).await?;
|
send_message_to_player_connection(
|
||||||
send_message_to_player(connections, game_match.player_black, message).await?;
|
connections.lock().await.get_mut(&game_match.player_white),
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
send_message_to_player_connection(
|
||||||
|
connections.lock().await.get_mut(&game_match.player_black),
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -125,6 +172,7 @@ pub async fn handle_connection(
|
|||||||
|
|
||||||
let ws_stream = accept_async(stream).await?;
|
let ws_stream = accept_async(stream).await?;
|
||||||
let (write, mut read) = ws_stream.split();
|
let (write, mut read) = ws_stream.split();
|
||||||
|
warn!("Accepted new connection");
|
||||||
|
|
||||||
let player_id = Uuid::new_v4();
|
let player_id = Uuid::new_v4();
|
||||||
|
|
||||||
@@ -142,45 +190,32 @@ pub async fn handle_connection(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("New connection: {}", player_id);
|
info!("id: {}", &player_id);
|
||||||
|
|
||||||
// Send welcome message
|
|
||||||
let _ = send_message_to_player(
|
|
||||||
&connections,
|
|
||||||
player_id,
|
|
||||||
&format!(r#"{{"type": "welcome", "player_id": "{}"}}"#, player_id),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// Message processing loop
|
// Message processing loop
|
||||||
while let Some(Ok(message)) = read.next().await {
|
while let Some(Ok(message)) = read.next().await {
|
||||||
if message.is_text() {
|
if message.is_text() {
|
||||||
let text = message.to_text()?;
|
let text = message.to_text()?;
|
||||||
println!("Received from {}: {}", player_id, text);
|
info!("Received from {}: {}", player_id, text);
|
||||||
|
|
||||||
let client_data: ClientEvent = serde_json::from_str(text)
|
let client_data: ClientEvent = serde_json::from_str(text)
|
||||||
.expect("Failed to convert data into json at handle_connection");
|
.expect("Failed to convert data into json at handle_connection");
|
||||||
|
|
||||||
println!("client: {:?}", client_data);
|
|
||||||
|
|
||||||
match client_data {
|
match client_data {
|
||||||
Join { username } => {
|
Join { username } => {
|
||||||
{
|
{
|
||||||
let mut conn_map = connections.lock().await;
|
let mut conn_map = connections.lock().await;
|
||||||
let player = conn_map.get_mut(&player_id).unwrap();
|
let player = conn_map.get_mut(&player_id).unwrap();
|
||||||
player.username = Some(username);
|
player.username = Some(username.clone());
|
||||||
|
info!("player: {}, set username: {}", &player_id, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
//respone to client
|
//respone to client
|
||||||
let response: EventResponse = EventResponse {
|
let response = ServerMessage2::Ok { response: Ok(()) };
|
||||||
response: core::result::Result::Ok(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("response: {:?}", response);
|
let mut conn_map = connections.lock().await;
|
||||||
|
let _ = send_message_to_player_connection(
|
||||||
let _ = send_message_to_player(
|
conn_map.get_mut(&player_id),
|
||||||
&connections,
|
|
||||||
player_id,
|
|
||||||
&serde_json::to_string(&response).unwrap(),
|
&serde_json::to_string(&response).unwrap(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@@ -188,28 +223,150 @@ pub async fn handle_connection(
|
|||||||
FindMatch => {
|
FindMatch => {
|
||||||
let mut wait_queue = waiting_queue.lock().await;
|
let mut wait_queue = waiting_queue.lock().await;
|
||||||
wait_queue.push_back(player_id.clone());
|
wait_queue.push_back(player_id.clone());
|
||||||
println!("Appended {} to the waiting queue", player_id);
|
info!("Appended {} to the waiting queue", player_id);
|
||||||
println!("queue {:?}", wait_queue);
|
info!("queue {:?}", wait_queue);
|
||||||
}
|
}
|
||||||
Move { from, to } => {}
|
Move { step } => {
|
||||||
RequestLegalMoves { fen } => {
|
let match_id = connections
|
||||||
let moves = get_available_moves(&fen);
|
.lock()
|
||||||
let _ = send_message_to_player(
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
{
|
||||||
|
info!("updating board state in match: {}", &match_id);
|
||||||
|
let mut matches = matches.lock().await;
|
||||||
|
matches.get_mut(&match_id).unwrap().board_state =
|
||||||
|
engine::get_board_after_move(
|
||||||
|
&matches.get(&match_id).unwrap().board_state,
|
||||||
|
&step,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let message = ServerMessage2::UIUpdate {
|
||||||
|
fen: matches
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&match_id)
|
||||||
|
.unwrap()
|
||||||
|
.board_state
|
||||||
|
.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = broadcast_to_match(
|
||||||
&connections,
|
&connections,
|
||||||
player_id,
|
&matches,
|
||||||
|
match_id,
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let is_game_end = engine::is_game_over(
|
||||||
|
&matches.lock().await.get(&match_id).unwrap().board_state,
|
||||||
|
);
|
||||||
|
|
||||||
|
match is_game_end {
|
||||||
|
Some(res) => {
|
||||||
|
warn!("A player won the match: {}", &match_id);
|
||||||
|
let message = ServerMessage2::GameEnd { winner: res };
|
||||||
|
let _ = broadcast_to_match(
|
||||||
|
&connections,
|
||||||
|
&matches,
|
||||||
|
match_id,
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
clean_up_match(&matches, &match_id);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
info!("No winner match continues. Id: {}", &match_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RequestLegalMoves { fen } => {
|
||||||
|
info!("Requesting legal moves player: {}", &player_id);
|
||||||
|
let moves = get_available_moves(&fen);
|
||||||
|
let _ = send_message_to_player_connection(
|
||||||
|
connections.lock().await.get_mut(&player_id),
|
||||||
&serde_json::to_string(&moves).unwrap(),
|
&serde_json::to_string(&moves).unwrap(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
println!("Sent moves to player: {}", player_id);
|
info!("Sent moves to player: {}", player_id);
|
||||||
|
}
|
||||||
|
Resign => {
|
||||||
|
warn!("Resigned!");
|
||||||
|
let (fuck, fuck_id): (ServerMessage2, &Uuid) = {
|
||||||
|
let matches = matches.lock().await;
|
||||||
|
|
||||||
|
let curr_match = matches
|
||||||
|
.get(
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if player_id == curr_match.player_white {
|
||||||
|
(
|
||||||
|
ServerMessage2::GameEnd {
|
||||||
|
winner: GameEnd::BlackWon("Resigned".to_string()),
|
||||||
|
},
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
ServerMessage2::GameEnd {
|
||||||
|
winner: GameEnd::WhiteWon("Resigned".to_string()),
|
||||||
|
},
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
broadcast_to_match(
|
||||||
|
&connections,
|
||||||
|
&matches,
|
||||||
|
connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
&serde_json::to_string(&fuck).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
clean_up_match(&matches, fuck_id);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
warn!("Not known client event");
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup on disconnect
|
// Cleanup on disconnect
|
||||||
cleanup_player(player_id, &connections, &matches, &waiting_queue).await;
|
cleanup_player(player_id, &connections, &matches, &waiting_queue).await;
|
||||||
println!("Connection {} closed", player_id);
|
warn!("Connection {} closed", player_id);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -226,7 +383,7 @@ async fn cleanup_player(
|
|||||||
// Remove from connections
|
// Remove from connections
|
||||||
connections.lock().await.remove(&player_id);
|
connections.lock().await.remove(&player_id);
|
||||||
|
|
||||||
println!("Cleaned up player {}", player_id);
|
warn!("Cleaned up player {}", player_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -239,8 +396,24 @@ mod tests {
|
|||||||
let connections = new_connection_map();
|
let connections = new_connection_map();
|
||||||
let player_id = Uuid::new_v4();
|
let player_id = Uuid::new_v4();
|
||||||
|
|
||||||
let result = send_message_to_player(&connections, player_id, "test message").await;
|
// Test 1: Pass None directly (non-existent player)
|
||||||
assert!(result.is_ok(), "Should handle missing player gracefully");
|
let result = send_message_to_player_connection(None, "test message").await;
|
||||||
|
|
||||||
|
assert!(result.is_err(), "Should return error for None connection");
|
||||||
|
println!("Test passed: Handles None connection correctly");
|
||||||
|
|
||||||
|
// Test 2: Try to get non-existent player from map
|
||||||
|
let mut conn = connections.lock().await;
|
||||||
|
let non_existent_connection = conn.get_mut(&player_id); // This will be None
|
||||||
|
|
||||||
|
let result2 =
|
||||||
|
send_message_to_player_connection(non_existent_connection, "test message").await;
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result2.is_err(),
|
||||||
|
"Should return error for non-existent player"
|
||||||
|
);
|
||||||
|
println!("Test passed: Handles non-existent player in map correctly");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
mod connection;
|
mod connection;
|
||||||
mod matchmaking;
|
mod matchmaking;
|
||||||
mod messages;
|
use env_logger::{Env, Logger};
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use std::env;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
let env = Env::default().filter_or("MY_LOG_LEVEL", "INFO");
|
||||||
|
env_logger::init_from_env(env);
|
||||||
|
|
||||||
let address = "0.0.0.0:9001";
|
let address = "0.0.0.0:9001";
|
||||||
let listener = TcpListener::bind(address).await?;
|
let listener = TcpListener::bind(address).await?;
|
||||||
println!("Server running on ws://{}", address);
|
info!("Server running on ws://{}", address);
|
||||||
|
|
||||||
// Shared state initialization using the new helper functions
|
// Shared state initialization using the new helper functions
|
||||||
let connections = connection::new_connection_map();
|
let connections = connection::new_connection_map();
|
||||||
@@ -34,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
connection::handle_connection(stream, connections, matches, waiting_queue).await
|
connection::handle_connection(stream, connections, matches, waiting_queue).await
|
||||||
{
|
{
|
||||||
eprintln!("Connection error: {}", e);
|
error!("Connection error: {}", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
use crate::connection::ServerMessage2;
|
||||||
|
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue, broadcast_to_match};
|
||||||
|
use log::{error, info, warn};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -20,21 +22,30 @@ impl MatchmakingSystem {
|
|||||||
pub async fn run(&self) {
|
pub async fn run(&self) {
|
||||||
loop {
|
loop {
|
||||||
self.try_create_match().await;
|
self.try_create_match().await;
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn clean_up(&self, match_id: Uuid) {
|
||||||
|
self.matches.lock().await.remove(&match_id);
|
||||||
|
}
|
||||||
|
|
||||||
async fn try_create_match(&self) {
|
async fn try_create_match(&self) {
|
||||||
|
info!("Checking for new matches!");
|
||||||
let mut queue = self.waiting_queue.lock().await;
|
let mut queue = self.waiting_queue.lock().await;
|
||||||
|
|
||||||
while queue.len() >= 2 {
|
while queue.len() >= 2 {
|
||||||
let player1 = queue.pop_front().unwrap();
|
let player1 = queue.pop_front().unwrap();
|
||||||
let player2 = queue.pop_front().unwrap();
|
let player2 = queue.pop_front().unwrap();
|
||||||
|
|
||||||
|
info!("Creating new match. Players: {}, {}", &player1, &player2);
|
||||||
|
|
||||||
let match_id = Uuid::new_v4();
|
let match_id = Uuid::new_v4();
|
||||||
let (white_player, black_player) = if random::<bool>() {
|
let (white_player, black_player) = if random::<bool>() {
|
||||||
|
info!("player1 is white, player2 is black");
|
||||||
(player1, player2)
|
(player1, player2)
|
||||||
} else {
|
} else {
|
||||||
|
info!("player2 is white, player1 is black");
|
||||||
(player2, player1)
|
(player2, player1)
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -46,6 +57,8 @@ impl MatchmakingSystem {
|
|||||||
move_history: Vec::new(),
|
move_history: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
info!("Match id: {}", &game_match.id);
|
||||||
|
|
||||||
// Store the match
|
// Store the match
|
||||||
self.matches.lock().await.insert(match_id, game_match);
|
self.matches.lock().await.insert(match_id, game_match);
|
||||||
|
|
||||||
@@ -54,52 +67,68 @@ impl MatchmakingSystem {
|
|||||||
let mut conn_map = self.connections.lock().await;
|
let mut conn_map = self.connections.lock().await;
|
||||||
if let Some(player) = conn_map.get_mut(&white_player) {
|
if let Some(player) = conn_map.get_mut(&white_player) {
|
||||||
player.current_match = Some(match_id);
|
player.current_match = Some(match_id);
|
||||||
|
} else {
|
||||||
|
error!("Could not store match id for white player");
|
||||||
}
|
}
|
||||||
if let Some(player) = conn_map.get_mut(&black_player) {
|
if let Some(player) = conn_map.get_mut(&black_player) {
|
||||||
player.current_match = Some(match_id);
|
player.current_match = Some(match_id);
|
||||||
|
} else {
|
||||||
|
error!("Could not store match id for black player");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify players
|
// Notify players
|
||||||
|
info!(
|
||||||
|
"Notifying player for a match: {:?} | {:?}",
|
||||||
|
white_player, black_player
|
||||||
|
);
|
||||||
self.notify_players(white_player, black_player, match_id)
|
self.notify_players(white_player, black_player, match_id)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn notify_players(&self, white: Uuid, black: Uuid, match_id: Uuid) {
|
async fn notify_players(&self, white: Uuid, black: Uuid, match_id: Uuid) {
|
||||||
let conn_map = self.connections.lock().await;
|
let mut conn_map = self.connections.lock().await;
|
||||||
|
|
||||||
// Get opponent names
|
|
||||||
let white_name = conn_map
|
|
||||||
.get(&black)
|
|
||||||
.and_then(|c| c.username.as_deref())
|
|
||||||
.unwrap_or("Opponent");
|
|
||||||
let black_name = conn_map
|
|
||||||
.get(&white)
|
|
||||||
.and_then(|c| c.username.as_deref())
|
|
||||||
.unwrap_or("Opponent");
|
|
||||||
|
|
||||||
// Notify white player
|
// Notify white player
|
||||||
if let Some(_) = conn_map.get(&white) {
|
if let Some(_) = conn_map.get(&white) {
|
||||||
let message = format!(
|
let message = ServerMessage2::MatchFound {
|
||||||
r#"{{"type": "match_found", "match_id": "{}", "opponent": "{}", "color": "white"}}"#,
|
match_id: match_id.clone(),
|
||||||
match_id, black_name
|
color: String::from("white"),
|
||||||
);
|
opponent_name: conn_map
|
||||||
let _ =
|
.get(&white)
|
||||||
crate::connection::send_message_to_player(&self.connections, white, &message).await;
|
.and_then(|c| c.username.as_deref())
|
||||||
|
.unwrap_or("Opponent")
|
||||||
|
.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = crate::connection::send_message_to_player_connection(
|
||||||
|
conn_map.get_mut(&white),
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify black player
|
// Notify black player
|
||||||
if let Some(_) = conn_map.get(&black) {
|
if let Some(_) = conn_map.get(&black) {
|
||||||
let message = format!(
|
let message = ServerMessage2::MatchFound {
|
||||||
r#"{{"type": "match_found", "match_id": "{}", "opponent": "{}", "color": "black"}}"#,
|
match_id: match_id.clone(),
|
||||||
match_id, white_name
|
color: String::from("black"),
|
||||||
);
|
opponent_name: conn_map
|
||||||
let _ =
|
.get(&black)
|
||||||
crate::connection::send_message_to_player(&self.connections, black, &message).await;
|
.and_then(|c| c.username.as_deref())
|
||||||
|
.unwrap_or("Opponent")
|
||||||
|
.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = crate::connection::send_message_to_player_connection(
|
||||||
|
conn_map.get_mut(&white),
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Match created: {} (white) vs {} (black)", white, black);
|
info!("Match created: {} (white) vs {} (black)", white, black);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
#[serde(tag = "type")]
|
|
||||||
pub enum ServerMessage {
|
|
||||||
Welcome {
|
|
||||||
player_id: String,
|
|
||||||
},
|
|
||||||
MatchFound {
|
|
||||||
match_id: String,
|
|
||||||
opponent: String,
|
|
||||||
color: String,
|
|
||||||
},
|
|
||||||
GameStart {
|
|
||||||
fen: String,
|
|
||||||
white_time: u32,
|
|
||||||
black_time: u32,
|
|
||||||
},
|
|
||||||
MoveResult {
|
|
||||||
valid: bool,
|
|
||||||
from: String,
|
|
||||||
to: String,
|
|
||||||
new_fen: String,
|
|
||||||
},
|
|
||||||
OpponentMove {
|
|
||||||
from: String,
|
|
||||||
to: String,
|
|
||||||
},
|
|
||||||
GameEnd {
|
|
||||||
result: String,
|
|
||||||
reason: String,
|
|
||||||
},
|
|
||||||
Error {
|
|
||||||
reason: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user