From 94723e9d125c250b4f3c2b8917ea34ddd5fa22af Mon Sep 17 00:00:00 2001 From: rasul Date: Sat, 4 Apr 2020 11:14:37 -0500 Subject: [PATCH] Quit command to disconnect from the game --- src/command/command.rs | 3 +++ src/command/mod.rs | 2 ++ src/command/quit.rs | 11 +++++++++++ src/game/iter_once.rs | 26 +++++++++++++++++++------- 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 src/command/quit.rs diff --git a/src/command/command.rs b/src/command/command.rs index 17207a3..a2d0025 100644 --- a/src/command/command.rs +++ b/src/command/command.rs @@ -25,6 +25,7 @@ pub enum Command { Dig, Help, Look, + Quit, Save, Say, Set(CommandSet), @@ -55,6 +56,7 @@ impl Parse for Command { Self::Dig => "dig DIRECTION:: Dig a new path to a new room in the provided direction.", Self::Help => "help [COMMAND]:: Provide help on a specific command, or an overview.", Self::Look => "look :: Take a look around the current room.", + Self::Quit => "quit :: Quit playing and disconnect from the game.", Self::Save => "save :: Write player information to disk.", Self::Say => "say MESSAGE:: Say something to the room.", Self::Set(_) => "set OPTIONS :: Set various parameters.", @@ -98,6 +100,7 @@ impl Parse for Command { Self::Dig => actions::dig, Self::Help => actions::help, Self::Look => actions::look, + Self::Quit => Self::dispatch_quit, Self::Save => actions::save, Self::Say => actions::say, Self::Set(_) => Self::dispatch_map_subcommand, diff --git a/src/command/mod.rs b/src/command/mod.rs index ca67c96..410f455 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -1,9 +1,11 @@ mod command; mod parse; mod parser_error; +mod quit; mod set; pub use command::Command; pub use parse::Parse; pub use parser_error::ParserError; +pub use quit::*; pub use set::*; diff --git a/src/command/quit.rs b/src/command/quit.rs new file mode 100644 index 0000000..23956b8 --- /dev/null +++ b/src/command/quit.rs @@ -0,0 +1,11 @@ +use mio::Token; + +use crate::command::Command; +use crate::database::Db; +use crate::queue::SendQueue; + +impl Command { + pub fn dispatch_quit(&self, _: String, token: Token, _: &mut Db) -> SendQueue { + SendQueue(vec![(token, "Goodbye".into(), false)].into()) + } +} diff --git a/src/game/iter_once.rs b/src/game/iter_once.rs index ac57da7..7bff895 100644 --- a/src/game/iter_once.rs +++ b/src/game/iter_once.rs @@ -23,16 +23,28 @@ impl Game { send_queue.append(&mut queue); } + + // send everything in the send queue while let Some((token, message, prompt)) = send_queue.pop() { - if let Some(client) = self.clients.get_mut(&token) { - if prompt { - client.send_with_prompt(message); - } else { - client.send_without_prompt(message); - } + // get the client + let client = if let Some(client) = self.clients.get_mut(&token) { + client } else { - debug!("no client?"); + // no client, so put the token back and move on + self.tokens.push_back(token); + continue; + }; + + if prompt { + client.send_with_prompt(message); + } else { + client.send_without_prompt(&message); + if message == "Goodbye".to_string() { + log::info!("Disconnect from {}", client); + self.clients.remove(&token); + self.tokens.push_back(token); + } } }