public GameRoomPlayerValidator(IGameRoomServiceAsync gameRoomService, IPlayerServiceAsync playerService) { _gameRoomService = gameRoomService; _playerService = playerService; RuleFor(dto => dto.GameRoomId) .MustAsync(async(dto, gameRoomId, context, cancellation) => { var gameRoom = await _gameRoomService.GetById(gameRoomId); if (gameRoom == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Game room was not found."); return(false); } return(true); }); RuleFor(dto => dto.PlayerId).NotEmpty() .MustAsync(async(dto, playerId, context, cancellation) => { var player = await playerService.GetById(playerId); if (player == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player was not found."); return(false); } return(true); }); }
public async Task <ActionResult <Player> > GetById(Guid id) { _logger.LogInformation("Calling playerService to get player with id: {id:l}", id); var player = await _playerServiceAsync.GetById(id); if (player != null) { return(Ok(player)); } return(NotFound()); }
public GuessWordCreationHostValidation(IGameRoomServiceAsync gameRoomService, IPlayerServiceAsync playerService) { _gameRoomService = gameRoomService; _playerService = playerService; RuleFor(dto => dto.GameRoomId) .MustAsync(async(dto, gameRoomId, context, cancellation) => { var gameRoom = await _gameRoomService.GetById(gameRoomId); if (gameRoom == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player was not found."); return(false); } return(true); }); RuleFor(dto => dto.PlayerId).NotEmpty() .MustAsync(async(dto, playerId, context, cancellation) => { var player = await playerService.GetById(playerId); if (player == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player was not found."); return(false); } return(true); }); RuleFor(dto => dto.GameRoomId).NotEmpty() .MustAsync(async(dto, gameRoomId, context, cancellation) => { var gameRoomPlayerData = await gameRoomService.GetPlayerRoomData(dto.GameRoomId, dto.PlayerId); if (gameRoomPlayerData == null || !gameRoomPlayerData.IsInRoom) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player is not the room."); return(false); } // TODO: activate so only hosts can create guess words // if (!gameRoomPlayerData.IsHost) return false; return(true); }); }
public NewGuessLetterValidator(IGameRoomServiceAsync gameRoomService, IPlayerServiceAsync playerService) { _gameRoomService = gameRoomService; _playerService = playerService; RuleFor(dto => dto.GuessWordId).Cascade(CascadeMode.Stop).NotEmpty() .MustAsync(async(dto, guessWordId, context, cancellation) => { var gameRoom = await _gameRoomService.GetById(dto.GameRoomId); if (gameRoom == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Game room was not found."); return(false); } var guessWord = await _gameRoomService.GetGuessedWord(guessWordId); if (guessWord == null || guessWord.GameRoomId != dto.GameRoomId) { context.MessageFormatter.AppendArgument("ValidationMessage", "Guess word was not found in such room."); return(false); } var gameRound = guessWord.Round; if (gameRound.IsOver) { context.MessageFormatter.AppendArgument("ValidationMessage", "The round is over for this guess wooord."); return(false); } var alreadyGuessedLetter = guessWord.GuessLetters.FirstOrDefault(letter => letter.Letter == dto.GuessLetter); if (alreadyGuessedLetter != null) { context.MessageFormatter.AppendArgument("ValidationMessage", "This letter has already been guessed."); return(false); } return(true); }); RuleFor(dto => dto.PlayerId).NotEmpty() .MustAsync(async(dto, playerId, context, cancellation) => { var player = await playerService.GetById(playerId); if (player == null) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player was not found."); return(false); } var gameRoomPlayerData = await gameRoomService.GetPlayerRoomData(dto.GameRoomId, playerId); if (gameRoomPlayerData == null || !gameRoomPlayerData.IsInRoom) { context.MessageFormatter.AppendArgument("ValidationMessage", "Player is not in such room."); return(false); } return(true); }); RuleFor(dto => dto.GuessLetter) .NotEmpty() .MaximumLength(1) .Matches("[a-zA-Z]") .WithMessage("GuessLetter must be a string with single char (no numbers)."); }