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>
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import type { Payload, Where } from "payload";
|
|
|
|
import type { EconomyItemType } from "./types";
|
|
import type { ItemRef } from "./persistence";
|
|
|
|
/**
|
|
* Consumption-side helpers: the market system reads ledger state to price new
|
|
* NPC listings, ingests real demand from completed purchases, and GMs can
|
|
* rebuild a doc from its baselines when the ledger drifts from reality.
|
|
*/
|
|
|
|
export interface MarketPriceContext {
|
|
priceModifier: number;
|
|
currentSupply: number;
|
|
artificialDemand: number;
|
|
realDemand: number;
|
|
}
|
|
|
|
type PayloadLike = Payload;
|
|
|
|
export async function getMarketPriceContext(
|
|
payload: PayloadLike,
|
|
itemType: EconomyItemType,
|
|
itemId: number,
|
|
): Promise<MarketPriceContext | null> {
|
|
const docs = await payload.find({
|
|
collection: "market-state",
|
|
where: { and: [{ itemType: { equals: itemType } }, { itemId: { equals: itemId } }] },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const doc = docs.docs[0];
|
|
if (!doc) return null;
|
|
return {
|
|
priceModifier: doc.priceModifier,
|
|
currentSupply: doc.currentSupply,
|
|
artificialDemand: doc.artificialDemand,
|
|
realDemand: doc.realDemand,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Fire-and-forget real-demand ingestion for completed purchases of NPC stock.
|
|
* The purchase has already succeeded, so failures are logged, never thrown.
|
|
*/
|
|
export async function recordPurchase(
|
|
payload: PayloadLike,
|
|
{ itemId, quantity }: { itemId: number; quantity: number },
|
|
): Promise<void> {
|
|
try {
|
|
const docs = await payload.find({
|
|
collection: "market-state",
|
|
where: { and: [{ itemType: { equals: "asset" } }, { itemId: { equals: itemId } }] },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const doc = docs.docs[0];
|
|
if (!doc) return;
|
|
await payload.update({
|
|
collection: "market-state",
|
|
id: doc.id,
|
|
data: { realDemand: doc.realDemand + quantity },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
} catch (error: unknown) {
|
|
payload.logger.error({ err: error }, "economy purchase ingestion failed");
|
|
}
|
|
}
|
|
|
|
/** Rebuilds market-state docs from their GM baselines (admin repair action). */
|
|
export async function resetMarketStateDocs(
|
|
payload: PayloadLike,
|
|
{ items }: { items?: ItemRef[] },
|
|
): Promise<{ reset: number; scanned: number }> {
|
|
const where: Where | undefined = items
|
|
? {
|
|
or: items.map((item): Where => ({
|
|
and: [
|
|
{ itemType: { equals: item.itemType } },
|
|
{ itemId: { equals: item.itemId } },
|
|
],
|
|
})),
|
|
}
|
|
: undefined;
|
|
|
|
const found = await payload.find({
|
|
collection: "market-state",
|
|
...(where ? { where } : {}),
|
|
limit: 500,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
let reset = 0;
|
|
for (const doc of found.docs) {
|
|
try {
|
|
await payload.update({
|
|
collection: "market-state",
|
|
id: doc.id,
|
|
data: {
|
|
currentSupply: doc.gmBaselineSupply,
|
|
artificialDemand: doc.gmBaselineDemand,
|
|
realDemand: 0,
|
|
priceModifier: 1,
|
|
lastComputedAt: null,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
reset += 1;
|
|
} catch (error: unknown) {
|
|
payload.logger.error({ err: error }, `failed to reset market-state doc ${doc.id}`);
|
|
}
|
|
}
|
|
|
|
return { reset, scanned: found.docs.length };
|
|
}
|