From b9156b0300ad60b0f14011a07240121a52f27825 Mon Sep 17 00:00:00 2001 From: rasul Date: Tue, 7 Apr 2020 20:41:00 -0500 Subject: [PATCH] check that name is valid per name requirements --- src/game/state/login.rs | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/game/state/login.rs b/src/game/state/login.rs index a148872..b82661c 100644 --- a/src/game/state/login.rs +++ b/src/game/state/login.rs @@ -3,7 +3,7 @@ use log::warn; use mio::Token; use crate::command::Command; -use crate::game::Game; +use crate::game::{Game, PlayerCheck}; use crate::player::Player; use crate::queue::SendQueue; use crate::state::*; @@ -26,24 +26,35 @@ impl Game { if message.is_empty() { send_queue.push(token, "\n\nUsername: ", false, None); } else { - match self.db.find_player_by_name(&message) { - Ok(Some(_)) => { - send_queue.push(token, "\nPassword: ", false, None); - client.state = State::Login(Login::Password(message)); - } - Ok(None) => { - send_queue.push( - token, - format!("\nCreate {}? [y/N]: ", message.clone()), - false, - None, - ); - client.state = State::Login(Login::CreateUser(message)); - } - Err(_) => { - send_queue.push(token, "\nError\n\nUsername: ", false, None); - } - } + 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)); + } + Ok(None) => { + send_queue.push( + token, + format!("\nCreate {}? [y/N]: ", name), + false, + None, + ); + client.state = State::Login(Login::CreateUser(name)); + } + Err(_) => { + send_queue.push(token, "\nError\n\nUsername: ", false, None); + } + } + }, + PlayerCheck::Err(err) => { + send_queue.push(token, "\nInvalid username:\n", false, None); + for line in err { + send_queue.push(token, format!("{}\n", line), false, None); + } + send_queue.push(token, "\n\nUsername: ", false, None); + }, + }; } }