Add clientside and serverside map check

This commit is contained in:
Felix Koppe
2024-10-06 19:33:30 +02:00
parent 1f548f5ee5
commit 95fdd86100
3 changed files with 46 additions and 4 deletions

View File

@@ -2,7 +2,7 @@
<configuration default="false" name="Projekte [test]" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalProjectPath" value="$PROJECT_DIR$/Projekte" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="--continue" />
<option name="taskDescriptions">

View File

@@ -140,10 +140,20 @@ public Player addPlayer(int id) {
*/
@Override
public void received(MapMessage msg, int from) {
Player player = getPlayerById(from);
List<Battleship> ships = msg.getShips();
if (state != ServerState.SET_UP)
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
else
playerReady(getPlayerById(from), msg.getShips());
else {
if (!checkShips(player, ships)) {
LOGGER.log(Level.ERROR, "{0} submited an illegal map", player); //NON-NLS
send(player, new GameDetails(config));
}
else {
playerReady(player, msg.getShips());
}
}
}
/**
@@ -173,6 +183,7 @@ void playerReady(Player player, List<Battleship> ships) {
return;
}
ships.forEach(player.getMap()::add);
if (readyPlayers.size() == 2) {
for (Player p : players)
send(p, new StartBattleMessage(p == activePlayer));
@@ -180,6 +191,29 @@ void playerReady(Player player, List<Battleship> ships) {
}
}
/**
* Checks if all ships are placed correctly.
*
* @param player the player who is ready
* @param ships the list of ships placed by the player
* @return true if all ships are placed correctly, false otherwise
*/
private boolean checkShips(Player player, List<Battleship> ships) {
for(Battleship ship : ships) {
for(Battleship other : ships) {
if(ship.collidesWith(other)) {
return false;
}
}
if(!player.getMap().isValid(ship)) {
return false;
}
}
return true;
}
/**
* Handles the shooting action by the player.
*

View File

@@ -53,7 +53,7 @@ public ShipMapDTO(ShipMap map) {
* Checks if the current ship map fits the game details provided.
*
* @param details the game details to be matched against
* @return true if the ship map fits the game details, false otherwise
* @return true if the ship map fits the game details and all ships have a valid position, false otherwise
*/
public boolean fits(GameDetails details) {
if (width != details.getWidth() || height != details.getHeight())
@@ -61,6 +61,14 @@ public boolean fits(GameDetails details) {
int numShips = details.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
if (numShips != ships.size())
return false;
ShipMap map = new ShipMap(width, height, null);
for(Battleship ship : map.getShips().toList()) {
if(!map.isValid(ship)) {
return false;
}
}
for (var e : details.getShipNums().entrySet()) {
final int shipLength = e.getKey();
final int requiredNum = e.getValue();