Files
Dune/game/lobby-bot-configuration.ts
Quintine b9718acda8 Configure existing AI seats in the lobby
Let the host change an existing permanent AI seat's difficulty, faction and player circle without removing and readding it. Shared strict validation limits configuration to the authenticated host in the lobby, existing profiles, enabled available factions and unoccupied circles. Preserve seat identity and initialize changed faction rosters consistently. Changes reset human readiness, keep bots ready and explain the changed fields in public history.

Use existing AI strategies without calibration or strength changes. Full AI development waits for all non-AI game features; the goal retains the three adjacent 75% targets, mandatory Git checkpoints and selective subagents without a usage cutoff. No mode or publication gate opens.

Independent implementation and review cover the quote, engine, controls and persistence tests. Types, lint, 4916 offline tests, production build and 46 HTTP checks pass on stable final source. Ten new offline cases cover all factions/profiles, strict guards, roster/identity, immutable rejection, restart/privacy and both configuration-versus-start race outcomes. HTTP verifies authentication, host authority, CSRF, stale/future requests, private views and saved settings. Identical direct actions preserve players/readiness/history but retain the generic action store's version increment; controls suppress them.

Browser QA changed difficulty, faction and circle, verified readiness reset and history, restored settings after refresh and used keyboard selection. Expanded mobile cards were widened after discovering cramped controls; measured selects/buttons are 272 by 44 pixels at a 390-pixel viewport without document overflow. Desktop controls measure 169 by 44 pixels. Screenshot capture timed out, so screenshot-based visual acceptance remains unverified. Configuration controls disappear after starting and saved AI settings persist in setup. The final source rerun follows a preliminary run invalidated by the mobile/readability edits.

All 1262 original rooms, 2644 seats and every prior recovery/entry/handover/delegation/migration row remain unchanged. SQLite integrity and foreign keys pass. No database reset, migration, server restart, usage check or automation was performed. Broader rules, components, durable unattended continuation and final AI acceptance remain unfinished.

Verified source tree: 8c1baf4602d000ee37f081b2a03f8a87e89a63ef14a7121f24268c575a1ac281
2026-09-20 16:57:38 +10:00

113 lines
3.5 KiB
TypeScript

import { DIFFICULTIES, type Difficulty } from './bot-profiles';
import { FACTIONS, faction, type FactionId } from './catalog';
export type LobbyBotConfiguration = Readonly<{
target: string;
difficulty: Difficulty;
faction: FactionId;
position: number;
}>;
export type LobbyBotConfigurationContext = Readonly<{
status: string;
host: string;
players: ReadonlyArray<
Readonly<{
id: string;
faction: FactionId;
bot?: Difficulty;
}>
>;
expansions: readonly string[];
positions: Readonly<Record<string, number>>;
}>;
export type LobbyBotConfigurationQuote =
| Readonly<{
ok: true;
configuration: LobbyBotConfiguration;
changed: boolean;
}>
| Readonly<{ ok: false; reason: string }>;
const rejection = (reason: string): LobbyBotConfigurationQuote => ({
ok: false,
reason,
});
export function quoteLobbyBotConfiguration(
context: LobbyBotConfigurationContext,
actorId: string,
input: unknown,
): LobbyBotConfigurationQuote {
if (context.status !== 'lobby')
return rejection('AI seats can be configured only in the lobby.');
if (actorId !== context.host)
return rejection('Only the host can configure AI seats.');
if (!input || typeof input !== 'object' || Array.isArray(input))
return rejection('Choose an AI seat configuration.');
const value = input as Record<string, unknown>;
const keys = ['type', 'target', 'difficulty', 'faction', 'position'];
if (
Object.keys(value).length !== keys.length ||
!keys.every((key) => Object.hasOwn(value, key)) ||
value.type !== 'configureBot'
)
return rejection(
'Choose only the AI seat, difficulty, faction and player circle.',
);
if (typeof value.target !== 'string')
return rejection('Choose a permanent AI seat.');
const target = context.players.find((player) => player.id === value.target);
if (!target?.bot) return rejection('Choose a permanent AI seat.');
if (
typeof value.difficulty !== 'string' ||
!DIFFICULTIES.some((difficulty) => difficulty === value.difficulty)
)
return rejection('Choose a valid AI difficulty.');
if (
typeof value.faction !== 'string' ||
!FACTIONS.some((entry) => entry.id === value.faction)
)
return rejection('Choose a valid faction.');
const selectedFaction = value.faction as FactionId;
const expansion = faction(selectedFaction).expansion;
if (expansion !== 'base' && !context.expansions.includes(expansion))
return rejection('That faction expansion is disabled.');
if (
context.players.some(
(player) => player.id !== target.id && player.faction === selectedFaction,
)
)
return rejection('That faction is taken.');
if (
typeof value.position !== 'number' ||
!Number.isSafeInteger(value.position) ||
value.position < 1 ||
value.position > 6
)
return rejection('Player circle must be an integer from 1 to 6.');
if (
context.players.some(
(player) =>
player.id !== target.id &&
context.positions[player.id] === value.position,
)
)
return rejection('That player circle is occupied.');
const configuration: LobbyBotConfiguration = {
target: target.id,
difficulty: value.difficulty as Difficulty,
faction: selectedFaction,
position: value.position,
};
return {
ok: true,
configuration,
changed:
target.bot !== configuration.difficulty ||
target.faction !== configuration.faction ||
context.positions[target.id] !== configuration.position,
};
}