41 lines
1.8 KiB
TypeScript
41 lines
1.8 KiB
TypeScript
import { getGameDb } from '@/lib/db/client'
|
|
import { parseCrewTeam } from '@/app/api/crew-team'
|
|
import { checkJumpTarget, getWorldState, JumpError, queueStateFromSave, saveTaskRow } from '@/lib/game/save'
|
|
import { queueTask, TaskError } from '@/lib/game/tasks'
|
|
|
|
// Jump to a system within range: assign the crew team (lead + helpers) and queue the JUMP
|
|
// task — the move lands when the task completes on the turn tick (runTurn). Body:
|
|
// { systemId, leadCrewId, helperCrewIds? }. Queueing spends no turn; returns the same fresh
|
|
// world state as the instant jump did.
|
|
export async function POST(req: Request) {
|
|
let body: unknown
|
|
try {
|
|
body = await req.json()
|
|
} catch {
|
|
return Response.json({ error: 'Invalid JSON body' }, { status: 400 })
|
|
}
|
|
|
|
const systemId = (body as { systemId?: unknown }).systemId
|
|
if (typeof systemId !== 'number' || !Number.isInteger(systemId)) {
|
|
return Response.json({ error: 'systemId must be an integer' }, { status: 400 })
|
|
}
|
|
const team = parseCrewTeam(body)
|
|
if ('error' in team) {
|
|
return Response.json({ error: team.error }, { status: team.status })
|
|
}
|
|
|
|
try {
|
|
const db = await getGameDb()
|
|
const hops = await checkJumpTarget(db, systemId) // existing pre-checks: 404 unknown · 409 already there / out of range
|
|
const state = await queueStateFromSave(db)
|
|
const { task } = queueTask(state, { type: 'JUMP', payload: { systemId, hops }, leadCrewId: team.leadCrewId, helperCrewIds: team.helperCrewIds })
|
|
await saveTaskRow(db, task) // inventory is unchanged for a JUMP — no hold write
|
|
return Response.json(await getWorldState(db))
|
|
} catch (err) {
|
|
if (err instanceof JumpError || err instanceof TaskError) {
|
|
return Response.json({ error: err.message }, { status: err.status })
|
|
}
|
|
throw err
|
|
}
|
|
}
|