From 068701d48e30af7bb821c5bd4102f52ffc7b0815 Mon Sep 17 00:00:00 2001
From: Martin <ms618@hdm-stuttgart.de>
Date: Wed, 10 Jan 2024 23:44:18 +0100
Subject: [PATCH] Update: Parser.java (method for parsing IP now contains
 additional check; method for parsing mapData completely rebuilt to allow
 different map sizes and Tile-Numbers from 0 to 99)

---
 .../DataStorage/Classes/Utilities/Parser.java | 51 +++++++++++--------
 1 file changed, 29 insertions(+), 22 deletions(-)

diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java
index 14f873d3..62994d42 100644
--- a/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java
+++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Classes/Utilities/Parser.java
@@ -88,11 +88,11 @@ public class Parser {
         }
 
         if(address.length() > 15){
-            throw new ParserException("IP-Address too long. Must have length of 15 characters including octet dividers (.) !");
+            throw new ParserException("IP-Address too long. Must have maximum length of 15 characters including octet dividers (.) !");
         }
 
         if(address.length() < 7){
-            throw new ParserException("IP-Address too short. Must have length of 7 characters including octet dividers (.) !");
+            throw new ParserException("IP-Address too short. Must have minimum length of 7 characters including octet dividers (.) !");
         }
 
         if(address.charAt(0) == '.' | address.charAt(address.length()-1) == '.'){
@@ -157,6 +157,14 @@ public class Parser {
         int minMapDataLength = ((mapHeight * mapWidth) - 1) + (mapHeight * mapWidth * minTileDigits);
         int spacesAmount = (mapHeight * mapWidth) - 1;
         int tilesAmount = mapHeight * mapWidth;
+        int walkableTileMinNumber = 0;
+        int walkableTileMaxNumber = 9;
+        int indestructibleTileMinNumber = 10;
+        int indestructibleTileMaxNumber = 19;
+        int destructibleTileMinNumber = 20;
+        int destructibleTileMaxNumber = 29;
+        int minTileNumber = 0;
+        int maxTileNumber = 29;
 
         if(mapData.length() > maxMapDataLength){
             throw new ParserException("Map-Data corrupted - mapData String must not exceed length of " + maxMapDataLength + " chars including spaces for " + mapWidth + "x" + mapHeight + " Tiles map!");
@@ -167,7 +175,7 @@ public class Parser {
         }
 
         if(mapData.charAt(0) == ' ' | mapData.charAt(mapData.length() - 1) == ' '){
-            throw new ParserException("Map-Data corrupted - must not start or end with space!");
+            throw new ParserException("Map-Data corrupted - must not start nor end with space!");
         }
 
         for(int i = 0; mapData.length() > i ; i++){
@@ -206,26 +214,26 @@ public class Parser {
         }
         for(int i = 0; positions.length > i; i++){
             if(positions.length - 1 > i) {
-                if (Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > 29 | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < 0) {
-                    throw new ParserException("Map-Data corrupted - Tile number must be between 0 and 29!");
+                if (Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > maxTileNumber | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < minTileNumber) {
+                    throw new ParserException("Map-Data corrupted - Tile number must be between " + minTileNumber + " and " + maxTileNumber + "!");
                 }
             }
             else{
-                if (Integer.parseInt(mapData.substring(positions[i], mapData.length())) > 29 | Integer.parseInt(mapData.substring(positions[i], mapData.length())) < 0) {
-                    throw new ParserException("Map-Data corrupted - Tile number must be between 0 and 29!");
+                if (Integer.parseInt(mapData.substring(positions[i], mapData.length())) > maxTileNumber | Integer.parseInt(mapData.substring(positions[i], mapData.length())) < minTileNumber) {
+                    throw new ParserException("Map-Data corrupted - Tile number must be between " + minTileNumber + " and " + maxTileNumber + "!");
                 }
             }
         }
 
-        //Alternative code for checking if tile number is between 0 and 29:
+        //Alternative code for checking if tile number is between minTileNumber and maxTileNumber:
 
         /*
         String combinedNumbers = "";
         for(int i = 0; i < mapData.length() - 1; i++){
             if(mapData.charAt(i+1) == ' '){
                 combinedNumbers = combinedNumbers + mapData.charAt(i);
-                if((Integer.parseInt(combinedNumbers) > 29) | (Integer.parseInt(combinedNumbers) < 0)){
-                    throw new ParserException("Map-Data corrupted - Tile number must be between 0 and 29!");
+                if((Integer.parseInt(combinedNumbers) > maxTileNumber) | (Integer.parseInt(combinedNumbers) < minTileNumber)){
+                    throw new ParserException("Map-Data corrupted - Tile number must be between " + minTileNumber + " and " + maxTileNumber + "!");
                 }
                 combinedNumbers = "";
                 if(i < mapData.length() - 1){
@@ -234,8 +242,8 @@ public class Parser {
             }
             else if(i >= mapData.length() - 2){
                 combinedNumbers = combinedNumbers + mapData.charAt(i) + mapData.charAt(i+1);
-                if((Integer.parseInt(combinedNumbers) > 29) | (Integer.parseInt(combinedNumbers) < 0)){
-                    throw new ParserException("Map-Data corrupted - Tile number must be between 0 and 29!");
+                if((Integer.parseInt(combinedNumbers) > maxTileNumber) | (Integer.parseInt(combinedNumbers) < minTileNumber)){
+                    throw new ParserException("Map-Data corrupted - Tile number must be between " + minTileNumber + " and " + maxTileNumber + "!");
                 }
             }
             else{
@@ -245,52 +253,51 @@ public class Parser {
         */
 
         for(int i = 0; i < mapWidth; i++){
-            if(Integer.parseInt(mapData.substring(positions[i], positions[i+1] - 1)) > 19 | Integer.parseInt(mapData.substring(positions[i], positions[i+1] - 1)) < 10){
+            if(Integer.parseInt(mapData.substring(positions[i], positions[i+1] - 1)) > indestructibleTileMaxNumber | Integer.parseInt(mapData.substring(positions[i], positions[i+1] - 1)) < indestructibleTileMinNumber){
                 throw new ParserException("Map-Data corrupted - Top-Line must be border tiles!");
             }
         }
 
         for(int i = positions.length - mapWidth; i < positions.length; i++){
             if(positions.length - 1 > i) {
-                if (Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > 19 | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < 10) {
+                if (Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > indestructibleTileMaxNumber | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < indestructibleTileMinNumber) {
                     throw new ParserException("Map-Data corrupted - Bottom-Line must be border tiles!");
                 }
             }
             else{
-                if (Integer.parseInt(mapData.substring(positions[i], mapData.length())) > 19 | Integer.parseInt(mapData.substring(positions[i], mapData.length())) < 10) {
+                if (Integer.parseInt(mapData.substring(positions[i], mapData.length())) > indestructibleTileMaxNumber | Integer.parseInt(mapData.substring(positions[i], mapData.length())) < indestructibleTileMinNumber) {
                     throw new ParserException("Map-Data corrupted - Bottom-Line must be border tiles!");
                 }
             }
         }
 
         for(int i = mapWidth; i < positions.length - mapWidth; i = i + mapWidth){
-            if(i % mapWidth == 0 && Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > 19 | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < 10){
+            if(i % mapWidth == 0 && Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > indestructibleTileMaxNumber | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < indestructibleTileMinNumber){
                 throw new ParserException("Map-Data corrupted - Left Edge must be border tiles!");
             }
         }
 
         for(int i = mapWidth - 1; i < positions.length - mapWidth; i = i + mapWidth){
-            if((i + 1) % mapWidth == 0 && Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > 19 | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < 10){
+            if((i + 1) % mapWidth == 0 && Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) > indestructibleTileMaxNumber | Integer.parseInt(mapData.substring(positions[i], positions[i + 1] - 1)) < indestructibleTileMinNumber){
                 throw new ParserException("Map-Data corrupted - Right Edge must be border tiles!");
             }
         }
 
-        if(Integer.parseInt(mapData.substring(positions[mapWidth + 1], positions[mapWidth + 2] - 1)) < 0 | Integer.parseInt(mapData.substring(positions[mapWidth + 1], positions[mapWidth + 2] - 1)) > 9){
+        if(Integer.parseInt(mapData.substring(positions[mapWidth + 1], positions[mapWidth + 2] - 1)) < walkableTileMinNumber | Integer.parseInt(mapData.substring(positions[mapWidth + 1], positions[mapWidth + 2] - 1)) > walkableTileMaxNumber){
             throw new ParserException("Map-Data corrupted - Player spawn top left must use walkable tile!");
         }
 
-        if(Integer.parseInt(mapData.substring(positions[(mapWidth*2) - 2], positions[(mapWidth*2) - 1] - 1)) < 0 | Integer.parseInt(mapData.substring(positions[(mapWidth*2) - 2], positions[(mapWidth*2) - 1] - 1)) > 9){
+        if(Integer.parseInt(mapData.substring(positions[(mapWidth*2) - 2], positions[(mapWidth*2) - 1] - 1)) < walkableTileMinNumber | Integer.parseInt(mapData.substring(positions[(mapWidth*2) - 2], positions[(mapWidth*2) - 1] - 1)) > walkableTileMaxNumber){
             throw new ParserException("Map-Data corrupted - Player spawn top right must use walkable tile!");
         }
 
-        if(Integer.parseInt(mapData.substring(positions[positions.length - (mapWidth*2) + 1], positions[positions.length - (mapWidth*2) + 2] - 1)) < 0 | Integer.parseInt(mapData.substring(positions[positions.length - (mapWidth*2) + 1], positions[positions.length - (mapWidth*2) + 2] - 1)) > 9) {
+        if(Integer.parseInt(mapData.substring(positions[positions.length - (mapWidth*2) + 1], positions[positions.length - (mapWidth*2) + 2] - 1)) < walkableTileMinNumber | Integer.parseInt(mapData.substring(positions[positions.length - (mapWidth*2) + 1], positions[positions.length - (mapWidth*2) + 2] - 1)) > walkableTileMaxNumber) {
             throw new ParserException("Map-Data corrupted - Player spawn bottom left must use walkable tile!");
         }
 
-        if(Integer.parseInt(mapData.substring(positions[positions.length - mapWidth - 2], positions[positions.length - mapWidth - 1] - 1)) < 0 | Integer.parseInt(mapData.substring(positions[positions.length - mapWidth - 2], positions[positions.length - mapWidth - 1] - 1)) > 9){
+        if(Integer.parseInt(mapData.substring(positions[positions.length - mapWidth - 2], positions[positions.length - mapWidth - 1] - 1)) < walkableTileMinNumber | Integer.parseInt(mapData.substring(positions[positions.length - mapWidth - 2], positions[positions.length - mapWidth - 1] - 1)) > walkableTileMaxNumber){
             throw new ParserException("Map-Data corrupted - Player spawn bottom right must use walkable tile!");
         }
-
     }
 
 }
-- 
GitLab