public Result PositionShipOnGrid(Guid gameId, Guid playerId, ShipKind shipKind, GridCoordinate[] segmentCoordinates) { /* Look up game in repository */ IGame retrievedGame = this.gameRepository.GetById(gameId); /* throws DataNotFoundException when not found */ /* Cannot move ships */ if (retrievedGame.IsStarted == true && retrievedGame.Settings.CanMoveUndamagedShipsDuringGame == false) { return(Result.CreateFailureResult("cannot move ship anymore")); } /* Find the player in the game */ IPlayer player = retrievedGame.GetPlayerById(playerId); if (player != null) { /* Player is found - get the fleet */ IFleet fleet = player.Fleet; /* Try to position ship on the grid */ Result result = fleet.TryMoveShipTo(shipKind, segmentCoordinates, player.Grid); return(result); } else { /* Player not found in game */ throw new ApplicationException("unknown player"); } }
public Result PositionShipOnGrid(Guid gameId, Guid playerId, ShipKind shipKind, GridCoordinate[] segmentCoordinates) { var game = _gameRepository.GetById(gameId); var player1 = game.GetPlayerById(playerId); return(player1.Fleet.TryMoveShipTo(shipKind, segmentCoordinates, player1.Grid)); }
public Result PositionShipOnGrid(Guid gameId, Guid playerId, ShipKind shipKind, GridCoordinate[] segmentCoordinates) { IGame game = _gameRepository.GetById(gameId); IPlayer speler = game.GetPlayerById(playerId); return(speler.Fleet.TryMoveShipTo(shipKind, segmentCoordinates, speler.Grid)); }
public GridSquareArrayBuilder(ShipKind kind) { _gridSquareMocks = new Mock<IGridSquare>[kind.Size]; for (int i = 0; i < kind.Size; i++) { _gridSquareMocks[i] = new GridSquareBuilder().BuildMock(); } }
public ShipBuilder(ShipKind kind) { _kind = kind; _shipMock = new Mock <IShip>(); _shipMock.SetupGet(s => s.Squares).Returns(() => null); _shipMock.SetupGet(s => s.Kind).Returns(kind); _shipMock.SetupGet(s => s.HasSunk).Returns(false); _shipMock.Setup(s => s.CanBeFoundAtCoordinate(It.IsAny <GridCoordinate>())).Returns(false); }
private Mock <IShip>[] ReplaceShipsWithMocks() { var shipMocks = new Mock <IShip> [_internalDictionary.Count]; for (var index = 0; index < ShipKind.All.Length; index++) { ShipKind shipKind = ShipKind.All[index]; shipMocks[index] = new ShipBuilder(shipKind).WithSquares().BuildMock(); _internalDictionary[shipKind] = shipMocks[index].Object; } return(shipMocks); }
private IShip ArrangeShip(ShipKind kind, bool hasSunk, bool isPositioned = true) { var shipMock = new Mock <IShip>(); shipMock.SetupGet(s => s.HasSunk).Returns(hasSunk); shipMock.SetupGet(s => s.Kind).Returns(kind); if (isPositioned) { shipMock.SetupGet(s => s.Squares).Returns(BuildSquares(kind.Size)); } return(shipMock.Object); }
// Token: 0x06000122 RID: 290 RVA: 0x0000A7F0 File Offset: 0x000089F0 public void ConfigureShipTexture(ShipKind spaceshipKind, AirStrikeDef strikeDef = null) { switch (spaceshipKind) { /* * case SpaceshipKind.CargoPeriodic: * case SpaceshipKind.CargoRequested: * case SpaceshipKind.Damaged: * this.spaceshipTexture = FlyingSpaceship.supplySpaceshipTexture; * this.spaceshipShadowTexture = FlyingSpaceship.supplySpaceshipShadowTexture; * this.baseSpaceshipScale = FlyingSpaceship.supplySpaceshipScale; * break; * case SpaceshipKind.DispatcherDrop: * case SpaceshipKind.DispatcherPick: * this.spaceshipTexture = FlyingSpaceship.dispatcherTexture; * this.spaceshipShadowTexture = FlyingSpaceship.supplySpaceshipShadowTexture; * this.baseSpaceshipScale = FlyingSpaceship.supplySpaceshipScale; * break; * case SpaceshipKind.Medical: * this.spaceshipTexture = FlyingSpaceship.medicalSpaceshipTexture; * this.spaceshipShadowTexture = FlyingSpaceship.medicalSpaceshipShadowTexture; * this.baseSpaceshipScale = FlyingSpaceship.medicalSpaceshipScale; * break; */ case ShipKind.Airstrike: this.spaceshipTexture = FlyingShip.strikeshipTexture; this.spaceshipShadowTexture = FlyingShip.strikeshipShadowTexture; this.baseSpaceshipScale = FlyingShip.supplySpaceshipScale; break; default: Log.ErrorOnce("Adeptus Airstrike: unhandled ShipKind (" + this.spaceshipKind.ToString() + ").", 123456784); break; } if (strikeDef != null && spaceshipKind == ShipKind.Airstrike) { if (strikeDef.graphicData != null) { this.spaceshipTexture = strikeDef.graphicData.GraphicColoredFor(this).MatSingle; } if (strikeDef.shadowData != null) { this.spaceshipShadowTexture = strikeDef.shadowData.GraphicColoredFor(this).MatSingle; } if (strikeDef.scale != default(Vector2)) { this.spaceshipScale = new Vector3(strikeDef.scale.x, 1f, strikeDef.scale.y); this.spaceshipShadowScale = new Vector3(strikeDef.scale.x, 1f, strikeDef.scale.y); } } }
private void RegisterSunkenShip(ShipKind sunkenShipKind) { var shipBuilder = new ShipBuilder(sunkenShipKind).WithSquares(GridSquareStatus.Hit); var ship = shipBuilder.Build(); for (var index = 0; index < ship.Squares.Length - 1; index++) { IGridSquare shipSquare = ship.Squares[index]; _strategy.RegisterShotResult(shipSquare.Coordinate, ShotResult.CreateHit(ship, true)); } shipBuilder.WithHasSunk(true); _strategy.RegisterShotResult(ship.Squares[ship.Kind.Size - 1].Coordinate, ShotResult.CreateHit(ship, true)); }
private void AssertRandomCoordinateGeneration(ShipKind kind, int gridSize, out GridCoordinate[] generatedCoordinates) { //Act generatedCoordinates = kind.GenerateRandomSegmentCoordinates(gridSize); //Assert Assert.That(generatedCoordinates.Length, Is.EqualTo(kind.Size), $"The number of coordinates must be equal to the size of the shipKind ({kind.Name} has size {kind.Size})."); Assert.That(generatedCoordinates.HasAnyOutOfBounds(gridSize), Is.False, $"{generatedCoordinates.Print()} is out of bounds for grid with size {gridSize}."); Assert.That(generatedCoordinates.AreLinked(), Is.True, $"{generatedCoordinates.Print()} has two or more coordinates that are not linked."); Assert.That(generatedCoordinates.AreAligned(), Is.True, $"{generatedCoordinates.Print()} has two or more coordinates that are not aligned."); }
public ShipPositioningModelBuilder() { ShipKind kind = ShipKind.All.NextRandomElement(); _model = new ShipPositioningModel { ShipCode = kind.Code, SegmentCoordinates = new GridCoordinateModel[kind.Size] }; for (int i = 0; i < kind.Size; i++) { _model.SegmentCoordinates[i] = new GridCoordinateModelBuilder().Build(); } }
public void PositionShipOnGrid_RetrievesTheGameFromTheRepositoryThenGetsThePlayerAndUsesTheFleetOfThePlayerToPositionTheShip() { //Arrange Guid userId = Guid.NewGuid(); Guid gameId = Guid.NewGuid(); ShipKind shipKind = ShipKind.All.NextRandomElement(); GridCoordinate[] segmentCoordinates = { new GridCoordinateBuilder().Build(), new GridCoordinateBuilder().Build() }; var gridMock = new Mock <IGrid>(); IGrid grid = gridMock.Object; Result expectedResult = Result.CreateSuccessResult(); var fleetMock = new Mock <IFleet>(); fleetMock.Setup(f => f.TryMoveShipTo(shipKind, segmentCoordinates, grid)).Returns(expectedResult); IFleet fleet = fleetMock.Object; var playerMock = new Mock <IPlayer>(); IPlayer player = playerMock.Object; playerMock.SetupGet(p => p.Fleet).Returns(fleet); playerMock.SetupGet(p => p.Grid).Returns(grid); var existingGameMock = new Mock <IGame>(); existingGameMock.Setup(g => g.GetPlayerById(userId)).Returns(player); IGame existingGame = existingGameMock.Object; _gameRepositoryMock.Setup(repo => repo.GetById(gameId)).Returns(existingGame); //Act var result = _service.PositionShipOnGrid(gameId, userId, shipKind, segmentCoordinates); //Assert Assert.That(result, Is.SameAs(expectedResult), "The Result returned should be an instance created by the TryMoveShipTo method of the Fleet of the Player."); _gameRepositoryMock.Verify(); existingGameMock.Verify(); fleetMock.Verify(); }
public void PositionShipOnGrid_ShouldNotCatchDataNotFoundExceptions() { //Arrange Guid userId = Guid.NewGuid(); Guid gameId = Guid.NewGuid(); ShipKind shipKind = ShipKind.All.NextRandomElement(); GridCoordinate[] segmentCoordinates = { new GridCoordinateBuilder().Build(), new GridCoordinateBuilder().Build() }; _gameRepositoryMock.Setup(repo => repo.GetById(It.IsAny <Guid>())).Throws <DataNotFoundException>(); //Act Assert.That(() => _service.PositionShipOnGrid(gameId, userId, shipKind, segmentCoordinates), Throws.InstanceOf <DataNotFoundException>()); }
private void RegisterSunkenShip(ShipKind sunkenShipKind, GridCoordinate[] coordinates) { // Get ship squares IGridSquare[] squares = coordinates.Select(c => { GridSquare square = new GridSquare(c); square.Status = GridSquareStatus.Hit; return(square); }).ToArray(); var shipBuilder = new ShipBuilder(sunkenShipKind).WithSquares(squares); var ship = shipBuilder.Build(); for (var index = 0; index < ship.Squares.Length - 1; index++) { IGridSquare shipSquare = ship.Squares[index]; _strategy.RegisterShotResult(shipSquare.Coordinate, ShotResult.CreateHit(ship, true)); } shipBuilder.WithHasSunk(true); _strategy.RegisterShotResult(ship.Squares[ship.Kind.Size - 1].Coordinate, ShotResult.CreateHit(ship, true)); }
public async Task <IActionResult> PositionShipOnGrid(Guid id, ShipPositioningModel model) { var currentUser = await _userManager.GetUserAsync(User); try { GridCoordinate[] segmentCoordinates = model.SegmentCoordinates.Select(cm => new GridCoordinate(cm.Row, cm.Column)).ToArray(); ShipKind shipKind = ShipKind.CreateFromCode(model.ShipCode); Result result = _gameService.PositionShipOnGrid(id, currentUser.Id, shipKind, segmentCoordinates); return(Ok(result)); } catch (DataNotFoundException) { ModelState.AddModelError("gameNotFound", $"Could not find a game with id {id}"); } catch (ApplicationException applicationException) { ModelState.AddModelError("applicationException", applicationException.Message); } return(BadRequest(ModelState)); }
public void PositionShipOnGrid_ShouldNotCatchApplicationExceptions() { //Arrange Guid userId = Guid.NewGuid(); Guid gameId = Guid.NewGuid(); ShipKind shipKind = ShipKind.All.NextRandomElement(); GridCoordinate[] segmentCoordinates = { new GridCoordinateBuilder().Build(), new GridCoordinateBuilder().Build() }; var gridMock = new Mock <IGrid>(); IGrid grid = gridMock.Object; var fleetMock = new Mock <IFleet>(); fleetMock.Setup(f => f.TryMoveShipTo(It.IsAny <ShipKind>(), It.IsAny <GridCoordinate[]>(), It.IsAny <IGrid>())).Throws <ApplicationException>(); IFleet fleet = fleetMock.Object; var playerMock = new Mock <IPlayer>(); IPlayer player = playerMock.Object; playerMock.SetupGet(p => p.Fleet).Returns(fleet); playerMock.SetupGet(p => p.Grid).Returns(grid); var existingGameMock = new Mock <IGame>(); existingGameMock.Setup(g => g.GetPlayerById(userId)).Returns(player); IGame existingGame = existingGameMock.Object; _gameRepositoryMock.Setup(repo => repo.GetById(gameId)).Returns(existingGame); //Act Assert.That(() => _service.PositionShipOnGrid(gameId, userId, shipKind, segmentCoordinates), Throws.InstanceOf <ApplicationException>()); }
public void Setup() { _kind = ShipKind.All.NextRandomElement(); _ship = new Ship(_kind); }
public Result PositionShipOnGrid(Guid gameId, Guid playerId, ShipKind shipKind, GridCoordinate[] segmentCoordinates) { throw new NotImplementedException("PositionShipOnGrid method of GameService class is not implemented"); }
public void PositionShipOnGrid_RetrievesTheGameFromTheRepositoryThenGetsThePlayerAndUsesTheFleetOfThePlayerToPositionTheShip() { //Arrange Guid userId = Guid.NewGuid(); Guid gameId = Guid.NewGuid(); ShipKind shipKind = ShipKind.All.NextRandomElement(); GridCoordinate[] segmentCoordinates = { new GridCoordinateBuilder().Build(), new GridCoordinateBuilder().Build() }; var gridMock = new Mock <IGrid>(); IGrid grid = gridMock.Object; Result expectedResult = Result.CreateSuccessResult(); var fleetMock = new Mock <IFleet>(); fleetMock.Setup(f => f.TryMoveShipTo(It.IsAny <ShipKind>(), It.IsAny <GridCoordinate[]>(), It.IsAny <IGrid>())) .Returns(expectedResult); IFleet fleet = fleetMock.Object; var playerMock = new Mock <IPlayer>(); IPlayer player = playerMock.Object; playerMock.SetupGet(p => p.Fleet).Returns(fleet); playerMock.SetupGet(p => p.Grid).Returns(grid); var existingGameMock = new Mock <IGame>(); existingGameMock.Setup(g => g.GetPlayerById(It.IsAny <Guid>())).Returns(player); IGame existingGame = existingGameMock.Object; _gameRepositoryMock.Setup(repo => repo.GetById(It.IsAny <Guid>())).Returns(existingGame); //Act var result = _service.PositionShipOnGrid(gameId, userId, shipKind, segmentCoordinates); //Assert Assert.That(result, Is.SameAs(expectedResult), "The Result returned should be an instance created by the TryMoveShipTo method of the Fleet of the Player."); _gameRepositoryMock.Verify(repo => repo.GetById(gameId), Times.Once, "The 'GetById' method of the IGameRepository is not called correctly."); existingGameMock.Verify(g => g.GetPlayerById(userId), Times.Once, "The 'GetPlayerById' method of the game returned by the IGameRepository is not called correctly. " + "The userId should be provided."); playerMock.VerifyGet(p => p.Fleet, Times.AtLeastOnce, "The Fleet property of the player returned by the 'GetPlayerById' method of the game, should be used. " + "On this Fleet property the 'TryMoveShipTo' method can be called."); playerMock.VerifyGet(p => p.Grid, Times.AtLeastOnce, "The Grid property of the player returned by the 'GetPlayerById' method of the game, " + "should be used to pass as an argument to the 'TryMoveShipTo' of the Fleet of the player."); fleetMock.Verify(f => f.TryMoveShipTo(shipKind, segmentCoordinates, grid), Times.Once, "The 'TryMoveShipTo' method of the Fleet of the player returned by the 'GetPlayerById' method of the game, is not called correctly."); }