Compare commits
28 Commits
Engine/API
...
v170
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fd04238f5 | ||
|
|
c7bad7665f | ||
|
|
b2d1d3f809 | ||
|
|
fe8f8621d7 | ||
|
|
767e7d14fd | ||
|
|
074f181791 | ||
|
|
542dd39aa6 | ||
|
|
6db4ae6d07 | ||
|
|
171a7b8020 | ||
|
|
205e8811d5 | ||
|
|
2394da84ce | ||
|
|
e7c7743682 | ||
|
|
2375f28ee3 | ||
|
|
036f57daa0 | ||
| a0bca32733 | |||
|
|
5b6318442e | ||
|
|
24ddc74573 | ||
| bdbe93932d | |||
| 75c39ccbe4 | |||
| df02c2eee1 | |||
| 5763716848 | |||
| 2112109470 | |||
|
|
933bb46f17 | ||
| 5deebb0621 | |||
| 60911f93f6 | |||
| 7820555fa8 | |||
| b5c18419d8 | |||
|
|
32ab155ff5 |
1
.github/workflows/dispatcher.yml
vendored
1
.github/workflows/dispatcher.yml
vendored
@@ -77,6 +77,7 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
|
needs: [engine, server, ui]
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/master'
|
||||||
uses: ./.github/workflows/release.yml
|
uses: ./.github/workflows/release.yml
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|||||||
3
.github/workflows/release.yml
vendored
3
.github/workflows/release.yml
vendored
@@ -12,9 +12,6 @@ jobs:
|
|||||||
name: Build and release (master only)
|
name: Build and release (master only)
|
||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Build Engine for Linux
|
- name: Build Engine for Linux
|
||||||
run: |
|
run: |
|
||||||
cd $(git rev-parse --show-toplevel)
|
cd $(git rev-parse --show-toplevel)
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ mod movegen;
|
|||||||
|
|
||||||
pub mod board;
|
pub mod board;
|
||||||
pub(in super) mod bitmove;
|
pub(in super) mod bitmove;
|
||||||
pub(in super) mod movebuffer;
|
pub(in super) mod movebuffer;
|
||||||
|
pub(in super) mod makemove;
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::bitboard::utils::notation_from_square_number;
|
||||||
|
|
||||||
use super::utils::try_get_square_number_from_notation;
|
use super::utils::try_get_square_number_from_notation;
|
||||||
|
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
@@ -12,6 +14,7 @@ pub struct Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
pub const EMPTY_SQUARE: u8 = 12;
|
||||||
|
|
||||||
pub fn new_clear() -> Self {
|
pub fn new_clear() -> Self {
|
||||||
let mut bit_board: Self = Self {
|
let mut bit_board: Self = Self {
|
||||||
@@ -159,6 +162,57 @@ impl Board {
|
|||||||
return if self.side_to_move == 0 { self.bitboards[5].trailing_zeros() } else { self.bitboards[11].trailing_zeros() };
|
return if self.side_to_move == 0 { self.bitboards[5].trailing_zeros() } else { self.bitboards[11].trailing_zeros() };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fen(&self) -> String {
|
||||||
|
let mut fen = String::new();
|
||||||
|
|
||||||
|
for row in (0..8).rev() {
|
||||||
|
let mut empty = 0;
|
||||||
|
for col in 0..8 {
|
||||||
|
let sq = row * 8 + col;
|
||||||
|
if let Some(piece) = self.get_piece_character(sq) {
|
||||||
|
if empty > 0 {
|
||||||
|
fen.push_str(&empty.to_string());
|
||||||
|
empty = 0;
|
||||||
|
}
|
||||||
|
fen.push(piece);
|
||||||
|
} else {
|
||||||
|
empty += 1;
|
||||||
|
if col == 7 {
|
||||||
|
fen.push_str(&empty.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if row > 0 {
|
||||||
|
fen.push('/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.side_to_move() == 0 { fen.push('w'); } else { fen.push('b'); }
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.castling_rights() == 0 {
|
||||||
|
fen.push('-');
|
||||||
|
} else {
|
||||||
|
if self.castling_rights() & (1 << 3) != 0 { fen.push('K'); }
|
||||||
|
if self.castling_rights() & (1 << 2) != 0 { fen.push('Q'); }
|
||||||
|
if self.castling_rights() & (1 << 1) != 0 { fen.push('k'); }
|
||||||
|
if self.castling_rights() & (1 << 0) != 0 { fen.push('q'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.en_passant_square() == 0 {
|
||||||
|
fen.push('-');
|
||||||
|
} else {
|
||||||
|
let sq = self.en_passant_square().trailing_zeros();
|
||||||
|
fen.push_str(¬ation_from_square_number(sq as u8));
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push_str(" 0 1");
|
||||||
|
|
||||||
|
return fen;
|
||||||
|
}
|
||||||
|
|
||||||
fn calc_occupancy(&mut self) {
|
fn calc_occupancy(&mut self) {
|
||||||
self.occupancy = [0u64; 3];
|
self.occupancy = [0u64; 3];
|
||||||
for b in 0..6 {
|
for b in 0..6 {
|
||||||
@@ -196,4 +250,46 @@ impl Board {
|
|||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn get_piece_character(&self, index: i32) -> Option<char> {
|
||||||
|
let sq = 1 << index;
|
||||||
|
|
||||||
|
if (self.bitboards[0] & sq) != 0 {
|
||||||
|
return Some('P');
|
||||||
|
}
|
||||||
|
if (self.bitboards[1] & sq) != 0 {
|
||||||
|
return Some('N');
|
||||||
|
}
|
||||||
|
if (self.bitboards[2] & sq) != 0 {
|
||||||
|
return Some('B');
|
||||||
|
}
|
||||||
|
if (self.bitboards[3] & sq) != 0 {
|
||||||
|
return Some('R');
|
||||||
|
}
|
||||||
|
if (self.bitboards[4] & sq) != 0 {
|
||||||
|
return Some('Q');
|
||||||
|
}
|
||||||
|
if (self.bitboards[5] & sq) != 0 {
|
||||||
|
return Some('K');
|
||||||
|
}
|
||||||
|
if (self.bitboards[6] & sq) != 0 {
|
||||||
|
return Some('p');
|
||||||
|
}
|
||||||
|
if (self.bitboards[7] & sq) != 0 {
|
||||||
|
return Some('n');
|
||||||
|
}
|
||||||
|
if (self.bitboards[8] & sq) != 0 {
|
||||||
|
return Some('b');
|
||||||
|
}
|
||||||
|
if (self.bitboards[9] & sq) != 0 {
|
||||||
|
return Some('r');
|
||||||
|
}
|
||||||
|
if (self.bitboards[10] & sq) != 0 {
|
||||||
|
return Some('q');
|
||||||
|
}
|
||||||
|
if (self.bitboards[11] & sq) != 0 {
|
||||||
|
return Some('k');
|
||||||
|
}
|
||||||
|
|
||||||
|
return None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
38
engine/src/bitboard/makemove.rs
Normal file
38
engine/src/bitboard/makemove.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
mod quiets;
|
||||||
|
mod captures;
|
||||||
|
mod castles;
|
||||||
|
|
||||||
|
use super::bitmove::BitMoveType;
|
||||||
|
use super::bitmove::BitMove;
|
||||||
|
use super::board::Board;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn make_move(&mut self, played_move: &BitMove) {
|
||||||
|
let move_type = played_move.move_type();
|
||||||
|
|
||||||
|
match move_type {
|
||||||
|
BitMoveType::Quiet => {
|
||||||
|
self.make_quiet(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::Capture => {
|
||||||
|
self.make_capture(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::Castle => {
|
||||||
|
self.make_castle(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::EnPassant => {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.occupancy[2] = self.occupancy[0] | self.occupancy[1];
|
||||||
|
|
||||||
|
if self.en_passant_square != 0 {
|
||||||
|
self.en_passant_square = 0u64;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.side_to_move = 1 - self.side_to_move;
|
||||||
|
}
|
||||||
|
}
|
||||||
82
engine/src/bitboard/makemove/captures.rs
Normal file
82
engine/src/bitboard/makemove/captures.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_capture(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let color_offset = self.side_to_move * 6;
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
let castling_rights = self.castling_rights >> castling_offset;
|
||||||
|
|
||||||
|
let mut taken_piece = 0u8;
|
||||||
|
|
||||||
|
taken_piece = self.piece_board(main_to as u8);
|
||||||
|
let secondary_piece = taken_piece as usize;
|
||||||
|
let secondary_from = main_to;
|
||||||
|
|
||||||
|
let opponent_castling_offset = 2 * self.side_to_move as usize;
|
||||||
|
let opponent_castling_rights = self.castling_rights >> opponent_castling_offset;
|
||||||
|
|
||||||
|
let opponent_occupancy = 1 - self.side_to_move as usize;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] &= !(1 << secondary_from);
|
||||||
|
self.occupancy[opponent_occupancy] &= !(1 << secondary_from);
|
||||||
|
self.piece_board[secondary_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
if opponent_castling_rights != 0
|
||||||
|
&& secondary_piece == 9 - color_offset as usize{
|
||||||
|
|
||||||
|
let back_rank_offset = 56 - 56 * self.side_to_move as usize;
|
||||||
|
if opponent_castling_rights & 0b01 != 0
|
||||||
|
&& secondary_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << opponent_castling_offset);
|
||||||
|
}
|
||||||
|
else if opponent_castling_rights & 0b10 != 0
|
||||||
|
&& secondary_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << opponent_castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = played_move.promotion_piece() {
|
||||||
|
let promotion_piece = (color_offset + promotion_piece) as usize;
|
||||||
|
self.bitboards[promotion_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = promotion_piece as u8;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
if main_piece == 5 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
if castling_rights & 0b1 != 0 {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
if castling_rights & 0b10 != 0 {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if main_piece == 3 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
let back_rank_offset = 56 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
if castling_rights & 0b10 != 0
|
||||||
|
&& main_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
else if castling_rights & 0b1 != 0
|
||||||
|
&& main_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
engine/src/bitboard/makemove/castles.rs
Normal file
35
engine/src/bitboard/makemove/castles.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_castle(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
let secondary_piece: usize = main_piece - 2;
|
||||||
|
let is_kingside = main_to%8 > 4;
|
||||||
|
let secondary_from: usize = if is_kingside { main_to + 1 } else { main_to - 2 };
|
||||||
|
let secondary_to: usize = if is_kingside { main_to - 1 } else { main_to + 1 };
|
||||||
|
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] |= 1 << secondary_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << secondary_to;
|
||||||
|
self.piece_board[secondary_to] = secondary_piece as u8;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] &= !(1 << secondary_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << secondary_from);
|
||||||
|
self.piece_board[secondary_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.castling_rights &= !(3 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
engine/src/bitboard/makemove/quiets.rs
Normal file
61
engine/src/bitboard/makemove/quiets.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_quiet(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let color_offset = self.side_to_move * 6;
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
let castling_rights = self.castling_rights >> castling_offset;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = played_move.promotion_piece() {
|
||||||
|
let promotion_piece = (color_offset + promotion_piece) as usize;
|
||||||
|
self.bitboards[promotion_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = promotion_piece as u8;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
if main_piece == 0 && (main_to - main_from) == 16 {
|
||||||
|
let new_en_passant = main_to - 8;
|
||||||
|
self.en_passant_square = 1 << new_en_passant;
|
||||||
|
}
|
||||||
|
else if main_piece == 6 && (main_from - main_to) == 16 {
|
||||||
|
let new_en_passant = main_to + 8;
|
||||||
|
self.en_passant_square = 1 << new_en_passant;
|
||||||
|
}
|
||||||
|
else if main_piece == 5 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
if castling_rights & 0b1 != 0 {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
if castling_rights & 0b10 != 0 {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if main_piece == 3 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
let back_rank_offset = 56 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
if castling_rights & 0b10 != 0
|
||||||
|
&& main_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
else if castling_rights & 0b1 != 0
|
||||||
|
&& main_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,8 +43,13 @@ pub fn is_game_over(fen: &str) -> Option<GameEnd> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
|
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
|
||||||
|
let mut board = Board::build(fen);
|
||||||
|
let played_move = chess_move.to_bitmove();
|
||||||
|
|
||||||
println!("get_board_after_move answered");
|
println!("get_board_after_move answered");
|
||||||
return String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
board.make_move(&played_move);
|
||||||
|
|
||||||
|
return board.fen();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
1
server/Cargo.lock
generated
1
server/Cargo.lock
generated
@@ -768,7 +768,6 @@ dependencies = [
|
|||||||
"env_logger",
|
"env_logger",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
"futures-util",
|
|
||||||
"rand 0.9.2",
|
"rand 0.9.2",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|||||||
@@ -7,12 +7,6 @@ use tokio_tungstenite::{connect_async, tungstenite::Message};
|
|||||||
use url::Url;
|
use url::Url;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct Step {
|
|
||||||
from: String,
|
|
||||||
to: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
enum ClientMessage {
|
enum ClientMessage {
|
||||||
@@ -24,17 +18,6 @@ enum ClientMessage {
|
|||||||
RequestLegalMoves { fen: String },
|
RequestLegalMoves { fen: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct ServerMessage {
|
|
||||||
#[serde(rename = "type")]
|
|
||||||
message_type: String,
|
|
||||||
player_id: Option<String>,
|
|
||||||
match_id: Option<String>,
|
|
||||||
opponent: Option<String>,
|
|
||||||
color: Option<String>,
|
|
||||||
reason: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub enum ServerMessage2 {
|
pub enum ServerMessage2 {
|
||||||
GameEnd {
|
GameEnd {
|
||||||
@@ -48,6 +31,9 @@ pub enum ServerMessage2 {
|
|||||||
color: String,
|
color: String,
|
||||||
opponent_name: String,
|
opponent_name: String,
|
||||||
},
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use engine::{get_available_moves, is_game_over};
|
|||||||
use futures_util::{SinkExt, StreamExt};
|
use futures_util::{SinkExt, StreamExt};
|
||||||
use log::{error, info, warn};
|
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;
|
||||||
@@ -46,18 +45,6 @@ pub struct Step {
|
|||||||
pub to: String,
|
pub to: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*#[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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub enum ServerMessage2 {
|
pub enum ServerMessage2 {
|
||||||
GameEnd {
|
GameEnd {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
mod connection;
|
mod connection;
|
||||||
mod matchmaking;
|
mod matchmaking;
|
||||||
use env_logger::{Env, Logger};
|
use env_logger::Env;
|
||||||
use log::{error, info, warn};
|
use log::{error, info};
|
||||||
use std::env;
|
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::connection::ServerMessage2;
|
use crate::connection::ServerMessage2;
|
||||||
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue, broadcast_to_match};
|
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -26,12 +26,8 @@ impl MatchmakingSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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!");
|
//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 {
|
||||||
@@ -72,6 +68,7 @@ impl MatchmakingSystem {
|
|||||||
}
|
}
|
||||||
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);
|
||||||
|
//TODO: at the end of a match delete this from player
|
||||||
} else {
|
} else {
|
||||||
error!("Could not store match id for black player");
|
error!("Could not store match id for black player");
|
||||||
}
|
}
|
||||||
@@ -122,7 +119,7 @@ impl MatchmakingSystem {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let _ = crate::connection::send_message_to_player_connection(
|
let _ = crate::connection::send_message_to_player_connection(
|
||||||
conn_map.get_mut(&white),
|
conn_map.get_mut(&black),
|
||||||
&serde_json::to_string(&message).unwrap(),
|
&serde_json::to_string(&message).unwrap(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
@@ -6,5 +6,17 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
eframe = "0.33.0"
|
eframe = "0.33.0"
|
||||||
egui = "0.33.0"
|
egui = "0.33.0"
|
||||||
tokio-tungstenite = "0.28.0"
|
|
||||||
winit = "0.30.12"
|
winit = "0.30.12"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tokio-tungstenite = "0.21"
|
||||||
|
tungstenite = "0.21"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
futures-util = "0.3.31"
|
||||||
|
url = "2.5.7"
|
||||||
|
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||||
|
engine = {path = "../engine/"}
|
||||||
|
log = {version = "0.4.28"}
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
local-ip-address = "0.6.5"
|
||||||
|
anyhow = "1.0.100"
|
||||||
|
|||||||
117
ui/src/connection.rs
Normal file
117
ui/src/connection.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
use engine::{chessmove::ChessMove, gameend::GameEnd};
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use local_ip_address::local_ip;
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
net::{IpAddr, Ipv4Addr},
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
use tokio_tungstenite::connect_async;
|
||||||
|
use tungstenite::Message;
|
||||||
|
use url::Url;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{ChessApp, ClientEvent, SharedGameState};
|
||||||
|
|
||||||
|
#[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")]
|
||||||
|
enum ClientMessage {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { step: ChessMove, fen: String },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ip_address() -> IpAddr {
|
||||||
|
let ip = local_ip().unwrap_or(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
|
||||||
|
|
||||||
|
ip
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_connection(
|
||||||
|
server_port: &str,
|
||||||
|
shared_state: SharedGameState,
|
||||||
|
ui_events: Arc<Mutex<Vec<ClientEvent>>>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let address = get_ip_address();
|
||||||
|
|
||||||
|
//start main loop
|
||||||
|
let server_address = String::from("ws://") + &address.to_string() + ":" + server_port;
|
||||||
|
warn!(
|
||||||
|
"Machine IpAddress is bound for listener. Ip: {}",
|
||||||
|
server_address
|
||||||
|
);
|
||||||
|
|
||||||
|
let url = Url::parse(&server_address)?;
|
||||||
|
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
while let Some(message) = read.next().await {
|
||||||
|
info!("connection");
|
||||||
|
match message {
|
||||||
|
Ok(msg) => {
|
||||||
|
if msg.is_text() {
|
||||||
|
let text = msg.to_text().unwrap();
|
||||||
|
info!("text: {}", text);
|
||||||
|
|
||||||
|
/*if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
match parsed {
|
||||||
|
ServerMessage2::GameEnd { winner } => {}
|
||||||
|
ServerMessage2::UIUpdate { fen } => {}
|
||||||
|
ServerMessage2::MatchFound {
|
||||||
|
match_id,
|
||||||
|
color,
|
||||||
|
opponent_name,
|
||||||
|
} => {
|
||||||
|
//chess_app.player_color = Some(color);
|
||||||
|
}
|
||||||
|
ServerMessage2::Ok { response } => {}
|
||||||
|
_ => {
|
||||||
|
error!("Received unkown servermessage2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
// Update shared state with server message
|
||||||
|
shared_state.update_from_server_message(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send UI events to server
|
||||||
|
let events = ui_events.lock().unwrap().drain(..).collect::<Vec<_>>();
|
||||||
|
for event in events {
|
||||||
|
let message = serde_json::to_string(&event)?;
|
||||||
|
write.send(Message::Text(message)).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error receiving message: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
1084
ui/src/main.rs
1084
ui/src/main.rs
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user