mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
fix: client playing when hands werent shaken yet
This commit is contained in:
@@ -58,7 +58,11 @@ impl Client {
|
|||||||
let settings_clone = settings.clone();
|
let settings_clone = settings.clone();
|
||||||
let update_sender_clone = update_sender.clone();
|
let update_sender_clone = update_sender.clone();
|
||||||
|
|
||||||
|
let (hanshake_tx, handshake_rx) = tokio::sync::oneshot::channel::<bool>();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
let mut hanshake_tx = Some(hanshake_tx);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
oldest_msg = conn_reciever.recv() => {
|
oldest_msg = conn_reciever.recv() => {
|
||||||
@@ -78,10 +82,16 @@ impl Client {
|
|||||||
|
|
||||||
ServerMessage::AcceptHandshake => {
|
ServerMessage::AcceptHandshake => {
|
||||||
info!("Server accepted us! Yay :D");
|
info!("Server accepted us! Yay :D");
|
||||||
|
if let Some(hanshake_tx) = hanshake_tx.take() {
|
||||||
|
let _ = hanshake_tx.send(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::DeclineHandshake { reason } => {
|
ServerMessage::DeclineHandshake { reason } => {
|
||||||
error!("Server declined connection: {}", reason);
|
error!("Server declined connection: {}", reason);
|
||||||
|
if let Some(hanshake_tx) = hanshake_tx.take() {
|
||||||
|
let _ = hanshake_tx.send(false);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,10 +109,22 @@ impl Client {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(Client {
|
let is_handshake_accepted: bool = match handshake_rx.await {
|
||||||
conn_sender_outgoing,
|
Ok(is_accepted) => is_accepted,
|
||||||
update_receiver: Some(update_receiver),
|
Err(e) => {
|
||||||
})
|
error!("Error while receiving handshake: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if is_handshake_accepted {
|
||||||
|
Some(Client {
|
||||||
|
conn_sender_outgoing,
|
||||||
|
update_receiver: Some(update_receiver),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_message(&mut self, msg: Result<Message<ClientMessage>, postcard::Error>) {
|
async fn send_message(&mut self, msg: Result<Message<ClientMessage>, postcard::Error>) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
|
|
||||||
use checkers::{
|
use checkers::{
|
||||||
@@ -9,25 +10,35 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn game_integration() {
|
async fn game_integration() {
|
||||||
|
let _ = env_logger::builder().is_test(true).try_init();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut server = Server::new().await;
|
let mut server = Server::new().await;
|
||||||
server.start().await;
|
server.start().await;
|
||||||
server.update().await;
|
server.update().await;
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(2)).await; // wait for the server to run
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
|
|
||||||
let bot1_handle = tokio::spawn(async move {
|
let bot1_handle = tokio::spawn(async move {
|
||||||
let bot1 = Bot::new(BotDificulty::Easy).await;
|
let bot1 = Bot::new(BotDificulty::Easy).await;
|
||||||
bot1.game_loop().await;
|
bot1.game_loop().await
|
||||||
});
|
});
|
||||||
|
|
||||||
let bot2_handle = tokio::spawn(async move {
|
let bot2_handle = tokio::spawn(async move {
|
||||||
let bot2 = Bot::new(BotDificulty::Easy).await;
|
let bot2 = Bot::new(BotDificulty::Easy).await;
|
||||||
bot2.game_loop().await;
|
bot2.game_loop().await
|
||||||
});
|
});
|
||||||
|
|
||||||
bot1_handle.await.unwrap();
|
let bot1_result = bot1_handle.await.unwrap();
|
||||||
bot2_handle.await.unwrap();
|
let bot2_result = bot2_handle.await.unwrap();
|
||||||
|
|
||||||
|
if bot1_result.is_some() && bot2_result.is_some() {
|
||||||
|
assert_eq!(bot1_result.unwrap(), bot2_result.unwrap());
|
||||||
|
} else if bot1_result.is_none() && bot2_result.is_none() {
|
||||||
|
()
|
||||||
|
} else {
|
||||||
|
panic!("Bot result differanciate");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user