From 05477922651daec977eb923f135a6e75bf741d75 Mon Sep 17 00:00:00 2001 From: rasul Date: Mon, 6 Apr 2020 14:00:09 -0500 Subject: [PATCH] pass quit state back when player quits, and check for that to close the connection --- src/command/dispatch/quit.rs | 3 ++- src/game/iter_once.rs | 14 ++++++++------ src/game/process_recv_message.rs | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/command/dispatch/quit.rs b/src/command/dispatch/quit.rs index 484b2d8..db57936 100644 --- a/src/command/dispatch/quit.rs +++ b/src/command/dispatch/quit.rs @@ -3,9 +3,10 @@ use mio::Token; use crate::command::Command; use crate::database::Db; use crate::queue::SendQueue; +use crate::state::State; impl Command { pub fn dispatch_quit(&self, _: String, token: Token, _: &mut Db) -> SendQueue { - SendQueue(vec![(token, "Goodbye\n\n".into(), false, None)].into()) + SendQueue(vec![(token, "Goodbye\n\n".into(), false, Some(State::Quit))].into()) } } diff --git a/src/game/iter_once.rs b/src/game/iter_once.rs index 3f662e5..247cbca 100644 --- a/src/game/iter_once.rs +++ b/src/game/iter_once.rs @@ -1,6 +1,7 @@ use crate::game::Game; use crate::queue::SendQueue; use crate::result::RudeResult; +use crate::state::State; use crate::try_log; impl Game { @@ -36,12 +37,13 @@ impl Game { client.send_with_prompt(message); } else { client.send_without_prompt(&message); - if message == "Goodbye\n\n".to_string() { - let _ = client.close(); - log::info!("Disconnect from {}", client); - self.clients.remove(&token); - self.tokens.push_back(token); - } + } + + if state == Some(State::Quit) { + let _ = client.close(); + log::info!("Disconnect from {}", client); + self.clients.remove(&token); + self.tokens.push_back(token); } } diff --git a/src/game/process_recv_message.rs b/src/game/process_recv_message.rs index bce090c..cf06f7c 100644 --- a/src/game/process_recv_message.rs +++ b/src/game/process_recv_message.rs @@ -26,7 +26,7 @@ impl Game { let mut queue = self.login(token, message, login_state); send_queue.append(&mut queue); } - State::Action => { + State::Action | State::Quit => { let mut queue = self.action(token, message); send_queue.append(&mut queue); }