21 Companion server.js (13/13)

      case 'out': {
        const state = loadOrStart(sid, game);
        const t = Number(q('t'));
        const entry = state.history.find((h) => h.turn === t) || state.history[state.history.length - 1];
        return send(res, 200, entryHtml(game, entry), 'text/html; charset=utf-8');
      }
      case 'transcript.txt': {
        loadOrStart(sid, game);
        let text = '';
        try { text = fs.readFileSync(transcriptFile(sid, gid), 'utf8'); } catch { /* new game */ }
        return send(res, 200, text, 'text/plain; charset=utf-8');
      }
      default:
        return send(res, 404, { error: 'not found' });
    }
  });
}

export function createServer() {
  return http.createServer((req, res) => {
    handle(req, res).catch((e) => {
      console.error(e);
      if (!res.headersSent) send(res, 500, { error: String(e.message || e) });
    });
  });
}

if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
  createServer().listen(PORT, () => {
    console.log(`Text adventures on http://0.0.0.0:${PORT}/  games: ${GAMES_DIR}  saves: ${DATA_DIR}`);
    if (!games().size) console.log('No games yet. Run ./fetch-games.sh or copy .z5/.z8 files into games/.');
  });
}
‹ PreviousReader file (.txt)Next ›