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, }; 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 <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 <GameModel> IGameValidator.ValidateGameForMoveAsync(int id, int userId, CancellationToken cancellationToken) { var games = await _gameDataService.GetGamesAsync( new GameSearchFilter { GameId = id, }, userId, cancellationToken : cancellationToken ); var game = games.Results.FirstOrDefault(); if (game == null) { throw new InvalidGameException($"Game { id } does not exist."); } var gameUserId = game.UserIds.SingleOrDefault(x => x == userId); if (gameUserId == default) { throw new InvalidPlayerException(); } if (!game.CurrentUserId.HasValue) { throw new InvalidMoveException($"Game { game.Id } has not started."); } if (game.CurrentUserId.Value != gameUserId) { throw new InvalidTurnException("It is not your turn."); } return(game); }