Example #1
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);
            }
        }
Example #2
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);
            }
        }