feat(api): jump + explore endpoints queue tasks with lead + helpers instead of resolving instantly (#44)

This commit is contained in:
2026-10-02 15:47:31 +00:00
parent d7faf739a9
commit 4e312d054f
10 changed files with 455 additions and 66 deletions
+34 -5
View File
@@ -1,8 +1,13 @@
import { getGameDb } from '@/lib/db/client'
import { ExploreError, explorePlanet } from '@/lib/game/save'
import { MAX_TEAM_SIZE } from '@/lib/game/crew'
import { checkExploreTarget, ExploreError, getSystemState, queueStateFromSave, saveTaskRow } from '@/lib/game/save'
import { queueTask, TaskError } from '@/lib/game/tasks'
// Scan one body of the current system. Body: { "planetId": number }. Returns the fresh
// system state plus what this scan found. Costs a turn; each body can only be scanned once.
// Scan one body of the current system: assign the crew team (lead + helpers) and queue the
// EXPLORE task — the haul, the scanned flag, the crew roll and the event land when the task
// completes on the turn tick (runTurn). Body: { planetId, leadCrewId, helperCrewIds? }.
// Queueing spends no turn; returns the same system snapshot as the instant scan, with the
// findings key empty until the scan resolves (JC-2).
export async function POST(req: Request) {
let body: unknown
try {
@@ -15,12 +20,36 @@ export async function POST(req: Request) {
if (typeof planetId !== 'number' || !Number.isInteger(planetId)) {
return Response.json({ error: 'planetId must be an integer' }, { status: 400 })
}
const leadCrewId = (body as { leadCrewId?: unknown }).leadCrewId
if (typeof leadCrewId !== 'number' || !Number.isInteger(leadCrewId)) {
return Response.json({ error: 'leadCrewId must be an integer' }, { status: 400 })
}
let helperCrewIds: number[] = []
const rawHelpers = (body as { helperCrewIds?: unknown }).helperCrewIds
if (rawHelpers !== undefined) {
if (!Array.isArray(rawHelpers) || !rawHelpers.every((h) => typeof h === 'number' && Number.isInteger(h))) {
return Response.json({ error: 'helperCrewIds must be an array of integers' }, { status: 400 })
}
helperCrewIds = rawHelpers
}
// Team rules (route layer, not queueTask's): one task team, distinct members, size cap.
const team = [leadCrewId, ...helperCrewIds]
if (new Set(team).size !== team.length) {
return Response.json({ error: 'crew members must be unique' }, { status: 400 })
}
if (team.length > MAX_TEAM_SIZE) {
return Response.json({ error: `a task team holds at most ${MAX_TEAM_SIZE} crew members` }, { status: 400 })
}
try {
const db = await getGameDb()
return Response.json(await explorePlanet(db, planetId))
await checkExploreTarget(db, planetId) // existing pre-checks: 404 unknown body · 409 already scanned
const state = await queueStateFromSave(db)
const { task } = queueTask(state, { type: 'EXPLORE', payload: { planetId }, leadCrewId, helperCrewIds })
await saveTaskRow(db, task) // inventory is unchanged until the scan resolves — no hold write
return Response.json({ ...(await getSystemState(db)), outcome: [], crewFound: null })
} catch (err) {
if (err instanceof ExploreError) {
if (err instanceof ExploreError || err instanceof TaskError) {
return Response.json({ error: err.message }, { status: err.status })
}
throw err