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

@@ -5,3 +5,5 @@ edition = "2024"
[dependencies]
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 x: usize,
pub y: usize
pub y: usize,
}
impl BoardSquare {
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!");
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!");
println!(
"Warning: y coordinate of square is bigger than 7, it might not be on the board!"
);
}
}
return Self {
x: x,
y: y
};
return Self { x: x, y: y };
}
}

View File

@@ -1,10 +1,11 @@
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,
@@ -12,16 +13,15 @@ pub struct ChessMove {
pub to_square: BoardSquare,
pub rook_from: BoardSquare,
pub rook_to: BoardSquare,
pub promotion_piece: Option<PieceType>
pub promotion_piece: Option<PieceType>,
}
impl ChessMove {
pub fn quiet(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
promotion_piece: Option<PieceType>,
) -> Self {
return Self {
move_type: MoveType::Quiet,
@@ -30,15 +30,15 @@ impl ChessMove {
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece
}
promotion_piece: promotion_piece,
};
}
pub fn capture(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
promotion_piece: Option<PieceType>,
) -> Self {
return Self {
move_type: MoveType::Capture,
@@ -47,8 +47,8 @@ impl ChessMove {
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece
}
promotion_piece: promotion_piece,
};
}
pub fn castle(
@@ -56,7 +56,7 @@ impl ChessMove {
from_square: BoardSquare,
to_square: BoardSquare,
rook_from: BoardSquare,
rook_to: BoardSquare
rook_to: BoardSquare,
) -> Self {
return Self {
move_type: MoveType::Quiet,
@@ -65,7 +65,7 @@ impl ChessMove {
to_square: to_square,
rook_from: rook_from,
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 {
Quiet,
Capture,
Castle,
EnPassant
EnPassant,
}

View File

@@ -1,4 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub enum PieceType {
WhitePawn,
WhiteKnight,
@@ -11,5 +13,6 @@ pub enum PieceType {
BlackBishop,
BlackRook,
BlackQueen,
BlackKing
BlackKing,
}

View File

@@ -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"

View File

@@ -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);
}
_ => {}
}
}