From e318b02db7761814778d22694364cb8a1cea5725 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sat, 5 Jun 2021 16:47:33 +0200 Subject: [PATCH] feat: implemented proper validation of configs --- .../teamname/marvelous/server/Server.java | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Server.java b/Server/src/main/java/uulm/teamname/marvelous/server/Server.java index 3adfd30..fa87176 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/Server.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Server.java @@ -5,9 +5,11 @@ import com.beust.jcommander.ParameterException; import org.tinylog.Logger; import org.tinylog.configuration.Configuration; import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig; +import uulm.teamname.marvelous.gamelibrary.config.FieldType; import uulm.teamname.marvelous.gamelibrary.config.PartyConfig; import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; import uulm.teamname.marvelous.gamelibrary.json.JSON; +import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility; import uulm.teamname.marvelous.server.args.ServerArgs; import uulm.teamname.marvelous.server.netconnector.MarvelousServer; @@ -124,7 +126,30 @@ public class Server { System.exit(1); } - // TODO: Check whether Scenario Config is valid + Logger.trace("Validating configuration file..."); + + var violations = ValidationUtility.validate(config.get()); + + if (violations.isPresent()) { + Logger.error("Scenario Configuration vas invalid: {}", violations.get()); + System.exit(1); + } + + int grassFields = 0; + + for (FieldType[] row: config.get().scenario) { + for (FieldType type: row) { + if (type == FieldType.GRASS) grassFields++; + } + if (grassFields > 18) break; + } + + if (grassFields <= 18) { + Logger.error( + "Scenario Configuration vas invalid: Only {} grass fields found, which is less than 18" + , grassFields); + System.exit(1); + } Logger.info("Scenario Config file loaded"); @@ -144,8 +169,12 @@ public class Server { System.exit(1); } + var violations = ValidationUtility.validate(config.get()); - // TODO: Check whether Character Config is valid + if (violations.isPresent()) { + Logger.error("Scenario Configuration vas invalid: {}", violations.get()); + System.exit(1); + } Logger.info("Character Config file loaded"); @@ -165,7 +194,13 @@ public class Server { System.exit(1); } - // TODO: Check whether Party Config is valid + + var violations = ValidationUtility.validate(config.get()); + + if (violations.isPresent()) { + Logger.error("Party Configuration vas invalid: {}", violations.get()); + System.exit(1); + } Logger.info("Party Config file loaded");