mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-28 11:10:44 +01:00
feat: add revoking readiness
This commit is contained in:
@@ -168,6 +168,12 @@ impl Client {
|
||||
self.send_message(msg).await;
|
||||
}
|
||||
|
||||
pub async fn revoke_readiness(&mut self) {
|
||||
debug!("Revoking readiness and sending it to the server...");
|
||||
let msg = Message::new(ClientMessage::SignalUnreadiness);
|
||||
self.send_message(msg).await;
|
||||
}
|
||||
|
||||
pub fn get_update_receiver(&mut self) -> Option<Receiver<ServerMessage>> {
|
||||
self.update_receiver.take()
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ pub enum ServerMessage {
|
||||
pub enum ClientMessage {
|
||||
AnswerHandshake { player_name: String },
|
||||
SignalReadiness,
|
||||
SignalUnreadiness,
|
||||
|
||||
ConnectionDead { addr: SocketAddr },
|
||||
|
||||
|
||||
@@ -189,6 +189,10 @@ impl Server {
|
||||
self.handle_readiness(addr).await
|
||||
}
|
||||
|
||||
ClientMessage::SignalUnreadiness => {
|
||||
self.handle_unreadiness(addr).await;
|
||||
}
|
||||
|
||||
ClientMessage::RequestCapture { capture_path } => {
|
||||
self.process_capture(capture_path).await;
|
||||
}
|
||||
@@ -337,6 +341,37 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_unreadiness(&mut self, addr: SocketAddr) {
|
||||
if !self.connections[&addr].is_handshaken {
|
||||
warn!(
|
||||
"Connection: {} tried revoking readiness, but hands were not shaken. Ignoring....",
|
||||
addr
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if self.state.stage != ServerStage::Lobby {
|
||||
warn!(
|
||||
"Player {} tried revoking readiness, but game already started. Ignoring...",
|
||||
addr
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
self.state.ready_count -= 1;
|
||||
|
||||
self.connections
|
||||
.get_mut(&addr)
|
||||
.expect("Could not get the indentity of the player that wanted to revoke readinessl")
|
||||
.identity
|
||||
.is_ready = false;
|
||||
|
||||
info!(
|
||||
"Player {} revoked his readiness. Currently ready: {}/{}",
|
||||
addr, self.state.ready_count, MAX_PLAYABLE_CONNECTIONS
|
||||
);
|
||||
}
|
||||
|
||||
/* === Sending messages === */
|
||||
|
||||
/// Should be used after every players move
|
||||
|
||||
Reference in New Issue
Block a user