added move history to ui and server

This commit is contained in:
2025-12-03 22:06:11 +01:00
parent 86b5f172ee
commit 568fdba7e7
2 changed files with 33 additions and 26 deletions

View File

@@ -39,12 +39,6 @@ pub fn new_waiting_queue() -> WaitingQueue {
Arc::new(Mutex::new(VecDeque::new())) Arc::new(Mutex::new(VecDeque::new()))
} }
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Step {
pub from: String,
pub to: String,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub enum ServerMessage2 { pub enum ServerMessage2 {
GameEnd { GameEnd {
@@ -53,6 +47,7 @@ pub enum ServerMessage2 {
UIUpdate { UIUpdate {
fen: String, fen: String,
turn_player: String, turn_player: String,
move_history: Vec<String>,
}, },
MatchFound { MatchFound {
match_id: Uuid, match_id: Uuid,
@@ -102,7 +97,7 @@ pub struct GameMatch {
pub player_white: Uuid, pub player_white: Uuid,
pub player_black: Uuid, pub player_black: Uuid,
pub board_state: String, pub board_state: String,
pub move_history: Vec<Step>, pub move_history: Vec<String>,
} }
// Message sending utilities // Message sending utilities
@@ -258,17 +253,24 @@ pub async fn handle_connection(
"board after engine fn: {}", "board after engine fn: {}",
matches.get_mut(&match_id).unwrap().board_state.clone() matches.get_mut(&match_id).unwrap().board_state.clone()
); );
matches
.get_mut(&match_id)
.unwrap()
.move_history
.push(step.clone().notation());
} }
let message = ServerMessage2::UIUpdate { let message = ServerMessage2::UIUpdate {
fen: matches fen: {
.lock() let mut matches = matches.lock().await;
.await matches.get(&match_id).unwrap().board_state.clone()
.get(&match_id) },
.unwrap()
.board_state
.clone(),
turn_player: turn_player, turn_player: turn_player,
move_history: {
let mut matches = matches.lock().await;
matches.get(&match_id).unwrap().move_history.clone()
},
}; };
let _ = broadcast_to_match( let _ = broadcast_to_match(

View File

@@ -1,4 +1,5 @@
use eframe::egui; use eframe::egui;
use egui::Color32;
use engine::gameend::GameEnd; use engine::gameend::GameEnd;
use engine::{boardsquare::BoardSquare, chessmove::ChessMove}; use engine::{boardsquare::BoardSquare, chessmove::ChessMove};
use env_logger::Env; use env_logger::Env;
@@ -55,6 +56,7 @@ pub enum ServerMessage2 {
UIUpdate { UIUpdate {
fen: String, fen: String,
turn_player: String, turn_player: String,
move_history: Vec<String>,
}, },
MatchFound { MatchFound {
match_id: Uuid, match_id: Uuid,
@@ -180,8 +182,6 @@ impl Default for ChessApp {
rx_from_network: None, rx_from_network: None,
selected_square: None, selected_square: None,
server_ip: "127.0.0.1".to_string(), server_ip: "127.0.0.1".to_string(),
// TODO: for the online server (reverse proxy?)
start_local_server_instance: false, start_local_server_instance: false,
} }
} }
@@ -275,11 +275,14 @@ impl ChessApp {
// Update game state // Update game state
if let Ok(mut state) = game_state_clone.lock() { if let Ok(mut state) = game_state_clone.lock() {
match &server_msg { match &server_msg {
ServerMessage2::UIUpdate { fen, turn_player } => { ServerMessage2::UIUpdate { fen, turn_player, move_history } => {
info!("raw fen: {}", &fen); info!("raw fen: {}", &fen);
state.fen = fen.clone(); state.fen = fen.clone();
state.turn_player = Some(turn_player.clone()); state.turn_player = Some(turn_player.clone());
warn!("turn player: {}", &state.turn_player.clone().unwrap()); warn!("turn player: {}", &state.turn_player.clone().unwrap());
state.move_history = move_history.clone();
info!("MOVE HISTORY: {:?}", move_history);
} }
ServerMessage2::MatchFound { ServerMessage2::MatchFound {
color, color,
@@ -425,6 +428,7 @@ impl ChessApp {
None => { self.selected_square = None; return Err("wrong move".to_string()); } None => { self.selected_square = None; return Err("wrong move".to_string()); }
}; };
let move_event = ClientEvent::Move { let move_event = ClientEvent::Move {
step: unwrapped_move, step: unwrapped_move,
turn_player: if player_color == Some("white".to_string()) { turn_player: if player_color == Some("white".to_string()) {
@@ -509,7 +513,7 @@ impl ChessApp {
self.state = AppState::FindingMatch; self.state = AppState::FindingMatch;
} }
} }
ServerMessage2::UIUpdate { fen, turn_player } => { ServerMessage2::UIUpdate { fen, turn_player, move_history } => {
if let Some(tx) = &self.tx_to_network { if let Some(tx) = &self.tx_to_network {
let _ = tx.send(ClientEvent::RequestLegalMoves {fen: self.game_state.lock().unwrap().fen.clone()}); let _ = tx.send(ClientEvent::RequestLegalMoves {fen: self.game_state.lock().unwrap().fen.clone()});
} }
@@ -1051,7 +1055,7 @@ impl eframe::App for ChessApp {
if self.dark_mode { if self.dark_mode {
egui::Color32::from_rgb(70, 70, 70) egui::Color32::from_rgb(70, 70, 70)
} else { } else {
egui::Color32::from_rgb(250, 250, 250) egui::Color32::from_rgb(70, 250, 250)
}; };
} else { } else {
ui.visuals_mut().widgets.noninteractive.bg_fill = ui.visuals_mut().widgets.noninteractive.bg_fill =
@@ -1061,12 +1065,13 @@ impl eframe::App for ChessApp {
egui::Color32::from_rgb(230, 230, 230) egui::Color32::from_rgb(230, 230, 230)
}; };
} }
ui.label(egui::RichText::new(move_text.to_string()).size(16.0).color(text_color)); if i % 2 == 0 {
ui.label(egui::RichText::new(move_text.to_string()).size(25.0).color(Color32::from_rgb(255, 0, 0)));
if ui.small_button("📋").clicked() { }else{
info!("Copy move: {}", move_text);
} ui.label(egui::RichText::new(move_text.to_string()).size(25.0).color(Color32::from_rgb(0, 255, 0)));
}
}); });
if i < game_state.move_history.len() - 1 { if i < game_state.move_history.len() - 1 {
@@ -1258,7 +1263,7 @@ mod tests {
r#"{"UIUpdate":{"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","turn_player":"white"}}"#; r#"{"UIUpdate":{"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","turn_player":"white"}}"#;
let message: ServerMessage2 = serde_json::from_str(ui_update_json).unwrap(); let message: ServerMessage2 = serde_json::from_str(ui_update_json).unwrap();
match message { match message {
ServerMessage2::UIUpdate { fen, turn_player } => { ServerMessage2::UIUpdate { fen, turn_player, move_history } => {
assert!(fen.contains("rnbqkbnr")); assert!(fen.contains("rnbqkbnr"));
assert_eq!(turn_player, "white"); assert_eq!(turn_player, "white");
} }