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
CI / build + unit + e2e (pull_request) Failing after 3m58s
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { getGameDb } from '@/lib/db/client'
|
||||
import { ShipError, expandSystem } from '@/lib/game/ship'
|
||||
import { parseCrewTeam } from '@/app/api/crew-team'
|
||||
import { checkExpandPreconditions, getShipState, ShipError } from '@/lib/game/ship'
|
||||
import { queueStateFromSave, saveInventory, saveTaskRow } from '@/lib/game/save'
|
||||
import { queueTask, TaskError } from '@/lib/game/tasks'
|
||||
|
||||
// Raise a built ship system's capacity by one (capped at max capacity). Body: { "systemId": number }.
|
||||
// Costs inventory items + a turn; returns the fresh ship state.
|
||||
// Raise a built ship system's capacity by one (capped at max capacity): assign the crew team
|
||||
// (lead + helpers) and queue the SHIP_EXPAND task — the cost (`capacity` units of any
|
||||
// resource) is deducted at queue time and the capacity bump lands when the task completes on
|
||||
// the turn tick (runTurn). Body: { systemId, leadCrewId, helperCrewIds? }. Queueing spends no
|
||||
// turn; returns the same fresh ship state the instant action returned.
|
||||
export async function POST(req: Request) {
|
||||
let body: unknown
|
||||
try {
|
||||
@@ -15,12 +21,21 @@ export async function POST(req: Request) {
|
||||
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()
|
||||
return Response.json(await expandSystem(db, systemId))
|
||||
await checkExpandPreconditions(db, systemId) // existing pre-checks: 404 unknown · 409 not built / max capacity
|
||||
const state = await queueStateFromSave(db)
|
||||
const { task, inventory } = queueTask(state, { type: 'SHIP_EXPAND', payload: { systemId }, 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 getShipState(db))
|
||||
} catch (err) {
|
||||
if (err instanceof ShipError) {
|
||||
if (err instanceof ShipError || err instanceof TaskError) {
|
||||
return Response.json({ error: err.message }, { status: err.status })
|
||||
}
|
||||
throw err
|
||||
|
||||
Reference in New Issue
Block a user