Files
kamilkrzywdaandOpenCode e2936e85bb History: AI-driven alternate-history simulator
Pax Historia-style sim where the LLM is the world engine.

- React 19 + Vite 6 + Tailwind v4 + shadcn/ui frontend
- Express single-port backend (/api + /sse + built client + SPA fallback)
- SQLite (better-sqlite3) persistence, one row per game
- MapLibre choropleth map (OpenFreeMap dark + ISO_A3 regions)
- Defensive turn loop: strip fences, balanced-{} extract, parse, clamp 0-100,
  retry-once nudge, generic 'unclear' fallback; never crashes on bad output
- 5 seed presets (wwii-1939, coldwar-1955, modern-2025, no-wwii-1940, aegis-2400)
- LLM client over native fetch w/ injectable transport (mesh-bot pattern);
  LLM_STUB=1 deterministic transport for tests/e2e
- Unit (vitest, 19) + deterministic Playwright e2e
- Multi-stage node:22-slim Docker + Traefik (history.dev.kkhost.pl:4321)

Co-Authored-By: OpenCode <opencode@users.noreply.github.com>
2026-10-10 21:20:23 +00:00

53 lines
1.5 KiB
TypeScript

// SQLite persistence for games (one row per game id; the full GameState as JSON).
import { mkdirSync } from 'node:fs';
import path from 'node:path';
import Database from 'better-sqlite3';
import type { GameState } from '../shared/types';
export interface Db {
save(state: GameState): void;
load(id: string): GameState | null;
listIds(): string[];
close(): void;
}
export function openDb(filePath: string): Db {
const dir = path.dirname(filePath);
if (dir) mkdirSync(dir, { recursive: true });
const db = new Database(filePath);
db.pragma('journal_mode = WAL');
db.exec(
`CREATE TABLE IF NOT EXISTS games (
id TEXT PRIMARY KEY,
data TEXT NOT NULL,
updated_at TEXT NOT NULL
)`,
);
const upsert = db.prepare(
`INSERT INTO games (id, data, updated_at) VALUES (?, ?, ?)
ON CONFLICT(id) DO UPDATE SET data = excluded.data, updated_at = excluded.updated_at`,
);
const byId = db.prepare('SELECT data FROM games WHERE id = ?');
const all = db.prepare('SELECT id FROM games ORDER BY updated_at DESC');
return {
save(state) {
upsert.run(state.id, JSON.stringify(state), new Date().toISOString());
},
load(id) {
const row = byId.get(id) as { data: string } | undefined;
if (!row) return null;
try {
return JSON.parse(row.data) as GameState;
} catch {
return null;
}
},
listIds() {
return (all.all() as { id: string }[]).map((r) => r.id);
},
close() {
db.close();
},
};
}