mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add revoking readiness
This commit is contained in:
@@ -15,6 +15,7 @@ const QUIT_COMMAND: &str = "/quit";
|
||||
const HELP_COMMAND: &str = "/help";
|
||||
const SEND_COMMAND: &str = "/send";
|
||||
const READY_COMMAND: &str = "/ready";
|
||||
const UNREADY_COMMAND: &str = "/unready";
|
||||
const SINGLEPLAYER_COMMAND: &str = "/single";
|
||||
|
||||
const CAPTURE_COMMAND: &str = "/capture";
|
||||
@@ -26,6 +27,8 @@ enum CliCommands {
|
||||
Send,
|
||||
|
||||
Ready,
|
||||
Unready,
|
||||
|
||||
Single,
|
||||
|
||||
Capture,
|
||||
@@ -50,6 +53,7 @@ impl TryFrom<&str> for CliCommands {
|
||||
HELP_COMMAND => Ok(CliCommands::Help),
|
||||
SEND_COMMAND => Ok(CliCommands::Send),
|
||||
READY_COMMAND => Ok(CliCommands::Ready),
|
||||
UNREADY_COMMAND => Ok(CliCommands::Unready),
|
||||
SINGLEPLAYER_COMMAND => Ok(CliCommands::Single),
|
||||
|
||||
CAPTURE_COMMAND => Ok(CliCommands::Capture),
|
||||
@@ -119,6 +123,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
│\t {} - quit
|
||||
│\t {} - show this message
|
||||
│\t {} - message the server you're ready
|
||||
│\t {} - revoke readiness
|
||||
│\t {} - send text message to the server
|
||||
│\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
|
||||
└───────────────────────────────────────
|
||||
",
|
||||
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───────");
|
||||
@@ -209,6 +214,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
client.signal_readiness().await;
|
||||
}
|
||||
|
||||
CliCommands::Unready => {
|
||||
client.revoke_readiness().await;
|
||||
}
|
||||
|
||||
CliCommands::Single => {
|
||||
println!("Tried connecting bot to the server! Make sure to be /ready");
|
||||
tokio::spawn(run_bot());
|
||||
|
||||
@@ -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