Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void loadConfig(File configFile) {
Cache.mercenaryDefaultBreachRefund = config.getDouble("mercenary-default-breach-refund", 500.0);
net.tfminecraft.simplefactions.guild.hub.SupplyHubService.loadConfig(config);
net.tfminecraft.simplefactions.guild.hub.HubTransport.loadConfig(config);
Cache.infrastructureFull = config.getDouble("infrastructure.full", 20);
double infrastructureFull = config.getDouble("infrastructure.full", 20);
Cache.infrastructureFull = Double.isFinite(infrastructureFull) && infrastructureFull > 0
? infrastructureFull : 20;
Cache.infrastructureTarget = config.getDouble("infrastructure.target", 0.75);
Cache.infrastructureWildernessSpread = config.getDouble("infrastructure.wilderness-spread", 0.25);
double infrastructureSpreadFloor = config.getDouble("infrastructure.spread-floor", 0.5);
Expand Down
73 changes: 44 additions & 29 deletions src/main/java/net/tfminecraft/simplefactions/map/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.google.gson.Gson;
Expand All @@ -15,15 +16,17 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import net.tfminecraft.simplefactions.Cache;
import net.tfminecraft.simplefactions.SimpleFactions;
import net.tfminecraft.simplefactions.guild.Guild;
import net.tfminecraft.simplefactions.managers.FactionManager;
import net.tfminecraft.simplefactions.managers.ProvinceManager;
import net.tfminecraft.simplefactions.managers.TitleManager;
import net.tfminecraft.simplefactions.managers.WarManager;
import net.tfminecraft.simplefactions.map.export.OccupationMapExport;
import net.tfminecraft.simplefactions.map.infra.EffectiveTerrain;
import net.tfminecraft.simplefactions.map.provinces.Province;
import net.tfminecraft.simplefactions.map.provinces.ProvinceDataEntry;
import net.tfminecraft.simplefactions.SimpleFactions;

public class Compiler {
public void exportQueue(HashMap<String, List<String>> queues) {
Expand Down Expand Up @@ -71,6 +74,45 @@ private static double r2(double v) {
return Math.round(v * 100.0) / 100.0;
}

static JsonObject provinceToJson(Province p, String occupierId) {
JsonObject o = new JsonObject();

o.addProperty("id", p.getId());
o.addProperty("prosperity", p.getProsperity());
if (occupierId != null) {
o.addProperty("occupied_by", occupierId);
}

// trade data
JsonObject trade = new JsonObject();
for (Map.Entry<String, ProvinceDataEntry> e : p.getAllData().entrySet()) {
ProvinceDataEntry d = e.getValue();
if (d.getTrade() < 0.1) continue;

JsonObject g = new JsonObject();
g.addProperty("trade", r2(d.getTrade()));
g.addProperty("production", r2(d.getProduction()));
trade.add(e.getKey(), g);
}
o.add("trade", trade);

if (!p.isSea()) {
double terrain = p.getTradeCarry();
double infrastructure = p.getInfrastructure();
o.addProperty("terrain", p.getTerrain().name().toLowerCase(Locale.ROOT));
o.addProperty("terrain_value", terrain);
if (infrastructure > 0 && Double.isFinite(Cache.infrastructureFull) && Cache.infrastructureFull > 0) {
o.addProperty("infrastructure", r2(infrastructure));
o.addProperty("infrastructure_fill", r2(Math.max(0, Math.min(1,
infrastructure / Cache.infrastructureFull))));
}
o.addProperty("effective_terrain", r2(EffectiveTerrain.calculate(terrain, infrastructure,
Cache.infrastructureFull, Cache.infrastructureTarget, 1)));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return o;
}

public void exportProvincesToJson(File out) throws Exception {
JsonArray arr = new JsonArray();

Expand All @@ -80,34 +122,7 @@ public void exportProvincesToJson(File out) throws Exception {
TitleManager::getByProvince);

for (Province p : pm.getProvinces()) {
JsonObject o = new JsonObject();

o.addProperty("id", p.getId());
o.addProperty("prosperity", p.getProsperity());
String occupierId = occupiedBy.get(p.getId());
if (occupierId != null) {
o.addProperty("occupied_by", occupierId);
}

// trade data
JsonObject trade = new JsonObject();
double totalTrade = 0;

for (Map.Entry<String, ProvinceDataEntry> e : p.getAllData().entrySet()) {
ProvinceDataEntry d = e.getValue();
if (d.getTrade() < 0.1) continue;

JsonObject g = new JsonObject();
g.addProperty("trade", r2(d.getTrade()));
g.addProperty("production", r2(d.getProduction()));

trade.add(e.getKey(), g);
totalTrade += d.getTrade();
}

o.add("trade", trade);

arr.add(o);
arr.add(provinceToJson(p, occupiedBy.get(p.getId())));
}

try (FileWriter w = new FileWriter(out)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ void configuredKeysAreReadIntoCache() throws IOException {
assertValues(30, 0.80, 0.10, 0.25, 12, 8, 4);
}

@Test
void zeroFullUsesDefault() throws IOException {
load("infrastructure:\n full: 0\n");

assertEquals(20, Cache.infrastructureFull, 1e-9);
}

@Test
void negativeFullUsesDefault() throws IOException {
load("infrastructure:\n full: -5\n");

assertEquals(20, Cache.infrastructureFull, 1e-9);
}

@Test
void zeroSpreadFloorUsesDefault() throws IOException {
load("infrastructure:\n spread-floor: 0\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package net.tfminecraft.simplefactions.map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.google.gson.JsonObject;

import net.tfminecraft.simplefactions.Cache;
import net.tfminecraft.simplefactions.enums.Terrain;
import net.tfminecraft.simplefactions.guild.Guild;
import net.tfminecraft.simplefactions.map.provinces.Province;
import net.tfminecraft.simplefactions.map.provinces.ProvinceDataEntry;

class CompilerProvinceExportTest {
private final Map<Terrain, Double> previousCarry = new HashMap<>(Cache.tradeCarry);
private final double previousFull = Cache.infrastructureFull;
private final double previousTarget = Cache.infrastructureTarget;

@BeforeEach
void setUp() {
Cache.infrastructureFull = 20;
Cache.infrastructureTarget = 0.75;
Cache.tradeCarry.clear();
Cache.tradeCarry.put(Terrain.BOG, 0.40);
Cache.tradeCarry.put(Terrain.PLAINS, 0.75);
Cache.tradeCarry.put(Terrain.SEA, 0.60);
Cache.tradeCarry.put(Terrain.WATER, 0.75);
}

@AfterEach
void restore() {
Cache.infrastructureFull = previousFull;
Cache.infrastructureTarget = previousTarget;
Cache.tradeCarry.clear();
Cache.tradeCarry.putAll(previousCarry);
}

@Test
void bogWithInfrastructureExportsFillAndEffectiveTerrain() {
Province province = new Province(1, "bog", 0);
province.setInfrastructure(12);

JsonObject json = Compiler.provinceToJson(province, null);

assertEquals("bog", json.get("terrain").getAsString());
assertEquals(0.40, json.get("terrain_value").getAsDouble());
assertEquals(12, json.get("infrastructure").getAsDouble());
assertEquals(0.60, json.get("infrastructure_fill").getAsDouble());
assertEquals(0.61, json.get("effective_terrain").getAsDouble());
}

@Test
void terrainValueIsNotRounded() {
Cache.tradeCarry.put(Terrain.BOG, 0.445);

JsonObject json = Compiler.provinceToJson(new Province(1, "bog", 0), null);

assertEquals(0.445, json.get("terrain_value").getAsDouble());
}

@Test
void invalidInfrastructureFullOmitsInfrastructureFields() {
Cache.infrastructureFull = -5;
Province province = new Province(1, "bog", 0);
province.setInfrastructure(12);

JsonObject json = Compiler.provinceToJson(province, null);

assertFalse(json.has("infrastructure"));
assertFalse(json.has("infrastructure_fill"));
}

@Test
void bogWithoutInfrastructureOmitsInfrastructureFields() {
JsonObject json = Compiler.provinceToJson(new Province(1, "bog", 0), null);

assertEquals(0.40, json.get("effective_terrain").getAsDouble());
assertFalse(json.has("infrastructure"));
assertFalse(json.has("infrastructure_fill"));
}

@Test
void infrastructureDoesNotRaisePlainsAboveItsTerrainValue() {
Province province = new Province(1, "plains", 0);
province.setInfrastructure(20);

assertEquals(0.75, Compiler.provinceToJson(province, null).get("effective_terrain").getAsDouble());
}

@Test
void seaAndWaterOmitAllNewTerrainFields() {
for (String terrain : new String[] {"sea", "water"}) {
JsonObject json = Compiler.provinceToJson(new Province(1, terrain, 0), null);
assertFalse(json.has("terrain"));
assertFalse(json.has("terrain_value"));
assertFalse(json.has("infrastructure"));
assertFalse(json.has("infrastructure_fill"));
assertFalse(json.has("effective_terrain"));
}
}

@Test
void tradeObjectAndExistingFieldOrderArePreserved() {
Province province = new Province(1, "bog", 0);
Guild guild = mock(Guild.class);
province.setData("guild-id", new ProvinceDataEntry(guild, 1.234, 0.567));

JsonObject json = Compiler.provinceToJson(province, "occupier");

assertEquals("{\"trade\":1.23,\"production\":0.57}", json.getAsJsonObject("trade")
.getAsJsonObject("guild-id").toString());
assertEquals("id", json.keySet().toArray()[0]);
assertEquals("prosperity", json.keySet().toArray()[1]);
assertEquals("occupied_by", json.keySet().toArray()[2]);
assertEquals("trade", json.keySet().toArray()[3]);
assertTrue(json.has("effective_terrain"));
}
}
Loading