Пример #1
0
        public async Task CanPlaceShipAsync(
            CreateShipRequest request)
        {
            var shipCreated = await _boardService.AddShipAsync(request);

            Assert.IsTrue(shipCreated);
        }
Пример #2
0
        public void CannotPlaceOverlappingShipAsync(
            CreateShipRequest request)
        {
            var ex = Assert.ThrowsAsync <BadRequestException>(() => _boardService.AddShipAsync(request));

            Assert.That(ex.Message, Is.EqualTo("Cannot place ship, invalid position!"));
        }
        public async Task SecondShipCollideWithFirstOne()
        {
            var createShipRequest = new CreateShipRequest
            {
                BoardId = 1,
                StartAt = new Lib.Position {
                    X = 0, Y = 0
                },
                Length      = 5,
                Orientation = Lib.Orientation.Horizontal
            };

            var createShipResponse = await _client.PostAsync($"/api/ships", Helpers.GetHttpRequestContent(createShipRequest));

            createShipResponse.EnsureSuccessStatusCode();

            createShipRequest = new CreateShipRequest
            {
                BoardId = 1,
                StartAt = new Lib.Position {
                    X = 4, Y = 0
                },
                Length      = 5,
                Orientation = Lib.Orientation.Horizontal
            };

            createShipResponse = await _client.PostAsync($"/api/ships", Helpers.GetHttpRequestContent(createShipRequest));

            createShipResponse.StatusCode.Should().Be(HttpStatusCode.BadRequest);
        }
        public async Task MissedTheBattleship()
        {
            var createShipRequest = new CreateShipRequest
            {
                BoardId = 1,
                StartAt = new Lib.Position {
                    X = 0, Y = 1
                },
                Length      = 5,
                Orientation = Lib.Orientation.Horizontal
            };

            var createShipResponse = await _client.PostAsync($"/api/ships", Helpers.GetHttpRequestContent(createShipRequest));

            createShipResponse.EnsureSuccessStatusCode();

            var attackRequest = new AttackRequest()
            {
                AttackAt = new Position()
                {
                    X = 6,
                    Y = 6
                },
                BoardId = 1
            };
            var attackResponse = await _client.PutAsync($"/api/ships/attack", Helpers.GetHttpRequestContent(attackRequest));

            attackResponse.EnsureSuccessStatusCode();
            var attackModel = await Helpers.GetHttpResponseContent <AttackResponse>(attackResponse);

            attackModel.Should().NotBeNull();
            attackModel.IsHit.Should().BeFalse();
        }
Пример #5
0
        public async Task <IActionResult> CreateShip(CreateShipRequest createShipRequest)
        {
            var ship = await _shipService.CreateShip(createShipRequest);

            var shipUri = new Uri($"/ships/{ship.Id}", UriKind.Relative);

            return(Created(shipUri, ship));
        }
Пример #6
0
        public async Task <Ship> CreateShip(CreateShipRequest createShipRequest)
        {
            var          shipId = Guid.NewGuid();
            const string sql    = @"INSERT INTO Ship(Id, Size, Status) VALUES(@id, @size, @status)";

            await _database.ExecuteAsync(sql, new { Id = shipId, createShipRequest.Size, Status = ShipStatus.Active.ToString() });

            return(new Ship
            {
                Id = shipId,
                Size = createShipRequest.Size,
                Status = ShipStatus.Active
            });
        }
Пример #7
0
        public async Task <bool> AddShipAsync(CreateShipRequest request)
        {
            // Add ship to board based on it orientation and size
            var board = await  GetBoardAsync();

            for (var i = 0; i < request.Size; i++)
            {
                try
                {
                    if (board[request.XCoordinate, request.YCoordinate].IsHit || board[request.XCoordinate, request.YCoordinate].Name != "-")
                    {
                        throw new BadRequestException("Cannot place ship, invalid position!");
                    }

                    if (request.XCoordinate < board.GetLength(0) && request.YCoordinate < board.GetLength(1))
                    {
                        board[request.XCoordinate, request.YCoordinate] = new Tile
                        {
                            IsHit = false,
                            Name  = request.ShipName
                        };
                        if (request.Orientation == Orientation.Horizontal)
                        {
                            request.YCoordinate += 1;
                        }
                        else
                        {
                            request.XCoordinate += 1;
                        }
                    }
                }
                catch (Exception)
                {
                    throw new BadRequestException("Cannot place ship, invalid position!");
                }
            }

            // Update cache
            var cacheExpirationOptions =
                new MemoryCacheEntryOptions
            {
                AbsoluteExpiration = DateTime.Now.AddMinutes(60),
                Priority           = CacheItemPriority.Normal
            };

            _memoryCache.Set <Tile[, ]>(CacheKey, board, cacheExpirationOptions);
            return(true);
        }
        public async Task ShipTooBigForTheBoard()
        {
            var createShipRequest = new CreateShipRequest
            {
                BoardId = 1,
                StartAt = new Lib.Position {
                    X = 1, Y = 1
                },
                Length      = 25,
                Orientation = Lib.Orientation.Horizontal
            };

            var createShipResponse = await _client.PostAsync($"/api/ships", Helpers.GetHttpRequestContent(createShipRequest));

            createShipResponse.StatusCode.Should().Be(HttpStatusCode.BadRequest);
        }
        public async Task <IActionResult> CreateShip([FromBody] CreateShipRequest createShipRequest)
        {
            try
            {
                var ship = await _gameProcessorService.CreateShip(createShipRequest.StartPoint, createShipRequest.Direction);

                return(Created(Request.Path, ship));
            }
            catch (InCreatableShipException excp)
            {
                _logger.LogError(excp.Message);
                return(Conflict(excp));
            }
            catch (Exception excp)
            {
                _logger.LogError(excp.Message);
                return(BadRequest(excp));
            }
        }
Пример #10
0
        public async Task <CreateShipResponse> Create([FromBody] CreateShipRequest value)
        {
            var ship = await this.ShipManager.CreateShipAsync(value.Name);

            var response = new CreateShipResponse
            {
                Name            = ship.Name,
                CommandCode     = ship.CommandCode,
                TransponderCode = ship.TransponderCode,
                Health          = ship.Statistics.BaseHealth,
                Location        = new LocationRequestOrResponse
                {
                    X = ship.Location.X,
                    Y = ship.Location.Y,
                    Z = ship.Location.Z
                }
            };

            return(response);
        }
Пример #11
0
 public ActionResult <ShipViewModel> Create([FromBody] CreateShipRequest request)
 {
     try
     {
         var ship = _battleshipProvider.AddShipToBoard(request.BoardId, request.ToBattleship());
         return(Ok(new ShipViewModel(ship)));
     }
     catch (InvalidRequestException iex)
     {
         return(BadRequest(new { error = iex.Message }));
     }
     catch (ShipCollisionException sx)
     {
         return(BadRequest(new { error = sx.Message }));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, new { error = ex.Message }));
     }
 }
        public async Task ShouldCreateAShip()
        {
            var createShipRequest = new CreateShipRequest
            {
                BoardId = 1,
                StartAt = new Lib.Position {
                    X = 0, Y = 0
                },
                Length      = 5,
                Orientation = Lib.Orientation.Horizontal
            };

            var createShipResponse = await _client.PostAsync($"/api/ships", Helpers.GetHttpRequestContent(createShipRequest));

            createShipResponse.EnsureSuccessStatusCode();
            var shipModel = await Helpers.GetHttpResponseContent <ShipViewModel>(createShipResponse);

            shipModel.Should().NotBeNull();
            shipModel.Id.Should().Be(1);
        }
Пример #13
0
        public async Task GivenSize_To_Create_Ship_Must_Create_New_Ship()
        {
            using (var server = new ServerFixture(databasePort: _databaseFixture.Port))
            {
                var createShipRequest = new CreateShipRequest
                {
                    Size = 3
                };
                var response = await server.Client.PostAsJsonAsync("ships", createShipRequest);

                response.StatusCode.Should().Be(HttpStatusCode.Created);

                var shipTask         = response.Content.ReadAsAsync <Ship>();
                var currentShipsTask = server.Assert().GetShips();

                await Task.WhenAll(shipTask, currentShipsTask);

                var ship         = await shipTask;
                var currentShips = await currentShipsTask;
                var currentShip  = currentShips.First();

                ship.Should().BeEquivalentTo(currentShip);
            }
        }
        public async Task <IActionResult> AddShipsAsync([FromBody] CreateShipRequest request)
        {
            var response = await _boardService.AddShipAsync(request);

            return(Ok(response));
        }