From c2a42c536f9c8240d10f25a4f7091fb03a2c9ade Mon Sep 17 00:00:00 2001 From: rasul Date: Wed, 8 Apr 2020 12:56:35 -0500 Subject: [PATCH] move state changes to iter_once --- src/game/state/login.rs | 107 ++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 36 deletions(-) diff --git a/src/game/state/login.rs b/src/game/state/login.rs index 79f764f..b15742f 100644 --- a/src/game/state/login.rs +++ b/src/game/state/login.rs @@ -12,14 +12,6 @@ impl Game { pub fn login(&mut self, token: Token, message: String, state: &Login) -> SendQueue { let mut send_queue = SendQueue::new(); - let mut client = if let Some(client) = self.clients.remove(&token) { - client - } else { - warn!("Can't find a client with token: {:?}", &token); - self.tokens.push_back(token); - return send_queue; - }; - match state { // get the username Login::Username => { @@ -29,17 +21,20 @@ impl Game { match self.check_player_name(message) { PlayerCheck::Ok(name) => match self.db.find_player_by_name(&name) { Ok(Some(_)) => { - send_queue.push(token, "\nPassword: ", false, None); - client.state = State::Login(Login::Password(name)); + send_queue.push( + token, + "\nPassword: ", + false, + Some(State::Login(Login::Password(name))), + ); } Ok(None) => { send_queue.push( token, format!("\nCreate {}? [y/N]: ", name), false, - None, + Some(State::Login(Login::CreateUser(name))), ); - client.state = State::Login(Login::CreateUser(name)); } Err(_) => { send_queue.push(token, "\nError\n\nUsername: ", false, None); @@ -59,25 +54,43 @@ impl Game { // username not found Login::CreateUser(username) => { if !message.clone().is_empty() && message == "y" { - send_queue.push(token, "\nNew password: ", false, None); - client.state = State::Login(Login::CreatePassword(username.to_owned())); + send_queue.push( + token, + "\nNew password: ", + false, + Some(State::Login(Login::CreatePassword(username.to_owned()))), + ); } else { - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } } // first new user password Login::CreatePassword(username) => { if message.is_empty() { - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } else { match self.check_player_password(message) { PlayerCheck::Ok(pass) => { - send_queue.push(token, "\nNew password again: ", false, None); - client.state = - State::Login(Login::CreatePassword2((username.to_owned(), pass))); + send_queue.push( + token, + "\nNew password again: ", + false, + Some(State::Login(Login::CreatePassword2(( + username.to_owned(), + pass, + )))), + ); } PlayerCheck::Err(err) => { send_queue.push(token, "\nInvalid password:\n", false, None); @@ -93,8 +106,12 @@ impl Game { Login::CreatePassword2((username, pass)) => { let pass = pass.to_owned(); if message.is_empty() || message != pass { - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } else { if let Ok(id) = self.db.new_player_id() { let player = Player { @@ -106,7 +123,12 @@ impl Game { }; if self.db.single_save_player(token, &player).is_ok() { - send_queue.push(token, format!("Welcome, {}\n", username), false, None); + send_queue.push( + token, + format!("Welcome, {}\n", username), + false, + Some(State::Action), + ); send_queue.push(token, "", true, None); send_queue.append(&mut Command::dispatch_look( @@ -116,7 +138,6 @@ impl Game { &mut self.db, )); - client.state = State::Action; } else { send_queue.push(token, "Error", true, None); } @@ -128,8 +149,12 @@ impl Game { Login::Password(username) => { if message.is_empty() { - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } else { match self.db.find_player_by_name(username) { Ok(Some(player)) => { @@ -139,9 +164,8 @@ impl Game { token, format!("Welcome back, {}\n\n", username), false, - None, + Some(State::Action), ); - client.state = State::Action; send_queue.append(&mut Command::dispatch_look( &Command::default(), @@ -151,26 +175,37 @@ impl Game { )); } else { send_queue.push(token, "Unable to login\n", false, None); - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } } else { send_queue.push(token, "Incorrect password\n", false, None); - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } } Ok(None) | Err(_) => { send_queue.push(token, "Error\n", false, None); - send_queue.push(token, "\n\nUsername: ", false, None); - client.state = State::Login(Login::Username); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); } } } } }; - self.clients.insert(token, client); send_queue } }