forked from WikiDeck/wikideck
Bots: engine hook gets only viewFor output, never MatchState
night-lead's rule on #5. The engine-level chooser now receives view.engine = { view: PlayerView }; a test checks that nothing else reaches it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
// Browser AI opponents (ui-b, issue #5): "easy" and "normal", in ui-a's Bot shape.
|
||||
// Today they play from the MatchView view model, so they work with the mock engine and any
|
||||
// adapter. When the shared/game adapter attaches the engine state (`view.engine`) and each
|
||||
// action's engine form (`action.engine`), an engine-level bot can be plugged in through
|
||||
// `makeUiBot(level, engineChoice)`; ui-b wires sim-balance's shared/bots there (follow-up PR).
|
||||
// Fairness: the bots only read the MatchView, which never contains the human's queued plays.
|
||||
// adapter. When the shared/game adapter attaches the engine's hidden-information view
|
||||
// (`view.engine = { view: viewFor(state, seat) }`) and each action's engine form (`action.engine`),
|
||||
// an engine-level bot can be plugged in through `makeUiBot(level, engineChoice)` (follow-up PR).
|
||||
// Hard rule (night-lead, #5): bots get only `viewFor` output, never the full MatchState.
|
||||
// The MatchView never contains the human's queued plays either.
|
||||
import type { MatchView, UIAction } from '../types.ts';
|
||||
|
||||
export type BotLevel = 'easy' | 'normal';
|
||||
@@ -17,8 +18,8 @@ export type Bot = {
|
||||
choose(view: MatchView, legal: UIAction[], rand: Rng): UIAction;
|
||||
};
|
||||
|
||||
/** Optional engine-level chooser: gets `view.engine` and returns one engine action. */
|
||||
export type EngineChoice = (engineView: { state: unknown; seat: 0 | 1 }, rng: Rng) => unknown;
|
||||
/** Optional engine-level chooser: gets the engine PlayerView (`viewFor` output only) and returns one engine action. */
|
||||
export type EngineChoice = (engineView: { view: unknown }, rng: Rng) => unknown;
|
||||
type WithEngine = { engine?: unknown };
|
||||
|
||||
/** Chance that "easy" makes a random move instead of its best one. */
|
||||
@@ -60,7 +61,7 @@ export function makeUiBot(level: BotLevel, engineChoice?: EngineChoice): Bot {
|
||||
? 'Plays big cards, but often makes a random move. Good for learning.'
|
||||
: 'Plays for the lead in each Topic. It cannot see your hidden plays.',
|
||||
choose(view, legal, rand) {
|
||||
const ev = (view as MatchView & { engine?: { state: unknown; seat: 0 | 1 } }).engine;
|
||||
const ev = (view as MatchView & { engine?: { view: unknown } }).engine;
|
||||
const mapped = legal as (UIAction & WithEngine)[];
|
||||
if (engineChoice && ev && mapped.length && mapped.every(a => a.engine !== undefined)) {
|
||||
const wanted = stable(engineChoice(ev, rand));
|
||||
|
||||
@@ -63,12 +63,14 @@ test('bots never concede or un-queue, and end the turn when nothing fits', () =>
|
||||
});
|
||||
|
||||
test('an engine-level chooser is used when the adapter attaches engine actions', () => {
|
||||
const bot = makeUiBot('normal', (_ev, _r) => ({ type: 'play', player: 1, cardId: 'b', lane: 2 }));
|
||||
const seen: unknown[] = [];
|
||||
const bot = makeUiBot('normal', (ev, _r) => { seen.push(ev); return { type: 'play', player: 1, cardId: 'b', lane: 2 }; });
|
||||
const legal = [
|
||||
{ type: 'play', cardId: 'a', lane: 0, engine: { type: 'play', player: 1, cardId: 'a', lane: 0 } },
|
||||
{ type: 'play', cardId: 'b', lane: 2, engine: { lane: 2, cardId: 'b', player: 1, type: 'play' } },
|
||||
{ type: 'endTurn', engine: { type: 'commit', player: 1 } },
|
||||
] as unknown as UIAction[];
|
||||
const view = { hand: [], lanes: [], engine: { state: {}, seat: 1 } } as unknown as MatchView;
|
||||
const view = { hand: [], lanes: [], engine: { view: { you: 1 } } } as unknown as MatchView;
|
||||
assert.equal(bot.choose(view, legal, rand(1)), legal[1]);
|
||||
assert.deepEqual(seen, [{ view: { you: 1 } }], 'the engine chooser gets only the viewFor output');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user