Files
Dune/tools/prototype-room.ts
T
Quintine 8e1cd7b75b Connect Emperor Nexus extra revival with private saved continuation
Add the absent-Emperor Secret Ally alternative returning exactly three eligible forces free in Basic/Advanced base-faction Nexus games. Preserve ordinary force/free-revival allowances and the shared Advanced Fedaykin cap. Keep the action optional and private, consume the physical Nexus once, and validate durable source receipts through later ordinary revival and Ghola. Purchase timing remains pending the earlier uniform winner-payment UX question; partial returns and combined-module interactions remain guarded. No mode or publication gate opens.

Connect private controls, explicit physical elite selection, all four legal AI profiles and a dedicated rules checklist entry. Extend the reusable local prototype writer with genuine twelve-card Nexus setup. Independent review covers rules timing, private projection, physical custody, JSON and production SQLite restore, lost-response replay and competing requests. Seventeen focused effect cases and four prototype-writer cases pass.

Validation: typecheck, lint, 4649 offline tests, production build and 43 HTTP tests passed with unchanged source. Genuine-start Basic and Advanced samples completed in 715 and 263 actions with 19 and 7 periodic JSON restores, respectively, and no Emperor uses. Two explicitly staged continuations completed in 18 and 17 actions with one normal-force Emperor return each. All four had zero rejections and preserved exact Treachery, force, elite and Nexus custody; all fourteen final seat views restored unchanged. They are bounded continuation evidence, not natural full-game acceptance of the effect or AI calibration.

In the conserved staged browser scenario, Advanced Fremen returned three forces including one Fedaykin using the Nexus, then three normal forces using the ordinary allowance. Reserves rose from three to nine, Tanks fell from seven to one, spice stayed three and the physical Nexus was spent once. The inspector was readable, the action notice displayed, and pending/resolved refresh restoration, physical custody and private projection passed.

After the reported power outage, all 733 opening games and original seat/recovery records were preserved (767 after HTTP checks); database integrity and foreign keys passed. All 734 rooms immediately before broad verification remained unchanged. Reused the healthy server without restart, reset or recurring automation.

Verified source tree: fd09a4bc71018cb7ef98c7e25c25dacc495ffed304dbd0cdfa223e539e8fa5e1
2026-09-14 01:05:11 +10:00

99 lines
3.0 KiB
TypeScript

import type { DatabaseSync } from 'node:sqlite';
import {
initializeIxGameForAudit,
initializeDiscoveryGameForAudit,
initializeLeaderSkillsGameForAudit,
initializeFactionExpansionsGameForAudit,
initializeNexusGameForAudit,
viewGame,
type Game,
} from '../game/engine';
export const PROTOTYPE_PROFILES = [
'ix',
'discovery',
'leader-skills',
'factions',
'nexus',
] as const;
export type PrototypeProfile = (typeof PROTOTYPE_PROFILES)[number];
export function isPrototypeProfile(value: string): value is PrototypeProfile {
return PROTOTYPE_PROFILES.some((profile) => profile === value);
}
/** Local development only. This cannot create a seat, change its owner, or
* redeal a started game. The caller backs up the database before invoking it. */
export function startIxPrototypeRoom(
db: DatabaseSync,
code: string,
expectedVersion: number,
) {
return startPrototypeRoom(db, code, expectedVersion, 'ix');
}
export function startPrototypeRoom(
db: DatabaseSync,
code: string,
expectedVersion: number,
profile: PrototypeProfile,
) {
if (
!/^[A-Z0-9]{8}$/.test(code) ||
!Number.isSafeInteger(expectedVersion) ||
expectedVersion < 0
)
throw new Error('Provide an eight-character room and its current version.');
const row = db
.prepare('SELECT state, version FROM rooms WHERE code = ?')
.get(code);
if (!row || typeof row.state !== 'string' || row.version !== expectedVersion)
throw new Error(
'The room is missing or changed. Refresh before starting the prototype.',
);
const initial = JSON.parse(row.state) as Game;
if (initial.code !== code)
throw new Error('The room identity is inconsistent.');
if (!isPrototypeProfile(profile))
throw new Error('Unknown prototype profile.');
const game =
profile === 'ix'
? initializeIxGameForAudit(initial)
: profile === 'factions'
? initializeFactionExpansionsGameForAudit(initial)
: profile === 'nexus'
? initializeNexusGameForAudit({
...initial,
nexusCards: initial.nexusCards ?? { cards: null, phase: null },
})
: profile === 'leader-skills'
? initializeLeaderSkillsGameForAudit(initial)
: initializeDiscoveryGameForAudit({
...initial,
discoveryEnabled: true,
});
// Exercise the same player projection before accepting the new saved state.
for (const player of game.players) viewGame(game, player.id);
const version = expectedVersion + 1;
game.version = version;
const result = db
.prepare(
'UPDATE rooms SET state = ?, version = ?, updated_at = ? WHERE code = ? AND version = ? AND state = ?',
)
.run(
JSON.stringify(game),
version,
Date.now(),
code,
expectedVersion,
row.state,
);
if (result.changes !== 1)
throw new Error('The lobby changed during setup. No prototype was saved.');
return {
code,
version,
status: game.status,
advanced: game.advanced,
players: game.players.length,
};
}