From 7a17fe4a0816256780db99d8a44bd7a10308d01a Mon Sep 17 00:00:00 2001 From: rasul Date: Tue, 7 Apr 2020 20:15:21 -0500 Subject: [PATCH] use config instead of hard coded values --- src/game/check_player.rs | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/game/check_player.rs b/src/game/check_player.rs index b02ed45..39663c5 100644 --- a/src/game/check_player.rs +++ b/src/game/check_player.rs @@ -1,35 +1,25 @@ -use lazy_static::lazy_static; - use crate::game::Game; -lazy_static! { - static ref NAME_CHARS: Vec = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".chars().collect(); - static ref PASS_CHARS: Vec = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 `~@#$%^&*()_+-=[]{};':\",./<>?" - .chars().collect(); -} - pub enum PlayerCheck { Ok(String), Err(Vec), } impl Game { - pub fn check_player_name>(name: S) -> PlayerCheck { + pub fn check_player_name>(&self, name: S) -> PlayerCheck { let name = name.into(); let name = name.trim(); let mut err_vec = Vec::::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>(password: S) -> PlayerCheck { + pub fn check_player_password>(&self, password: S) -> PlayerCheck { let password = password.into(); let mut err_vec = Vec::::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; } }