Add ink integration UI and media playback

This commit is contained in:
2026-05-15 21:23:46 +02:00
parent 44dc64f830
commit f2e786d5bc
89 changed files with 6561 additions and 556 deletions
+51 -17
View File
@@ -49,6 +49,8 @@ const socket_io_1 = require("socket.io");
const dotenv = __importStar(require("dotenv"));
const game_runner_1 = require("./cli/game-runner");
const fs_1 = require("fs");
const turn_result_1 = require("./interfaces/turn-result");
const game_config_1 = require("./config/game-config");
// Load environment variables
dotenv.config();
// Create Express application
@@ -61,7 +63,8 @@ exports.io = io;
// Get port from environment variables or use default
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
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
const engineConfig = (0, game_config_1.loadGameConfig)(process.env.YAML_CONFIG_FILE || './config/engines/yaml.json', 'yaml');
// 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.
@@ -74,22 +77,51 @@ app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
res.setHeader('Expires', '0');
}
}));
app.get('/api/game-config', (_req, res) => {
res.json((0, game_config_1.clientGameConfig)(engineConfig));
});
// Set up game sessions
const gameSessions = new Map();
const nextTurnIds = new Map();
function nextTurnId(socketId) {
const current = nextTurnIds.get(socketId) || 1;
nextTurnIds.set(socketId, current + 1);
return current;
}
function createTextTurn(socketId, text, gameState = {}, suggestions) {
const paragraphs = (0, turn_result_1.textToParagraphs)(text);
return {
turnId: nextTurnId(socketId),
paragraphs,
choices: [],
inputMode: 'text',
gameState,
suggestions,
};
}
function normalizeSaveSlot(slot) {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
}
async function startDemoGameForSocket(socket) {
nextTurnIds.set(socket.id, 1);
const gameRunner = new game_runner_1.GameRunner();
const worldFile = process.env.DEFAULT_WORLD_FILE || './data/worlds/example_world.yml';
const worldFile = (0, game_config_1.projectPath)(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
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
const paragraphs = [
...(0, turn_result_1.textToParagraphs)(gameState.world.introduction),
...(0, turn_result_1.textToParagraphs)(gameRunner.getCurrentRoomDescription()),
];
socket.emit('narrativeResponse', {
turnId: nextTurnId(socket.id),
paragraphs,
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: gameState.currentRoomId,
},
});
return gameRunner;
}
@@ -140,6 +172,7 @@ async function handleGameApi(socket, method, args = []) {
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
socket.emit('gameConfig', (0, game_config_1.clientGameConfig)(engineConfig));
socket.data.saveGames = new Map();
socket.on('gameApi', async (request, respond) => {
try {
@@ -176,13 +209,9 @@ io.on('connection', (socket) => {
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: command,
gameState: {
currentRoomId: gameRunner.getGameState().currentRoomId
},
suggestions: gameRunner.getSuggestions()
});
socket.emit('narrativeResponse', createTextTurn(socket.id, command, {
currentRoomId: gameRunner.getGameState().currentRoomId
}, gameRunner.getSuggestions()));
}
catch (error) {
console.error('Error processing command:', error);
@@ -232,6 +261,7 @@ io.on('connection', (socket) => {
if (gameSessions.has(socket.id)) {
gameSessions.delete(socket.id);
}
nextTurnIds.delete(socket.id);
});
});
// Ensure required asset folders exist
@@ -250,6 +280,7 @@ function ensureDirectories() {
(0, fs_1.mkdirSync)(dir, { recursive: true });
}
}
(0, game_config_1.ensureConfiguredAssetDirectories)(engineConfig);
}
// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
@@ -278,14 +309,16 @@ async function startServer(initialPort, range) {
}
// Try to start the server on the current port
await new Promise((resolve, reject) => {
server.listen(currentPort, () => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`AI Interactive Fiction web server running on http://localhost:${currentPort}`);
resolve();
});
server.on('error', (error) => {
server.once('error', (error) => {
// If port is in use, try next port
if (error.code === 'EADDRINUSE') {
console.log(`Port ${currentPort} is in use, trying next port...`);
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
server.close();
currentPort++;
reject();
@@ -296,6 +329,7 @@ async function startServer(initialPort, range) {
reject(error);
}
});
server.listen(currentPort);
});
// If we reach here, server started successfully
return;