fix: purge collapsed nations from city conflicts
This commit is contained in:
@@ -105,16 +105,22 @@ const applyNationTechGain = <TriggerState extends GeneralTriggerState>(
|
||||
nation.meta.tech = round(tech);
|
||||
};
|
||||
|
||||
const resolveConquerNation = (city: City, attackerNationId: number): number => {
|
||||
const resolveConquerNation = (city: City, attackerNationId: number, nations: Nation[]): number => {
|
||||
const rawConflict = city.meta[META_CONFLICT];
|
||||
if (!rawConflict) {
|
||||
return attackerNationId;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(String(rawConflict)) as Record<string, number>;
|
||||
const activeNationIds = new Set(nations.map((nation) => nation.id));
|
||||
const entries = Object.entries(parsed)
|
||||
.map(([key, value]) => [Number(key), value] as const)
|
||||
.filter(([key, value]) => Number.isFinite(key) && typeof value === 'number')
|
||||
.filter(
|
||||
([key, value]) =>
|
||||
Number.isFinite(key) &&
|
||||
typeof value === 'number' &&
|
||||
(key === attackerNationId || activeNationIds.has(key))
|
||||
)
|
||||
.sort(([, lhs], [, rhs]) => rhs - lhs);
|
||||
if (!entries.length) {
|
||||
return attackerNationId;
|
||||
@@ -183,7 +189,7 @@ const resolveConquerCity = <TriggerState extends GeneralTriggerState>(
|
||||
const affectedGenerals = new Set<General<TriggerState>>();
|
||||
const affectedNations = new Set<Nation>();
|
||||
|
||||
const conquerNationId = resolveConquerNation(defenderCity, attackerNation.id);
|
||||
const conquerNationId = resolveConquerNation(defenderCity, attackerNation.id, input.nations);
|
||||
const attackerLogger = new ActionLogger({
|
||||
generalId: attacker.id,
|
||||
nationId: attackerNation.id,
|
||||
|
||||
@@ -183,7 +183,7 @@ describe('war aftermath', () => {
|
||||
defenderNation.rice = 6000;
|
||||
const attackerCity = buildCity(1, 1);
|
||||
const defenderCity = buildCity(2, 2);
|
||||
defenderCity.meta.conflict = JSON.stringify({ 1: 100 });
|
||||
defenderCity.meta.conflict = JSON.stringify({ 99: 200, 1: 100 });
|
||||
const attacker = buildGeneral(1, 1, 1);
|
||||
const defender = buildGeneral(2, 2, 2);
|
||||
|
||||
@@ -235,6 +235,8 @@ describe('war aftermath', () => {
|
||||
expect(attackerNation.rice).toBe(4600);
|
||||
expect(defender.experience).toBe(90);
|
||||
expect(defender.dedication).toBe(50);
|
||||
// Removed nations can remain in old persisted conflict metadata. They
|
||||
// must never receive ownership during a later conquest.
|
||||
expect(defenderCity.nationId).toBe(attackerNation.id);
|
||||
expect(defenderCity.meta.conflict).toBe('{}');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user