feat(api): ship + research endpoints queue tasks with lead + helpers; mission accept is instant and free (#44)
CI / build + unit + e2e (pull_request) Failing after 3m58s

This commit is contained in:
2026-10-02 18:36:14 +00:00
parent 4e312d054f
commit 03b8b661f6
20 changed files with 772 additions and 103 deletions
+20 -4
View File
@@ -1,7 +1,14 @@
import { getGameDb } from '@/lib/db/client'
import { ResearchError, researchTech } from '@/lib/game/research'
import { parseCrewTeam } from '@/app/api/crew-team'
import { getTechState, checkResearchPreconditions, ResearchError } from '@/lib/game/research'
import { queueStateFromSave, saveInventory, saveTaskRow } from '@/lib/game/save'
import { queueTask, TaskError } from '@/lib/game/tasks'
// Research one tech. Body: { "techId": number }. Costs inventory items + a turn; returns the fresh tech tree state.
// Research one tech: assign the crew team (lead + helpers) and queue the RESEARCH task — the
// tech cost is deducted at queue time and the tech is marked researched (unlock effect
// included) when the task completes on the turn tick (runTurn). Body:
// { techId, leadCrewId, helperCrewIds? }. Queueing spends no turn; returns the same fresh
// tech tree state the instant action returned.
export async function POST(req: Request) {
let body: unknown
try {
@@ -14,12 +21,21 @@ export async function POST(req: Request) {
if (typeof techId !== 'number' || !Number.isInteger(techId)) {
return Response.json({ error: 'techId 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()
return Response.json(await researchTech(db, techId))
await checkResearchPreconditions(db, techId) // existing pre-checks: 404 unknown · 409 already researched / missing prereq
const state = await queueStateFromSave(db)
const { task, inventory } = queueTask(state, { type: 'RESEARCH', payload: { techId }, leadCrewId: team.leadCrewId, helperCrewIds: team.helperCrewIds })
await saveTaskRow(db, task)
await saveInventory(db, inventory) // the cost was deducted at queue time
return Response.json(await getTechState(db))
} catch (err) {
if (err instanceof ResearchError) {
if (err instanceof ResearchError || err instanceof TaskError) {
return Response.json({ error: err.message }, { status: err.status })
}
throw err