parent
6f35c4a8e4
commit
b7bfda95e9
@ -0,0 +1,67 @@
|
||||
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 {
|
||||
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());
|
||||
}
|
||||
|
||||
for c in name.chars() {
|
||||
if !NAME_CHARS.contains(&c) {
|
||||
err_vec.push("English alphabetic characters only.".into());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !err_vec.is_empty() {
|
||||
PlayerCheck::Err(err_vec)
|
||||
} else {
|
||||
PlayerCheck::Ok(name.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_player_password<S: Into<String>>(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());
|
||||
}
|
||||
|
||||
for c in password.chars() {
|
||||
if !PASS_CHARS.contains(&c) {
|
||||
err_vec.push("English alphanumeric characters or symbols only.".into());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if err_vec.is_empty() {
|
||||
PlayerCheck::Ok(password.into())
|
||||
} else {
|
||||
PlayerCheck::Err(err_vec)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue