Skip to content

Commit 14600ad

Browse files
ryanbarlow97claude
andcommitted
Export infrastructure and effective terrain per province for the web map.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2d4af29 commit 14600ad

2 files changed

Lines changed: 148 additions & 29 deletions

File tree

‎src/main/java/net/tfminecraft/simplefactions/map/Compiler.java‎

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
import com.google.gson.JsonObject;
1616
import com.google.gson.JsonParser;
1717

18+
import net.tfminecraft.simplefactions.Cache;
19+
import net.tfminecraft.simplefactions.SimpleFactions;
1820
import net.tfminecraft.simplefactions.guild.Guild;
1921
import net.tfminecraft.simplefactions.managers.FactionManager;
2022
import net.tfminecraft.simplefactions.managers.ProvinceManager;
2123
import net.tfminecraft.simplefactions.managers.TitleManager;
2224
import net.tfminecraft.simplefactions.managers.WarManager;
2325
import net.tfminecraft.simplefactions.map.export.OccupationMapExport;
26+
import net.tfminecraft.simplefactions.map.infra.EffectiveTerrain;
2427
import net.tfminecraft.simplefactions.map.provinces.Province;
2528
import net.tfminecraft.simplefactions.map.provinces.ProvinceDataEntry;
26-
import net.tfminecraft.simplefactions.SimpleFactions;
2729

