use config instead of hard coded values

master
rasul 5 years ago
parent f4134b1911
commit 7a17fe4a08

@ -1,35 +1,25 @@
use lazy_static::lazy_static;
use crate::game::Game; use crate::game::Game;
lazy_static! {
static ref NAME_CHARS: Vec<char> =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".chars().collect();
static ref PASS_CHARS: Vec<char> =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 `~@#$%^&*()_+-=[]{};':\",./<>?"
.chars().collect();
}
pub enum PlayerCheck { pub enum PlayerCheck {
Ok(String), Ok(String),
Err(Vec<String>), Err(Vec<String>),
} }
impl Game { impl Game {
pub fn check_player_name<S: Into<String>>(name: S) -> PlayerCheck { pub fn check_player_name<S: Into<String>>(&self, name: S) -> PlayerCheck {
let name = name.into(); let name = name.into();
let name = name.trim(); let name = name.trim();
let mut err_vec = Vec::<String>::new(); let mut err_vec = Vec::<String>::new();
if name.len() < 2 { if name.len() < self.config.player.name_min {
err_vec.push("2 character minimum".into()); err_vec.push(format!("{} character minimum", self.config.player.name_min));
} else if name.len() > 20 { } else if name.len() > self.config.player.name_max {
err_vec.push("20 character maximum.".into()); err_vec.push(format!("{} character maximum", self.config.player.name_max));
} }
for c in name.chars() { for c in name.chars() {
if !NAME_CHARS.contains(&c) { if !self.config.player.name_chars.contains(&c.to_string()) {
err_vec.push("English alphabetic characters only.".into()); err_vec.push(format!("Allowed characters are: {}", self.config.player.name_chars));
break; break;
} }
} }
@ -41,19 +31,19 @@ impl Game {
} }
} }
pub fn check_player_password<S: Into<String>>(password: S) -> PlayerCheck { pub fn check_player_password<S: Into<String>>(&self, password: S) -> PlayerCheck {
let password = password.into(); let password = password.into();
let mut err_vec = Vec::<String>::new(); let mut err_vec = Vec::<String>::new();
if password.len() < 8 { if password.len() < self.config.player.pass_min {
err_vec.push("8 character minimum.".into()); err_vec.push(format!("{} character minimum", self.config.player.pass_min));
} else if password.len() > 128 { } else if password.len() > self.config.player.pass_max {
err_vec.push("128 character maximum.".into()); err_vec.push(format!("{} character maximum", self.config.player.pass_max));
} }
for c in password.chars() { for c in password.chars() {
if !PASS_CHARS.contains(&c) { if !self.config.player.pass_chars.contains(&c.to_string()) {
err_vec.push("English alphanumeric characters or symbols only.".into()); err_vec.push(format!("Allowed characters are: {}", self.config.player.pass_chars));
break; break;
} }
} }

Loading…
Cancel
Save