Example #1
0
        public async Task <ActionResult <GamePlayModel> > GetByIdAsync(int id, CancellationToken cancellationToken)
        {
            var userId   = (await GetUserAsync(cancellationToken: cancellationToken))?.Id;
            var gamePlay = await _gamePlayService.GetGamePlayByIdAsync(id, userId, cancellationToken : cancellationToken);

            var moves = await _moveService.GetMovesAsync(id, cancellationToken : cancellationToken);

            var cardFilter = new CardSearchFilter
            {
                Ids = moves.Select(x => x.CardId).ToArray(),
            };
            var playedCards = await _cardService.GetCardsAsync(cardFilter, cancellationToken : cancellationToken);

            gamePlay.Moves = moves.Select(x => new MoveModel
            {
                CardId = x.CardId,
                GameDeckCardCollectionId = x.GameDeckCardCollectionId,
                Column = x.Column,
                Row    = x.Row,
            }).ToList();

            gamePlay.PlayedCards = Array.AsReadOnly(playedCards.Results);

            return(gamePlay);
        }
Example #2
0
        public async Task <ActionResult <GameViewModel> > GetByIdAsync(int id, CancellationToken cancellationToken)
        {
            var userId = (await GetUserAsync(cancellationToken: cancellationToken))?.Id;
            var game   = await _gameService.GetGameByIdAsync(id, userId, cancellationToken : cancellationToken);

            var moves = await _moveService.GetMovesAsync(id, cancellationToken : cancellationToken);

            var cardFilter = new CardSearchFilter
            {
                Ids = moves.Select(x => x.CardId).ToArray(),
            };
            var playedCards = await _cardService.GetCardsAsync(cardFilter, cancellationToken : cancellationToken);

            var data = new GameDataViewModel
            {
                Columns = game.Columns,
                Moves   = moves.Select(x => new GameMoveViewModel
                {
                    CardId = x.CardId,
                    GameDeckCardCollectionId = x.GameDeckCardCollectionId,
                    Column = x.Column,
                    Row    = x.Row,
                }).ToList(),
                PlayedCards = Array.AsReadOnly(playedCards.Results),
                Rows        = game.Rows,
            };

            var model = new GameViewModel(game)
            {
                Data         = data,
                LastActivity = game.StartTime.AddSeconds(data.Moves.Count()),
            };

            return(model);
        }
Example #3
0
        async Task IHandleWinnerHandler.HandleAsync(int gameId, IEnumerable <int> userIds, CancellationToken cancellationToken)
        {
            var moves = await _moveService.GetMovesAsync(gameId, cancellationToken : cancellationToken);

            var cardIds = moves.Select(x => x.CardId).ToArray();
            var result  = await _cardService.GetCardsAsync(
                new Abstractions.CardSearchFilter
            {
                Ids = cardIds,
            },
                cancellationToken : cancellationToken);

            var cards = result.Results;

            var newMoves = await _moveUserService.PopulateMoveUsersAsync(moves, cards, userIds, cancellationToken : cancellationToken);

            var groupedMoves = newMoves
                               .GroupBy(x => x.UserId)
                               .Select(x =>
                                       new
            {
                UserId = x.Key,
                Count  = x.Count() - (x.Key == userIds.First() ? 1 : 0),
            })
                               .OrderByDescending(x => x.Count)
                               .ToArray()
            ;

            var highestValue = groupedMoves[0].Count;

            var gameUpdate = new GameUpdateData
            {
                EndTime = DateTime.UtcNow,
            };

            if (groupedMoves.Skip(1).Any(x => x.Count == highestValue))
            {
                // draw
            }
            else if (groupedMoves.Skip(1).Any(x => x.Count > highestValue))
            {
                // winner is someone else
                gameUpdate.WinnerUserId = groupedMoves.Skip(1).First(x => x.Count > highestValue).UserId;
            }
            else
            {
                gameUpdate.WinnerUserId = groupedMoves[0].UserId;
            }

            await _gameRepository.UpdateGameAsync(gameId, gameUpdate, cancellationToken : cancellationToken);
        }
Example #4
0
        public async Task <ActionResult <GamePlayModel> > GetByIdAsync(int id, CancellationToken cancellationToken)
        {
            var userId   = (await GetUserAsync(cancellationToken: cancellationToken))?.Id;
            var gamePlay = await _gamePlayService.GetGamePlayByIdAsync(id, userId, cancellationToken : cancellationToken);

            var moves = await _moveService.GetMovesAsync(id, cancellationToken : cancellationToken);

            var cardFilter = new CardSearchFilter
            {
                Ids = moves.Select(x => x.CardId).ToArray(),
            };
            var playedCards = await _cardService.GetCardsAsync(cardFilter, cancellationToken : cancellationToken);

            gamePlay.Moves = await _moveUserService.PopulateMoveUsersAsync(moves, playedCards.Results, gamePlay.Game.UserIds, cancellationToken : cancellationToken);

            gamePlay.PlayedCards = playedCards.Results;

            return(gamePlay);
        }