Overnight build by 17 Claude agents (2026-11-16 22:45 → 01:30 EST), coordinated in [claude/wikideck milestone 63](https://gitea.eom.dev/claude/wikideck/milestone/63). **For Eric's review. Do not merge without it.** The full write-up is in `docs/night/REPORT.md`. **What's in it** (38 reviewed PRs, squash-merged into `night/2026-11-16`): - **Edit War:** - a 3-lane, 6-turn card game built on what WikiDeck cards actually hold - a pure, deterministic, off-chain rules engine (`shared/game/`, with the API in `docs/game/ENGINE_API.md`); rules are data and v1.1 is the default - a mobile-first Play UI with a lobby, deck builder, four loaner starter decks, a match against the AI, results and a tutorial - a landing page (fixes #3) - **Design and balance:** `docs/game/DESIGN.md`, `CARD_DATA.md` (236 sampled articles) and `BALANCE.md` (bot simulations). Starter pairings went from 23–77% on v1 to roughly even on v1.1. - **eom.dev accounts:** Dex OIDC with PKCE, plus off-chain wallet linking, where the client builds the message it signs (`server/account/`, `src/auth/`). - **Peer-hosted game modes:** modes are JSON data, never code. Node API at `/api/game/*`, with host credit and leaderboards. Three example modes (`shared/game/modes.ts`, `server/game/`, `docs/game/HOSTING.md`). - **Mobile:** Capacitor 7 Android app (a debug APK played a full match against the AI in the emulator), and an iOS project waiting on Xcode (`docs/night/ANDROID.md`, `docs/night/IOS.md`). - **Testnet tooling:** `deploy/night/` (LAN-only, 3 nodes) and an optional `WIKIDECK_USER_AGENT` (the default is unchanged). **Not touched:** consensus and protocol (`server/chain.ts`, `shared/protocol.ts`, `server/testnet/{consensus,ledger,…}.ts`, `native/`), the live deployment and mainnet. Matches run off-chain and never move or burn NFTs. **Checks:** the final integration gate passed in `node:24-alpine`: `npm test` 241/242 with 0 fail (1 skipped), and `npm run build` passes. The repo needs Node 24, because Node 20 fails 12 tests falsely. **Decisions for Eric** (see REPORT.md, Known issues and Next steps): 1. whether the live node should set `WIKIDECK_USER_AGENT` 2. the Xcode licence, which you should accept yourself 3. #49 in the fork, a bot change held after the freeze 4. on-chain host rewards (proposal only, in HOSTING.md) 🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: #22 Co-authored-by: Claude <claude@eom.dev>
132 lines
5.7 KiB
TypeScript
132 lines
5.7 KiB
TypeScript
// Reference bots: random, greedy (one-ply) and flat Monte Carlo lookahead.
|
|
// Each factory takes an EngineAdapter and returns a pure BotFn (state, player, rng) => action.
|
|
import type { BotFn, EngineAdapter, Rng, Seat } from './types.ts';
|
|
|
|
export type * from './types.ts';
|
|
|
|
export function mulberry32(seed: number): Rng {
|
|
let a = seed >>> 0;
|
|
return () => {
|
|
a = (a + 0x6d2b79f5) >>> 0;
|
|
let t = a;
|
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
const pick = <T>(xs: T[], rng: Rng): T => xs[Math.floor(rng() * xs.length)];
|
|
|
|
/** The state a bot reasons about: a determinization of its observation (or the state itself if the engine has no views). */
|
|
function see<S, A>(engine: EngineAdapter<S, A>, obs: unknown, seat: Seat, rng: Rng): S {
|
|
return engine.assume ? engine.assume(obs, seat, rng) : (obs as S);
|
|
}
|
|
|
|
function terminalValue<S, A>(engine: EngineAdapter<S, A>, s: S, seat: Seat): number {
|
|
const { winner } = engine.result(s);
|
|
return winner === null ? 0 : winner === seat ? 1 : -1;
|
|
}
|
|
|
|
function value<S, A>(engine: EngineAdapter<S, A>, s: S, seat: Seat): number {
|
|
return engine.isOver(s) ? terminalValue(engine, s, seat) * 10 : engine.evaluate(s, seat);
|
|
}
|
|
|
|
/** Uniformly random legal action. */
|
|
export function randomBot<S, A>(engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
return (obs, player, rng) => pick(engine.legalActions(see(engine, obs, player, rng), player), rng);
|
|
}
|
|
|
|
function greedyChoice<S, A>(engine: EngineAdapter<S, A>, state: S, seat: Seat, actions: A[], rng: Rng): A {
|
|
if (actions.length === 1) return actions[0];
|
|
let best: A[] = [];
|
|
let bestV = -Infinity;
|
|
for (const a of actions) {
|
|
const v = value(engine, engine.applyAction(state, a), seat);
|
|
if (v > bestV + 1e-9) { bestV = v; best = [a]; }
|
|
else if (Math.abs(v - bestV) <= 1e-9) best.push(a);
|
|
}
|
|
return pick(best, rng);
|
|
}
|
|
|
|
/** One-ply: take the action whose resulting state evaluates best for us. Ties broken randomly. */
|
|
export function greedyBot<S, A>(engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
return (obs, player, rng) => {
|
|
const state = see(engine, obs, player, rng);
|
|
return greedyChoice(engine, state, player, engine.legalActions(state, player), rng);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Naive "always play the highest-power card" player (DESIGN §8 target: < 55% vs a smart bot).
|
|
* Plays the highest printed-power affordable card into a random legal lane; ends the turn when nothing fits.
|
|
*/
|
|
export function maxPowerBot<S, A>(engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
return (obs, player, rng) => {
|
|
const state = see(engine, obs, player, rng);
|
|
const actions = engine.legalActions(state, player);
|
|
if (!engine.actionPower) return greedyChoice(engine, state, player, actions, rng);
|
|
let best: A[] = [], bestP = -Infinity;
|
|
for (const a of actions) {
|
|
const p = engine.actionPower(state, a);
|
|
if (p > bestP) { bestP = p; best = [a]; } else if (p === bestP) best.push(a);
|
|
}
|
|
return pick(best, rng);
|
|
};
|
|
}
|
|
|
|
export interface LookaheadOptions { k?: number; rollouts?: number; horizon?: number; epsilon?: number }
|
|
|
|
/**
|
|
* Flat Monte Carlo lookahead: shortlist the top-k greedy candidates, then for each run
|
|
* `rollouts` epsilon-greedy playouts for both seats, up to `horizon` actions, scoring
|
|
* terminal results (or the clamped heuristic at the horizon).
|
|
* With an engine that has views (observe/assume, e.g. shared/game), the bot sees only its seat's view:
|
|
* every rollout starts from a fresh determinization (hidden cards resampled), so it is not clairvoyant.
|
|
* Without views (toy engine) rollouts use the true state.
|
|
*/
|
|
export function lookaheadBot<S, A>(engine: EngineAdapter<S, A>, opts: LookaheadOptions = {}): BotFn<unknown, A> {
|
|
const k = opts.k ?? 4, rollouts = opts.rollouts ?? 8, horizon = opts.horizon ?? 40, eps = opts.epsilon ?? 0.25;
|
|
return (obs, seat, rng) => {
|
|
const state = see(engine, obs, seat, rng);
|
|
const actions = engine.legalActions(state, seat);
|
|
if (actions.length === 1) return actions[0];
|
|
const shortlist = actions.map(a => {
|
|
const next = engine.applyAction(state, a);
|
|
return { a, next, v: value(engine, next, seat) };
|
|
}).sort((x, y) => y.v - x.v).slice(0, k);
|
|
let best = shortlist[0], bestScore = -Infinity;
|
|
for (const c of shortlist) {
|
|
let total = 0;
|
|
for (let r = 0; r < rollouts; r++) {
|
|
let s = engine.assume ? engine.applyAction(engine.assume(obs, seat, rng), c.a) : c.next, steps = 0; // fresh hidden sample per rollout
|
|
while (!engine.isOver(s) && steps < horizon) {
|
|
const who = engine.toAct(s);
|
|
const legal = engine.legalActions(s, who);
|
|
if (!legal.length) break;
|
|
const a = rng() < eps ? pick(legal, rng) : greedyChoice(engine, s, who, legal, rng);
|
|
s = engine.applyAction(s, a);
|
|
steps++;
|
|
}
|
|
total += engine.isOver(s) ? terminalValue(engine, s, seat) : Math.max(-1, Math.min(1, engine.evaluate(s, seat)));
|
|
}
|
|
const score = total / rollouts + c.v * 1e-3; // tiny greedy tiebreak
|
|
if (score > bestScore) { bestScore = score; best = c; }
|
|
}
|
|
return best.a;
|
|
};
|
|
}
|
|
|
|
export const BOT_NAMES = ['random', 'maxpower', 'greedy', 'lookahead-lite', 'lookahead'] as const;
|
|
export type BotName = typeof BOT_NAMES[number];
|
|
|
|
export function makeBot<S, A>(name: string, engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
switch (name) {
|
|
case 'random': return randomBot(engine);
|
|
case 'maxpower': return maxPowerBot(engine);
|
|
case 'greedy': return greedyBot(engine);
|
|
case 'lookahead': return lookaheadBot(engine);
|
|
case 'lookahead-lite': return lookaheadBot(engine, { k: 3, rollouts: 4, horizon: 24 });
|
|
default: throw new Error(`unknown bot ${name}`);
|
|
}
|
|
}
|