Compare commits

...

9 Commits

5 changed files with 789 additions and 248 deletions

View File

@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BoardSquare {
pub x: usize,
pub y: usize,
@@ -28,10 +28,10 @@ impl BoardSquare {
return Self { x: x, y: y };
}
pub(in super) fn from_index(idx: u8) -> Self {
pub(super) fn from_index(idx: u8) -> Self {
let file = idx % 8;
let rank = idx / 8;
#[cfg(debug_assertions)]
{
if !(0..8).contains(&rank) {
@@ -39,10 +39,12 @@ impl BoardSquare {
}
}
return Self {x: file as usize, y: rank as usize};
return Self {
x: file as usize,
y: rank as usize,
};
}
pub(in super) fn to_index(&self) -> u8 {
pub(super) fn to_index(&self) -> u8 {
return (8 * self.y + self.x) as u8;
}
}

View File

@@ -1,10 +1,13 @@
use crate::{bitboard::{bitmove::{BitMove, BitMoveType}, board::Board}};
use crate::bitboard::{
bitmove::{BitMove, BitMoveType},
board::Board,
};
use super::boardsquare::BoardSquare;
use super::piecetype::PieceType;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
/*pub struct ChessMove {
pub move_type: MoveType,
pub piece_type: PieceType,
@@ -19,14 +22,14 @@ pub enum ChessMove {
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
promotion_piece: Option<PieceType>,
},
Capture {
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
captured_piece: PieceType,
promotion_piece: Option<PieceType>
promotion_piece: Option<PieceType>,
},
Castle {
king_type: PieceType,
@@ -34,15 +37,15 @@ pub enum ChessMove {
king_to: BoardSquare,
rook_type: PieceType,
rook_from: BoardSquare,
rook_to: BoardSquare
rook_to: BoardSquare,
},
EnPassant {
pawn_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
captured_piece: PieceType,
captured_from: BoardSquare
}
captured_from: BoardSquare,
},
}
impl ChessMove {
@@ -56,7 +59,7 @@ impl ChessMove {
piece_type,
from_square,
to_square,
promotion_piece
promotion_piece,
};
}
@@ -72,7 +75,7 @@ impl ChessMove {
from_square,
to_square,
captured_piece,
promotion_piece
promotion_piece,
};
}
@@ -90,11 +93,11 @@ impl ChessMove {
king_to,
rook_type,
rook_from,
rook_to
rook_to,
};
}
pub(in super) fn from_bitmove(bitmove: &BitMove, board: &Board) -> Self {
pub(super) fn from_bitmove(bitmove: &BitMove, board: &Board) -> Self {
match bitmove.move_type() {
BitMoveType::Quiet => {
let from_square_index = bitmove.from_square();
@@ -103,11 +106,16 @@ impl ChessMove {
let to_square = BoardSquare::from_index(bitmove.to_square());
let promotion_piece = match bitmove.promotion_piece() {
Some(piece) => Some(PieceType::from_index(piece)),
None => None
None => None,
};
return ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece }
},
return ChessMove::Quiet {
piece_type,
from_square,
to_square,
promotion_piece,
};
}
BitMoveType::Capture => {
let from_square_index = bitmove.from_square();
let to_square_index = bitmove.to_square();
@@ -117,18 +125,28 @@ impl ChessMove {
let captured_piece = PieceType::from_index(board.piece_board(to_square_index));
let promotion_piece = match bitmove.promotion_piece() {
Some(piece) => Some(PieceType::from_index(piece)),
None => None
None => None,
};
return ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece }
},
return ChessMove::Capture {
piece_type,
from_square,
to_square,
captured_piece,
promotion_piece,
};
}
BitMoveType::Castle => {
let from_square_index = bitmove.from_square();
let to_square_index = bitmove.to_square();
let king_type = PieceType::from_index(board.piece_board(from_square_index));
let king_from = BoardSquare::from_index(from_square_index);
let king_to = BoardSquare::from_index(to_square_index);
let rook_type = if bitmove.from_square() < 32 { PieceType::WhiteRook } else { PieceType::BlackRook };
let rook_type = if bitmove.from_square() < 32 {
PieceType::WhiteRook
} else {
PieceType::BlackRook
};
let rook_from_index = if bitmove.to_square() > bitmove.from_square() {
bitmove.from_square() + 3
} else {
@@ -142,44 +160,72 @@ impl ChessMove {
};
let rook_to = BoardSquare::from_index(rook_to_index);
return ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to }
},
return ChessMove::Castle {
king_type,
king_from,
king_to,
rook_type,
rook_from,
rook_to,
};
}
BitMoveType::EnPassant => {
panic!("ChessMove::from_bitmove was left unimplemented");
}
}
}
pub(in super) fn to_bitmove(&self) -> BitMove {
pub(super) fn to_bitmove(&self) -> BitMove {
let bitmove = match self {
ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece } => {
ChessMove::Quiet {
piece_type,
from_square,
to_square,
promotion_piece,
} => {
let promotion_piece = match promotion_piece {
Some(piece) => Some(piece.to_index()),
None => None
None => None,
};
return BitMove::quiet(
from_square.to_index(),
to_square.to_index(),
promotion_piece
promotion_piece,
);
},
ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece } => {
}
ChessMove::Capture {
piece_type,
from_square,
to_square,
captured_piece,
promotion_piece,
} => {
let promotion_piece = match promotion_piece {
Some(piece) => Some(piece.to_index()),
None => None
None => None,
};
return BitMove::capture(
from_square.to_index(),
to_square.to_index(),
promotion_piece
promotion_piece,
);
},
ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to } => {
return BitMove::castle(
king_from.to_index(),
king_to.to_index()
);
},
ChessMove::EnPassant { pawn_type, from_square, to_square, captured_piece, captured_from } => {
}
ChessMove::Castle {
king_type,
king_from,
king_to,
rook_type,
rook_from,
rook_to,
} => {
return BitMove::castle(king_from.to_index(), king_to.to_index());
}
ChessMove::EnPassant {
pawn_type,
from_square,
to_square,
captured_piece,
captured_from,
} => {
panic!("ChessMove::to_bitmove was left unimplemented");
}
};

View File

@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum PieceType {
WhitePawn,
WhiteKnight,
@@ -17,8 +17,7 @@ pub enum PieceType {
}
impl PieceType {
pub(in super) fn from_index(idx: u8) -> Self {
pub(super) fn from_index(idx: u8) -> Self {
return match idx {
0 => PieceType::WhitePawn,
1 => PieceType::WhiteKnight,
@@ -32,10 +31,10 @@ impl PieceType {
9 => PieceType::BlackRook,
10 => PieceType::BlackQueen,
11 => PieceType::BlackKing,
_ => panic!("invalid piece index! should NEVER appear")
}
_ => panic!("invalid piece index! should NEVER appear"),
};
}
pub(in super) fn to_index(&self) -> u8 {
pub(super) fn to_index(&self) -> u8 {
return match self {
&PieceType::WhitePawn => 0,
&PieceType::WhiteKnight => 1,
@@ -48,7 +47,8 @@ impl PieceType {
&PieceType::BlackBishop => 8,
&PieceType::BlackRook => 9,
&PieceType::BlackQueen => 10,
&PieceType::BlackKing => 11
}
&PieceType::BlackKing => 11,
};
}
}
}

View File

@@ -52,6 +52,7 @@ pub enum ServerMessage2 {
},
UIUpdate {
fen: String,
turn_player: String,
},
MatchFound {
match_id: Uuid,
@@ -66,12 +67,22 @@ pub enum ServerMessage2 {
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
enum ClientEvent {
Join { username: String },
Join {
username: String,
},
FindMatch,
Move { step: ChessMove },
Move {
step: ChessMove,
turn_player: String,
},
Resign,
Chat { text: String },
RequestLegalMoves { fen: String },
Chat {
text: String,
},
RequestLegalMoves {
fen: String,
},
CloseConnection,
}
#[derive(Debug)]
@@ -213,7 +224,7 @@ pub async fn handle_connection(
info!("Appended {} to the waiting queue", player_id);
info!("queue {:?}", wait_queue);
}
Move { step } => {
Move { step, turn_player } => {
let match_id = connections
.lock()
.await
@@ -239,6 +250,7 @@ pub async fn handle_connection(
.unwrap()
.board_state
.clone(),
turn_player: turn_player,
};
let _ = broadcast_to_match(
@@ -265,7 +277,7 @@ pub async fn handle_connection(
&serde_json::to_string(&message).unwrap(),
)
.await;
clean_up_match(&matches, &match_id);
clean_up_match(&matches, &match_id).await;
}
None => {
info!("No winner match continues. Id: {}", &match_id);
@@ -329,7 +341,7 @@ pub async fn handle_connection(
}
};
broadcast_to_match(
let _ = broadcast_to_match(
&connections,
&matches,
connections
@@ -342,7 +354,11 @@ pub async fn handle_connection(
&serde_json::to_string(&fuck).unwrap(),
)
.await;
clean_up_match(&matches, fuck_id);
clean_up_match(&matches, fuck_id).await;
}
CloseConnection => {
warn!("Closing connection for: {}", &player_id);
break;
}
_ => {
warn!("Not known client event");

File diff suppressed because it is too large Load Diff