feat: v0.7.0 — server-authoritative sim engine: tick-on-read clock (time-scale + pause via POST /api/sim), contract expiry + market spawn, analytic plane movement/finalization
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { applyTimeControls } from '@/lib/sim/engine';
|
||||
|
||||
// POST /api/sim — set sim time controls (v0.7.0). Body: { timeScale?: 1–60, paused?: boolean }.
|
||||
// Ticks the clock first (so the response reflects current game time), then applies
|
||||
// and persists the new controls. Returns the updated simState.
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let body: unknown;
|
||||
try {
|
||||
const text = await req.text();
|
||||
body = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
return Response.json({ error: 'invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
||||
return Response.json({ error: 'body must be a JSON object' }, { status: 400 });
|
||||
}
|
||||
|
||||
const input = body as Record<string, unknown>;
|
||||
for (const key of Object.keys(input)) {
|
||||
if (key !== 'timeScale' && key !== 'paused') {
|
||||
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
|
||||
}
|
||||
}
|
||||
if (input.timeScale !== undefined && (typeof input.timeScale !== 'number' || !Number.isFinite(input.timeScale))) {
|
||||
return Response.json({ error: 'timeScale must be a finite number (1–60)' }, { status: 400 });
|
||||
}
|
||||
if (input.paused !== undefined && typeof input.paused !== 'boolean') {
|
||||
return Response.json({ error: 'paused must be a boolean' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const sim = await applyTimeControls({
|
||||
timeScale: input.timeScale as number | undefined,
|
||||
paused: input.paused as boolean | undefined,
|
||||
});
|
||||
return Response.json({ clockMs: sim.clockMs, timeScale: sim.timeScale, paused: sim.paused });
|
||||
} catch (err) {
|
||||
console.error('failed to apply sim controls', err);
|
||||
return Response.json({ error: 'failed to apply sim controls' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user