diff --git a/engine/Cargo.toml b/engine/Cargo.toml index 15addf3..0ffa59b 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] -once_cell = "1.19" \ No newline at end of file +once_cell = "1.19" +serde = { version = "1", features = ["derive"] } +serde_json = "1" diff --git a/engine/src/boardsquare.rs b/engine/src/boardsquare.rs index b4613fe..2587406 100644 --- a/engine/src/boardsquare.rs +++ b/engine/src/boardsquare.rs @@ -1,32 +1,31 @@ +use serde::{Deserialize, Serialize}; +#[derive(Serialize, Deserialize)] pub struct BoardSquare { - pub x: usize, - pub y: usize + pub x: usize, + pub y: usize, } impl BoardSquare { - - pub fn new() -> Self { - return Self{ - x: 0, - y: 0 - }; - } - - pub fn from_coord(x: usize, y: usize) -> Self { - - #[cfg(debug_assertions)] - { - if x > 7 { - println!("Warning: x coordinate of square is bigger than 7, it might not be on the board!"); - } - if y > 7 { - println!("Warning: y coordinate of square is bigger than 7, it might not be on the board!"); - } + pub fn new() -> Self { + return Self { x: 0, y: 0 }; } - return Self { - x: x, - y: y - }; - } -} \ No newline at end of file + + pub fn from_coord(x: usize, y: usize) -> Self { + #[cfg(debug_assertions)] + { + if x > 7 { + println!( + "Warning: x coordinate of square is bigger than 7, it might not be on the board!" + ); + } + if y > 7 { + println!( + "Warning: y coordinate of square is bigger than 7, it might not be on the board!" + ); + } + } + return Self { x: x, y: y }; + } +} + diff --git a/engine/src/chessmove.rs b/engine/src/chessmove.rs index cf92de1..ae46a6a 100644 --- a/engine/src/chessmove.rs +++ b/engine/src/chessmove.rs @@ -1,71 +1,71 @@ use crate::piecetype; use super::boardsquare::BoardSquare; -use super::piecetype::PieceType; use super::movetype::MoveType; +use super::piecetype::PieceType; +use serde::{Deserialize, Serialize}; - +#[derive(Serialize, Deserialize)] pub struct ChessMove { - pub move_type: MoveType, - pub piece_type: PieceType, - pub from_square: BoardSquare, - pub to_square: BoardSquare, - pub rook_from: BoardSquare, - pub rook_to: BoardSquare, - pub promotion_piece: Option + pub move_type: MoveType, + pub piece_type: PieceType, + pub from_square: BoardSquare, + pub to_square: BoardSquare, + pub rook_from: BoardSquare, + pub rook_to: BoardSquare, + pub promotion_piece: Option, } impl ChessMove { - - pub fn quiet( - piece_type: PieceType, - from_square: BoardSquare, - to_square: BoardSquare, - promotion_piece: Option - ) -> Self { - return Self { - move_type: MoveType::Quiet, - piece_type: piece_type, - from_square: from_square, - to_square: to_square, - rook_from: BoardSquare::new(), - rook_to: BoardSquare::new(), - promotion_piece: promotion_piece + pub fn quiet( + piece_type: PieceType, + from_square: BoardSquare, + to_square: BoardSquare, + promotion_piece: Option, + ) -> Self { + return Self { + move_type: MoveType::Quiet, + piece_type: piece_type, + from_square: from_square, + to_square: to_square, + rook_from: BoardSquare::new(), + rook_to: BoardSquare::new(), + promotion_piece: promotion_piece, + }; } - } - pub fn capture( - piece_type: PieceType, - from_square: BoardSquare, - to_square: BoardSquare, - promotion_piece: Option - ) -> Self { - return Self { - move_type: MoveType::Capture, - piece_type: piece_type, - from_square: from_square, - to_square: to_square, - rook_from: BoardSquare::new(), - rook_to: BoardSquare::new(), - promotion_piece: promotion_piece + pub fn capture( + piece_type: PieceType, + from_square: BoardSquare, + to_square: BoardSquare, + promotion_piece: Option, + ) -> Self { + return Self { + move_type: MoveType::Capture, + piece_type: piece_type, + from_square: from_square, + to_square: to_square, + rook_from: BoardSquare::new(), + rook_to: BoardSquare::new(), + promotion_piece: promotion_piece, + }; } - } - pub fn castle( - piece_type: PieceType, - from_square: BoardSquare, - to_square: BoardSquare, - rook_from: BoardSquare, - rook_to: BoardSquare - ) -> Self { - return Self { - move_type: MoveType::Quiet, - piece_type: piece_type, - from_square: from_square, - to_square: to_square, - rook_from: rook_from, - rook_to: rook_to, - promotion_piece: None + pub fn castle( + piece_type: PieceType, + from_square: BoardSquare, + to_square: BoardSquare, + rook_from: BoardSquare, + rook_to: BoardSquare, + ) -> Self { + return Self { + move_type: MoveType::Quiet, + piece_type: piece_type, + from_square: from_square, + to_square: to_square, + rook_from: rook_from, + rook_to: rook_to, + promotion_piece: None, + }; } - } -} \ No newline at end of file +} diff --git a/engine/src/movetype.rs b/engine/src/movetype.rs index ec045a5..3c531dd 100644 --- a/engine/src/movetype.rs +++ b/engine/src/movetype.rs @@ -1,7 +1,11 @@ +use serde::Deserialize; +use serde::Serialize; +#[derive(Serialize, Deserialize)] pub enum MoveType { - Quiet, - Capture, - Castle, - EnPassant -} \ No newline at end of file + Quiet, + Capture, + Castle, + EnPassant, +} + diff --git a/engine/src/piecetype.rs b/engine/src/piecetype.rs index 3eb23f1..b79d341 100644 --- a/engine/src/piecetype.rs +++ b/engine/src/piecetype.rs @@ -1,15 +1,18 @@ +use serde::{Deserialize, Serialize}; +#[derive(Serialize, Deserialize)] pub enum PieceType { - WhitePawn, - WhiteKnight, - WhiteBishop, - WhiteRook, - WhiteQueen, - WhiteKing, - BlackPawn, - BlackKnight, - BlackBishop, - BlackRook, - BlackQueen, - BlackKing -} \ No newline at end of file + WhitePawn, + WhiteKnight, + WhiteBishop, + WhiteRook, + WhiteQueen, + WhiteKing, + BlackPawn, + BlackKnight, + BlackBishop, + BlackRook, + BlackQueen, + BlackKing, +} + diff --git a/server/Cargo.toml b/server/Cargo.toml index c0086f4..ab5f7bf 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,6 +14,8 @@ url = "2.5.7" uuid = {version = "1.18.1", features = ["v4", "serde"] } anyhow = "1.0.100" rand = "0.9.2" +engine = {path = "../engine/"} + [[bin]] name = "client" diff --git a/server/src/connection.rs b/server/src/connection.rs index 1fe78e8..0b9a534 100644 --- a/server/src/connection.rs +++ b/server/src/connection.rs @@ -1,4 +1,5 @@ use crate::connection::ClientEvent::*; +use engine::get_available_moves; use futures_util::{SinkExt, StreamExt}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; @@ -190,6 +191,17 @@ pub async fn handle_connection( println!("Appended {} to the waiting queue", player_id); println!("queue {:?}", wait_queue); } + Move { from, to } => {} + RequestLegalMoves { fen } => { + let moves = get_available_moves(&fen); + let _ = send_message_to_player( + &connections, + player_id, + &serde_json::to_string(&moves).unwrap(), + ) + .await; + println!("Sent moves to player: {}", player_id); + } _ => {} } }