Ejemplo n.º 1
0
        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);
            });
        }
Ejemplo n.º 2
0
        public GuessWordInGameRoomValidator(IGameRoomServiceAsync gameRoomService,
                                            IPlayerServiceAsync playerService)
        {
            _gameRoomService = gameRoomService;
            _playerService   = playerService;

            RuleFor(dto => dto.GuessWordId).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);
                }

                return(true);
            });
        }
Ejemplo n.º 3
0
 public GameRoomController(IGameRoomServiceAsync gameRoomServiceAsync,
                           IPlayerServiceAsync playerServiceAsync,
                           ILogger <GameRoomController> logger)
 {
     _gameRoomServiceAsync = gameRoomServiceAsync;
     _playerServiceAsync   = playerServiceAsync;
     _logger = logger;
 }
Ejemplo n.º 4
0
        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);
            });
        }
Ejemplo n.º 5
0
        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).");
        }