Limit player name length
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e0132b68ed
commit
b1145cc362
@ -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))
|
||||
|
@ -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 {
|
||||
|
@ -21,3 +21,6 @@ pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5];
|
||||
pub const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
pub const INVITE_CODE_LENGTH: usize = 5;
|
||||
|
||||
pub const MIN_PLAYER_NAME_LENGTH: usize = 1;
|
||||
pub const MAX_PLAYER_NAME_LENGTH: usize = 10;
|
||||
|
@ -57,6 +57,8 @@ pub struct PlayConfiguration {
|
||||
pub max_boats_number: usize,
|
||||
pub bot_types: &'static [BotDescription],
|
||||
pub ordinate_alphabet: &'static str,
|
||||
pub min_player_name_len: usize,
|
||||
pub max_player_name_len: usize,
|
||||
}
|
||||
|
||||
impl Default for PlayConfiguration {
|
||||
@ -72,6 +74,8 @@ impl Default for PlayConfiguration {
|
||||
max_boats_number: MAX_BOATS_NUMBER,
|
||||
bot_types: &BOTS_TYPES,
|
||||
ordinate_alphabet: ALPHABET,
|
||||
min_player_name_len: MIN_PLAYER_NAME_LENGTH,
|
||||
max_player_name_len: MAX_PLAYER_NAME_LENGTH,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ use actix_web_actors::ws::{CloseCode, CloseReason, Message, ProtocolError, Webso
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::bot_player::BotPlayer;
|
||||
use crate::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
|
||||
use crate::data::{BoatsLayout, Coordinates, CurrentGameStatus, FireResult, GameRules};
|
||||
use crate::dispatcher_actor::{AcceptInvite, CreateInvite, DispatcherActor, PlayRandom};
|
||||
use crate::game::{AddPlayer, Game};
|
||||
@ -149,6 +150,12 @@ impl Actor for HumanPlayerWS {
|
||||
type Context = WebsocketContext<Self>;
|
||||
|
||||
fn started(&mut self, ctx: &mut Self::Context) {
|
||||
// Check player name length
|
||||
if self.name.len() < MIN_PLAYER_NAME_LENGTH || self.name.len() > MAX_PLAYER_NAME_LENGTH {
|
||||
ctx.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
self.hb(ctx);
|
||||
|
||||
self.send_message(ServerMessage::WaitingForAnotherPlayer, ctx);
|
||||
|
Loading…
Reference in New Issue
Block a user