mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Limit character name length
This commit is contained in:
parent
0da363e420
commit
8b4230db61
@ -7,6 +7,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
pub const MAX_CHARACTERS_PER_PLAYER: usize = 8;
|
pub const MAX_CHARACTERS_PER_PLAYER: usize = 8;
|
||||||
pub type CharacterId = i64;
|
pub type CharacterId = i64;
|
||||||
|
|
||||||
|
pub const MAX_NAME_LENGTH: usize = 20;
|
||||||
|
|
||||||
/// The minimum character data we need to create a new character on the server.
|
/// The minimum character data we need to create a new character on the server.
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Character {
|
pub struct Character {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use common::character::MAX_NAME_LENGTH;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@ -16,6 +17,10 @@ impl AliasValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate(&self, alias: &str) -> Result<(), ValidatorError> {
|
pub fn validate(&self, alias: &str) -> Result<(), ValidatorError> {
|
||||||
|
if alias.len() > MAX_NAME_LENGTH {
|
||||||
|
return Err(ValidatorError::TooLong(alias.to_owned(), alias.len()));
|
||||||
|
}
|
||||||
|
|
||||||
let lowercase_alias = alias.to_lowercase();
|
let lowercase_alias = alias.to_lowercase();
|
||||||
|
|
||||||
for banned_word in self.banned_substrings.iter() {
|
for banned_word in self.banned_substrings.iter() {
|
||||||
@ -33,6 +38,7 @@ impl AliasValidator {
|
|||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum ValidatorError {
|
pub enum ValidatorError {
|
||||||
Forbidden(String, String),
|
Forbidden(String, String),
|
||||||
|
TooLong(String, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ValidatorError {
|
impl Display for ValidatorError {
|
||||||
@ -43,6 +49,7 @@ impl Display for ValidatorError {
|
|||||||
"Character name \"{}\" contains a banned word",
|
"Character name \"{}\" contains a banned word",
|
||||||
name
|
name
|
||||||
),
|
),
|
||||||
|
Self::TooLong(name, _) => write!(formatter, "Character name \"{}\" too long", name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,7 +63,7 @@ mod tests {
|
|||||||
let banned_substrings = vec!["bad".to_owned(), "worse".to_owned()];
|
let banned_substrings = vec!["bad".to_owned(), "worse".to_owned()];
|
||||||
let validator = AliasValidator::new(banned_substrings);
|
let validator = AliasValidator::new(banned_substrings);
|
||||||
|
|
||||||
let bad_alias = "Badplayery Mc WorsePlayeryFace";
|
let bad_alias = "BadplayerMcWorseFace";
|
||||||
let result = validator.validate(bad_alias);
|
let result = validator.validate(bad_alias);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -112,4 +119,21 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(result, Ok(()));
|
assert_eq!(result, Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn too_long() {
|
||||||
|
let banned_substrings = vec!["orange".to_owned()];
|
||||||
|
let validator = AliasValidator::new(banned_substrings);
|
||||||
|
|
||||||
|
let bad_alias = "Thisnameistoolong Muchtoolong MuchTooLongByFar";
|
||||||
|
let result = validator.validate(bad_alias);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
Err(ValidatorError::TooLong(
|
||||||
|
bad_alias.to_owned(),
|
||||||
|
bad_alias.chars().count()
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ use crate::{
|
|||||||
use client::Client;
|
use client::Client;
|
||||||
use common::{
|
use common::{
|
||||||
assets::AssetHandle,
|
assets::AssetHandle,
|
||||||
character::{CharacterId, CharacterItem, MAX_CHARACTERS_PER_PLAYER},
|
character::{CharacterId, CharacterItem, MAX_CHARACTERS_PER_PLAYER, MAX_NAME_LENGTH},
|
||||||
comp::{self, humanoid, inventory::slot::EquipSlot, Inventory, Item},
|
comp::{self, humanoid, inventory::slot::EquipSlot, Inventory, Item},
|
||||||
LoadoutBuilder,
|
LoadoutBuilder,
|
||||||
};
|
};
|
||||||
@ -1238,7 +1238,7 @@ impl Controls {
|
|||||||
},
|
},
|
||||||
Message::Name(value) => {
|
Message::Name(value) => {
|
||||||
if let Mode::Create { name, .. } = &mut self.mode {
|
if let Mode::Create { name, .. } = &mut self.mode {
|
||||||
*name = value;
|
*name = value.chars().take(MAX_NAME_LENGTH).collect();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Message::BodyType(value) => {
|
Message::BodyType(value) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user