2830
public class Compiler {
2931
public void exportQueue(HashMap<String, List<String>> queues) {
@@ -71,6 +73,44 @@ private static double r2(double v) {
7173
return Math.round(v * 100.0) / 100.0;
7274
}
7375

76+
static JsonObject provinceToJson(Province p, String occupierId) {
77+
JsonObject o = new JsonObject();
78+
79+
o.addProperty("id", p.getId());
80+
o.addProperty("prosperity", p.getProsperity());
81+
if (occupierId != null) {
82+
o.addProperty("occupied_by", occupierId);
83+
}
84+
85+
// trade data
86+
JsonObject trade = new JsonObject();
87+
for (Map.Entry<String, ProvinceDataEntry> e : p.getAllData().entrySet()) {
88+
ProvinceDataEntry d = e.getValue();
89+
if (d.getTrade() < 0.1) continue;
90+
91+
JsonObject g = new JsonObject();
92+
g.addProperty("trade", r2(d.getTrade()));
93+
g.addProperty("production", r2(d.getProduction()));
94+
trade.add(e.getKey(), g);
95+
}
96+
o.add("trade", trade);
97+
98+
if (!p.isSea()) {
99+
double terrain = p.getTradeCarry();
100+
double infrastructure = p.getInfrastructure();
101+
o.addProperty("terrain", p.getTerrain().name().toLowerCase());
102+
o.addProperty("terrain_value", r2(terrain));
103+
if (infrastructure > 0) {
104+
o.addProperty("infrastructure", r2(infrastructure));
105+
o.addProperty("infrastructure_fill", r2(Math.min(1, infrastructure / Cache.infrastructureFull)));
106+
}
107+
o.addProperty("effective_terrain", r2(EffectiveTerrain.calculate(terrain, infrastructure,
108+
Cache.infrastructureFull, Cache.infrastructureTarget, 1)));
109+
}
110+
111+
return o;
112+
}
113+
74114
public void exportProvincesToJson(File out) throws Exception {
75115
JsonArray arr = new JsonArray();
76116

@@ -80,34 +120,7 @@ public void exportProvincesToJson(File out) throws Exception {
80120
TitleManager::getByProvince);
81121

82122
for (Province p : pm.getProvinces()) {
83-
JsonObject o = new JsonObject();
84-
85-
o.addProperty("id", p.getId());
86-
o.addProperty("prosperity", p.getProsperity());
87-
String occupierId = occupiedBy.get(p.getId());
88-
if (occupierId != null) {
89-
o.addProperty("occupied_by", occupierId);
90-
}
91-
92-
// trade data
93-
JsonObject trade = new JsonObject();
94-
double totalTrade = 0;
95-
96-
for (Map.Entry<String, ProvinceDataEntry> e : p.getAllData().entrySet()) {
97-
ProvinceDataEntry d = e.getValue();
98-
if (d.getTrade() < 0.1) continue;
99-
100-
JsonObject g = new JsonObject();
101-
g.addProperty("trade", r2(d.getTrade()));
102-
g.addProperty("production", r2(d.getProduction()));
103-
104-
trade.add(e.getKey(), g);
105-
totalTrade += d.getTrade();
106-
}
107-
108-
o.add("trade", trade);
109-
110-
arr.add(o);
123+
arr.add(provinceToJson(p, occupiedBy.get(p.getId())));
111124
}
112125

113126
try (FileWriter w = new FileWriter(out)) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package net.tfminecraft.simplefactions.map;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.Mockito.mock;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import com.google.gson.JsonObject;
16+
17+
import net.tfminecraft.simplefactions.Cache;
18+
import net.tfminecraft.simplefactions.enums.Terrain;
19+
import net.tfminecraft.simplefactions.guild.Guild;
20+
import net.tfminecraft.simplefactions.map.provinces.Province;
21+
import net.tfminecraft.simplefactions.map.provinces.ProvinceDataEntry;
22+
23+
class CompilerProvinceExportTest {
24+
private final Map<Terrain, Double> previousCarry = new HashMap<>(Cache.tradeCarry);
25+
private final double previousFull = Cache.infrastructureFull;
26+
private final double previousTarget = Cache.infrastructureTarget;
27+
28+
@BeforeEach
29+
void setUp() {
30+
Cache.infrastructureFull = 20;
31+
Cache.infrastructureTarget = 0.75;
32+
Cache.tradeCarry.clear();
33+
Cache.tradeCarry.put(Terrain.BOG, 0.40);
34+
Cache.tradeCarry.put(Terrain.PLAINS, 0.75);
35+
Cache.tradeCarry.put(Terrain.SEA, 0.60);
36+
Cache.tradeCarry.put(Terrain.WATER, 0.75);
37+
}
38+
39+
@AfterEach
40+
void restore() {
41+
Cache.infrastructureFull = previousFull;
42+
Cache.infrastructureTarget = previousTarget;
43+
Cache.tradeCarry.clear();
44+
Cache.tradeCarry.putAll(previousCarry);
45+
}
46+
47+
@Test
48+
void bogWithInfrastructureExportsFillAndEffectiveTerrain() {
49+
Province province = new Province(1, "bog", 0);
50+
province.setInfrastructure(12);
51+
52+
JsonObject json = Compiler.provinceToJson(province, null);
53+
54+
assertEquals("bog", json.get("terrain").getAsString());
55+
assertEquals(0.40, json.get("terrain_value").getAsDouble());
56+
assertEquals(12, json.get("infrastructure").getAsDouble());
57+
assertEquals(0.60, json.get("infrastructure_fill").getAsDouble());
58+
assertEquals(0.61, json.get("effective_terrain").getAsDouble());
59+
}
60+
61+
@Test
62+
void bogWithoutInfrastructureOmitsInfrastructureFields() {
63+
JsonObject json = Compiler.provinceToJson(new Province(1, "bog", 0), null);
64+
65+
assertEquals(0.40, json.get("effective_terrain").getAsDouble());
66+
assertFalse(json.has("infrastructure"));
67+
assertFalse(json.has("infrastructure_fill"));
68+
}
69+
70+
@Test
71+
void infrastructureDoesNotRaisePlainsAboveItsTerrainValue() {
72+
Province province = new Province(1, "plains", 0);
73+
province.setInfrastructure(20);
74+
75+
assertEquals(0.75, Compiler.provinceToJson(province, null).get("effective_terrain").getAsDouble());
76+
}
77+
78+
@Test
79+
void seaAndWaterOmitAllNewTerrainFields() {
80+
for (String terrain : new String[] {"sea", "water"}) {
81+
JsonObject json = Compiler.provinceToJson(new Province(1, terrain, 0), null);
82+
assertFalse(json.has("terrain"));
83+
assertFalse(json.has("terrain_value"));
84+
assertFalse(json.has("infrastructure"));
85+
assertFalse(json.has("infrastructure_fill"));
86+
assertFalse(json.has("effective_terrain"));
87+
}
88+
}
89+
90+
@Test
91+
void tradeObjectAndExistingFieldOrderArePreserved() {
92+
Province province = new Province(1, "bog", 0);
93+
Guild guild = mock(Guild.class);
94+
province.setData("guild-id", new ProvinceDataEntry(guild, 1.234, 0.567));
95+
96+
JsonObject json = Compiler.provinceToJson(province, "occupier");
97+
98+
assertEquals("{\"trade\":1.23,\"production\":0.57}", json.getAsJsonObject("trade")
99+
.getAsJsonObject("guild-id").toString());
100+
assertEquals("id", json.keySet().toArray()[0]);
101+
assertEquals("prosperity", json.keySet().toArray()[1]);
102+
assertEquals("occupied_by", json.keySet().toArray()[2]);
103+
assertEquals("trade", json.keySet().toArray()[3]);
104+
assertTrue(json.has("effective_terrain"));
105+
}
106+
}

0 commit comments

Comments
 (0)