From 446413c1b2fbe3207047cc2a908450db9ff526bc Mon Sep 17 00:00:00 2001 From: htom Date: Sun, 30 Nov 2025 11:58:42 +0100 Subject: [PATCH] added turn changing and checking before moving if the player is allowed to move --- server/src/connection.rs | 23 +++++++++++++----- ui/src/main.rs | 51 ++++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/server/src/connection.rs b/server/src/connection.rs index 9e0d84b..072a370 100644 --- a/server/src/connection.rs +++ b/server/src/connection.rs @@ -52,6 +52,7 @@ pub enum ServerMessage2 { }, UIUpdate { fen: String, + turn_player: String, }, MatchFound { match_id: Uuid, @@ -66,12 +67,21 @@ 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, } @@ -214,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 @@ -240,6 +250,7 @@ pub async fn handle_connection( .unwrap() .board_state .clone(), + turn_player: turn_player, }; let _ = broadcast_to_match( @@ -266,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); diff --git a/ui/src/main.rs b/ui/src/main.rs index 0c533cf..44bccc9 100644 --- a/ui/src/main.rs +++ b/ui/src/main.rs @@ -4,7 +4,6 @@ use env_logger::Env; use futures_util::{SinkExt, StreamExt}; use log::{error, info, warn}; use serde::{Deserialize, Serialize}; -use std::os::unix::process::CommandExt; use std::path::Path; use std::process::Command; use std::sync::{Arc, Mutex}; @@ -55,6 +54,7 @@ pub enum ServerMessage2 { }, UIUpdate { fen: String, + turn_player: String, }, MatchFound { match_id: Uuid, @@ -69,12 +69,21 @@ pub enum ServerMessage2 { #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type")] pub 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, } @@ -87,6 +96,7 @@ struct GameState { match_id: Option, game_over: Option, available_moves: Option>, + turn_player: Option, } impl Default for GameState { @@ -98,6 +108,7 @@ impl Default for GameState { match_id: None, game_over: None, available_moves: None, + turn_player: Some("white".to_string()), } } } @@ -138,7 +149,7 @@ impl Default for ChessApp { rx_from_network: None, selected_square: None, server_ip: "127.0.0.1".to_string(), - // for the online server (reverse proxy?) + // TODO: for the online server (reverse proxy?) start_local_server_instance: false, } } @@ -221,8 +232,9 @@ impl ChessApp { // Update game state if let Ok(mut state) = game_state_clone.lock() { match &server_msg { - ServerMessage2::UIUpdate { fen } => { + ServerMessage2::UIUpdate { fen, turn_player } => { state.fen = fen.clone(); + state.turn_player = Some(turn_player.clone()); } ServerMessage2::MatchFound { color, @@ -273,6 +285,15 @@ impl ChessApp { if let Some((from_row, from_col)) = self.selected_square { // Send move to server if let Some(tx) = &self.tx_to_network { + let player_color = self.game_state.lock().unwrap().player_color.clone(); + + //check if its the players turn + if self.game_state.lock().unwrap().turn_player != player_color { + warn!("it is not the current players turn!"); + self.selected_square = None; + return; + } + // TODO: kinyerni a tenyleges kivalasztott babut let chess_move = ChessMove::Quiet { piece_type: engine::piecetype::PieceType::WhiteKing, @@ -280,12 +301,21 @@ impl ChessApp { to_square: BoardSquare { x: 2, y: 2 }, promotion_piece: None, }; - let move_event = ClientEvent::Move { step: chess_move }; + + let move_event = ClientEvent::Move { + step: chess_move, + turn_player: if player_color == Some("white".to_string()) { + "black".to_string() + } else { + "white".to_string() + }, + }; + let _ = tx.send(move_event); } + self.selected_square = None; } else { - // Select square self.selected_square = Some((row, col)); } } @@ -353,11 +383,12 @@ impl ChessApp { self.state = AppState::FindingMatch; } } - ServerMessage2::UIUpdate { fen } => { + ServerMessage2::UIUpdate { fen, turn_player } => { info!("Board updated with FEN: {}", fen); // UI will automatically redraw with new FEN if let Ok(mut game_state) = self.game_state.lock() { game_state.fen = fen; + game_state.turn_player = Some(turn_player); } } }