added engine as a crate into server project, implemented request moves event

This commit is contained in:
2025-11-18 16:43:14 +01:00
parent 6dfe92f211
commit 48f66aac75
7 changed files with 124 additions and 102 deletions

View File

@@ -4,4 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
once_cell = "1.19" once_cell = "1.19"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

View File

@@ -1,32 +1,31 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct BoardSquare { pub struct BoardSquare {
pub x: usize, pub x: usize,
pub y: usize pub y: usize,
} }
impl BoardSquare { impl BoardSquare {
pub fn new() -> Self {
pub fn new() -> Self { return Self { x: 0, y: 0 };
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!");
}
} }
return Self {
x: x, pub fn from_coord(x: usize, y: usize) -> Self {
y: y #[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 };
}
}

View File

@@ -1,71 +1,71 @@
use crate::piecetype; use crate::piecetype;
use super::boardsquare::BoardSquare; use super::boardsquare::BoardSquare;
use super::piecetype::PieceType;
use super::movetype::MoveType; use super::movetype::MoveType;
use super::piecetype::PieceType;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ChessMove { pub struct ChessMove {
pub move_type: MoveType, pub move_type: MoveType,
pub piece_type: PieceType, pub piece_type: PieceType,
pub from_square: BoardSquare, pub from_square: BoardSquare,
pub to_square: BoardSquare, pub to_square: BoardSquare,
pub rook_from: BoardSquare, pub rook_from: BoardSquare,
pub rook_to: BoardSquare, pub rook_to: BoardSquare,
pub promotion_piece: Option<PieceType> pub promotion_piece: Option<PieceType>,
} }
impl ChessMove { impl ChessMove {
pub fn quiet(
pub fn quiet( piece_type: PieceType,
piece_type: PieceType, from_square: BoardSquare,
from_square: BoardSquare, to_square: BoardSquare,
to_square: BoardSquare, promotion_piece: Option<PieceType>,
promotion_piece: Option<PieceType> ) -> Self {
) -> Self { return Self {
return Self { move_type: MoveType::Quiet,
move_type: MoveType::Quiet, piece_type: piece_type,
piece_type: piece_type, from_square: from_square,
from_square: from_square, to_square: to_square,
to_square: to_square, rook_from: BoardSquare::new(),
rook_from: BoardSquare::new(), rook_to: BoardSquare::new(),
rook_to: BoardSquare::new(), promotion_piece: promotion_piece,
promotion_piece: promotion_piece };
} }
}
pub fn capture( pub fn capture(
piece_type: PieceType, piece_type: PieceType,
from_square: BoardSquare, from_square: BoardSquare,
to_square: BoardSquare, to_square: BoardSquare,
promotion_piece: Option<PieceType> promotion_piece: Option<PieceType>,
) -> Self { ) -> Self {
return Self { return Self {
move_type: MoveType::Capture, move_type: MoveType::Capture,
piece_type: piece_type, piece_type: piece_type,
from_square: from_square, from_square: from_square,
to_square: to_square, to_square: to_square,
rook_from: BoardSquare::new(), rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(), rook_to: BoardSquare::new(),
promotion_piece: promotion_piece promotion_piece: promotion_piece,
};
} }
}
pub fn castle( pub fn castle(
piece_type: PieceType, piece_type: PieceType,
from_square: BoardSquare, from_square: BoardSquare,
to_square: BoardSquare, to_square: BoardSquare,
rook_from: BoardSquare, rook_from: BoardSquare,
rook_to: BoardSquare rook_to: BoardSquare,
) -> Self { ) -> Self {
return Self { return Self {
move_type: MoveType::Quiet, move_type: MoveType::Quiet,
piece_type: piece_type, piece_type: piece_type,
from_square: from_square, from_square: from_square,
to_square: to_square, to_square: to_square,
rook_from: rook_from, rook_from: rook_from,
rook_to: rook_to, rook_to: rook_to,
promotion_piece: None promotion_piece: None,
};
} }
} }
}

View File

@@ -1,7 +1,11 @@
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize)]
pub enum MoveType { pub enum MoveType {
Quiet, Quiet,
Capture, Capture,
Castle, Castle,
EnPassant EnPassant,
} }

View File

@@ -1,15 +1,18 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub enum PieceType { pub enum PieceType {
WhitePawn, WhitePawn,
WhiteKnight, WhiteKnight,
WhiteBishop, WhiteBishop,
WhiteRook, WhiteRook,
WhiteQueen, WhiteQueen,
WhiteKing, WhiteKing,
BlackPawn, BlackPawn,
BlackKnight, BlackKnight,
BlackBishop, BlackBishop,
BlackRook, BlackRook,
BlackQueen, BlackQueen,
BlackKing BlackKing,
} }

View File

@@ -14,6 +14,8 @@ url = "2.5.7"
uuid = {version = "1.18.1", features = ["v4", "serde"] } 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/"}
[[bin]] [[bin]]
name = "client" name = "client"

View File

@@ -1,4 +1,5 @@
use crate::connection::ClientEvent::*; use crate::connection::ClientEvent::*;
use engine::get_available_moves;
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
@@ -190,6 +191,17 @@ pub async fn handle_connection(
println!("Appended {} to the waiting queue", player_id); println!("Appended {} to the waiting queue", player_id);
println!("queue {:?}", wait_queue); 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);
}
_ => {} _ => {}
} }
} }