File: examples/companions/text-adventures/server.js (403 lines)
// Text adventure server for the CrossPoint "text-adventures" example plugin.
//
// node server.js (PORT=8095, GAMES_DIR=./games, DATA_DIR=./data)
//
// Put .z3/.z5/.z8 (or .zblorb) files into games/ (see fetch-games.sh). The server
// runs the games itself (zmachine.js) and turns every turn into catalog rows for
// the reader:
//
// GET /games list picker: one list per game
// GET /s/<session>/view?game=<id> the current turn as rows (sets the active game)
// GET /s/<session>/cmd?q=<text> Search box = type a command; answers with rows
// GET /s/<session>/do?g=&t=&c= a quick-command row's Details page (HTML)
// GET /s/<session>/out?g=&t= a past turn's text (HTML)
// GET /s/<session>/transcript.txt the Fetch button: the whole game as a .txt
//
// Every request loads the game state from DATA_DIR, runs one command and saves it
// again. Nothing is kept in memory between requests except the story files.
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import crypto from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { ZMachine, ZError } from './zmachine.js';