updated move event to use chessmove struct from engine
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use engine::{boardsquare::BoardSquare, chessmove::ChessMove, piecetype::PieceType};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{self, Write};
|
||||
@@ -10,12 +11,12 @@ struct Step {
|
||||
to: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
enum ClientMessage {
|
||||
Join { username: String },
|
||||
FindMatch,
|
||||
Move { step: Step, fen: String },
|
||||
Move { step: ChessMove, fen: String },
|
||||
Resign,
|
||||
Chat { text: String },
|
||||
RequestLegalMoves { fen: String },
|
||||
@@ -142,14 +143,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🔍 Searching for a match...");
|
||||
}
|
||||
"move" => {
|
||||
if parts.len() >= 4 {
|
||||
let from = parts[1].to_string();
|
||||
let to = parts[2].to_string();
|
||||
let fen = parts[3].to_string();
|
||||
let step: Step = Step { from, to };
|
||||
if parts.len() >= 3 {
|
||||
//let from = parts[1].to_string();
|
||||
//let to = parts[2].to_string();
|
||||
let fen = parts[1].to_string();
|
||||
|
||||
let step = ChessMove::quiet(
|
||||
engine::piecetype::PieceType::WhiteBishop,
|
||||
BoardSquare::new(),
|
||||
BoardSquare { x: 1, y: 1 },
|
||||
None,
|
||||
);
|
||||
|
||||
let message = ClientMessage::Move { step, fen };
|
||||
send_message(&mut write, &message).await?;
|
||||
println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
||||
//println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
||||
} else {
|
||||
println!("Usage: move <from> <to> (e.g., move e2 e4)");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::connection::ClientEvent::*;
|
||||
use engine::chessmove::ChessMove;
|
||||
use engine::get_available_moves;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -46,24 +47,15 @@ struct ServerMessage {
|
||||
response: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
enum ClientEvent {
|
||||
Join {
|
||||
username: String,
|
||||
},
|
||||
Join { username: String },
|
||||
FindMatch,
|
||||
Move {
|
||||
step: Step, //to put into move list
|
||||
fen: String,
|
||||
},
|
||||
Move { step: ChessMove, fen: String },
|
||||
Resign,
|
||||
Chat {
|
||||
text: String,
|
||||
},
|
||||
RequestLegalMoves {
|
||||
fen: String,
|
||||
},
|
||||
Chat { text: String },
|
||||
RequestLegalMoves { fen: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@@ -186,7 +178,7 @@ pub async fn handle_connection(
|
||||
let client_data: ClientEvent = serde_json::from_str(text)
|
||||
.expect("Failed to convert data into json at handle_connection");
|
||||
|
||||
println!("client: {:?}", client_data);
|
||||
//println!("client: {:?}", client_data);
|
||||
|
||||
match client_data {
|
||||
Join { username } => {
|
||||
@@ -224,7 +216,9 @@ 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
|
||||
|
||||
// TODO: discuss if the fen sent by the client is before or after the move
|
||||
let new_fen = engine::get_board_after_move(&fen, &step);
|
||||
|
||||
let message: ServerMessage = ServerMessage {
|
||||
message_type: String::from("move"),
|
||||
@@ -250,7 +244,9 @@ pub async fn handle_connection(
|
||||
match_id,
|
||||
&serde_json::to_string(&message).unwrap(),
|
||||
)
|
||||
.await; //client needs updating to test this
|
||||
.await;
|
||||
|
||||
let res = engine::is_game_over(&new_fen); // TODO: discuss how to handle this
|
||||
}
|
||||
RequestLegalMoves { fen } => {
|
||||
let moves = get_available_moves(&fen);
|
||||
@@ -261,7 +257,9 @@ pub async fn handle_connection(
|
||||
.await;
|
||||
println!("Sent moves to player: {}", player_id);
|
||||
}
|
||||
_ => {}
|
||||
_ => {
|
||||
println!("Not known client event");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user