feat: 플레이 감사 월별 상태와 국가 집계 기반 추가
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
import { asNumber } from '@sammo-ts/common';
|
||||
import type { City, Nation } from '@sammo-ts/logic';
|
||||
|
||||
import { resolveAppliedNationRate } from '../turn/nationTaxRate.js';
|
||||
import type { TurnGeneral } from '../turn/types.js';
|
||||
|
||||
export const AUDIT_DEX_KEYS = ['dex1', 'dex2', 'dex3', 'dex4', 'dex5'] as const;
|
||||
export type AuditDex = Record<(typeof AUDIT_DEX_KEYS)[number], number>;
|
||||
export type AuditPopulation = 'human' | 'npc' | 'troopNpc';
|
||||
|
||||
export interface AuditPopulationSummary {
|
||||
count: number;
|
||||
gold: number;
|
||||
rice: number;
|
||||
dex: AuditDex;
|
||||
averageGold: number | null;
|
||||
averageRice: number | null;
|
||||
averageDex: Record<keyof AuditDex, number | null>;
|
||||
}
|
||||
|
||||
/** 정산 처리에서 관측한 값만 전달한다. prev_income_*는 이번 달 흐름이 아니다. */
|
||||
export interface AuditSettlement {
|
||||
nationId: number;
|
||||
resource: 'gold' | 'rice';
|
||||
income: number;
|
||||
paid: number;
|
||||
}
|
||||
|
||||
export interface AuditNationSnapshot {
|
||||
id: number;
|
||||
name: string;
|
||||
color: string;
|
||||
gold: number;
|
||||
rice: number;
|
||||
tech: number;
|
||||
appliedRate: number;
|
||||
incomeGold: number | null;
|
||||
incomeRice: number | null;
|
||||
paidGold: number | null;
|
||||
paidRice: number | null;
|
||||
populations: Record<AuditPopulation, AuditPopulationSummary>;
|
||||
}
|
||||
|
||||
const emptyDex = (): AuditDex => ({ dex1: 0, dex2: 0, dex3: 0, dex4: 0, dex5: 0 });
|
||||
const emptyPopulation = (): AuditPopulationSummary => ({
|
||||
count: 0,
|
||||
gold: 0,
|
||||
rice: 0,
|
||||
dex: emptyDex(),
|
||||
averageGold: null,
|
||||
averageRice: null,
|
||||
averageDex: { dex1: null, dex2: null, dex3: null, dex4: null, dex5: null },
|
||||
});
|
||||
|
||||
export const classifyAuditPopulation = (npcState: number): AuditPopulation =>
|
||||
npcState === 5 ? 'troopNpc' : npcState < 2 ? 'human' : 'npc';
|
||||
|
||||
export interface AuditGeneralSnapshot extends Pick<
|
||||
TurnGeneral,
|
||||
| 'id'
|
||||
| 'name'
|
||||
| 'nationId'
|
||||
| 'cityId'
|
||||
| 'troopId'
|
||||
| 'npcState'
|
||||
| 'gold'
|
||||
| 'rice'
|
||||
| 'stats'
|
||||
| 'experience'
|
||||
| 'dedication'
|
||||
| 'officerLevel'
|
||||
| 'injury'
|
||||
| 'age'
|
||||
| 'crew'
|
||||
| 'crewTypeId'
|
||||
| 'train'
|
||||
| 'atmos'
|
||||
| 'role'
|
||||
> {
|
||||
userId: string | null;
|
||||
population: AuditPopulation;
|
||||
dex: AuditDex;
|
||||
}
|
||||
|
||||
export const projectAuditGeneral = (general: TurnGeneral): AuditGeneralSnapshot => ({
|
||||
id: general.id,
|
||||
name: general.name,
|
||||
userId: general.userId ?? null,
|
||||
nationId: general.nationId,
|
||||
cityId: general.cityId,
|
||||
troopId: general.troopId,
|
||||
npcState: general.npcState,
|
||||
population: classifyAuditPopulation(general.npcState),
|
||||
gold: general.gold,
|
||||
rice: general.rice,
|
||||
stats: { ...general.stats },
|
||||
experience: general.experience,
|
||||
dedication: general.dedication,
|
||||
officerLevel: general.officerLevel,
|
||||
injury: general.injury,
|
||||
age: general.age,
|
||||
crew: general.crew,
|
||||
crewTypeId: general.crewTypeId,
|
||||
train: general.train,
|
||||
atmos: general.atmos,
|
||||
role: { ...general.role, items: { ...general.role.items } },
|
||||
dex: {
|
||||
dex1: asNumber(general.meta.dex1, 0),
|
||||
dex2: asNumber(general.meta.dex2, 0),
|
||||
dex3: asNumber(general.meta.dex3, 0),
|
||||
dex4: asNumber(general.meta.dex4, 0),
|
||||
dex5: asNumber(general.meta.dex5, 0),
|
||||
},
|
||||
});
|
||||
|
||||
export interface AuditCitySnapshot extends Omit<City, 'meta' | 'conflict'> {
|
||||
trust: number;
|
||||
}
|
||||
|
||||
export const projectAuditCity = (city: City): AuditCitySnapshot => ({
|
||||
id: city.id,
|
||||
name: city.name,
|
||||
nationId: city.nationId,
|
||||
level: city.level,
|
||||
state: city.state,
|
||||
population: city.population,
|
||||
populationMax: city.populationMax,
|
||||
agriculture: city.agriculture,
|
||||
agricultureMax: city.agricultureMax,
|
||||
commerce: city.commerce,
|
||||
commerceMax: city.commerceMax,
|
||||
security: city.security,
|
||||
securityMax: city.securityMax,
|
||||
wall: city.wall,
|
||||
wallMax: city.wallMax,
|
||||
defence: city.defence,
|
||||
defenceMax: city.defenceMax,
|
||||
supplyState: city.supplyState,
|
||||
frontState: city.frontState,
|
||||
trust: asNumber(city.meta.trust, 50),
|
||||
});
|
||||
|
||||
/** 이미 로드한 world를 한 번씩 순회한다. DB/RNG/가변 world 객체는 보관하지 않는다. */
|
||||
export const buildAuditSnapshot = (input: {
|
||||
nations: Iterable<Nation>;
|
||||
cities: Iterable<City>;
|
||||
generals: Iterable<TurnGeneral>;
|
||||
settlements: Iterable<AuditSettlement>;
|
||||
settlementsComplete: boolean;
|
||||
}): { nations: AuditNationSnapshot[]; cities: AuditCitySnapshot[]; generals: AuditGeneralSnapshot[] } => {
|
||||
const nations = new Map<number, AuditNationSnapshot>();
|
||||
for (const nation of input.nations) {
|
||||
const flow = input.settlementsComplete ? 0 : null;
|
||||
nations.set(nation.id, {
|
||||
id: nation.id,
|
||||
name: nation.name,
|
||||
color: nation.color,
|
||||
gold: nation.gold,
|
||||
rice: nation.rice,
|
||||
tech: asNumber(nation.meta.tech, 0),
|
||||
appliedRate: resolveAppliedNationRate(nation.meta),
|
||||
incomeGold: flow,
|
||||
incomeRice: flow,
|
||||
paidGold: flow,
|
||||
paidRice: flow,
|
||||
populations: { human: emptyPopulation(), npc: emptyPopulation(), troopNpc: emptyPopulation() },
|
||||
});
|
||||
}
|
||||
const generals: AuditGeneralSnapshot[] = [];
|
||||
for (const general of input.generals) {
|
||||
const projected = projectAuditGeneral(general);
|
||||
generals.push(projected);
|
||||
const population = nations.get(general.nationId)?.populations[projected.population];
|
||||
if (!population) continue;
|
||||
population.count++;
|
||||
population.gold += projected.gold;
|
||||
population.rice += projected.rice;
|
||||
for (const key of AUDIT_DEX_KEYS) population.dex[key] += projected.dex[key];
|
||||
}
|
||||
for (const nation of nations.values()) {
|
||||
for (const population of Object.values(nation.populations)) {
|
||||
if (!population.count) continue;
|
||||
population.averageGold = population.gold / population.count;
|
||||
population.averageRice = population.rice / population.count;
|
||||
for (const key of AUDIT_DEX_KEYS) population.averageDex[key] = population.dex[key] / population.count;
|
||||
}
|
||||
}
|
||||
for (const settlement of input.settlements) {
|
||||
const nation = nations.get(settlement.nationId);
|
||||
if (!nation || !input.settlementsComplete) continue;
|
||||
if (settlement.resource === 'gold') {
|
||||
nation.incomeGold = (nation.incomeGold ?? 0) + settlement.income;
|
||||
nation.paidGold = (nation.paidGold ?? 0) + settlement.paid;
|
||||
} else {
|
||||
nation.incomeRice = (nation.incomeRice ?? 0) + settlement.income;
|
||||
nation.paidRice = (nation.paidRice ?? 0) + settlement.paid;
|
||||
}
|
||||
}
|
||||
return { nations: [...nations.values()], cities: Array.from(input.cities, projectAuditCity), generals };
|
||||
};
|
||||
@@ -0,0 +1,178 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { City, Nation } from '@sammo-ts/logic';
|
||||
import type { TurnGeneral } from '../src/turn/types.js';
|
||||
import { buildAuditSnapshot } from '../src/playAudit/snapshot.js';
|
||||
|
||||
const turnTime = new Date('0200-01-01T00:00:00.000Z');
|
||||
|
||||
const buildGeneral = (id: number, nationId: number): TurnGeneral => ({
|
||||
id,
|
||||
name: `장수${id}`,
|
||||
nationId,
|
||||
cityId: nationId,
|
||||
troopId: 0,
|
||||
stats: { leadership: 80, strength: 70, intelligence: 60 },
|
||||
experience: 1_000,
|
||||
dedication: 900,
|
||||
officerLevel: 1,
|
||||
role: {
|
||||
personality: null,
|
||||
specialDomestic: null,
|
||||
specialWar: null,
|
||||
items: { horse: null, weapon: null, book: null, item: null },
|
||||
},
|
||||
injury: 0,
|
||||
gold: 2_000,
|
||||
rice: 2_000,
|
||||
crew: 0,
|
||||
crewTypeId: 0,
|
||||
train: 0,
|
||||
atmos: 0,
|
||||
age: 30,
|
||||
npcState: nationId === 0 ? 2 : 0,
|
||||
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
|
||||
meta: { killturn: 24 },
|
||||
turnTime,
|
||||
});
|
||||
|
||||
const buildCity = (id: number, nationId: number): City => ({
|
||||
id,
|
||||
name: `도시${id}`,
|
||||
nationId,
|
||||
level: 1,
|
||||
state: 0,
|
||||
population: 10_000,
|
||||
populationMax: 20_000,
|
||||
agriculture: 1_000,
|
||||
agricultureMax: 2_000,
|
||||
commerce: 1_000,
|
||||
commerceMax: 2_000,
|
||||
security: 1_000,
|
||||
securityMax: 2_000,
|
||||
supplyState: 1,
|
||||
frontState: 0,
|
||||
defence: 1_000,
|
||||
defenceMax: 2_000,
|
||||
wall: 1_000,
|
||||
wallMax: 2_000,
|
||||
meta: {},
|
||||
});
|
||||
|
||||
const buildNation = (id: number, power: number, meta: Nation['meta']): Nation => ({
|
||||
id,
|
||||
name: id === 0 ? '재야' : `국가${id}`,
|
||||
color: '#777777',
|
||||
capitalCityId: id === 0 ? null : id,
|
||||
chiefGeneralId: null,
|
||||
gold: 10_000,
|
||||
rice: 20_000,
|
||||
power,
|
||||
level: id === 0 ? 0 : 1,
|
||||
typeCode: 'che_중립',
|
||||
meta,
|
||||
});
|
||||
|
||||
describe('play audit monthly projection', () => {
|
||||
it('separates humans, NPCs and troop NPCs and retains empty nations and neutral generals', () => {
|
||||
const humans = [buildGeneral(1, 1), { ...buildGeneral(2, 1), npcState: 1, gold: 0 }];
|
||||
humans[0]!.meta.dex1 = 10;
|
||||
const result = buildAuditSnapshot({
|
||||
nations: [buildNation(0, 0, {}), buildNation(1, 0, {}), buildNation(2, 0, {})],
|
||||
cities: [buildCity(1, 2)],
|
||||
generals: [
|
||||
...humans,
|
||||
{ ...buildGeneral(3, 1), npcState: 2 },
|
||||
{ ...buildGeneral(4, 1), npcState: 5 },
|
||||
buildGeneral(5, 0),
|
||||
],
|
||||
settlements: [],
|
||||
settlementsComplete: true,
|
||||
});
|
||||
const nation = result.nations.find((row) => row.id === 1)!;
|
||||
expect(nation.populations.human).toMatchObject({
|
||||
count: 2,
|
||||
gold: 2000,
|
||||
averageGold: 1000,
|
||||
averageDex: { dex1: 5 },
|
||||
});
|
||||
expect(nation.populations.npc.count).toBe(1);
|
||||
expect(nation.populations.troopNpc.count).toBe(1);
|
||||
expect(result.nations.find((row) => row.id === 2)!.populations.human.averageGold).toBeNull();
|
||||
expect(result.nations.find((row) => row.id === 0)!.populations.npc.count).toBe(1);
|
||||
expect(result.cities[0]!.nationId).toBe(2);
|
||||
expect(result.generals[0]!.nationId).toBe(1);
|
||||
expect(result.generals[0]!.cityId).toBe(1);
|
||||
});
|
||||
|
||||
it('uses observed settlements, preserves fractions and does not reuse stale income', () => {
|
||||
const input = {
|
||||
nations: [buildNation(1, 0, { prev_income_gold: 999999 })],
|
||||
cities: [],
|
||||
generals: [],
|
||||
settlements: [{ nationId: 1, resource: 'gold' as const, income: 943.5, paid: 123 }],
|
||||
};
|
||||
expect(buildAuditSnapshot({ ...input, settlementsComplete: true }).nations[0]).toMatchObject({
|
||||
incomeGold: 943.5,
|
||||
paidGold: 123,
|
||||
incomeRice: 0,
|
||||
paidRice: 0,
|
||||
});
|
||||
expect(
|
||||
buildAuditSnapshot({ ...input, settlements: [], settlementsComplete: true }).nations[0]!.incomeGold
|
||||
).toBe(0);
|
||||
expect(buildAuditSnapshot({ ...input, settlementsComplete: false }).nations[0]).toMatchObject({
|
||||
incomeGold: null,
|
||||
paidGold: null,
|
||||
incomeRice: null,
|
||||
paidRice: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('takes detached allowlisted state without credentials or mutable metadata', () => {
|
||||
const general = buildGeneral(1, 1);
|
||||
general.userId = 'owner';
|
||||
general.meta.secret = 'must-not-copy';
|
||||
general.role.items.horse = 'horse';
|
||||
const city = buildCity(1, 1);
|
||||
const result = buildAuditSnapshot({
|
||||
nations: [buildNation(1, 0, {})],
|
||||
cities: [city],
|
||||
generals: [general],
|
||||
settlements: [],
|
||||
settlementsComplete: true,
|
||||
});
|
||||
general.name = 'renamed';
|
||||
general.stats.strength = 1;
|
||||
general.role.items.horse = null;
|
||||
city.population = 0;
|
||||
expect(result.generals[0]).toMatchObject({
|
||||
name: '장수1',
|
||||
userId: 'owner',
|
||||
stats: { strength: 70 },
|
||||
role: { items: { horse: 'horse' } },
|
||||
});
|
||||
expect(JSON.stringify(result)).not.toContain('must-not-copy');
|
||||
expect(result.cities[0]!.population).toBe(10000);
|
||||
});
|
||||
|
||||
it('consumes each source once without per-nation scans', () => {
|
||||
function once<T>(rows: T[]): Iterable<T> {
|
||||
let used = false;
|
||||
return {
|
||||
*[Symbol.iterator]() {
|
||||
if (used) throw new Error('second full scan');
|
||||
used = true;
|
||||
yield* rows;
|
||||
},
|
||||
};
|
||||
}
|
||||
const result = buildAuditSnapshot({
|
||||
nations: once([buildNation(1, 0, {})]),
|
||||
cities: once([buildCity(1, 1)]),
|
||||
generals: once([buildGeneral(1, 1)]),
|
||||
settlements: once([]),
|
||||
settlementsComplete: true,
|
||||
});
|
||||
expect(result.generals).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user