added logic for server- and clientside verification of JSON-files
This commit is contained in:
@@ -16,8 +16,11 @@
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.System.Logger.Level;
|
import java.lang.System.Logger.Level;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static pp.battleship.Resources.lookup;
|
import static pp.battleship.Resources.lookup;
|
||||||
|
import static pp.battleship.game.client.ClientGameLogic.LOGGER;
|
||||||
import static pp.battleship.model.Battleship.Status.INVALID_PREVIEW;
|
import static pp.battleship.model.Battleship.Status.INVALID_PREVIEW;
|
||||||
import static pp.battleship.model.Battleship.Status.NORMAL;
|
import static pp.battleship.model.Battleship.Status.NORMAL;
|
||||||
import static pp.battleship.model.Battleship.Status.VALID_PREVIEW;
|
import static pp.battleship.model.Battleship.Status.VALID_PREVIEW;
|
||||||
@@ -56,7 +59,7 @@ public boolean showEditor() {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void movePreview(IntPoint pos) {
|
public void movePreview(IntPoint pos) {
|
||||||
ClientGameLogic.LOGGER.log(Level.DEBUG, "move preview to {0}", pos); //NON-NLS
|
LOGGER.log(Level.DEBUG, "move preview to {0}", pos); //NON-NLS
|
||||||
if (preview == null || !ownMap().isValid(pos)) return;
|
if (preview == null || !ownMap().isValid(pos)) return;
|
||||||
preview.moveTo(pos);
|
preview.moveTo(pos);
|
||||||
setPreviewStatus(preview);
|
setPreviewStatus(preview);
|
||||||
@@ -71,7 +74,7 @@ public void movePreview(IntPoint pos) {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void clickOwnMap(IntPoint pos) {
|
public void clickOwnMap(IntPoint pos) {
|
||||||
ClientGameLogic.LOGGER.log(Level.DEBUG, "click at {0} in own map", pos); //NON-NLS
|
LOGGER.log(Level.DEBUG, "click at {0} in own map", pos); //NON-NLS
|
||||||
if (!ownMap().isValid(pos)) return;
|
if (!ownMap().isValid(pos)) return;
|
||||||
if (preview == null)
|
if (preview == null)
|
||||||
modifyShip(pos);
|
modifyShip(pos);
|
||||||
@@ -125,7 +128,7 @@ private void placeShip(IntPoint cursor) {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void clickHarbor(IntPoint pos) {
|
public void clickHarbor(IntPoint pos) {
|
||||||
ClientGameLogic.LOGGER.log(Level.DEBUG, "click at {0} in harbor", pos); //NON-NLS
|
LOGGER.log(Level.DEBUG, "click at {0} in harbor", pos); //NON-NLS
|
||||||
if (!harbor().isValid(pos)) return;
|
if (!harbor().isValid(pos)) return;
|
||||||
final Battleship shipAtCursor = harbor().findShipAt(pos);
|
final Battleship shipAtCursor = harbor().findShipAt(pos);
|
||||||
if (preview != null) {
|
if (preview != null) {
|
||||||
@@ -152,7 +155,7 @@ else if (shipAtCursor != null) {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void rotateShip() {
|
public void rotateShip() {
|
||||||
ClientGameLogic.LOGGER.log(Level.DEBUG, "pushed rotate"); //NON-NLS
|
LOGGER.log(Level.DEBUG, "pushed rotate"); //NON-NLS
|
||||||
if (preview == null) return;
|
if (preview == null) return;
|
||||||
preview.rotated();
|
preview.rotated();
|
||||||
ownMap().remove(preview);
|
ownMap().remove(preview);
|
||||||
@@ -238,6 +241,9 @@ public void loadMap(File file) throws IOException {
|
|||||||
final ShipMapDTO dto = ShipMapDTO.loadFrom(file);
|
final ShipMapDTO dto = ShipMapDTO.loadFrom(file);
|
||||||
if (!dto.fits(logic.getDetails()))
|
if (!dto.fits(logic.getDetails()))
|
||||||
throw new IOException(lookup("map.doesnt.fit"));
|
throw new IOException(lookup("map.doesnt.fit"));
|
||||||
|
else if (!checkMapToLoad(dto)) {
|
||||||
|
throw new IOException(lookup("ships.dont.fit.the.map"));
|
||||||
|
}
|
||||||
ownMap().clear();
|
ownMap().clear();
|
||||||
dto.getShips().forEach(ownMap()::add);
|
dto.getShips().forEach(ownMap()::add);
|
||||||
harbor().clear();
|
harbor().clear();
|
||||||
@@ -245,6 +251,33 @@ public void loadMap(File file) throws IOException {
|
|||||||
selectedInHarbor = null;
|
selectedInHarbor = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkMapToLoad(ShipMapDTO dto) {
|
||||||
|
int mapWidth = dto.getWidth();
|
||||||
|
int mapHeight = dto.getHeight();
|
||||||
|
|
||||||
|
// check if ship is out of bounds
|
||||||
|
for (int i = 0; i < dto.getShips().size(); i++) {
|
||||||
|
Battleship battleship = dto.getShips().get(i);
|
||||||
|
if (battleship.getMaxX() >= mapWidth || battleship.getMinX() < 0 || battleship.getMaxY() >= mapHeight || battleship.getMinY() < 0) {
|
||||||
|
LOGGER.log(Level.ERROR, "Ship is out of bounds ({0})", battleship.toString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if ships overlap
|
||||||
|
List<Battleship> ships = dto.getShips();
|
||||||
|
for(Battleship ship:ships) {
|
||||||
|
for (Battleship compareShip : ships) {
|
||||||
|
if (!(ship == compareShip)) {
|
||||||
|
if (ship.collidesWith(compareShip)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the player's own map may be loaded from a file.
|
* Checks if the player's own map may be loaded from a file.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -142,9 +142,47 @@ public Player addPlayer(int id) {
|
|||||||
public void received(MapMessage msg, int from) {
|
public void received(MapMessage msg, int from) {
|
||||||
if (state != ServerState.SET_UP)
|
if (state != ServerState.SET_UP)
|
||||||
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
|
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
|
||||||
|
else if(!checkMap(msg, from))
|
||||||
|
LOGGER.log(Level.ERROR, "The submitted map is not allowed");
|
||||||
else
|
else
|
||||||
playerReady(getPlayerById(from), msg.getShips());
|
playerReady(getPlayerById(from), msg.getShips());
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Returns true if the map contains correct ship placement and is of the correct size
|
||||||
|
*
|
||||||
|
* @param msg the received MapMessage of the player
|
||||||
|
* @param from the ID of the Player
|
||||||
|
* @return a boolean based on if the transmitted map ist correct
|
||||||
|
*/
|
||||||
|
|
||||||
|
private boolean checkMap(MapMessage msg, int from) {
|
||||||
|
int mapWidth = getPlayerById(from).getMap().getWidth();
|
||||||
|
int mapHeight = getPlayerById(from).getMap().getHeight();
|
||||||
|
|
||||||
|
if (mapHeight != 10 || mapWidth != 10)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// check if ship is out of bounds
|
||||||
|
for (Battleship ship : msg.getShips()){
|
||||||
|
if (ship.getMaxX() >= mapWidth || ship.getMinX() < 0 || ship.getMaxY() >= mapHeight || ship.getMinY() < 0) {
|
||||||
|
LOGGER.log(Level.ERROR, "Ship is out of bounds ({0})", ship.toString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if ships overlap
|
||||||
|
List<Battleship> ships = msg.getShips();
|
||||||
|
for(Battleship ship:ships){
|
||||||
|
for(Battleship compareShip:ships){
|
||||||
|
if(!(ship==compareShip)){
|
||||||
|
if(ship.collidesWith(compareShip)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the reception of a ShootMessage.
|
* Handles the reception of a ShootMessage.
|
||||||
@@ -160,7 +198,7 @@ public void received(ShootMessage msg, int from) {
|
|||||||
shoot(getPlayerById(from), msg.getPosition());
|
shoot(getPlayerById(from), msg.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks the player as ready and sets their ships.
|
* Marks the player as ready and sets their ships.
|
||||||
* Transitions the state to PLAY if both players are ready.
|
* Transitions the state to PLAY if both players are ready.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -114,4 +114,18 @@ public static ShipMapDTO loadFrom(File file) throws IOException {
|
|||||||
throw new IOException(e.getLocalizedMessage());
|
throw new IOException(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the width of the DTO
|
||||||
|
* @return with of DTO
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int getWidth() {return width;}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the height of the DTO
|
||||||
|
* @return height of DTO
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int getHeight(){return height;}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user