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 ShotResultModel CreateShotResult(ShipDomainModel ship, GameStatsDomainModel stats) { AssertExtensions.NotNull(ship, nameof(ship)); AssertExtensions.NotNull(stats, nameof(stats)); return(new ShotResultModel(ship.Health == 0, true, stats.IsEnded)); }
public void MaxHealthTest(int x1, int y1, int x2, int y2, int maxHealth) { ShipDomainModel model = new ShipDomainModel ( id: 1, start: new Point(x1, y1), end: new Point(x2, y2), shots: Array.Empty <ShotDomainModel>() ); Assert.Equal(maxHealth, model.MaxHealth); }
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()); }
public void ParseShipsCoordinates_CorrectData_ReturnCorrectResults(string text, int x1, int y1, int x2, int y2) { CoordinatesParser parser = Create(); IReadOnlyCollection <ShipDomainModel> results = parser.ParseShipsCoordinates(text); Assert.NotNull(results); Assert.Single(results); ShipDomainModel p = results.First(); Assert.Equal(Math.Min(x1, x2), p.Start.X); Assert.Equal(Math.Min(y1, y2), p.Start.Y); Assert.Equal(Math.Max(x1, x2), p.End.X); Assert.Equal(Math.Max(y1, y2), p.End.Y); }
public void HealthTest() { var shots = Enumerable .Range(0, 5) .Select(n => new ShotDomainModel(new Point(n, n))) .ToArray(); ShipDomainModel model = new ShipDomainModel ( id: 1, start: new Point(0, 0), end: new Point(4, 4), shots: shots ); Assert.Equal(20, model.Health); }
// 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)); }