public Task AddShot(GameDomainModel game, ShotDomainModel shot, ShipDomainModel ship = null) { ThrowIfGameNotExists(game); var localShot = new ShotDomainModel(new Point(shot.Point.X, shot.Point.Y)); _shots.AddOrUpdate(game.Id, new List <ShotDomainModel> { localShot }, (id, list) => { list.Add(localShot); return(list); }); if (ship != null) { _ = _ships.AddOrUpdate(game.Id, id => throw GetDefaultException(), (id, list) => { ShipDomainModel oldShip = list.Where(s => s.Id == ship.Id).Single(); ShipDomainModel newShip = oldShip.With(shot); list.Remove(oldShip); list.Add(newShip); return(list); }); } return(Task.CompletedTask); }
public void ParseShotCoordinate_CorrectData_ReturnResult(string text, int x, int y) { CoordinatesParser parser = Create(); ShotDomainModel result = parser.ParseCoordinate(text); Assert.NotNull(result); Assert.Equal(x, result.Point.X); Assert.Equal(y, result.Point.Y); }
public Task AddShot(GameDomainModel game, ShotDomainModel shot, ShipDomainModel ship = null) { ShotEntity shotEntity = new ShotEntity { ShipId = ship?.Id, GameId = game.Id, X = shot.Point.X, Y = shot.Point.Y }; _context.Shots.Add(shotEntity); return(_context.SaveChangesAsync()); }
private async Task ThrowIfShotCanNotAdded(ShotDomainModel shot, GameDomainModel game) { if (!game.Init) { throw new DataValidationException("The game is not ready to play."); } if (game.Ended) { throw new DataValidationException("The game is ended."); } if (!game.CanTakeShot(shot)) { throw new DataValidationException("The shot does not match the parameters of the game."); } ShotDomainModel existShot = await _gameStateRepository.TryGetShot(game, shot.Point.X, shot.Point.Y); if (existShot != null) { throw new DataValidationException("You can not repeat shots."); } }
// TODO UnitOfWork public async Task <ShotResultModel> Shot(ShotModel shotModel) { ShotDomainModel shot = _coordinatesParser.ParseCoordinate(shotModel.Coord); GameDomainModel game = await GetActiveGame(); await ThrowIfShotCanNotAdded(shot, game); ShipDomainModel ship = await TryKnockShip(game, shot); await _gameStateRepository.AddShot(game, shot, ship); GameStatsDomainModel stats = await _gameStateRepository.GetGameStats(game); if (stats.IsEnded) { await _gameStateRepository.EndGame(game); } return(ship is null ? _modelMapper.CreateShotResult(stats) : _modelMapper.CreateShotResult(ship.With(shot), stats)); }
private Task <ShipDomainModel> TryKnockShip(GameDomainModel game, ShotDomainModel shot) { SearchShipCriteria criteria = new SearchShipCriteria(game, shot.Point); return(_gameStateRepository.TryGetShip(criteria)); }