Files
Flight-Ops/lib/db/schema.ts
T
kamilkrzywda 69b708dad3
CI / build + unit + e2e (pull_request) Failing after 6m37s
Flight queue backend: planes hold a queue of upcoming legs, auto-chain on landing (ISS-043 part 1)
- planes.queue TEXT NOT NULL DEFAULT '[]' — ordered JSON array of not-yet-started
  leg ICACs; active leg stays destIcao + departureSimTime. DDL + guarded ALTER
  migration for existing DBs; seed sets queue: [] explicitly.
- deliverPackages() auto-chains on arrival: with a non-empty queue and fuel
  covering the first queued leg (same requiredFuelFor rule as /fly), the plane
  immediately starts that leg from the arrival airport (stays 'flying',
  departureSimTime = arrival instant, queue shortened). Short tank → grounds
  safely with the queue kept intact; empty queue → unchanged grounding. Cargo
  delivery for the reached airport runs first and is unaffected. Ordering kept:
  delivery still runs before tickOnce's pause check, so a chained plane counts
  as still flying — no premature last-plane pause (ISS-041 preserved).
- New endpoints via performAction(): POST /api/planes/{planeId}/queue {destIcao}
  (append; works grounded or flying; 400 bad body/unknown ICAO, 404 unknown
  plane) and POST /api/planes/{planeId}/queue/cancel {index} (remove queued leg
  by index; active in-flight leg never affected; 400 non-integer/out-of-range,
  404 unknown plane). Both return the full state snapshot.
- PlaneState.queue: string[] in /api/state (null/malformed-safe parse helper
  shared with the engine); OpenAPI updated (PlaneState.queue, QueueLegRequest,
  CancelQueuedLegRequest, both paths) and openapi.test.ts expectations extended.
- Tests: auto-chain block in engine.test.ts (chain / short-fuel ground / empty
  queue) + chained-plane-no-pause regression guard; queueLeg/cancelQueuedLeg
  blocks in actions.test.ts; type-compat queue: [] fixtures in deriveEvents,
  right-pane, mapData and seed tests.

v0.25.0 — part 1 (backend) of ISS-043; UI/map rendering lands in a follow-up.
2026-09-20 10:39:00 +00:00

66 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core';
// Drizzle schema for the game state. Column names/types here are the source of
// truth; the matching CREATE TABLE statements live in lib/db/client.ts (DDL) and
// must stay in sync with this file.
export const planes = sqliteTable('planes', {
id: text('id').primaryKey().notNull(),
model: text('model').notNull(),
status: text('status', { enum: ['available', 'flying'] }).notNull(),
fuel: real('fuel').notNull(),
maxFuel: real('max_fuel').notNull(),
locationIcao: text('location_icao').notNull(),
// Current flight destination (v0.12.0) — set by fly, cleared on arrival. The
// plane's origin is always its locationIcao while flying.
destIcao: text('dest_icao'),
lat: real('lat').notNull(),
lon: real('lon').notNull(),
// Sim-clock departure time of the current flight (null when available). While
// flying, `fuel` is frozen at its departure value — live position/fuel are pure
// functions of the clock (lib/sim/movement.ts), recomputed on read.
departureSimTime: integer('departure_sim_time'),
// Queue of upcoming legs NOT yet started (v0.25.0, ISS-043) — ordered JSON array
// of ICAO strings ('[]' when empty). The ACTIVE leg stays destIcao +
// departureSimTime; on landing the engine auto-chains queue[0] if fuel covers it.
queue: text('queue').notNull().default('[]'),
});
// Cargo packages (v0.12.0) replace the old contracts table end-to-end: a package
// is one cargo unit origin → dest with a reward and deadline. 'loaded' = bound to
// a plane's slot; it stays bound ('expired') or pays out ('delivered') when the
// plane arrives at its destination airport.
export const packages = sqliteTable('packages', {
id: text('id').primaryKey().notNull(),
originIcao: text('origin_icao').notNull(),
destIcao: text('dest_icao').notNull(),
reward: real('reward').notNull(),
cargoType: text('cargo_type').notNull(),
cargoCount: integer('cargo_count').notNull(),
deadlineSimTime: integer('deadline_sim_time').notNull(),
// 'expired' = an open OR loaded package whose deadline passed on a tick. Loaded
// expired packages keep occupying their slot, deliver no reward, and abandon free.
status: text('status', { enum: ['open', 'loaded', 'delivered', 'expired', 'abandoned'] }).notNull(),
planeId: text('plane_id'),
createdSimTime: integer('created_sim_time').notNull(),
});
export const account = sqliteTable('account', {
cash: real('cash').notNull(),
reputation: real('reputation').notNull(),
});
export const simState = sqliteTable('sim_state', {
clockMs: integer('clock_ms').notNull(),
timeScale: real('time_scale').notNull(),
paused: integer('paused', { mode: 'boolean' }).notNull(),
// Real-time (Date.now()) of the last tick — the tick-on-read engine advances
// clockMs by (now - lastRealTickMs) × timeScale, clamped per tick.
lastRealTickMs: integer('last_real_tick_ms').notNull(),
});
export type PlaneRow = typeof planes.$inferSelect;
export type PackageRow = typeof packages.$inferSelect;
export type AccountRow = typeof account.$inferSelect;
export type SimStateRow = typeof simState.$inferSelect;