コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        // 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));
        }