Пример #1
0
        public async Task SubmitAnswerForValidData()
        {
            var game = await Fixture.CreateGame();

            var round = await Fixture.GetCurrentGameRound(game.GameId);

            var userId = await Fixture.CreateRandomUser();

            var player = await Fixture.JoinPlayer(game.GameId, userId);

            await Fixture.StartGameRound(game.GameId, round.GameRoundId, game.OwnerId);

            var cardId = await Fixture.GetRandomPlayerCard(player.PlayerId);

            var request = new SubmitAnswer
            {
                GameId       = game.GameId,
                GameRoundId  = round.GameRoundId,
                UserId       = userId,
                PlayerCardId = cardId
            };

            var handler = Fixture.GetService <ISubmitAnswerHandler>();
            var result  = await handler.Handle(request, new System.Threading.CancellationToken());

            Assert.True(result.IsSuccessful);
            Assert.Null(result.Error);
        }
Пример #2
0
        public async Task ReturnFailureForInvalidCardAndPlayerCombination()
        {
            var baseMock      = BaseGameRequestsValidatorMock.CreateMock();
            var playersRepo   = PlayersRepositoryMock.Create().Object;
            var gameRoundRepo = GameRoundsRepositoryMock.Create(
                status: Common.Enums.GameRoundStatusEnum.InProgress).Object;
            var cardsRepo = CreatePlayerCardsRepoMock(
                isCardLinkedWithUser: false);
            var questionCardRepo  = CreateQuestionCardRepoMock();
            var playerAnswersRepo = CreatePlayerAnswersRepository();

            var validator = new SubmitAnswerValidator(baseMock.Object, playersRepo, gameRoundRepo, cardsRepo, questionCardRepo, playerAnswersRepo);

            var request = new SubmitAnswer
            {
                GameRoundId  = MockConstants.RoundId,
                UserId       = 1,
                GameId       = 1,
                PlayerCardId = 1
            };

            var result = await validator.Validate(request);

            Assert.False(result.IsSuccessful);
            Assert.Equal(ValidatorErrors.Games.CardNotLinkedWithPlayer, result.Error);
        }
Пример #3
0
        public async Task ReturnSuccessForValidData()
        {
            var baseMock      = BaseGameRequestsValidatorMock.CreateMock();
            var playersRepo   = PlayersRepositoryMock.Create().Object;
            var gameRoundRepo = GameRoundsRepositoryMock.Create(
                status: Common.Enums.GameRoundStatusEnum.InProgress).Object;
            var cardsRepo         = CreatePlayerCardsRepoMock();
            var questionCardRepo  = CreateQuestionCardRepoMock();
            var playerAnswersRepo = CreatePlayerAnswersRepository();

            var validator = new SubmitAnswerValidator(baseMock.Object, playersRepo, gameRoundRepo, cardsRepo, questionCardRepo, playerAnswersRepo);

            var request = new SubmitAnswer
            {
                GameRoundId  = MockConstants.RoundId,
                UserId       = 1,
                GameId       = 1,
                PlayerCardId = 1
            };

            var result = await validator.Validate(request);

            Assert.True(result.IsSuccessful);
            baseMock.Verify(x => x.Validate(request));
        }
Пример #4
0
        public async Task ReturnFailureForMissingSecondPlayerCard()
        {
            var baseMock      = BaseGameRequestsValidatorMock.CreateMock();
            var playersRepo   = PlayersRepositoryMock.Create().Object;
            var gameRoundRepo = GameRoundsRepositoryMock.Create(
                status: Common.Enums.GameRoundStatusEnum.InProgress).Object;
            var cardsRepo        = CreatePlayerCardsRepoMock();
            var questionCardRepo = CreateQuestionCardRepoMock(
                isMultiChoiceQuestion: true);
            var playerAnswersRepo = CreatePlayerAnswersRepository();

            var validator = new SubmitAnswerValidator(baseMock.Object, playersRepo, gameRoundRepo, cardsRepo, questionCardRepo, playerAnswersRepo);

            var request = new SubmitAnswer
            {
                GameRoundId  = MockConstants.RoundId,
                UserId       = 1,
                GameId       = 1,
                PlayerCardId = 1
            };

            var result = await validator.Validate(request);

            Assert.False(result.IsSuccessful);
            Assert.Equal(ValidatorErrors.Games.SecondPlayerCardIdRequired, result.Error);
        }
Пример #5
0
        public async Task SubmitAnswer(int gameId, int gameRoundId, int userId, int playerCardId)
        {
            var request = new SubmitAnswer
            {
                GameId       = gameId,
                GameRoundId  = gameRoundId,
                UserId       = userId,
                PlayerCardId = playerCardId
            };

            var handler = GetService <ISubmitAnswerHandler>();
            await handler.Handle(request, new System.Threading.CancellationToken());
        }
Пример #6
0
        private async Task<int> SubmitAnswer(SubmitAnswer request, Model.DTO.Players.PlayerModel player)
        {
            var submitModel = new CreatePlayerAnswerModel
            {
                GameRoundId = request.GameRoundId,
                PlayerId = player.PlayerId,
                AnswerCardId = await GetAnswerCardId(request.PlayerCardId),
                SecondaryAnswerCardId = await GetSecondaryAnswerCardId(request.SecondPlayerCardId)
            };
            var answerId = await playerAnswerRepository.CreatePlayerAnswer(submitModel);

            await MarkCardAsUsed(request.PlayerCardId);
            await MarkCardAsUsed(request.SecondPlayerCardId);

            return answerId;
        }
Пример #7
0
        public async Task<SubmitAnswerResult> Handle(SubmitAnswer request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validatorResult = await requestValidator.Validate(request);
                if (!validatorResult.IsSuccessful)
                    return new SubmitAnswerResult(validatorResult.Error);

                var player = await playersRepository.GetPlayerByUserId(request.UserId, request.GameId);
                int playerAnswerId = await SubmitAnswer(request, player);
                if (playerAnswerId == 0)
                    return new SubmitAnswerResult(GameErrors.SubmitAnswerFailed);

                await gameCheckpointUpdater.Update(request.GameId, nameof(Model.Core.Games.SubmitAnswer));

                transaction.CommitTransaction();

                return new SubmitAnswerResult();
            }
        }
Пример #8
0
        public ActionResult <Answer[]> Post([FromBody] SubmitAnswer submitAnswer)
        {
            if (!submitAnswer.IsValid(_secrets))
            {
                return(BadRequest($"{submitAnswer.Solution} is not a valid solution for {submitAnswer.Flag}"));
            }

            var answer = new Answer
            {
                Name = submitAnswer.Name.Trim().ToUpper(),
                Flag = submitAnswer.Flag,
            };

            if (_database.Contains(answer))
            {
                return(Ok(_database.Answers.ToArray()));
            }

            _database.Add(answer);
            _database.SaveChanges();

            return(CreatedAtRoute("GetAnswers", _database.Answers.ToArray()));
        }