Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public async Task PopulateMoveUsersAsync_NullMoves_ThrowsArgumentNullException()
 {
     await Assert.ThrowsAsync <ArgumentNullException>(() => _service.PopulateMoveUsersAsync(null, Enumerable.Empty <CardModel>(), Enumerable.Empty <int>(), default));
 }