handling client move event
This commit is contained in:
@@ -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<dyn std::error::Error>> {
|
||||
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 {
|
||||
|
||||
@@ -38,23 +38,32 @@ pub struct Step {
|
||||
struct ServerMessage {
|
||||
#[serde(rename = "type")]
|
||||
message_type: String,
|
||||
player_id: Option<String>,
|
||||
match_id: Option<String>,
|
||||
opponent: Option<String>,
|
||||
player_id: Option<Uuid>,
|
||||
match_id: Option<Uuid>,
|
||||
opponent: Option<Uuid>,
|
||||
color: Option<String>,
|
||||
reason: Option<String>,
|
||||
fen: Option<String>,
|
||||
response: Option<String>,
|
||||
}
|
||||
|
||||
#[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);
|
||||
|
||||
Reference in New Issue
Block a user