import type { LaxPartial } from "@samual/lib" import { doMove } from "./doMove" import type { BinmatArgs } from "./generateArgs" import { generateArgsForAttacker, generateArgsForDefender } from "./generateArgs" import { makeState } from "./makeState" import { parseMove } from "./parseMove" import type { InjectShuffleOptions, State } from "./shared" import { MoveKind, Role, StatusCode, StatusCodeMessages } from "./shared" export type SimulateGameOptions = InjectShuffleOptions & { /** How many milliseconds the brain is allowed @default 5000 */ timeLimit: number /** @default "defender" */ defenderUserName: string /** @default "attacker" */ attackerUserName: string /** Pass instead of throwing when brain acts incorrectly (too slow, making move twice, etc) @default false */ noThrow: boolean /** Callback that is called after every move */ onMove: (state: State, binlog: string[]) => void /** Initial state of the game */ state: State } export type CLIContext = { /** The name of the user who is calling the script (i.e. n00b) */ caller: string /** The name of this script */ this_script: string /** The number of columns in the caller’s terminal, if reported by the client */ cols: number /** The number of rows in the caller’s terminal, if reported by the client */ rows: number /** The name of the script that directly called this script, or null if called on the command line or as a * scriptor */ calling_script: null } export type TransformScript = (args: { op: string }) => { ok: boolean } export type BrainScript = (context: CLIContext, args: BinmatArgs, xform: TransformScript) => void /** * @param defenderBrain defender brain script but with extra `xform` parameter to replace `#fs.binmat.x()` subscript * @param attackerBrain attacker brain script but with extra `xform` parameter to replace `#fs.binmat.x()` subscript * @param options {@link SimulateGameOptions details} * @returns who won */ export function simulateGame( defenderBrain: BrainScript, attackerBrain: BrainScript, { timeLimit = 5000, defenderUserName = `defender`, attackerUserName = `attacker`, noThrow = false, onMove, state = makeState(), shuffle }: LaxPartial = {} ) { let endTime: number let winner: Role | undefined let lastLastBinlog: string[] = [] let lastBinlog: string[] = [] let madeMove: boolean while (true) { madeMove = false endTime = Date.now() + timeLimit defenderBrain( { caller: defenderUserName, this_script: `${defenderUserName}.binmat_brain`, cols: 0, rows: 0, // eslint-disable-next-line unicorn/no-null calling_script: null }, generateArgsForDefender( state, defenderUserName, attackerUserName, [ ...lastLastBinlog, ...lastBinlog ] ), xform ) if (!madeMove as boolean) { if (noThrow) doDefaultMove() else throw new Error(`defender brain did not attempt to make a move`) } onMove?.(state, lastBinlog) if (winner) return winner madeMove = false endTime = Date.now() + timeLimit attackerBrain( { caller: attackerUserName, this_script: `${attackerUserName}.binmat_brain`, cols: 0, rows: 0, // eslint-disable-next-line unicorn/no-null calling_script: null }, generateArgsForAttacker( state, defenderUserName, attackerUserName, [ ...lastLastBinlog, ...lastBinlog ] ), xform ) if (!madeMove as boolean) { if (noThrow) doDefaultMove() else throw new Error(`attacker brain did not attempt to make a move`) } onMove?.(state, lastBinlog) if (winner) return winner } function xform({ op }: { op: string }) { if (madeMove) { if (noThrow) return { ok: false } throw new Error(`only 1 move per turn`) } madeMove = true if (Date.now() > endTime) { if (noThrow) return doDefaultMove() throw new Error(`made move too late`) } let move try { move = parseMove(op) } catch (error) { if (noThrow) return doDefaultMove() throw error } const result = doMove(state, move, { shuffle }) switch (result.status) { case StatusCode.Ok: break case StatusCode.AttackerWin: { winner = Role.Attacker } break case StatusCode.DefenderWin: { winner = Role.Defender } break default: { if (noThrow) return doDefaultMove() throw new Error(StatusCodeMessages[result.status]) } } lastLastBinlog = lastBinlog lastBinlog = result.binlog return { ok: true } } function doDefaultMove() { const result = doMove(state, { kind: MoveKind.Pass }, { shuffle }) switch (result.status) { case StatusCode.Ok: break case StatusCode.DefenderWin: { winner = Role.Defender } break default: throw new Error(`unexpected status code ${result.status}`) } lastLastBinlog = lastBinlog lastBinlog = result.binlog return { ok: false } } }