Checkpoint current interactive fiction state

This commit is contained in:
2026-05-14 21:17:43 +02:00
parent c745efd1d2
commit 873049f7e6
183 changed files with 13755 additions and 1459 deletions
+99 -31
View File
@@ -62,30 +62,103 @@ exports.io = io;
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 10; // Try up to 10 ports starting from the default
// Serve static files from the public directory
app.use(express_1.default.static(path_1.default.join(__dirname, '../public')));
// Serve static files from the public directory. During local development the
// browser must not keep stale ES modules, otherwise UI fixes appear to do
// nothing until a hard cache clear.
app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
// Set up game sessions
const gameSessions = new Map();
function normalizeSaveSlot(slot) {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
}
async function startDemoGameForSocket(socket) {
const gameRunner = new game_runner_1.GameRunner();
const worldFile = process.env.DEFAULT_WORLD_FILE || './data/worlds/example_world.yml';
await gameRunner.initialize(worldFile);
gameSessions.set(socket.id, gameRunner);
const gameState = gameRunner.getGameState();
socket.emit('gameIntroduction', {
introduction: gameState.world.introduction,
initialRoomDescription: gameRunner.getCurrentRoomDescription(),
currentRoomId: gameState.currentRoomId
});
return gameRunner;
}
async function handleGameApi(socket, method, args = []) {
const saveGames = socket.data.saveGames || new Map();
socket.data.saveGames = saveGames;
switch (method) {
case 'newGame':
case 'newGame()':
await startDemoGameForSocket(socket);
return { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!saveGames.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
await startDemoGameForSocket(socket);
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'saveGame':
case 'saveGame()': {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
saveGames.set(slot, gameRunner.getGameState());
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: saveGames.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return { success: true, result: Array.from(saveGames.keys()).sort((a, b) => a - b) };
case 'isGameRunning':
case 'isGameRunning()':
return { success: true, result: gameSessions.has(socket.id) };
default:
return { success: false, error: `unknown_method:${method}` };
}
}
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
socket.data.saveGames = new Map();
socket.on('gameApi', async (request, respond) => {
try {
const response = await handleGameApi(socket, String(request?.method || ''), Array.isArray(request?.args) ? request.args : []);
if (typeof respond === 'function') {
respond(response);
}
}
catch (error) {
console.error('Game API error:', error);
if (typeof respond === 'function') {
respond({ success: false, error: error instanceof Error ? error.message : String(error) });
}
}
});
// Start a new game
socket.on('startGame', async () => {
try {
// Initialize game runner
const gameRunner = new game_runner_1.GameRunner();
const worldFile = process.env.DEFAULT_WORLD_FILE || './data/worlds/example_world.yml';
// Initialize the game
await gameRunner.initialize(worldFile);
// Store game session
gameSessions.set(socket.id, gameRunner);
// Send introduction to client
const gameState = gameRunner.getGameState();
socket.emit('gameIntroduction', {
introduction: gameState.world.introduction,
initialRoomDescription: gameRunner.getCurrentRoomDescription(),
currentRoomId: gameState.currentRoomId
});
await handleGameApi(socket, 'newGame', []);
}
catch (error) {
console.error('Error starting game:', error);
@@ -100,11 +173,11 @@ io.on('connection', (socket) => {
socket.emit('error', { message: 'Game session not found. Please start a new game.' });
return;
}
// Process command and get response
const response = await gameRunner.processCommand(data.command);
// Send narrative response to client
const command = String(data?.command || '').trim();
// During typography and animation work, mirror the command back through
// the real socket path so the UI pipeline can be tested end to end.
socket.emit('narrativeResponse', {
text: response,
text: command,
gameState: {
currentRoomId: gameRunner.getGameState().currentRoomId
},
@@ -124,8 +197,7 @@ io.on('connection', (socket) => {
socket.emit('error', { message: 'Game session not found. Please start a new game.' });
return;
}
// Store save data in session
socket.data.savedGame = gameRunner.getGameState();
socket.data.saveGames.set(1, gameRunner.getGameState());
socket.emit('gameSaved');
}
catch (error) {
@@ -134,7 +206,7 @@ io.on('connection', (socket) => {
}
});
// Load game state
socket.on('loadGame', () => {
socket.on('loadGame', async () => {
try {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
@@ -142,17 +214,11 @@ io.on('connection', (socket) => {
return;
}
// Check if there's a saved game
if (!socket.data.savedGame) {
if (!socket.data.saveGames?.has(1)) {
socket.emit('error', { message: 'No saved game found.' });
return;
}
// Load saved game
gameRunner.loadGameState(socket.data.savedGame);
// Send current state to client
socket.emit('gameLoaded', {
currentRoomDescription: gameRunner.getCurrentRoomDescription(),
currentRoomId: gameRunner.getGameState().currentRoomId
});
await handleGameApi(socket, 'loadGame', [1]);
}
catch (error) {
console.error('Error loading game:', error);
@@ -175,6 +241,8 @@ function ensureDirectories() {
path_1.default.join(__dirname, '../public/js'),
path_1.default.join(__dirname, '../public/css'),
path_1.default.join(__dirname, '../public/images'),
path_1.default.join(__dirname, '../public/music'),
path_1.default.join(__dirname, '../public/sounds'),
path_1.default.join(__dirname, '../public/fonts')
];
for (const dir of dirs) {