14 Companion server.js (6/13)

// Runs one command. Special commands: "#restart", "#key" (Enter for a key prompt).
function play(sid, game, cmd) {
  cmd = String(cmd || '').trim().slice(0, 120);
  if (cmd === '#restart') return newGame(sid, game).entry;
  const state = loadOrStart(sid, game);
  if (state.quit) return { ...state.history[state.history.length - 1], cmd };
  const m = machineFor(game, state);
  let r;
  try {
    r = cmd === '#key' || (state.waiting === 'key' && !cmd) ? m.sendKey('\n') : m.sendLine(cmd);
  } catch (e) {
    if (!(e instanceof ZError)) throw e;
    console.error(`[${game.id}] ${e.message}`);
    return { turn: state.turn, cmd, output: `[Interpreter error: ${e.message}]`, status: '', place: '' };
  }
  state.turn++;
  const shown = cmd === '#key' ? '' : cmd;
  const entry = record(state, m, shown, r, game);
  writeJson(stateFile(sid, game.id), state);
  fs.appendFileSync(transcriptFile(sid, game.id), shown + '\n' + r.output);
  return entry;
}
‹ PreviousReader file (.txt)Next ›