+24
-2
@@ -9,11 +9,16 @@ import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { deepClone } from '../shared/protocol';
|
||||
import type { AddActionRequest, AddDiplomacyRequest, CreateGameRequest } from '../shared/protocol';
|
||||
import type {
|
||||
AddActionRequest,
|
||||
AddDiplomacyRequest,
|
||||
CreateGameRequest,
|
||||
RemoveQueueItemRequest,
|
||||
} from '../shared/protocol';
|
||||
import type { GameState } from '../shared/types';
|
||||
import { createLlmClient } from './llm';
|
||||
import { makeStubLlmClient } from './stub';
|
||||
import { askAdvisor, queueRoundItem, runTurn } from './engine';
|
||||
import { askAdvisor, queueRoundItem, removeRoundItem, runTurn } from './engine';
|
||||
import { buildRegionMap } from './map';
|
||||
import { createPresetStore } from './presets';
|
||||
import { openDb, type Db } from './db';
|
||||
@@ -114,6 +119,7 @@ app.post('/api/games', (req: Request, res) => {
|
||||
nations: Object.fromEntries(preset.nations.map((n) => [n.id, deepClone(n)])),
|
||||
events: [],
|
||||
pending: [],
|
||||
version: 1, // the first saved revision; every later mutation bumps it
|
||||
};
|
||||
db.save(state);
|
||||
res.json({ game: state });
|
||||
@@ -179,6 +185,22 @@ app.post('/api/games/:id/diplomacy', (req, res) => {
|
||||
res.json({ game: next });
|
||||
});
|
||||
|
||||
// Remove one item from the pending round, by its index in `game.pending`. No
|
||||
// LLM call, no turn advance — same fast save → broadcast → respond order as the
|
||||
// add routes above. The mutated state bumps `version`, so clients (which apply
|
||||
// states "never go backwards") always reconcile to it.
|
||||
app.post('/api/games/:id/queue/remove', (req, res) => {
|
||||
const id = req.params.id;
|
||||
const game = db.load(id);
|
||||
if (!game) return res.status(404).json({ error: 'game not found' });
|
||||
const index = (req.body as RemoveQueueItemRequest | undefined)?.index;
|
||||
const next = removeRoundItem(game, index);
|
||||
if (!next) return res.status(400).json({ error: 'invalid queue index' });
|
||||
db.save(next);
|
||||
broadcast(id, 'state', { state: next });
|
||||
res.json({ game: next });
|
||||
});
|
||||
|
||||
// Jump forward: commit the whole pending round (or an empty one) to the model
|
||||
// in a single turn. applyTurnResult clears the pending queue on the returned
|
||||
// state. 404 for not-found/preset-missing; 502 for LLM/model (transport) errors.
|
||||
|
||||
Reference in New Issue
Block a user