def validate_private_message(self, message): if not message.selected_users: raise InvalidMessage(f"You need to choose users") if not message.content: raise InvalidMessage(f"Content can't be empty") if len( message.selected_users ) == 1 and message.selected_users[0] == self.client_handler.user.name: raise InvalidMessage("You can't send message just for yourself")
def validate_game_step(self, message): if not self.client_handler.active_game: raise InvalidMessage("You don't have an active game") if not message.content: raise InvalidMessage( "You need to pass an action inside of content") available_steps = GameSteps.list() if message.content not in available_steps: raise InvalidMessage( f"step must be one of {', '.join(available_steps)}")
def validate_new_user(self, message): if not message.content: raise InvalidMessage('You need to pass a name inside of content') name_pattern = r'^[a-zA-Z]{4,12}$' if not re.match(name_pattern, message.content): raise InvalidMessage( 'Name must include only alphabet symbols and length must be between 4 and 12' ) if message.content == 'server' or message.content in self.client_handler.handlers: raise InvalidMessage( f'Name {message.content} already reserved! Please use another one' )
def validate_message(self, message, required_fields=None, prop_values=None): if required_fields: for field in required_fields: if not getattr(message, field): raise InvalidMessage(f"{field} is required field!") if prop_values: for prop, values in prop_values: if getattr(message, prop) not in values: raise InvalidMessage( f'{prop} must be one of {", ".join(values)}')
def validate_message(self, message): super().validate_message(message, ['command'], [('command', Commands.list())]) if not self.client_handler.user: raise InvalidMessage("You need to authenticate first") validators = { Commands.ROCK_PAPER_SCISSORS: self.validate_rock_paper_scissors, Commands.GAME_STEP: lambda: self.validate_game_step(message), Commands.PRIVATE_MESSAGE: lambda: self.validate_private_message(message) } validator = validators.get(message.command, None) if validator: validator()
def validate_message(self, message): super().validate_message(message, ['content']) if not self.client_handler.user: raise InvalidMessage("You need to authenticate first")
def validate_rock_paper_scissors(self): if self.client_handler.active_game: raise InvalidMessage( "Please finish last game before starting a new")
def validate_exit(self): if not self.client_handler.user: raise InvalidMessage("You need to authenticate first")