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 {
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
}
}

Loading…
Cancel
Save