refactor(logic): replace arbitrary action hooks with typed events

This commit is contained in:
2026-07-30 03:31:31 +00:00
parent 96b41ef4b4
commit 7d3cb50a5b
168 changed files with 1554 additions and 631 deletions
@@ -22,7 +22,11 @@ const DISASTER_TEXT_BY_MONTH: Readonly<Record<number, DisasterText[]>> = {
1: [
{ title: '<M><b>【재난】</b></>', stateCode: 4, body: '역병이 발생하여 도시가 황폐해지고 있습니다.' },
{ title: '<M><b>【재난】</b></>', stateCode: 5, body: '지진으로 피해가 속출하고 있습니다.' },
{ title: '<M><b>【재난】</b></>', stateCode: 3, body: '추위가 풀리지 않아 얼어죽는 백성들이 늘어나고 있습니다.' },
{
title: '<M><b>【재난】</b></>',
stateCode: 3,
body: '추위가 풀리지 않아 얼어죽는 백성들이 늘어나고 있습니다.',
},
{ title: '<M><b>【재난】</b></>', stateCode: 9, body: '황건적이 출현해 도시를 습격하고 있습니다.' },
],
4: [
@@ -67,7 +71,7 @@ const roundLegacyIntegerColumn = (value: number): number => Math.round(value);
export const createRaiseDisasterHandler = (options: {
getWorld: () => InMemoryTurnWorld | null;
generalActionModules?: GeneralActionModule[];
generalActionModules?: ReadonlyArray<GeneralActionModule>;
}): MonthlyEventActionHandler => {
const generalPipeline = new GeneralActionPipeline(options.generalActionModules ?? []);
@@ -95,9 +99,7 @@ export const createRaiseDisasterHandler = (options: {
throw new Error(`Unsupported month for RaiseDisaster: ${environment.month}`);
}
const rng = new RandUtil(
new LiteHashDRBG(
simpleSerialize(resolveHiddenSeed(world), 'disater', environment.year, environment.month)
)
new LiteHashDRBG(simpleSerialize(resolveHiddenSeed(world), 'disater', environment.year, environment.month))
);
const isGood = rng.nextBool(boomingRate);
const targetCities = cities.filter((city) => {
@@ -135,9 +137,7 @@ export const createRaiseDisasterHandler = (options: {
world.updateCity(city.id, {
state: picked.stateCode,
population: roundLegacyIntegerColumn(
isGood
? Math.min(city.population * affectRatio, city.populationMax)
: city.population * affectRatio
isGood ? Math.min(city.population * affectRatio, city.populationMax) : city.population * affectRatio
),
agriculture: roundLegacyIntegerColumn(
isGood
@@ -171,8 +171,7 @@ export const createRaiseDisasterHandler = (options: {
nation: world.getNationById(general.nationId),
worldView: {
listGenerals: () => allGenerals,
listGeneralsByCity: (cityId) =>
allGenerals.filter((candidate) => candidate.cityId === cityId),
listGeneralsByCity: (cityId) => allGenerals.filter((candidate) => candidate.cityId === cityId),
},
rng,
});
@@ -186,8 +186,8 @@ export const buildReservedTurnDefinitions = async (options: {
},
])
);
options.env.generalActionModules = [...(options.env.generalActionModules ?? []), ...moduleBundle.general];
options.env.warActionModules = [...(options.env.warActionModules ?? []), ...moduleBundle.war];
options.env.generalActionModules ??= moduleBundle.general;
options.env.warActionModules ??= moduleBundle.war;
options.env.nationTraitModules = moduleBundle.nationTraitModules;
const generalSpecs = await loadGeneralTurnCommandSpecs(options.commandProfile.general);
@@ -976,6 +976,11 @@ export const createReservedTurnHandler = async (options: {
getAdditionalOccupiedUniqueItemKeys: options.getAdditionalOccupiedUniqueItemKeys,
});
let actionRng = sharedActionRng ?? buildRng(actionKey);
const actionTime = {
year: context.world.currentYear,
month: context.world.currentMonth,
startYear: resolveStartYear(context.world, options.scenarioMeta),
};
let baseContext: ActionContextBase = {
general: currentGeneral,
city: currentCity,
@@ -991,6 +996,7 @@ export const createReservedTurnHandler = async (options: {
}
: {}),
rng: actionRng,
time: actionTime,
uniqueLottery,
};
let specificContext = buildActionContext(
@@ -1023,6 +1029,7 @@ export const createReservedTurnHandler = async (options: {
city: currentCity,
nation: currentNation,
rng: actionRng,
time: actionTime,
};
specificContext = baseContext;
}