public async Task <ValidatorResult> Validate(EndSubmissions request)
        {
            var baseResult = await baseGameRequestsValidator.Validate(request);

            if (!baseResult.IsSuccessful)
            {
                return(new ValidatorResult(baseResult.Error));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (round.Status != Common.Enums.GameRoundStatusEnum.InProgress)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            var isOwnerResult = await gameOrRoundOwnerRule.Validate(request);

            if (!isOwnerResult.IsSuccessful)
            {
                return(new ValidatorResult(isOwnerResult.Error));
            }

            var numberOfAnswers = await playerAnswersRepository.GetNumberOfAnswers(request.GameRoundId);

            if (numberOfAnswers < GameConstants.MinimumAnswersCount)
            {
                return(new ValidatorResult(ValidatorErrors.Games.NotEnoughSubmittedAnswers));
            }

            return(new ValidatorResult());
        }
示例#2
0
        public async Task <ValidatorResult> Validate(StartGameRound request)
        {
            var baseResult = await baseGameRequestsValidator.Validate(request);

            if (!baseResult.IsSuccessful)
            {
                return(new ValidatorResult(baseResult.Error));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (round.Status != Common.Enums.GameRoundStatusEnum.Pending)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            var isOwnerResult = await gameOrRoundOwnerRule.Validate(request);

            if (!isOwnerResult.IsSuccessful)
            {
                return(new ValidatorResult(isOwnerResult.Error));
            }

            var playerCount = await playersRepository.GetActivePlayersCount(request.GameId);

            if (playerCount < GameConstants.MinimumPlayersCount)
            {
                return(new ValidatorResult(ValidatorErrors.Games.NotEnoughPlayers));
            }

            return(new ValidatorResult());
        }
示例#3
0
        public async Task <ValidatorResult> Validate(SkipRound request)
        {
            var baseResult = await baseGameRequestsValidator.Validate(request);

            if (!baseResult.IsSuccessful)
            {
                return(new ValidatorResult(baseResult.Error));
            }

            var isOwnerResult = await gameOrRoundOwnerRule.Validate(request);

            if (!isOwnerResult.IsSuccessful)
            {
                return(new ValidatorResult(isOwnerResult.Error));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (!round.IsActive || !statuses.Contains(round.Status))
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            return(new ValidatorResult());
        }
示例#4
0
        public async Task <ValidatorResult> Validate(SubmitAnswer request)
        {
            var baseResult = await baseGameRequestsValidator.Validate(request);

            if (!baseResult.IsSuccessful)
            {
                return(new ValidatorResult(baseResult.Error));
            }

            var player = await playersRepository.GetPlayerByUserId(request.UserId, request.GameId);

            if (await playerAnswersRepository.HasPlayerSubmittedAnswer(player.PlayerId, request.GameRoundId))
            {
                return(new ValidatorResult(ValidatorErrors.Games.PlayerAlreadySubmittedAnswer));
            }

            if (request.PlayerCardId == 0)
            {
                return(new ValidatorResult(ValidatorErrors.Games.PlayerCardIdRequired));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (round.Status != Common.Enums.GameRoundStatusEnum.InProgress)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            var firstPlayerCard = await playerCardsRepository.GetPlayerCard(request.PlayerCardId);

            if (firstPlayerCard == null || firstPlayerCard.PlayerId != player.PlayerId)
            {
                return(new ValidatorResult(ValidatorErrors.Games.CardNotLinkedWithPlayer));
            }

            var questionCard = await questionCardsRepository.GetActiveQuestionCardForRound(request.GameRoundId);

            if (questionCard.NumberOfAnswers > 1)
            {
                if (!request.SecondPlayerCardId.HasValue)
                {
                    return(new ValidatorResult(ValidatorErrors.Games.SecondPlayerCardIdRequired));
                }

                var secondPlayerCard = await playerCardsRepository.GetPlayerCard(request.SecondPlayerCardId.Value);

                if (secondPlayerCard == null || secondPlayerCard.PlayerId != player.PlayerId)
                {
                    return(new ValidatorResult(ValidatorErrors.Games.CardNotLinkedWithPlayer));
                }
            }

            return(new ValidatorResult());
        }
示例#5
0
        public async Task <ValidatorResult> Validate <T>(T request) where T : IGameRoundRequest, IPlayerRequest
        {
            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            var player = await playersRepository.GetPlayerByUserId(request.UserId, request.GameId);

            if (round.OwnerPlayerId != player.PlayerId)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidUserAction));
            }

            return(new ValidatorResult());
        }
示例#6
0
        public async Task <ValidatorResult> Validate(IGameRoundRequest request)
        {
            if (request.GameRoundId == 0)
            {
                return(new ValidatorResult(ValidatorErrors.Games.GameRoundIdRequired));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (round == null)
            {
                return(new ValidatorResult(ValidatorErrors.Games.RoundNotLinkedWithGame));
            }
            if (!round.IsActive)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            return(new ValidatorResult());
        }
示例#7
0
        public async Task <CreateRoundResult> CreateRound(int gameId, int gameRoundId)
        {
            var questionCard = await questionCardsRepository
                               .GetRandomQuestionCardForGame(gameId);

            if (questionCard == null)
            {
                return(new CreateRoundResult(GameErrors.NoQuestionCardsAvailable));
            }

            var prevRound = await gameRoundsRepository
                            .GetGameRound(gameId, gameRoundId);

            var nextOwnerResult = await nextGameRoundOwnerProvider.GetNextOwner(gameId, prevRound.OwnerPlayerId);

            if (!nextOwnerResult.IsSuccessful)
            {
                return(new CreateRoundResult(GameErrors.CouldNotFindNextRoundOwner));
            }

            return(await CreateRound(gameId, nextOwnerResult.PlayerId, questionCard.QuestionCardId, ++prevRound.Number));
        }
示例#8
0
        public async Task <ValidatorResult> Validate(SelectAnswer request)
        {
            var baseResult = await baseGameRequestsValidator.Validate(request);

            if (!baseResult.IsSuccessful)
            {
                return(new ValidatorResult(baseResult.Error));
            }

            var isOwnerResult = await roundOwnerRule.Validate(request);

            if (!isOwnerResult.IsSuccessful)
            {
                return(new ValidatorResult(isOwnerResult.Error));
            }

            var round = await gameRoundsRepository.GetGameRound(request.GameId, request.GameRoundId);

            if (round.Status != Common.Enums.GameRoundStatusEnum.Selection)
            {
                return(new ValidatorResult(ValidatorErrors.Games.InvalidGameRoundStatus));
            }

            var answerExists = await playerAnswersRepository.IsAnswerSubmitted(request.PlayerAnswerId, request.GameRoundId);

            if (!answerExists)
            {
                return(new ValidatorResult(ValidatorErrors.Games.PlayerAnswerDoesNotExists));
            }

            var playerIsActive = await playerAnswersRepository.IsAnsweringPlayerActive(request.PlayerAnswerId, request.GameRoundId);

            if (!playerIsActive)
            {
                return(new ValidatorResult(ValidatorErrors.Games.SelectedAnswerPlayerIsNotActive));
            }

            return(new ValidatorResult());
        }