feat: add revoking readiness

This commit is contained in:
2026-08-22 14:48:52 +02:00
parent 5f650e3b99
commit a280d525b1
4 changed files with 52 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ const QUIT_COMMAND: &str = "/quit";
const HELP_COMMAND: &str = "/help"; const HELP_COMMAND: &str = "/help";
const SEND_COMMAND: &str = "/send"; const SEND_COMMAND: &str = "/send";
const READY_COMMAND: &str = "/ready"; const READY_COMMAND: &str = "/ready";
const UNREADY_COMMAND: &str = "/unready";
const SINGLEPLAYER_COMMAND: &str = "/single"; const SINGLEPLAYER_COMMAND: &str = "/single";
const CAPTURE_COMMAND: &str = "/capture"; const CAPTURE_COMMAND: &str = "/capture";
@@ -26,6 +27,8 @@ enum CliCommands {
Send, Send,
Ready, Ready,
Unready,
Single, Single,
Capture, Capture,
@@ -50,6 +53,7 @@ impl TryFrom<&str> for CliCommands {
HELP_COMMAND => Ok(CliCommands::Help), HELP_COMMAND => Ok(CliCommands::Help),
SEND_COMMAND => Ok(CliCommands::Send), SEND_COMMAND => Ok(CliCommands::Send),
READY_COMMAND => Ok(CliCommands::Ready), READY_COMMAND => Ok(CliCommands::Ready),
UNREADY_COMMAND => Ok(CliCommands::Unready),
SINGLEPLAYER_COMMAND => Ok(CliCommands::Single), SINGLEPLAYER_COMMAND => Ok(CliCommands::Single),
CAPTURE_COMMAND => Ok(CliCommands::Capture), CAPTURE_COMMAND => Ok(CliCommands::Capture),
@@ -119,6 +123,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
│\t {} - quit │\t {} - quit
│\t {} - show this message │\t {} - show this message
│\t {} - message the server you're ready │\t {} - message the server you're ready
│\t {} - revoke readiness
│\t {} - send text message to the server │\t {} - send text message to the server
│\t {} - play alone │\t {} - play alone
│ │
@@ -126,7 +131,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
│\t {} <index> <index>- choose move from the list. First number is pawn you want to move and second is position it should take │\t {} <index> <index>- choose move from the list. First number is pawn you want to move and second is position it should take
└─────────────────────────────────────── └───────────────────────────────────────
", ",
QUIT_COMMAND, HELP_COMMAND, READY_COMMAND, SEND_COMMAND, SINGLEPLAYER_COMMAND , CAPTURE_COMMAND, MOVE_COMMAND QUIT_COMMAND, HELP_COMMAND, READY_COMMAND, UNREADY_COMMAND, SEND_COMMAND, SINGLEPLAYER_COMMAND , CAPTURE_COMMAND, MOVE_COMMAND
); );
println!("───────Type /help for help───────"); println!("───────Type /help for help───────");
@@ -209,6 +214,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
client.signal_readiness().await; client.signal_readiness().await;
} }
CliCommands::Unready => {
client.revoke_readiness().await;
}
CliCommands::Single => { CliCommands::Single => {
println!("Tried connecting bot to the server! Make sure to be /ready"); println!("Tried connecting bot to the server! Make sure to be /ready");
tokio::spawn(run_bot()); tokio::spawn(run_bot());

View File

@@ -168,6 +168,12 @@ impl Client {
self.send_message(msg).await; 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>> { pub fn get_update_receiver(&mut self) -> Option<Receiver<ServerMessage>> {
self.update_receiver.take() self.update_receiver.take()
} }

View File

@@ -60,6 +60,7 @@ pub enum ServerMessage {
pub enum ClientMessage { pub enum ClientMessage {
AnswerHandshake { player_name: String }, AnswerHandshake { player_name: String },
SignalReadiness, SignalReadiness,
SignalUnreadiness,
ConnectionDead { addr: SocketAddr }, ConnectionDead { addr: SocketAddr },

View File

@@ -189,6 +189,10 @@ impl Server {
self.handle_readiness(addr).await self.handle_readiness(addr).await
} }
ClientMessage::SignalUnreadiness => {
self.handle_unreadiness(addr).await;
}
ClientMessage::RequestCapture { capture_path } => { ClientMessage::RequestCapture { capture_path } => {
self.process_capture(capture_path).await; 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 === */ /* === Sending messages === */
/// Should be used after every players move /// Should be used after every players move