move state changes to iter_once

master
rasul 5 years ago
parent 53e67fcd1f
commit c2a42c536f

@ -12,14 +12,6 @@ impl Game {
pub fn login(&mut self, token: Token, message: String, state: &Login) -> SendQueue { pub fn login(&mut self, token: Token, message: String, state: &Login) -> SendQueue {
let mut send_queue = SendQueue::new(); 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 { match state {
// get the username // get the username
Login::Username => { Login::Username => {
@ -29,17 +21,20 @@ impl Game {
match self.check_player_name(message) { match self.check_player_name(message) {
PlayerCheck::Ok(name) => match self.db.find_player_by_name(&name) { PlayerCheck::Ok(name) => match self.db.find_player_by_name(&name) {
Ok(Some(_)) => { Ok(Some(_)) => {
send_queue.push(token, "\nPassword: ", false, None); send_queue.push(
client.state = State::Login(Login::Password(name)); token,
"\nPassword: ",
false,
Some(State::Login(Login::Password(name))),
);
} }
Ok(None) => { Ok(None) => {
send_queue.push( send_queue.push(
token, token,
format!("\nCreate {}? [y/N]: ", name), format!("\nCreate {}? [y/N]: ", name),
false, false,
None, Some(State::Login(Login::CreateUser(name))),
); );
client.state = State::Login(Login::CreateUser(name));
} }
Err(_) => { Err(_) => {
send_queue.push(token, "\nError\n\nUsername: ", false, None); send_queue.push(token, "\nError\n\nUsername: ", false, None);
@ -59,25 +54,43 @@ impl Game {
// username not found // username not found
Login::CreateUser(username) => { Login::CreateUser(username) => {
if !message.clone().is_empty() && message == "y" { if !message.clone().is_empty() && message == "y" {
send_queue.push(token, "\nNew password: ", false, None); send_queue.push(
client.state = State::Login(Login::CreatePassword(username.to_owned())); token,
"\nNew password: ",
false,
Some(State::Login(Login::CreatePassword(username.to_owned()))),
);
} else { } else {
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} }
} }
// first new user password // first new user password
Login::CreatePassword(username) => { Login::CreatePassword(username) => {
if message.is_empty() { if message.is_empty() {
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} else { } else {
match self.check_player_password(message) { match self.check_player_password(message) {
PlayerCheck::Ok(pass) => { PlayerCheck::Ok(pass) => {
send_queue.push(token, "\nNew password again: ", false, None); send_queue.push(
client.state = token,
State::Login(Login::CreatePassword2((username.to_owned(), pass))); "\nNew password again: ",
false,
Some(State::Login(Login::CreatePassword2((
username.to_owned(),
pass,
)))),
);
} }
PlayerCheck::Err(err) => { PlayerCheck::Err(err) => {
send_queue.push(token, "\nInvalid password:\n", false, None); send_queue.push(token, "\nInvalid password:\n", false, None);
@ -93,8 +106,12 @@ impl Game {
Login::CreatePassword2((username, pass)) => { Login::CreatePassword2((username, pass)) => {
let pass = pass.to_owned(); let pass = pass.to_owned();
if message.is_empty() || message != pass { if message.is_empty() || message != pass {
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} else { } else {
if let Ok(id) = self.db.new_player_id() { if let Ok(id) = self.db.new_player_id() {
let player = Player { let player = Player {
@ -106,7 +123,12 @@ impl Game {
}; };
if self.db.single_save_player(token, &player).is_ok() { 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.push(token, "", true, None);
send_queue.append(&mut Command::dispatch_look( send_queue.append(&mut Command::dispatch_look(
@ -116,7 +138,6 @@ impl Game {
&mut self.db, &mut self.db,
)); ));
client.state = State::Action;
} else { } else {
send_queue.push(token, "Error", true, None); send_queue.push(token, "Error", true, None);
} }
@ -128,8 +149,12 @@ impl Game {
Login::Password(username) => { Login::Password(username) => {
if message.is_empty() { if message.is_empty() {
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} else { } else {
match self.db.find_player_by_name(username) { match self.db.find_player_by_name(username) {
Ok(Some(player)) => { Ok(Some(player)) => {
@ -139,9 +164,8 @@ impl Game {
token, token,
format!("Welcome back, {}\n\n", username), format!("Welcome back, {}\n\n", username),
false, false,
None, Some(State::Action),
); );
client.state = State::Action;
send_queue.append(&mut Command::dispatch_look( send_queue.append(&mut Command::dispatch_look(
&Command::default(), &Command::default(),
@ -151,26 +175,37 @@ impl Game {
)); ));
} else { } else {
send_queue.push(token, "Unable to login\n", false, None); send_queue.push(token, "Unable to login\n", false, None);
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} }
} else { } else {
send_queue.push(token, "Incorrect password\n", false, None); send_queue.push(token, "Incorrect password\n", false, None);
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} }
} }
Ok(None) | Err(_) => { Ok(None) | Err(_) => {
send_queue.push(token, "Error\n", false, None); send_queue.push(token, "Error\n", false, None);
send_queue.push(token, "\n\nUsername: ", false, None); send_queue.push(
client.state = State::Login(Login::Username); token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
} }
} }
} }
} }
}; };
self.clients.insert(token, client);
send_queue send_queue
} }
} }

Loading…
Cancel
Save