This way it can be called from methods that are not members of Game such as the password set command. It does require some global variables and an initializion to get setup from the configuration.master
parent
73fbc8750d
commit
bdf8f86ba5
@ -0,0 +1,90 @@
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate::config::Player;
|
||||
|
||||
// this global stuff is so that we can load up the config and check stuff without
|
||||
// being part of the Game struct
|
||||
thread_local! {
|
||||
pub static NAME_MIN: RefCell<usize> = RefCell::new(0);
|
||||
pub static NAME_MAX: RefCell<usize> = RefCell::new(0);
|
||||
pub static NAME_CHARS: RefCell<&'static str> = RefCell::new("");
|
||||
pub static PASS_MIN: RefCell<usize> = RefCell::new(0);
|
||||
pub static PASS_MAX: RefCell<usize> = RefCell::new(0);
|
||||
pub static PASS_CHARS: RefCell<&'static str> = RefCell::new("");
|
||||
}
|
||||
|
||||
/// Return value for check functions
|
||||
#[derive(Debug)]
|
||||
pub enum PlayerCheck {
|
||||
/// The value checks out
|
||||
Ok(String),
|
||||
|
||||
/// There is one or more issues
|
||||
Err(Vec<String>),
|
||||
}
|
||||
|
||||
pub fn init(config: &Player) {
|
||||
NAME_MIN.with(|n| *n.borrow_mut() = config.name_min);
|
||||
NAME_MAX.with(|n| *n.borrow_mut() = config.name_max);
|
||||
NAME_CHARS.with(|n| *n.borrow_mut() = Box::leak(config.name_chars.clone().into_boxed_str()));
|
||||
PASS_MIN.with(|p| *p.borrow_mut() = config.pass_min);
|
||||
PASS_MAX.with(|p| *p.borrow_mut() = config.pass_max);
|
||||
PASS_CHARS.with(|p| *p.borrow_mut() = Box::leak(config.pass_chars.clone().into_boxed_str()));
|
||||
}
|
||||
|
||||
/// Check the player name to ensure it meets guidelines
|
||||
pub fn player_name<S: Into<String>>(name: S) -> PlayerCheck {
|
||||
let name = name.into();
|
||||
let name = name.trim();
|
||||
let mut err_vec = Vec::<String>::new();
|
||||
|
||||
if name.len() < NAME_MIN.with(|n| *n.borrow()) {
|
||||
err_vec.push(format!("{} character minimum", NAME_MIN.with(|n| *n.borrow())));
|
||||
} else if name.len() > NAME_MAX.with(|n| *n.borrow()) {
|
||||
err_vec.push(format!("{} character maximum", NAME_MAX.with(|n| *n.borrow())));
|
||||
}
|
||||
|
||||
for c in name.chars() {
|
||||
if !NAME_CHARS.with(|n| *n.borrow()).contains(&c.to_string()) {
|
||||
err_vec.push(format!(
|
||||
"Allowed characters are: {}",
|
||||
NAME_CHARS.with(|n| *n.borrow())
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !err_vec.is_empty() {
|
||||
PlayerCheck::Err(err_vec)
|
||||
} else {
|
||||
PlayerCheck::Ok(name.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Check the password to ensure it meets password requirements
|
||||
pub fn player_password<S: Into<String>>(password: S) -> PlayerCheck {
|
||||
let password = password.into();
|
||||
let mut err_vec = Vec::<String>::new();
|
||||
|
||||
if password.len() < PASS_MIN.with(|p| *p.borrow()) {
|
||||
err_vec.push(format!("{} character minimum", PASS_MIN.with(|p| *p.borrow())));
|
||||
} else if password.len() > PASS_MAX.with(|p| *p.borrow()) {
|
||||
err_vec.push(format!("{} character maximum", PASS_MAX.with(|p| *p.borrow())));
|
||||
}
|
||||
|
||||
for c in password.chars() {
|
||||
if !PASS_CHARS.with(|p| *p.borrow()).contains(&c.to_string()) {
|
||||
err_vec.push(format!(
|
||||
"Allowed characters are: {}",
|
||||
PASS_CHARS.with(|p| *p.borrow())
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if err_vec.is_empty() {
|
||||
PlayerCheck::Ok(password.into())
|
||||
} else {
|
||||
PlayerCheck::Err(err_vec)
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
use crate::game::Game;
|
||||
|
||||
/// Return value for check functions
|
||||
#[derive(Debug)]
|
||||
pub enum PlayerCheck {
|
||||
/// The value checks out
|
||||
Ok(String),
|
||||
|
||||
/// There is one or more issues
|
||||
Err(Vec<String>),
|
||||
}
|
||||
|
||||
impl Game {
|
||||
/// Check the player name to ensure it meets guidelines
|
||||
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() < 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 !self.config.player.name_chars.contains(&c.to_string()) {
|
||||
err_vec.push(format!(
|
||||
"Allowed characters are: {}",
|
||||
self.config.player.name_chars
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !err_vec.is_empty() {
|
||||
PlayerCheck::Err(err_vec)
|
||||
} else {
|
||||
PlayerCheck::Ok(name.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Check the password to ensure it meets password requirements
|
||||
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() < 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 !self.config.player.pass_chars.contains(&c.to_string()) {
|
||||
err_vec.push(format!(
|
||||
"Allowed characters are: {}",
|
||||
self.config.player.pass_chars
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if err_vec.is_empty() {
|
||||
PlayerCheck::Ok(password.into())
|
||||
} else {
|
||||
PlayerCheck::Err(err_vec)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue