Skip to lesson
Flawless Poker
Galaxy Bot AcademyProgramming Languages and Coding Techniques
BEGINNER · 16 MIN READ · PROGRAMMING LANGUAGES AND CODING TECHNIQUES

JavaScript and Node.js: Streaming Decisions

Learning objectiveParse JSON-line input and write a legal JSON-line answer in Node.js.

Understand the idea

Install a current Node.js release. A Node program can use built-in readline to process the adapter's stdin one line at a time. JavaScript objects map directly to JSON objects. Keep the choice function synchronous unless you truly need asynchronous data; the adapter has a 20-second response wait.

Use optional chaining for missing fields, booleans for legal flags, and process.stderr for diagnostics. The program below requires no package and can be launched as node bot.mjs through FLAWLESS_BOT_COMMAND.

At the poker table

With legal.check=false, legal.call=true, and callAmount=10, the bot emits {"kind":"call"} followed by a newline.

Code and decision sketch

JavaScript · bot.mjs
import readline from 'node:readline';

function choose(obs) {
  const legal = obs?.legal ?? {};
  if (legal.check) return { kind: 'check' };
  if (legal.call && legal.callAmount <= 20) return { kind: 'call' };
  if (legal.fold) return { kind: 'fold' };
  throw new Error('No supported legal action');
}

for await (const line of readline.createInterface({ input: process.stdin })) {
  try {
    process.stdout.write(JSON.stringify(choose(JSON.parse(line))) + '\n');
  } catch (error) {
    console.error('Observation error:', error);
    process.stdout.write('{"kind":"fold"}\n');
  }
}

Common mistake

Do not use console.log for debugging: it writes to stdout. Use console.error. Waiting on a remote model or long simulation may exceed the adapter's response window.

Try it yourself

Add a conditional that calls only when the amount is at most 2% of your current stack. Test with two stack values.