コード例 #1
0
ファイル: ValidationService.cs プロジェクト: IgooorGP/hangman
        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);
            });
        }
コード例 #2
0
ファイル: ValidationService.cs プロジェクト: IgooorGP/hangman
        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);
            });
        }
コード例 #3
0
 public GameRoomController(IGameRoomServiceAsync gameRoomServiceAsync,
                           IPlayerServiceAsync playerServiceAsync,
                           ILogger <GameRoomController> logger)
 {
     _gameRoomServiceAsync = gameRoomServiceAsync;
     _playerServiceAsync   = playerServiceAsync;
     _logger = logger;
 }
コード例 #4
0
ファイル: ValidationService.cs プロジェクト: IgooorGP/hangman
        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);
            });
        }
コード例 #5
0
ファイル: ValidationService.cs プロジェクト: IgooorGP/hangman
        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).");
        }