Quit command to disconnect from the game

master
rasul 5 years ago
parent 87367644cc
commit 94723e9d12

@ -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,

@ -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::*;

@ -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())
}
}

@ -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);
}
}
}

Loading…
Cancel
Save