Examples > text-adventures COMPANION SERVER.JS (5/13) function record(state, m, cmd, r, game) { let output = cleanOutput(r.output); if (r.quit) output += '\n\n[The game has ended. Pick "Start over" to play again.]'; const loc = location(m); const entry = { turn: state.turn, cmd, output, status: m.upperText().join(' ? '), place: loc ? m.objectName(loc) : '', }; state.snap = m.snapshot(); state.undo = m.undoState; state.slot = m.saveSlot; state.waiting = r.waiting; state.quit = r.quit; state.history = [...(state.history || []), entry].slice(-HISTORY); state.exits = null; // computed lazily for the current turn return entry; } function newGame(sid, game) { const m = new ZMachine(game.story, { seed: crypto.randomInt(1, 2 ** 31) }); const r = m.run(); const state = { game: game.id, turn: 0, history: [] }; const entry = record(state, m, '', r, game); fs.mkdirSync(sessionDir(sid), { recursive: true }); fs.writeFileSync(transcriptFile(sid, game.id), `${game.title}\n\n${r.output}`); writeJson(stateFile(sid, game.id), state); return { state, entry }; } function loadOrStart(sid, game) { const state = readJson(stateFile(sid, game.id), null); if (state && state.snap) return state; return newGame(sid, game).state; } Next: 14 Companion server.js (6/13)