Example #1
0
        public async Task <JoinGameResult> Handle(JoinGame request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validationResult = await requestValidator.Validate(request);

                if (!validationResult.IsSuccessful)
                {
                    return(new JoinGameResult(validationResult.Error));
                }

                var maxNumber = await playersRepository.GetMaxPlayerNumberForGame(request.GameId);

                var player = await CreatePlayer(request.GameId, request.UserId, ++maxNumber);

                var cardCount = await CreatePlayerAnswerCards(request.GameId, player.PlayerId, GameConstants.StartingPlayerCardsCount);

                if (cardCount != GameConstants.StartingPlayerCardsCount)
                {
                    return(new JoinGameResult(GameErrors.NotEnoughAnswerCards));
                }

                await gameCheckpointUpdater.Update(request.GameId, nameof(JoinGame));

                transaction.CommitTransaction();

                return(new JoinGameResult
                {
                    PlayerId = player.PlayerId
                });
            }
        }
Example #2
0
        public async Task <StartGameRoundResult> Handle(StartGameRound request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validatorResult = await validator.Validate(request);

                if (!validatorResult.IsSuccessful)
                {
                    return(new StartGameRoundResult(validatorResult.Error));
                }

                var started = await gameRoundsRepository
                              .UpdateGameRoundStatus(request.GameRoundId, Common.Enums.GameRoundStatusEnum.InProgress);

                if (!started)
                {
                    return(new StartGameRoundResult(GameErrors.GameRoundCouldNotBeStarted));
                }

                var checkpoint = await gameCheckpointUpdater.Update(request.GameId, nameof(StartGameRound));

                transaction.CommitTransaction();

                return(new StartGameRoundResult());
            }
        }
Example #3
0
        public async Task <EndSubmissionsResult> Handle(EndSubmissions request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validationResult = await requestValidator.Validate(request);

                if (!validationResult.IsSuccessful)
                {
                    return(new EndSubmissionsResult(validationResult.Error));
                }

                var updated = await gameRoundsRepository
                              .UpdateGameRoundStatus(request.GameRoundId, Common.Enums.GameRoundStatusEnum.Selection);

                if (!updated)
                {
                    return(new EndSubmissionsResult(GameErrors.CouldNotEndAnswersSubmissions));
                }

                var checkpoint = await gameCheckpointUpdater.Update(request.GameId, nameof(EndSubmissions));

                transaction.CommitTransaction();

                return(new EndSubmissionsResult());
            }
        }
Example #4
0
        public async Task <CancelGameResult> Handle(CancelGame request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validatorResult = await validator.Validate(request);

                if (!validatorResult.IsSuccessful)
                {
                    return(new CancelGameResult(validatorResult.Error));
                }

                var ended = await gamesRepository.CancelGame(request.GameId);

                if (!ended)
                {
                    return(new CancelGameResult(GameErrors.GameCouldNotBeCancelled));
                }

                var checkpoint = await gameCheckpointUpdater.Update(request.GameId, nameof(SkipRound));

                transaction.CommitTransaction();

                return(new CancelGameResult());
            }
        }
Example #5
0
        public async Task <CreateGameResult> Handle(CreateGame request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validationResult = await requestValidator.Validate(request);

                if (!validationResult.IsSuccessful)
                {
                    return(new CreateGameResult(validationResult.Error));
                }

                var gameCode = codeGenerator.Generate();

                var game = await CreateGame(request, gameCode);

                var player = await CreatePlayer(game.GameId, request.UserId);

                var createRoundResult = await gameRoundCreator
                                        .CreateFirstRound(game.GameId, player.PlayerId);

                if (!createRoundResult.IsSuccessful)
                {
                    return(new CreateGameResult(createRoundResult.Error));
                }

                var createCardsResult = await playerCardsCreator.CreateCardsForPlayer(game.GameId, player.PlayerId);

                if (!createCardsResult.IsSuccessful)
                {
                    return(new CreateGameResult(createCardsResult.Error));
                }

                var checkpoint = await gameCheckpointUpdater.Update(game.GameId, nameof(CreateGame));

                transaction.CommitTransaction();

                return(new CreateGameResult
                {
                    GameId = game.GameId,
                    PlayerId = player.PlayerId,
                    Code = gameCode,
                    Checkpoint = checkpoint
                });
            }
        }
