Пример #1
0
        private async Task <DeckModel> GetDeckByIdInternalAsync(int id, int userId, bool includeCards, CancellationToken cancellationToken)
        {
            var deck = await _deckRepository.GetDeckByIdAsync(id, userId, cancellationToken : cancellationToken);

            var model = deck == null ? null : _deckDataMapper.Map(deck);

            if (includeCards && model != null)
            {
                var cardIds = deck.Cards.Select(x => x.CardId).ToArray();

                var cardResults = await _cardRepository.FindCardsAsync(
                    new Data.Abstractions.CardSearchFilter
                {
                    Ids    = cardIds,
                    UserId = deck.UserId,
                },
                    cancellationToken : cancellationToken
                    );

                var cards = cardResults.Results;

                model.Cards = model.Cards.Select(x => new DeckCardModel
                {
                    Card             = cards.FirstOrDefault(c => c.Id == x.Card.Id),
                    CardCollectionId = x.CardCollectionId,
                });
            }

            return(model);
        }
Пример #2
0
        async Task IAddUserToGameHandler.HandleAsync(int id, int userId, int deckId, CancellationToken cancellationToken)
        {
            var game = await _gameRepository.GetGameByIdAsync(id, cancellationToken : cancellationToken);

            if (game == null)
            {
                throw new InvalidGameException($"Game { id } does not exist.");
            }

            var gameUsers = await _gameRepository.GetGameUsersAsync(id, cancellationToken : cancellationToken);

            if (gameUsers.Any(x => x.Id == userId))
            {
                throw new InvalidPlayerException($"User { userId } is already in game { id }.");
            }

            var gul = gameUsers.Length;

            if (gul >= game.MaxPlayers)
            {
                throw new InvalidPlayerException($"Game { id } is already filled.");
            }

            var deck = await _deckRepository.GetDeckByIdAsync(deckId, userId, cancellationToken : cancellationToken);

            if (deck == null)
            {
                throw new InvalidDeckException($"Deck { deckId } does not exist.");
            }

            if (deck.UserId != userId)
            {
                throw new InvalidDeckException($"Deck { deckId } does not belong to user { userId }.");
            }

            var dc  = deck.Cards.Select(x => x.CardId).ToArray();
            var dcc = dc.Length;

            if (dcc < deck.MaxCards)
            {
                throw new InvalidDeckException($"Deck { deckId } needs { deck.MaxCards } cards. Currently only has { dcc }.");
            }

            await _gameDeckRepository.AddGameDeckAsync(id, userId, deck.Name, deck.Description, dc, cancellationToken : cancellationToken);

            if (gul + 1 == game.MaxPlayers)
            {
                var allUsers = gameUsers.Select(x => x.Id).Concat(new int[] { userId }).ToArray();
                await PrepareGameForPlayAsync(id, allUsers, cancellationToken);
            }
        }
Пример #3
0
        private async Task AddUserToGameInternalAsync(int id, int userId, int deckId, CancellationToken cancellationToken = default)
        {
            var game = await _gameRepository.GetGameByIdAsync(id);

            if (game == null)
            {
                throw new InvalidGameException($"Game { id } does not exist.");
            }

            var gameUsers = await _gameRepository.GetGameUsersAsync(id, cancellationToken : cancellationToken);

            if (gameUsers.Any(x => x.Id == userId))
            {
                throw new InvalidPlayerException($"User { userId } is already in game { id }.");
            }

            var gul = gameUsers.Length;

            if (gul >= game.MaxPlayers)
            {
                throw new InvalidPlayerException($"Game { id } is already filled.");
            }

            var deck = await _deckRepository.GetDeckByIdAsync(deckId, userId, cancellationToken : cancellationToken);

            if (deck == null)
            {
                throw new InvalidDeckException($"Deck { deckId } does not exist.");
            }

            if (deck.UserId != userId)
            {
                throw new InvalidDeckException($"Deck { deckId } does not belong to user { userId }.");
            }

            var dc  = deck.Cards.Select(x => x.CardId).ToArray();
            var dcc = dc.Length;

            if (dcc < deck.MaxCards)
            {
                throw new InvalidDeckException($"Deck { deckId } needs { deck.MaxCards } cards. Currently only has { dcc }.");
            }

            await _gameDeckRepository.AddGameDeckAsync(id, userId, deck.Name, deck.Description, dc, cancellationToken : cancellationToken);

            if (gul + 1 == game.MaxPlayers)
            {
                var allUsers       = gameUsers.Select(x => x.Id).Concat(new int[] { userId }).ToArray();
                var currentUserIdx = new Random().Next(0, allUsers.Length);
                var currentUserId  = allUsers[currentUserIdx];
                var updateGame     = new GameUpdateData
                {
                    CurrentUserId = currentUserId,
                };

                await _gameRepository.UpdateGameAsync(id, updateGame, cancellationToken : cancellationToken);

                var newTurn = new TurnData
                {
                    CurrentUserId = currentUserId,
                    GameId        = game.Id,
                };
                await _turnRepository.AddTurnAsync(newTurn, cancellationToken : cancellationToken);
            }
        }