Ejemplo n.º 1
0
        public async Task <IActionResult> Shot([FromBody] ShotRequest request)
        {
            AssertExtensions.NotNull(request, nameof(request));
            ShotModel       model  = _applicationMapper.Map(request);
            ShotResultModel result = await _seeBattleGameService.Shot(model);

            ShotResponse response = _contractMapper.Map(result);

            return(new JsonResult(response));
        }
        // 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));
        }
Ejemplo n.º 3
0
        public ActionResult <ShotResult> TakeShot(ShotModel shotModel)
        {
            if (shotModel == null)
            {
                return(BadRequest());
            }
            Coordinates coords;

            try
            {
                coords = _coordinatesParser.ParseCellCoords(shotModel.Coordinates, _game.Matrix.Size);
            }
            catch (BadCoordinatesException e)
            {
                return(BadRequest(e.ToString()));
            }
            if (_battleService.CheckCellIsAlive(coords))
            {
                return(BadRequest($"Shot at coordinates: {coords.StringRepresentation} was taken earlier."));
            }
            _statisticsService.IncrementShotsCount();
            return(_battleService.TakeShot(coords));
        }
        public Task <ShotResultModel> Shot(ShotModel shotModel)
        {
            ThrowIfHasErrors(_validationService.Validate(shotModel));

            return(_service.Shot(shotModel));
        }