Limit player name length
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-16 18:51:05 +02:00
parent e0132b68ed
commit b1145cc362
5 changed files with 31 additions and 1 deletions

View File

@ -20,6 +20,7 @@ use cli_player::ui_screens::input_screen::InputScreen;
use cli_player::ui_screens::popup_screen::PopupScreen;
use cli_player::ui_screens::select_play_mode_screen::{SelectPlayModeResult, SelectPlayModeScreen};
use cli_player::ui_screens::*;
use sea_battle_backend::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
use sea_battle_backend::data::GameRules;
use sea_battle_backend::utils::Res;
@ -72,12 +73,17 @@ async fn run_dev<B: Backend>(
/// Ask the user to specify its username
fn query_username<B: Backend>(terminal: &mut Terminal<B>) -> Res<String> {
let hostname = hostname::get()?.to_string_lossy().to_string();
let mut hostname = hostname::get()?.to_string_lossy().to_string();
if hostname.len() > MAX_PLAYER_NAME_LENGTH {
hostname = hostname[0..MAX_PLAYER_NAME_LENGTH].to_string();
}
let res =
InputScreen::new("Please specify the name to which other players should identify you:")
.set_title("Player name")
.set_value(&hostname)
.set_min_length(MIN_PLAYER_NAME_LENGTH)
.set_max_length(MAX_PLAYER_NAME_LENGTH)
.show(terminal)?;
Ok(res.value().unwrap_or(hostname))

View File

@ -55,6 +55,16 @@ impl<'a> InputScreen<'a> {
self
}
pub fn set_min_length(mut self, v: usize) -> Self {
self.min_len = v;
self
}
pub fn set_max_length(mut self, v: usize) -> Self {
self.max_len = v;
self
}
/// Get error contained in input
fn error(&self) -> Option<&'static str> {
if self.value.len() > self.max_len {