async Task <GamePlayModel> IGamePlayService.GetGamePlayByIdAsync(int id, int?userId, CancellationToken cancellationToken) { var filter = new Abstractions.GameSearchFilter { GameId = id, }; var game = (await _gameDataService.GetGamesAsync(filter, userId: userId, cancellationToken: cancellationToken)).Results.SingleOrDefault(); if (game == null) { throw new InvalidGameException($"Game { id } does not exist."); } var gamePlay = new GamePlayModel { Game = game, }; if (userId.HasValue) { await _gameDataService.PopulateGameUsersAsync(game, cancellationToken : cancellationToken); var uid = game.UserIds.SingleOrDefault(x => x == userId.Value); if (uid != default) { var gameDeck = await _gameDeckRepository.GetGameDeckByGameAndUserIdAsync(id, uid, cancellationToken : cancellationToken); var deckCards = await _gameDeckRepository.GetGameDeckCardCollectionAsync(gameDeck.Id, cancellationToken : cancellationToken); gamePlay.GameDeckId = gameDeck.Id; gamePlay.GameDeck = _gameDeckMapper.Map(gameDeck); gamePlay.GameDeck.CardCollection = deckCards.Select(_gameDeckCardCollectionMapper.Map).ToArray(); //TODO: Replace with data layer var cardFilter = new Abstractions.CardSearchFilter { Ids = deckCards.Select(x => x.CardId).ToArray(), }; var cards = await _cardService.GetCardsAsync(cardFilter, cancellationToken : cancellationToken); foreach (var cc in gamePlay.GameDeck.CardCollection) { cc.Card = cards.Results.SingleOrDefault(x => x.Id == cc.CardId); } } } return(gamePlay); }
async Task <GameModel> IGameService.GetGameByIdAsync(int id, int?userId, CancellationToken cancellationToken) { var filter = new Abstractions.GameSearchFilter { GameId = id, }; var game = (await _gameDataService.GetGamesAsync(filter, userId: userId, cancellationToken: cancellationToken)).Results.SingleOrDefault(); if (game == null) { throw new InvalidGameException($"Game { id } does not exist."); } if (userId.HasValue) { await _gameDataService.PopulateGameUsersAsync(game, cancellationToken : cancellationToken); } return(game); }
async Task <GamePlayModel> IGamePlayService.GetGamePlayByIdAsync(int id, int?userId, CancellationToken cancellationToken) { var filter = new Abstractions.GameSearchFilter { GameId = id, }; var game = (await _gameDataService.GetGamesAsync(filter, userId: userId, cancellationToken: cancellationToken)).Results.SingleOrDefault(); if (game == null) { throw new InvalidGameException($"Game { id } does not exist."); } var gamePlay = new GamePlayModel { Game = game, }; await _gameDeckHelper.PopulateDeckAsync(userId, game, gamePlay, cancellationToken : cancellationToken); return(gamePlay); }
async Task <Abstractions.SearchResult <GameModel> > IGameDataService.GetGamesAsync(Abstractions.GameSearchFilter filter, int?userId, CancellationToken cancellationToken) { var result = await _gameRepository.FindGamesAsync( new Data.Abstractions.GameSearchFilter { GameId = filter.GameId, Type = filter.Type, }, cancellationToken : cancellationToken ); var results = new Abstractions.SearchResult <GameModel> { Count = result.TotalCount, Results = result.Results.Select(_gameMapper.Map).ToArray(), }; if (userId.HasValue) { foreach (var game in results.Results) { //TODO: Fix loop to no make multiple calls await PopulateGameUsersInternalAsync(game, cancellationToken : cancellationToken); } } return(results); }
Task <Abstractions.SearchResult <GameModel> > IGameService.GetGamesAsync(Abstractions.GameSearchFilter filter, int?userId, CancellationToken cancellationToken) { return(_gameDataService.GetGamesAsync(filter, userId: userId, cancellationToken: cancellationToken)); }