diff --git a/src/game/duel/bots.ts b/src/game/duel/bots.ts new file mode 100644 index 0000000..25cdd5d --- /dev/null +++ b/src/game/duel/bots.ts @@ -0,0 +1,83 @@ +// View-only fallback duel bots for the browser bot match. They see only `viewFor` output (Night 1 rule, #5). +// sim-balance owns the real duel bots (shared/bots/duel-bots.ts); these are swapped for its heuristic +// once a PlayerView lens exists (#64). Deterministic given the rng. +import type { DuelAction, PermanentView, PlayerView, Seat } from './contract.ts'; +import type { DuelBot } from './decks.ts'; + +const value = (p: PermanentView) => p.power + p.toughness + p.keywords.length; +const opp = (s: Seat) => (1 - s) as Seat; + +function chooseNormal(v: PlayerView, legal: DuelAction[], rng: () => number): DuelAction { + const me = v.seat, them = opp(me); + const mine = v.battlefield.filter(p => p.controller === me), theirs = v.battlefield.filter(p => p.controller === them); + const find = (t: T) => legal.filter((a): a is Extract => a.type === t); + + if (find('keep').length) return find('keep')[0]; + + const plays = find('play'); + if (plays.length) { + const hand = new Map(v.you.hand.map(i => [i.uid, i.card])); + // Prefer the most expensive card. Targets: harmful effects (damage, destroy, bounce, tap, fight, a + // negative pump) aim at the opponent's side; everything else (pumps, Adopt) at ours. + const owner = (uid: string) => v.battlefield.find(p => p.uid === uid)?.controller; + const harmful = (effects: unknown[]) => effects.some(e => { + const x = e as { op?: string; power?: number; amount?: number }; + return ['damage', 'damageEach', 'destroy', 'bounce', 'tap', 'fight', 'drain'].includes(x.op ?? '') || (x.op === 'pump' && ((x.power ?? x.amount ?? 0) < 0)); + }); + const score = (a: Extract) => { + const c = hand.get(a.uid); + if (!c) return -1; + const hostile = harmful(c.effects); + let s = c.cost * 10 + rng(); + for (const t of a.targets ?? []) s += ((t.kind === 'card' ? owner(t.uid) : t.seat) === them) === hostile ? 3 : -3; + return s; + }; + return plays.reduce((b, a) => (score(a) > score(b) ? a : b)); + } + + const toggles = find('toggleAttacker'); + if (toggles.length || find('confirmAttack').length) { + const blockers = theirs.filter(p => !p.tapped && (p.card.type === 'Figure' || p.toughness > 0)); + const ready = mine.filter(p => v.combat.attackers.includes(p.uid) || toggles.some(t => t.uid === p.uid)); + const lethal = ready.map(p => p.power).sort((a, b) => b - a).slice(blockers.length).reduce((t, x) => t + x, 0) >= v.opponent.life; + const want = toggles.find(t => { + const c = mine.find(p => p.uid === t.uid)!; + if (v.combat.attackers.includes(c.uid)) return false; + if (lethal) return true; + return c.power > 0 && !blockers.some(b => b.power >= c.toughness && b.toughness > c.power); + }); + return want ?? find('confirmAttack')[0] ?? legal[0]; + } + + const blocks = find('block'); + if (blocks.length || find('confirmBlocks').length) { + const used = new Set(Object.keys(v.combat.blocks)), covered = new Set(Object.values(v.combat.blocks)); + const incoming = v.combat.attackers.filter(a => !covered.has(a)).reduce((t, a) => t + (theirs.find(p => p.uid === a)?.power ?? 0), 0); + const lethal = incoming >= v.you.life; + let best: DuelAction | undefined, bestScore = 0; + for (const b of blocks) { + if (used.has(b.blocker) || covered.has(b.attacker)) continue; + const B = mine.find(p => p.uid === b.blocker), A = theirs.find(p => p.uid === b.attacker); + if (!A || !B) continue; + const kills = B.power >= A.toughness, dies = A.power >= B.toughness; + const s = kills && !dies ? 10 + value(A) : kills ? (value(A) > value(B) ? 5 : lethal ? 3 : 0) : !dies ? 2 : lethal ? 1 : 0; + if (s > bestScore) { bestScore = s; best = b; } + } + return best ?? find('confirmBlocks')[0] ?? legal[0]; + } + + return find('endPhase')[0] ?? legal[0]; +} + +/** Chance that Easy makes a random legal move (never concede) instead of Normal's. */ +export const EASY_BLUNDER_RATE = 0.3; + +export const NORMAL_BOT: DuelBot = { name: 'Normal', choose: chooseNormal }; +export const EASY_BOT: DuelBot = { + name: 'Easy', + choose(v, legal, rng) { + if (rng() < EASY_BLUNDER_RATE) return legal[Math.floor(rng() * legal.length)]; + return chooseNormal(v, legal, rng); + }, +}; +export const DUEL_BOTS: DuelBot[] = [EASY_BOT, NORMAL_BOT]; diff --git a/tests/ui-duel-bots.test.ts b/tests/ui-duel-bots.test.ts new file mode 100644 index 0000000..2f341a9 --- /dev/null +++ b/tests/ui-duel-bots.test.ts @@ -0,0 +1,53 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { EASY_BOT, NORMAL_BOT } from '../src/game/duel/bots.ts'; +import type { DuelAction, DuelCard, PermanentView, PlayerView } from '../src/game/duel/contract.ts'; + +const card = (id: string, extra: Partial = {}): DuelCard => ({ + cardId: id, pageId: 1, title: id, type: 'Figure', portal: 'Society', cost: 2, power: 2, toughness: 2, keywords: [], effects: [], rulesText: '', flavor: '', ...extra, +}); +const perm = (uid: string, controller: 0 | 1, power: number, toughness: number, extra: Partial = {}): PermanentView => ({ + uid, owner: controller, controller, card: card(uid, { power, toughness }), tapped: false, sick: false, damage: 0, counters: 0, power, toughness, keywords: [], ...extra, +}); +const side = (seat: 0 | 1, life = 20) => ({ seat, life, citations: 3, maxCitations: 3, handSize: 0, librarySize: 30, graveyard: [], bankSeconds: 300, ready: true, mulligansTaken: 0 }); +const view = (p: Partial): PlayerView => ({ + engine: 'duel', seat: 1, turn: 4, active: 1, phase: 'main', toAct: 1, you: { ...side(1), hand: [] }, opponent: side(0), + battlefield: [], combat: { attackers: [], blocks: {} }, result: null, version: 1, rules: {}, ...p, +}); +const rng = () => 0.5; + +test('normal bot: plays the priciest card, harmful effects at the opponent', () => { + const hand = [{ uid: '1:1', owner: 1 as const, card: card('cheap', { cost: 1 }) }, + { uid: '1:2', owner: 1 as const, card: card('bolt', { type: 'Event', cost: 3, power: null, toughness: null, effects: [{ op: 'damage', amount: 3 }] }) }]; + const v = view({ you: { ...side(1), hand }, battlefield: [perm('0:9', 0, 3, 3), perm('1:8', 1, 2, 2)] }); + const legal: DuelAction[] = [{ type: 'play', uid: '1:1' }, + { type: 'play', uid: '1:2', targets: [{ kind: 'card', uid: '1:8' }] }, { type: 'play', uid: '1:2', targets: [{ kind: 'card', uid: '0:9' }] }, { type: 'endPhase' }]; + assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[2]); +}); + +test('normal bot: attacks unless a blocker eats it, all-in on lethal, then confirms', () => { + const bf = [perm('1:1', 1, 3, 3), perm('1:2', 1, 1, 1), perm('0:1', 0, 2, 4)]; + const legal: DuelAction[] = [{ type: 'toggleAttacker', uid: '1:1' }, { type: 'toggleAttacker', uid: '1:2' }, { type: 'confirmAttack' }]; + // 1:1 (3/3) faces a 2/4 that survives but doesn't kill: attack. 1:2 (1/1) would die to it for nothing. + const v = view({ phase: 'attack', battlefield: bf }); + assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[0]); + const after = view({ phase: 'attack', battlefield: bf, combat: { attackers: ['1:1'], blocks: {} } }); + assert.deepEqual(NORMAL_BOT.choose(after, legal.slice(1), rng), { type: 'confirmAttack' }); + const lethal = view({ phase: 'attack', battlefield: bf, opponent: side(0, 1), combat: { attackers: ['1:1'], blocks: {} } }); + assert.deepEqual(NORMAL_BOT.choose(lethal, legal.slice(1), rng), legal[1]); +}); + +test('normal bot: takes the kill-and-survive block, otherwise no block', () => { + const bf = [perm('0:1', 0, 2, 2), perm('1:1', 1, 3, 3), perm('1:2', 1, 1, 1)]; + const v = view({ phase: 'block', active: 0, battlefield: bf, combat: { attackers: ['0:1'], blocks: {} } }); + const legal: DuelAction[] = [{ type: 'block', blocker: '1:1', attacker: '0:1' }, { type: 'block', blocker: '1:2', attacker: '0:1' }, { type: 'confirmBlocks' }]; + assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[0]); + assert.deepEqual(NORMAL_BOT.choose(v, legal.slice(1), rng), { type: 'confirmBlocks' }); +}); + +test('bots always return a listed action and keep their opening hand', () => { + const legal: DuelAction[] = [{ type: 'keep' }, { type: 'mulligan' }]; + assert.deepEqual(NORMAL_BOT.choose(view({ phase: 'mulligan' }), legal, rng), { type: 'keep' }); + let x = 1; const r = () => ((x = (x * 48271) % 2147483647) / 2147483647); + for (let i = 0; i < 50; i++) assert.ok(legal.includes(EASY_BOT.choose(view({ phase: 'mulligan' }), legal, r))); +});