浏览代码

Economy is getting loaded from the file

Jan 6 月之前
父节点
当前提交
9226ffcaa7

+ 2 - 0
src/main/java/main/Main.java

@@ -3,6 +3,7 @@ package main;
 import controller.GameController;
 import model.GameSaver;
 import util.Translator;
+import util.economy.EconomyInfo;
 import view.GamePanel;
 
 import javax.swing.*;
@@ -13,6 +14,7 @@ public class Main {
     public static void main(String[] args) throws IOException {
 
         Translator.load("lang/de_de.json");
+        EconomyInfo.load();
 
         GameController gameController = GameSaver.loadGame();
 

+ 39 - 0
src/main/java/util/economy/EconomyData.java

@@ -0,0 +1,39 @@
+package util.economy;
+
+import model.items.ITEM_NAME;
+
+import java.util.Map;
+
+public class EconomyData {
+    private final Map<Integer, Map<ITEM_NAME, Integer>> levelCosts;
+    private final int maxLevel;
+    private final Map<Integer, Integer> lighthouseLevels;
+    private final Map<Integer, Integer> productionTimePerLevel;
+
+    public EconomyData(Map<Integer, Map<ITEM_NAME, Integer>> costs, int maxLevel, Map<Integer, Integer> lighthouseLevels, Map<Integer, Integer> productionTimePerLevel) {
+        this.levelCosts = costs;
+        this.maxLevel = maxLevel;
+        this.lighthouseLevels = lighthouseLevels;
+        this.productionTimePerLevel = productionTimePerLevel;
+    }
+
+    public Map<Integer, Map<ITEM_NAME, Integer>> getCosts() {
+        return levelCosts;
+    }
+
+    public int getMaxLevel() {
+        return maxLevel;
+    }
+
+    public Map<Integer, Integer> getRequiredLighthouseLevels() {
+        return lighthouseLevels;
+    }
+
+    public Map<Integer, Integer> getProductionTimePerLevel() {
+        return productionTimePerLevel;
+    }
+
+    public int getProductionTime(int level) {
+        return productionTimePerLevel.getOrDefault(level, -1);
+    }
+}

+ 221 - 0
src/main/java/util/economy/EconomyInfo.java

@@ -0,0 +1,221 @@
+package util.economy;
+
+import model.items.ITEM_NAME;
+import model.tiles.InteractiveTileType;
+
+import java.io.*;
+import java.util.*;
+
+public class EconomyInfo {
+    private static final String FILE_PATH = "/economy/economy.json";
+    private static final Map<InteractiveTileType, EconomyData> economyHashmap = new HashMap<>();
+
+    public static void load() {
+        economyHashmap.clear();
+        try (InputStream is = EconomyInfo.class.getResourceAsStream(FILE_PATH);
+             BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
+
+            StringBuilder jsonBuilder = new StringBuilder();
+            String line;
+            while ((line = reader.readLine()) != null) {
+                jsonBuilder.append(line);
+            }
+
+            String json = jsonBuilder.toString().trim();
+            if (json.startsWith("{") && json.endsWith("}")) {
+                json = json.substring(1, json.length() - 1);
+            }
+
+            parseTopLevel(json);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void parseTopLevel(String json) {
+        int index = 0;
+        while (index < json.length()) {
+            int keyStart = json.indexOf('"', index);
+            if (keyStart == -1) break;
+
+            int keyEnd = json.indexOf('"', keyStart + 1);
+            String typeName = json.substring(keyStart + 1, keyEnd);
+
+            int dataStart = json.indexOf('{', keyEnd);
+            if (dataStart == -1) break;
+
+            int depth = 1;
+            int dataEnd = dataStart + 1;
+            while (dataEnd < json.length() && depth > 0) {
+                if (json.charAt(dataEnd) == '{') depth++;
+                else if (json.charAt(dataEnd) == '}') depth--;
+                dataEnd++;
+            }
+
+            if (depth != 0) break;
+
+            String dataJson = json.substring(dataStart, dataEnd);
+            parseTileData(InteractiveTileType.valueOf(typeName), dataJson);
+
+            index = dataEnd;
+        }
+    }
+
+    private static void parseTileData(InteractiveTileType tileType, String json) {
+        int maxLevel = extractIntValue(json, "maxLevel");
+        Map<Integer, Map<ITEM_NAME, Integer>> costs = extractNestedMap(json, "costs");
+        Map<Integer, Integer> lighthouseLevels = extractFlatMap(json, "lighthouseLevels");
+        Map<Integer, Integer> productionTimes = extractFlatMap(json, "productionTimeInSec");
+
+        economyHashmap.put(tileType, new EconomyData(
+                costs,
+                maxLevel,
+                lighthouseLevels,
+                productionTimes
+        ));
+    }
+
+    private static int extractIntValue(String json, String key) {
+        int keyIndex = json.indexOf("\"" + key + "\":");
+        if (keyIndex == -1) return -1;
+
+        int valueStart = json.indexOf(':', keyIndex) + 1;
+        int valueEnd = findValueEnd(json, valueStart);
+        String value = json.substring(valueStart, valueEnd).trim();
+
+        try {
+            return Integer.parseInt(value);
+        } catch (NumberFormatException e) {
+            return -1;
+        }
+    }
+
+    private static Map<Integer, Map<ITEM_NAME, Integer>> extractNestedMap(String json, String key) {
+        Map<Integer, Map<ITEM_NAME, Integer>> result = new HashMap<>();
+        String mapJson = extractSubJson(json, key);
+        if (mapJson == null) return result;
+
+        int index = 0;
+        while (index < mapJson.length()) {
+            int levelStart = mapJson.indexOf('"', index);
+            if (levelStart == -1) break;
+
+            int levelEnd = mapJson.indexOf('"', levelStart + 1);
+            int level = Integer.parseInt(mapJson.substring(levelStart + 1, levelEnd));
+
+            int innerStart = mapJson.indexOf('{', levelEnd);
+            if (innerStart == -1) break;
+
+            int innerDepth = 1;
+            int innerEnd = innerStart + 1;
+            while (innerEnd < mapJson.length() && innerDepth > 0) {
+                if (mapJson.charAt(innerEnd) == '{') innerDepth++;
+                else if (mapJson.charAt(innerEnd) == '}') innerDepth--;
+                innerEnd++;
+            }
+
+            if (innerDepth != 0) break;
+
+            String innerJson = mapJson.substring(innerStart + 1, innerEnd - 1);
+            Map<ITEM_NAME, Integer> innerMap = parseInnerMap(innerJson);
+            result.put(level, innerMap);
+
+            index = innerEnd;
+        }
+
+        return result;
+    }
+
+    private static Map<ITEM_NAME, Integer> parseInnerMap(String innerJson) {
+        Map<ITEM_NAME, Integer> map = new HashMap<>();
+        int index = 0;
+
+        while (index < innerJson.length()) {
+            int keyStart = innerJson.indexOf('"', index);
+            if (keyStart == -1) break;
+
+            int keyEnd = innerJson.indexOf('"', keyStart + 1);
+            String itemName = innerJson.substring(keyStart + 1, keyEnd);
+
+            int valueStart = innerJson.indexOf(':', keyEnd) + 1;
+            int valueEnd = findValueEnd(innerJson, valueStart);
+            String valueStr = innerJson.substring(valueStart, valueEnd).trim();
+
+            try {
+                ITEM_NAME item = ITEM_NAME.valueOf(itemName);
+                int amount = Integer.parseInt(valueStr);
+                map.put(item, amount);
+            } catch (IllegalArgumentException ignored) {
+            }
+
+            index = valueEnd + 1;
+        }
+
+        return map;
+    }
+
+    private static Map<Integer, Integer> extractFlatMap(String json, String key) {
+        Map<Integer, Integer> result = new HashMap<>();
+        String mapJson = extractSubJson(json, key);
+        if (mapJson == null) return result;
+
+        int index = 0;
+        while (index < mapJson.length()) {
+            int keyStart = mapJson.indexOf('"', index);
+            if (keyStart == -1) break;
+
+            int keyEnd = mapJson.indexOf('"', keyStart + 1);
+            int level = Integer.parseInt(mapJson.substring(keyStart + 1, keyEnd));
+
+            int valueStart = mapJson.indexOf(':', keyEnd) + 1;
+            int valueEnd = findValueEnd(mapJson, valueStart);
+            String valueStr = mapJson.substring(valueStart, valueEnd).trim();
+
+            try {
+                int value = Integer.parseInt(valueStr);
+                result.put(level, value);
+            } catch (NumberFormatException ignored) {
+            }
+
+            index = valueEnd + 1;
+        }
+
+        return result;
+    }
+
+    private static String extractSubJson(String json, String key) {
+        String searchKey = "\"" + key + "\":";
+        int keyIndex = json.indexOf(searchKey);
+        if (keyIndex == -1) return null;
+
+        int start = json.indexOf('{', keyIndex + searchKey.length());
+        if (start == -1) return null;
+
+        int depth = 1;
+        int end = start + 1;
+        while (end < json.length() && depth > 0) {
+            if (json.charAt(end) == '{') depth++;
+            else if (json.charAt(end) == '}') depth--;
+            end++;
+        }
+
+        if (depth != 0) return null;
+        return json.substring(start, end);
+    }
+
+    private static int findValueEnd(String json, int start) {
+        int end = start;
+        while (end < json.length()) {
+            char c = json.charAt(end);
+            if (c == ',' || c == '}' || c == ']') break;
+            end++;
+        }
+        return end;
+    }
+
+    public static EconomyData infoOf(InteractiveTileType type) {
+        return economyHashmap.get(type);
+    }
+}
+

+ 48 - 0
src/main/resources/economy/economy.json

@@ -0,0 +1,48 @@
+{
+  "IRON_MINE": {
+    "maxLevel": 10,
+    "costs": {
+      "1": { "wood": 20 },
+      "2": { "stone": 5, "wood": 10 },
+      "3": { "stone": 50, "wood": 50 },
+      "4": { "stone": 500, "wood": 100 }
+    },
+    "lighthouseLevels": {
+      "1": 1,
+      "2": 4
+    },
+    "maxAmountPerLighthouseLevel": {
+      "1": 1,
+      "2": 2,
+      "3": 4
+    },
+    "productionTimeInSec": {
+      "1": 30,
+      "2": 28,
+      "3": 25,
+      "4": 20
+    }
+  },
+  "SAWMILL": {
+    "maxLevel": 5,
+    "costs": {
+      "1": { "wood": 1 },
+      "2": { "stone": 54, "wood": 40 }
+    },
+    "lighthouseLevels": {
+      "1": 1,
+      "2": 3
+    },
+    "maxAmountPerLighthouseLevel": {
+      "1": 1,
+      "2": 2,
+      "3": 4
+    },
+    "productionTimeInSec": {
+      "1": 30,
+      "2": 28,
+      "3": 25,
+      "4": 20
+    }
+  }
+}

+ 44 - 0
src/test/java/EconomyInfoTest.java

@@ -0,0 +1,44 @@
+import model.items.ITEM_NAME;
+import model.tiles.InteractiveTileType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import util.economy.EconomyData;
+import util.economy.EconomyInfo;
+
+import java.util.Map;
+
+
+public class EconomyInfoTest {
+    @Test
+    public void isReadingCorrectly(){
+        EconomyInfo.load();
+
+        EconomyData data = EconomyInfo.infoOf(InteractiveTileType.IRON_MINE);
+
+        Map<Integer, Map<ITEM_NAME, Integer>> costs = data.getCosts();
+
+        Map<ITEM_NAME, Integer> levelCosts2 = costs.get(2);
+
+        int max = data.getMaxLevel();
+
+        Integer stoneAmount = levelCosts2.get(ITEM_NAME.stone);
+        Integer woodAmount = levelCosts2.get(ITEM_NAME.wood);
+
+        Integer maxAmountLvl2 = data.getRequiredLighthouseLevels().get(1);
+        Integer maxAmountLvl3 = data.getRequiredLighthouseLevels().get(2);
+
+        Assertions.assertEquals(30, data.getProductionTime(1));
+        Assertions.assertEquals(28, data.getProductionTime(2));
+        Assertions.assertEquals(25, data.getProductionTime(3));
+        Assertions.assertEquals(20, data.getProductionTime(4));
+
+        Assertions.assertEquals(5, stoneAmount);
+        Assertions.assertEquals(10, woodAmount);
+
+        Assertions.assertEquals(10, max);
+
+        Assertions.assertEquals(1, maxAmountLvl2);
+
+        Assertions.assertEquals(4, maxAmountLvl3);
+    }
+}

+ 48 - 0
src/test/resources/economy/economy.json

@@ -0,0 +1,48 @@
+{
+  "IRON_MINE": {
+    "maxLevel": 10,
+    "costs": {
+      "1": { "wood": 20 },
+      "2": { "stone": 5, "wood": 10 },
+      "3": { "stone": 50, "wood": 50 },
+      "4": { "stone": 500, "wood": 100 }
+    },
+    "lighthouseLevels": {
+      "1": 1,
+      "2": 4
+    },
+    "maxAmountPerLighthouseLevel": {
+      "1": 1,
+      "2": 2,
+      "3": 4
+    },
+    "productionTimeInSec": {
+      "1": 30,
+      "2": 28,
+      "3": 25,
+      "4": 20
+    }
+  },
+  "SAWMILL": {
+    "maxLevel": 5,
+    "costs": {
+      "1": { "wood": 1 },
+      "2": { "stone": 54, "wood": 40 }
+    },
+    "lighthouseLevels": {
+      "1": 1,
+      "2": 3
+    },
+    "maxAmountPerLighthouseLevel": {
+      "1": 1,
+      "2": 2,
+      "3": 4
+    },
+    "productionTimeInSec": {
+      "1": 30,
+      "2": 28,
+      "3": 25,
+      "4": 20
+    }
+  }
+}