feat(content): add action costs and multi-yield schema

This commit is contained in:
ginnoir
2026-06-11 18:16:26 -05:00
parent 458a795014
commit ee3d463c9c
9 changed files with 85 additions and 18 deletions
+55 -3
View File
@@ -3,7 +3,12 @@ import { buildContent } from '../schema';
const validResources = [{ id: 'gold', name: 'Gold' }];
const validActions = [
{ id: 'forage', name: 'Forage', durationMs: 3000, yields: { resourceId: 'gold', amount: 1 } },
{
id: 'forage',
name: 'Forage',
durationMs: 3000,
yields: [{ resourceId: 'gold', amount: 1 }],
},
];
describe('buildContent()', () => {
@@ -24,7 +29,7 @@ describe('buildContent()', () => {
id: 'forage',
name: 'Forage',
durationMs: 3000,
yields: { resourceId: 'ghost', amount: 1 },
yields: [{ resourceId: 'ghost', amount: 1 }],
},
];
expect(() => buildContent({ resources: validResources, actions })).toThrow(/unknown resource/i);
@@ -40,8 +45,55 @@ describe('buildContent()', () => {
it('rejects a structurally invalid definition', () => {
const actions = [
{ id: 'forage', name: 'Forage', durationMs: -1, yields: { resourceId: 'gold', amount: 1 } },
{
id: 'forage',
name: 'Forage',
durationMs: -1,
yields: [{ resourceId: 'gold', amount: 1 }],
},
];
expect(() => buildContent({ resources: validResources, actions })).toThrow();
});
});
describe('costs and multi-yield', () => {
it('accepts optional costs and multiple yields', () => {
const resources = [
{ id: 'gold', name: 'Gold' },
{ id: 'wood', name: 'Wood' },
];
const actions = [
{
id: 'craft',
name: 'Craft',
durationMs: 1000,
costs: [{ resourceId: 'wood', amount: 2 }],
yields: [
{ resourceId: 'gold', amount: 1 },
{ resourceId: 'wood', amount: 1 },
],
},
];
const content = buildContent({ resources, actions });
expect(content.actionsById.craft.costs).toEqual([{ resourceId: 'wood', amount: 2 }]);
expect(content.actionsById.craft.yields).toHaveLength(2);
});
it('rejects cost referencing unknown resource', () => {
const actions = [
{
id: 'craft',
name: 'Craft',
durationMs: 1000,
costs: [{ resourceId: 'ghost', amount: 1 }],
yields: [{ resourceId: 'gold', amount: 1 }],
},
];
expect(() => buildContent({ resources: validResources, actions })).toThrow(/unknown resource/i);
});
it('defaults costs to empty array', () => {
const content = buildContent({ resources: validResources, actions: validActions });
expect(content.actionsById.forage.costs).toEqual([]);
});
});
+1 -1
View File
@@ -12,6 +12,6 @@ export const actionDefs = [
id: 'forage',
name: 'Forage for coin',
durationMs: 3000,
yields: { resourceId: 'gold', amount: 1 },
yields: [{ resourceId: 'gold', amount: 1 }],
},
];
+19 -6
View File
@@ -15,17 +15,21 @@ export const resourceDefSchema = z.object({
startAmount: z.number().nonnegative().default(0),
});
export const resourceAmountSchema = z.object({
resourceId: z.string().min(1),
amount: z.number().positive(),
});
export const actionDefSchema = z.object({
id: z.string().min(1),
name: z.string().min(1),
durationMs: z.number().positive(),
yields: z.object({
resourceId: z.string().min(1),
amount: z.number().positive(),
}),
costs: z.array(resourceAmountSchema).default([]),
yields: z.array(resourceAmountSchema).min(1),
});
export type ResourceDef = z.infer<typeof resourceDefSchema>;
export type ResourceAmount = z.infer<typeof resourceAmountSchema>;
export type ActionDef = z.infer<typeof actionDefSchema>;
export interface Content {
@@ -55,12 +59,21 @@ export function buildContent(input: { resources: unknown[]; actions: unknown[] }
const actionsById = indexById(actions, 'action');
for (const action of actions) {
if (!resourcesById[action.yields.resourceId]) {
for (const y of action.yields) {
if (!resourcesById[y.resourceId]) {
throw new Error(
`Action "${action.id}" yields unknown resource "${action.yields.resourceId}"`,
`Action "${action.id}" yields unknown resource "${y.resourceId}"`,
);
}
}
for (const c of action.costs) {
if (!resourcesById[c.resourceId]) {
throw new Error(
`Action "${action.id}" cost references unknown resource "${c.resourceId}"`,
);
}
}
}
return { resources, actions, resourcesById, actionsById };
}
+1 -1
View File
@@ -11,7 +11,7 @@ function testContent() {
id: 'forage',
name: 'Forage',
durationMs: 300,
yields: { resourceId: 'gold', amount: 1 },
yields: [{ resourceId: 'gold', amount: 1 }],
},
],
});
+1 -1
View File
@@ -6,7 +6,7 @@ function testContent() {
return buildContent({
resources: [{ id: 'gold', name: 'Gold', startAmount: 5 }],
actions: [
{ id: 'forage', name: 'Forage', durationMs: 300, yields: { resourceId: 'gold', amount: 2 } },
{ id: 'forage', name: 'Forage', durationMs: 300, yields: [{ resourceId: 'gold', amount: 2 }] },
],
});
}
+1 -1
View File
@@ -15,7 +15,7 @@ function testContent() {
return buildContent({
resources: [{ id: 'gold', name: 'Gold', startAmount: 0 }],
actions: [
{ id: 'forage', name: 'Forage', durationMs: 3000, yields: { resourceId: 'gold', amount: 1 } },
{ id: 'forage', name: 'Forage', durationMs: 3000, yields: [{ resourceId: 'gold', amount: 1 }] },
],
});
}
+3 -1
View File
@@ -50,6 +50,8 @@ export function tickGame(state: GameState, content: Content, tickMs: number): vo
state.actionElapsedMs += tickMs;
while (state.actionElapsedMs >= action.durationMs) {
state.actionElapsedMs -= action.durationMs;
state.resources[action.yields.resourceId] += action.yields.amount;
for (const y of action.yields) {
state.resources[y.resourceId] += y.amount;
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ function testContent() {
return buildContent({
resources: [{ id: 'gold', name: 'Gold', startAmount: 0 }],
actions: [
{ id: 'forage', name: 'Forage', durationMs: 3000, yields: { resourceId: 'gold', amount: 1 } },
{ id: 'forage', name: 'Forage', durationMs: 3000, yields: [{ resourceId: 'gold', amount: 1 }] },
],
});
}
+1 -1
View File
@@ -7,7 +7,7 @@ function testContent() {
return buildContent({
resources: [{ id: 'gold', name: 'Gold', startAmount: 4 }],
actions: [
{ id: 'forage', name: 'Forage', durationMs: 200, yields: { resourceId: 'gold', amount: 1 } },
{ id: 'forage', name: 'Forage', durationMs: 200, yields: [{ resourceId: 'gold', amount: 1 }] },
],
});
}