diff --git a/server/src/bin/client.rs b/server/src/bin/client.rs index 2817eea..5bde308 100644 --- a/server/src/bin/client.rs +++ b/server/src/bin/client.rs @@ -4,12 +4,18 @@ use std::io::{self, Write}; use tokio_tungstenite::{connect_async, tungstenite::Message}; use url::Url; +#[derive(Serialize, Deserialize, Debug)] +struct Step { + from: String, + to: String, +} + #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type")] enum ClientMessage { Join { username: String }, FindMatch, - Move { from: String, to: String }, + Move { step: Step, fen: String }, Resign, Chat { text: String }, RequestLegalMoves { fen: String }, @@ -136,10 +142,12 @@ async fn main() -> Result<(), Box> { println!("🔍 Searching for a match..."); } "move" => { - if parts.len() >= 3 { + if parts.len() >= 4 { let from = parts[1].to_string(); let to = parts[2].to_string(); - let message = ClientMessage::Move { from, to }; + let fen = parts[3].to_string(); + let step: Step = Step { from, to }; + let message = ClientMessage::Move { step, fen }; send_message(&mut write, &message).await?; println!("♟️ Sent move: {} -> {}", parts[1], parts[2]); } else { diff --git a/server/src/connection.rs b/server/src/connection.rs index 650a1cb..c077cfb 100644 --- a/server/src/connection.rs +++ b/server/src/connection.rs @@ -38,23 +38,32 @@ pub struct Step { struct ServerMessage { #[serde(rename = "type")] message_type: String, - player_id: Option, - match_id: Option, - opponent: Option, + player_id: Option, + match_id: Option, + opponent: Option, color: Option, reason: Option, - fen: Option, + response: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type")] enum ClientEvent { - Join { username: String }, + Join { + username: String, + }, FindMatch, - Move { fen: String }, + Move { + step: Step, //to put into move list + fen: String, + }, Resign, - Chat { text: String }, - RequestLegalMoves { fen: String }, + Chat { + text: String, + }, + RequestLegalMoves { + fen: String, + }, } #[derive(Serialize, Deserialize, Debug)] @@ -207,7 +216,7 @@ pub async fn handle_connection( println!("Appended {} to the waiting queue", player_id); println!("queue {:?}", wait_queue); } - Move { fen } => { + Move { step, fen } => { let match_id = connections .lock() .await @@ -215,8 +224,33 @@ pub async fn handle_connection( .unwrap() .current_match .unwrap(); + // TODO: potentially a problem that there is a lock on it and the broadcast needs it as well - let _ = broadcast_to_match(&connections, &matches, match_id, &fen).await; + let message: ServerMessage = ServerMessage { + message_type: String::from("move"), + player_id: (Some(player_id.clone())), + match_id: (Some(match_id.clone())), + opponent: (None), + color: (None), + reason: (Some(String::from( + "after player stepped we update the position for opponent board, and move history", + ))), + response: (Some(String::from( + &serde_json::to_string(&Move { + step: step, + fen: fen, + }) + .unwrap(), + ))), + }; + + let _ = broadcast_to_match( + &connections, + &matches, + match_id, + &serde_json::to_string(&message).unwrap(), + ) + .await; //client needs updating to test this } RequestLegalMoves { fen } => { let moves = get_available_moves(&fen);