1
0
Fork 0
polaris-task-force/src/lib/economy/config.ts
Z8MB1E 447df220f5 feat(economy): add economy tick with demand simulation and price modifiers
Add the src/lib/economy service (GM baselines, deterministic per-period demand drift, price modifier computation, event emission) and the economy-tick bin. Gated by GameRules economy.enabled; period-idempotent with a cold-start observation gate.

Ultraworked with [Sisyphus](https://github.com/code-yeongju/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-06 17:22:30 -04:00

115 lines
3.4 KiB
TypeScript

/**
* Supply & demand economy — configuration accessors.
*
* Resolves global knobs from the GameRules `economy` group and per-item
* configuration from the two item shapes (Resources use a top-level
* `economy` group, Assets nest theirs under `marketTrading`).
*/
import type { Payload } from "payload";
import type { EconomyKnobs, EconomyItemType, ItemEconomyConfig } from "./types";
import { ECONOMY_DEFAULTS, ITEM_ECONOMY_DEFAULTS } from "./types";
type GameRulesDoc = {
economy?: {
enabled?: boolean | null;
defaultPriceBand?: number | null;
realDemandWindowTicks?: number | null;
realDemandDecay?: number | null;
artificialDemandDrift?: number | null;
coldStartObservations?: number | null;
tickIntervalHintMinutes?: number | null;
} | null;
};
function clamp(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value));
}
function toNumber(value: number | null | undefined, fallback: number): number {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
/** Read the global economy knobs from GameRules, falling back to defaults. */
export function knobsFromGameRules(rules: GameRulesDoc | null | undefined): EconomyKnobs {
const economy = rules?.economy;
return {
defaultPriceBand: clamp(
toNumber(economy?.defaultPriceBand, ECONOMY_DEFAULTS.defaultPriceBand),
0,
1,
),
realDemandWindowTicks: Math.max(
1,
toNumber(economy?.realDemandWindowTicks, ECONOMY_DEFAULTS.realDemandWindowTicks),
),
realDemandDecay: clamp(
toNumber(economy?.realDemandDecay, ECONOMY_DEFAULTS.realDemandDecay),
0,
1,
),
artificialDemandDrift: clamp(
toNumber(economy?.artificialDemandDrift, ECONOMY_DEFAULTS.artificialDemandDrift),
0,
1,
),
coldStartObservations: Math.max(
0,
toNumber(economy?.coldStartObservations, ECONOMY_DEFAULTS.coldStartObservations),
),
tickIntervalHintMinutes: Math.max(
1,
toNumber(economy?.tickIntervalHintMinutes, ECONOMY_DEFAULTS.tickIntervalHintMinutes),
),
};
}
export async function getEconomySettings(
payload: Payload,
): Promise<{ enabled: boolean; knobs: EconomyKnobs }> {
const rules = (await payload.findGlobal({ slug: "game-rules", depth: 0 })) as GameRulesDoc;
return { enabled: rules?.economy?.enabled === true, knobs: knobsFromGameRules(rules) };
}
type ResourceLike = {
economy?: {
enabled?: boolean | null;
baselineSupply?: number | null;
baselineDemand?: number | null;
maxModifier?: number | null;
} | null;
};
/**
* Resolve per-item economy configuration. Both item shapes store the group
* at the top level (`economy.*`); returns null when the item's economy is
* disabled (no ledger, no pricing).
*/
export function resolveEconomyConfig(
_itemType: EconomyItemType,
doc: ResourceLike | null | undefined,
knobs: EconomyKnobs,
): ItemEconomyConfig | null {
const economy = doc?.economy;
if (!economy || economy.enabled !== true) {
return null;
}
const priceBand = clamp(
toNumber(economy.maxModifier, knobs.defaultPriceBand),
0,
1,
);
return {
enabled: true,
baselineSupply: Math.max(
0,
toNumber(economy.baselineSupply, ITEM_ECONOMY_DEFAULTS.baselineSupply),
),
baselineDemand: Math.max(
0,
toNumber(economy.baselineDemand, ITEM_ECONOMY_DEFAULTS.baselineDemand),
),
priceBand,
};
}