// The project's referee: five-player identity games (主公, 忠臣, two 反贼, 内奸) on the upstream engine. // check: on ten seeds the route's bot takes every seat in turn while one kind of field bot fills the other four; // the score is the mean over the four roles of the share of games the route's side won (each role a // subtask), over the whole field. // practice: one seed against each field bot, the route as 主公 and as a 反贼, a replay of each in replays/. // Same seeds and same bots, same games: the engine runs on a virtual clock and its shuffles are seeded. import { mkdirSync, readdirSync, statSync, writeFileSync } from 'node:fs'; import path from 'node:path'; import { Table, Heuristic, passIndex, type Ask } from './table'; import { Bot, REFEREE } from './bots'; export const ROOT = path.dirname(REFEREE); export const SEEDS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const MAX_MOVES = 3000; const ROLES = ['主公', '忠臣', '反贼', '内奸']; /** One ask to its end: the chooser gets up to three tries (each refused move is shown back to it in obs.refused), * then the seat passes. True when the engine took a move or the ask is gone anyway. */ export async function answer(t: Table, a: Ask, choose: (obs: object, legal: Ask['legal']) => Promise): Promise { const refused: unknown[] = []; for (let k = 0; k < 3; k++) { const move = await choose(refused.length ? { ...a.obs, refused } : a.obs, a.legal); if (move === undefined || move === null) break; if ((await t.act(a.seat, move)) || (await gone(t, a))) return true; refused.push(move); } return pass(t, a); } /** A refused move can still end the ask: a failed transform (解围 as 无懈可击) clears the engine's open asks. */ export async function gone(t: Table, a: Ask): Promise { return (await t.ask(a.seat))?.key !== a.key; } /** A move the engine takes for a seat that gave none: a pass, else what the upstream AI would play, else any. */ export async function pass(t: Table, a: Ask): Promise { const tries: unknown[] = [passIndex(a.legal)]; const h = new Heuristic(); for (let k = 0; k < 4; k++) tries.push(h.act({ ...a.obs, refused: tries }, a.legal)); tries.push(...a.legal.keys()); for (const m of tries) if ((await t.act(a.seat, m)) || (await gone(t, a))) return true; return false; } /** A whole game with every seat a bot process. */ /** A whole game with every seat a bot process; why it stopped when it did not end. */ async function game(t: Table, bots: Bot[]): Promise { for (let moves = 0; !t.over; moves++) { if (moves >= MAX_MOVES) return `${MAX_MOVES} moves`; const asks = await t.asks(); if (!asks.length) { if (!(await t.idle())) return 'stuck: nobody to move'; continue; } const a = asks[0]; if (!(await answer(t, a, (obs, legal) => bots[a.seat].ask({ obs, legal })))) return `stuck: seat ${a.seat} has no move the engine takes (${a.obs.pending?.requestType})`; } return ''; } export function routeName(): string { const d = (process.env.SS_ROUTE_DIR ?? 'routes').replace(/\/+$/, ''); return d === 'routes' ? 'main' : d.split('/').slice(1).join('/'); } export function field(): string[] { const dir = path.join(ROOT, 'field'); return readdirSync(dir).filter((d) => !d.startsWith('.') && statSync(path.join(dir, d)).isDirectory()).sort(); } /** replays/ per game, and replays/matches.json saying who sat where and how it ended (as kit/league.py). */ export class Replays { private index: Array<{ file: string; title: string; seats: string[]; result: string }> = []; constructor(private root = 'replays') { mkdirSync(root, { recursive: true }); } add(file: string, data: unknown, title: string, seats: string[], result: string): void { writeFileSync(path.join(this.root, file), JSON.stringify(data)); this.index.push({ file, title, seats, result }); writeFileSync(path.join(this.root, 'matches.json'), JSON.stringify(this.index, null, 2)); } } export function writeMetrics(score: number, subtasks: Record, file = 'metrics.json'): void { const r = (x: number) => Math.round(x * 1e6) / 1e6; const out = { score: r(score), subtasks: Object.fromEntries(Object.entries(subtasks).map(([k, v]) => [k, r(v)])) }; writeFileSync(file, JSON.stringify(out, null, 2)); console.log(`score ${out.score} over ${Object.keys(subtasks).length}: ${Object.entries(out.subtasks).map(([k, v]) => `${k} ${v}`).join(', ')}`); } export async function play(mode: string, route: string): Promise { if (mode !== 'check' && mode !== 'practice') throw new Error('usage: node referee/play.mjs check|practice '); const name = routeName(); const me = new Bot(route); const reps = mode === 'practice' ? new Replays() : null; const won: Record = Object.fromEntries(ROLES.map((r) => [r, []])); for (const opp of field()) { const others = [0, 1, 2, 3].map(() => new Bot(path.join(ROOT, 'field', opp))); await Promise.all([me.ready, ...others.map((b) => b.ready)]); for (const seed of mode === 'check' ? SEEDS : SEEDS.slice(0, 1)) { const roles = await identities(seed); const mine = mode === 'check' ? [0, 1, 2, 3, 4] : [0, roles.indexOf('反贼')]; for (const seat of mine) { const bots = [...others.slice(0, seat), me, ...others.slice(seat)]; const names = bots.map((b) => (b === me ? name : opp)); const t = new Table(seed); await t.open(names); const why = await game(t, bots); const role = t.identity(seat); const win = t.scores()[String(seat)] > 0; won[role].push(win ? 1 : 0); console.log(`seed ${seed}, ${name} as ${role} (seat ${seat}) vs ${opp}: ${t.result()}${why ? ` (${why})` : ''}`); reps?.add(`${opp}-${seed}-${seat}.json`, { ...(t.replay(`${name} vs ${opp}`) as object), shoal: { seat, names } }, `${name} as ${role}, four ${opp} bots, seed ${seed}`, names, `${t.result()}: ${name} ${win ? 'won' : 'lost'}`); } } others.forEach((b) => b.close()); } me.close(); const rate = (xs: number[]) => (xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : 0); const subtasks = Object.fromEntries(ROLES.filter((r) => won[r].length).map((r) => [r, rate(won[r])])); if (mode === 'check') writeMetrics(rate(Object.values(subtasks)), subtasks); } /** Who holds which identity on a seed (the lord is always seat 0; the rest follow the seeded shuffle). */ async function identities(seed: number): Promise { const t = new Table(seed); await t.open(['', '', '', '', '']); return t.state.players.map((p) => p.identity ?? ''); }