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

Loading…
Cancel
Save