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 @@ -121,7 +121,6 @@ public void recalculate() {
TradeGraph.refreshIfLive(this);
dropMissingGuilds();
for(Guild g : FactionManager.getAllGuilds()) {
if (!g.hasCapital()) continue;
recalculateGuild(g);
}
for(Guild g : FactionManager.getAllGuilds()) {
Expand Down Expand Up @@ -151,7 +150,7 @@ public void recalculateQuiet(List<Guild> guilds, List<Faction> factions) {
TradeGraph.refreshIfLive(this);
dropExcept(previewGuilds);
for (Guild guild : previewGuilds) {
if (guild != null && guild.hasCapital()) {
if (guild != null) {
recalculateGuild(guild);
}
}
Expand Down Expand Up @@ -179,7 +178,6 @@ public void recalculateForSingleGuild(Guild g, boolean save) {
InstallationAccess.beginRecalculation();
try {
installationAccess = new HashMap<>();
if (!g.hasCapital()) return;
TradeGraph.refreshIfLive(this);
dropMissingGuilds();
recalculateGuild(g);
Expand Down Expand Up @@ -210,7 +208,7 @@ private void recalculateProduction(Guild guild) {
}

private void recalculateGuild(Guild guild) {
// 1) Clear only this guild’s data
// Clear former trade and production even when the guild has lost its capital.
clearGuildData(guild.getId());

// 2) Recalculate trade graph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,12 @@ void disabledIncomeQueriesAndRecalculationRespectWhetherSavingWasRequested() {
assertEquals(0, guild.getTradeBreakdown().getIncome());
fixture.provincesEnabled(true);
Guild noCapital = fixture.guild(home, "landless", "Bob");
source.setData(noCapital.getId(), new ProvinceDataEntry(noCapital, 20, 8));
noCapital.getTradeBreakdown().setIncome(9);
manager.recalculateForSingleGuild(noCapital, true);
assertEquals(40, source.getProsperity());
assertEquals(3.73, source.getProsperity());
assertFalse(source.getAllData().containsKey(noCapital.getId()));
assertEquals(0, noCapital.getTradeBreakdown().getIncome());
manager.clearGuildData(null);
assertEquals(10, source.getStoredGuildTrade(guild));
manager.clearGuildData(guild.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,60 @@ void tearDown() {
Cache.provincesEnabled = provincesWereEnabled;
}

@Test
void losingCapitalClearsTradeAndProductionOnSingleGuildRecalculation() {
recalculate();
assertTrue(trade(1) > 0);
assertTrue(production(1) > 0);
when(guild.hasCapital()).thenReturn(false);
when(guild.getCapital()).thenReturn(-1);

recalculate();

assertEquals(0, trade(1));
assertEquals(0, production(1));
assertTrue(provinces.get(1).getAllData().isEmpty());
}

@Test
void losingCapitalClearsTradeAndProductionOnQuietRecalculation() {
recalculate();
assertTrue(trade(1) > 0);
when(guild.hasCapital()).thenReturn(false);
when(guild.getCapital()).thenReturn(-1);

var breakdown = new net.tfminecraft.simplefactions.guild.income.TradeBreakdown();
breakdown.setIncome(50);
when(guild.getTradeBreakdown()).thenReturn(breakdown);
provinces.recalculateQuiet(List.of(guild), List.of(guild.getFaction()));

assertEquals(0, trade(1));
assertEquals(0, production(1));
assertEquals(50, breakdown.getIncome());
}

@Test
void losingCapitalClearsTradeAndProductionOnFullRecalculation() {
recalculate();
assertTrue(trade(1) > 0);
when(guild.hasCapital()).thenReturn(false);
when(guild.getCapital()).thenReturn(-1);
var breakdown = new net.tfminecraft.simplefactions.guild.income.TradeBreakdown();
breakdown.setTradePower(20);
breakdown.setIncome(50);
when(guild.getTradeBreakdown()).thenReturn(breakdown);
try (MockedStatic<FactionManager> factions = mockStatic(FactionManager.class)) {
factions.when(() -> FactionManager.getGuildByString("guild")).thenReturn(guild);
factions.when(FactionManager::getAllGuilds).thenReturn(List.of(guild));
provinces.recalculate();
}

assertEquals(0, trade(1));
assertEquals(0, production(1));
assertEquals(0, breakdown.getTradePower());
assertEquals(0, breakdown.getIncome());
}

@Test
void snapshotRecalculationKeepsGraphTrade() {
connect(1, 22);
Expand Down
Loading