edited SeaSynchronizer , EditorState, ServerGameLogic
added JavaDocs for the new and adapted functions added a new invalid map for testing purposes fixed implemented the logic to check for invalid maps in the method received in ServerGameLogic
This commit is contained in:
		
							
								
								
									
										66
									
								
								Projekte/battleship/client/maps/map3.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								Projekte/battleship/client/maps/map3.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
{
 | 
			
		||||
  "width": 10,
 | 
			
		||||
  "height": 10,
 | 
			
		||||
  "ships": [
 | 
			
		||||
    {
 | 
			
		||||
      "length": 4,
 | 
			
		||||
      "x": 2,
 | 
			
		||||
      "y": 8,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 3,
 | 
			
		||||
      "x": 2,
 | 
			
		||||
      "y": 5,
 | 
			
		||||
      "rot": "DOWN"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 3,
 | 
			
		||||
      "x": 5,
 | 
			
		||||
      "y": 6,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 2,
 | 
			
		||||
      "x": 4,
 | 
			
		||||
      "y": 4,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 2,
 | 
			
		||||
      "x": 7,
 | 
			
		||||
      "y": 4,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 2,
 | 
			
		||||
      "x": 7,
 | 
			
		||||
      "y": 4,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 1,
 | 
			
		||||
      "x": 6,
 | 
			
		||||
      "y": 2,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 1,
 | 
			
		||||
      "x": 8,
 | 
			
		||||
      "y": 2,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 1,
 | 
			
		||||
      "x": 6,
 | 
			
		||||
      "y": 0,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "length": 1,
 | 
			
		||||
      "x": 8,
 | 
			
		||||
      "y": 0,
 | 
			
		||||
      "rot": "RIGHT"
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
@@ -138,7 +138,7 @@ public Spatial visit(Battleship ship) {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates the appropriate graphical representation of the specified battleship.
 | 
			
		||||
     * The representation is either a detailed model or a simple box based on the length of the ship.
 | 
			
		||||
     * The representation is a detailed model based on the length of the ship.
 | 
			
		||||
     *
 | 
			
		||||
     * @param ship the battleship to be represented
 | 
			
		||||
     * @return the spatial representing the battleship
 | 
			
		||||
@@ -208,6 +208,12 @@ private Spatial createBattleship(Battleship ship) {
 | 
			
		||||
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a detailed 3D model to represent a "Big Ship"
 | 
			
		||||
     * @param ship
 | 
			
		||||
     * @return the spatial representing the "Big Ship" battleship
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createBigShip(Battleship ship) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(DESTROYER);
 | 
			
		||||
 | 
			
		||||
@@ -219,6 +225,11 @@ private Spatial createBigShip(Battleship ship) {
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a detailed 3D model to represent a "Medium Ship"
 | 
			
		||||
     * @param ship
 | 
			
		||||
     * @return the spatial representing the "Medium Ship" battleship
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createMediumShip(Battleship ship) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(FERRY);
 | 
			
		||||
 | 
			
		||||
@@ -230,6 +241,11 @@ private Spatial createMediumShip(Battleship ship) {
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a detailed 3D model to represent a "Small Ship"
 | 
			
		||||
     * @param ship
 | 
			
		||||
     * @return the spatial representing the "Small Ship" battleship
 | 
			
		||||
     */
 | 
			
		||||
    private Spatial createSmallShip(Battleship ship) {
 | 
			
		||||
        final Spatial model = app.getAssetManager().loadModel(SMALL);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -227,7 +227,7 @@ private ShipMap harbor() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Loads a map from the specified file.
 | 
			
		||||
     * Loads a map from the specified file. Also Checks if the map is valid
 | 
			
		||||
     *
 | 
			
		||||
     * @param file the file to load the map from
 | 
			
		||||
     * @throws IOException if the map cannot be loaded
 | 
			
		||||
@@ -267,10 +267,20 @@ public boolean maySaveMap() {
 | 
			
		||||
        return harbor().getItems().isEmpty();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the map is valid in terms of overlapping and if the ship is within the boundries of the map
 | 
			
		||||
     * @param dto DataTransferObject is the loaded json file
 | 
			
		||||
     * @return returns true if the map is valid, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean validMap(ShipMapDTO dto) {
 | 
			
		||||
        return inBoundsClient(dto) && overLapClient(dto);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the Ships overlap on the map
 | 
			
		||||
     * @param dto DataTransferObject is the loaded json file
 | 
			
		||||
     * @return returns true if the ships arent overlapping, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean overLapClient(ShipMapDTO dto) {
 | 
			
		||||
        List<Battleship> battleshipList = dto.getShips();
 | 
			
		||||
 | 
			
		||||
@@ -286,6 +296,11 @@ private boolean overLapClient(ShipMapDTO dto) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the Ship is in the map Boundries
 | 
			
		||||
     * @param dto DataTransferObject is the loaded json file
 | 
			
		||||
     * @return true if the ship is in the maps boundriess, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean inBoundsClient(ShipMapDTO dto) {
 | 
			
		||||
        int widht = dto.getWidth();
 | 
			
		||||
        int height = dto.getHeight();
 | 
			
		||||
 
 | 
			
		||||
@@ -134,7 +134,7 @@ public Player addPlayer(int id) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handles the reception of a MapMessage.
 | 
			
		||||
     * Handles the reception of a MapMessage. Also Checks if the given map is valid or invalid
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg  the received MapMessage
 | 
			
		||||
     * @param from the ID of the sender client
 | 
			
		||||
@@ -143,16 +143,19 @@ public Player addPlayer(int id) {
 | 
			
		||||
    public void received(MapMessage msg, int from) {
 | 
			
		||||
        if (state != ServerState.SET_UP)
 | 
			
		||||
            LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
 | 
			
		||||
        else if(!validMap(msg,from)){
 | 
			
		||||
            LOGGER.log(Level.ERROR, "Map message not valid",state);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
            playerReady(getPlayerById(from), msg.getShips());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if the map is Valid in terms of boundries and overlaps
 | 
			
		||||
     *
 | 
			
		||||
     * *
 | 
			
		||||
     * @param msg the received MapMessage
 | 
			
		||||
     * @param id  the ID of the sender client
 | 
			
		||||
     * @return
 | 
			
		||||
     * @return returns true if the map is valid, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean validMap(MapMessage msg, int id) {
 | 
			
		||||
        return inBounds(msg, id) && overLap(msg);
 | 
			
		||||
@@ -162,7 +165,7 @@ private boolean validMap(MapMessage msg, int id) {
 | 
			
		||||
     * Checks if the Ships overLap
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the received MapMessage
 | 
			
		||||
     * @return
 | 
			
		||||
     * @return returns true if the ships arent overlapping, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean overLap(MapMessage msg) {
 | 
			
		||||
        List<Battleship> battleshipList = msg.getShips();
 | 
			
		||||
@@ -184,7 +187,7 @@ private boolean overLap(MapMessage msg) {
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg the received MapMessage
 | 
			
		||||
     * @param id  the ID of the sender client
 | 
			
		||||
     * @return
 | 
			
		||||
     * @return returns true if the ship is within the maps boundries, false otherwhise
 | 
			
		||||
     */
 | 
			
		||||
    private boolean inBounds(MapMessage msg, int id) {
 | 
			
		||||
        int widht = getPlayerById(id).getMap().getWidth();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user