Beispiel #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
                });
            }
        }