Example #6
0
        public async Task <SkipRoundResult> Handle(SkipRound request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validatorResult = await validator.Validate(request);

                if (!validatorResult.IsSuccessful)
                {
                    return(new SkipRoundResult(validatorResult.Error));
                }

                var skipped = await gameRoundsRepository
                              .SkipRound(request.GameRoundId);

                if (!skipped)
                {
                    return(new SkipRoundResult(GameErrors.GameRoundCouldNotBeSkipped));
                }

                var createNewRoundResult = await gameRoundCreator
                                           .CreateRound(request.GameId, request.GameRoundId);

                if (!createNewRoundResult.IsSuccessful)
                {
                    return(new SkipRoundResult(createNewRoundResult.Error));
                }

                var createCardsResult = await playerCardsCreator
                                        .CreateCards(request.GameId);

                if (!createCardsResult.IsSuccessful)
                {
                    return(new SkipRoundResult(createCardsResult.Error));
                }

                var checkpoint = await gameCheckpointUpdater.Update(request.GameId, nameof(SkipRound));

                transaction.CommitTransaction();

                return(new SkipRoundResult());
            }
        }
Example #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();
            }
        }
Example #8
0
        public async Task <SelectAnswerResult> Handle(SelectAnswer request, CancellationToken cancellationToken)
        {
            using (var transaction = repositoryTransactionsFactory.CreateTransaction())
            {
                var validationResult = await requestValidator.Validate(request);

                if (!validationResult.IsSuccessful)
                {
                    return(new SelectAnswerResult(validationResult.Error));
                }

                var answerSelected = await playerAnswersRepository.MarkAnswerAsSelected(request.PlayerAnswerId);

                if (!answerSelected)
                {
                    return(new SelectAnswerResult(GameErrors.SelectAnswerFailed));
                }

                var pointsAddedResult = await playerAnswersRepository.AddPointForAnswer(request.PlayerAnswerId, request.GameRoundId);

                if (!pointsAddedResult.IsSuccessful)
                {
                    return(new SelectAnswerResult(GameErrors.CouldNotAddPointToPlayer));
                }

                var updateWinnerResult = await gameRoundsRepository.SelectRoundWinner(request.GameRoundId, pointsAddedResult.PlayerPoints.PlayerId);

                if (!updateWinnerResult)
                {
                    return(new SelectAnswerResult(GameErrors.RoundWinnerSelectionFailed));
                }

                var game = await gamesRepository.GetGameById(request.GameId);

                if (pointsAddedResult.PlayerPoints.Points < game.PointsLimit)
                {
                    var createNewRoundResult = await gameRoundCreator
                                               .CreateRound(request.GameId, request.GameRoundId);

                    if (!createNewRoundResult.IsSuccessful)
                    {
                        return(new SelectAnswerResult(createNewRoundResult.Error));
                    }

                    var createCardsResult = await playerCardsCreator.CreateCards(request.GameId);

                    if (!createCardsResult.IsSuccessful)
                    {
                        return(new SelectAnswerResult(createCardsResult.Error));
                    }
                }
                else
                {
                    var endGameResult = await gamesRepository
                                        .EndGame(request.GameId, pointsAddedResult.PlayerPoints.UserId);

                    if (!endGameResult)
                    {
                        return(new SelectAnswerResult(GameErrors.GameCouldNotBeEnded));
                    }
                }

                var checkpoint = await gameCheckpointUpdater.Update(request.GameId, nameof(SelectAnswer));

                transaction.CommitTransaction();

                return(new SelectAnswerResult());
            }
        }