// A bot's own process: node referee/bot.mjs . Loads /bot.mjs, whose default export (or `act`) // is act(obs, legal) -> move, and answers the referee one JSON line per ask: {n, obs, legal} in, {n, answer} out. // A bot's prints go to stderr, so they cannot corrupt the channel. import { createInterface } from 'node:readline'; import path from 'node:path'; import { pathToFileURL } from 'node:url'; const write = process.stdout.write.bind(process.stdout); console.log = console.info = console.debug = console.warn = console.error; process.stdout.write = process.stderr.write.bind(process.stderr); const mod = await import(pathToFileURL(path.join(process.argv[2], 'bot.mjs')).href); const act = mod.default ?? mod.act; for await (const line of createInterface({ input: process.stdin })) { let m, answer = null; try { m = JSON.parse(line); answer = m.hello ? true : await act(m.obs, m.legal); } catch (e) { console.error(e); } write(`${JSON.stringify({ n: m?.n, answer: answer ?? null })}\n`); }