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:
@ -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);
|
||||
|
Reference in New Issue
Block a user