Checkpoint current interactive fiction state
This commit is contained in:
Vendored
+99
-31
@@ -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) {
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+91
-11
@@ -60,8 +60,17 @@ 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. Keep browser modules uncached
|
||||
// during local development so fixes are visible without 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');
|
||||
}
|
||||
}));
|
||||
// Test paragraphs to send to the client
|
||||
const TEST_PARAGRAPHS = [
|
||||
"You stand at the entrance of a mysterious cave. The air is cool and damp, carrying the scent of earth and ancient stone. Shadows dance on the walls as your torch flickers in the gentle breeze.",
|
||||
@@ -72,16 +81,87 @@ const TEST_PARAGRAPHS = [
|
||||
io.on('connection', (socket) => {
|
||||
console.log(`New client connected: ${socket.id}`);
|
||||
let currentParagraphIndex = 0;
|
||||
let gameRunning = false;
|
||||
const saveGames = new Set();
|
||||
const startDemoGame = () => {
|
||||
gameRunning = true;
|
||||
currentParagraphIndex = 0;
|
||||
socket.emit('gameIntroduction', {
|
||||
introduction: "::chapter[Interactive Fiction Test]\n\nWelcome to the Interactive Fiction Test. This is a simplified version that sends predefined paragraphs instead of using an LLM.",
|
||||
initialRoomDescription: TEST_PARAGRAPHS[0],
|
||||
currentRoomId: "test-room"
|
||||
});
|
||||
};
|
||||
const normalizeSaveSlot = (slot) => {
|
||||
const value = Number(slot);
|
||||
return Number.isInteger(value) && value > 0 ? value : 1;
|
||||
};
|
||||
socket.on('gameApi', (request, respond) => {
|
||||
try {
|
||||
const method = String(request?.method || '');
|
||||
const args = Array.isArray(request?.args) ? request.args : [];
|
||||
let response;
|
||||
switch (method) {
|
||||
case 'newGame':
|
||||
case 'newGame()':
|
||||
startDemoGame();
|
||||
response = { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
|
||||
break;
|
||||
case 'loadGame':
|
||||
case 'loadGame()': {
|
||||
const slot = normalizeSaveSlot(args[0]);
|
||||
if (!saveGames.has(slot)) {
|
||||
response = { success: false, error: 'missing_save', result: false };
|
||||
break;
|
||||
}
|
||||
startDemoGame();
|
||||
socket.emit('gameLoaded', { slot });
|
||||
response = { success: true, result: true, running: true, slot };
|
||||
break;
|
||||
}
|
||||
case 'saveGame':
|
||||
case 'saveGame()': {
|
||||
if (!gameRunning) {
|
||||
response = { success: false, error: 'game_not_running', result: false };
|
||||
break;
|
||||
}
|
||||
const slot = normalizeSaveSlot(args[0]);
|
||||
saveGames.add(slot);
|
||||
socket.emit('gameSaved', { slot });
|
||||
response = { success: true, result: true, slot };
|
||||
break;
|
||||
}
|
||||
case 'hasSaveGame':
|
||||
case 'hasSaveGame()': {
|
||||
const slot = normalizeSaveSlot(args[0]);
|
||||
response = { success: true, result: saveGames.has(slot), slot };
|
||||
break;
|
||||
}
|
||||
case 'getSaveGames':
|
||||
case 'getSaveGames()':
|
||||
response = { success: true, result: Array.from(saveGames).sort((a, b) => a - b) };
|
||||
break;
|
||||
case 'isGameRunning':
|
||||
case 'isGameRunning()':
|
||||
response = { success: true, result: gameRunning };
|
||||
break;
|
||||
default:
|
||||
response = { success: false, error: `unknown_method:${method}` };
|
||||
}
|
||||
if (typeof respond === 'function')
|
||||
respond(response);
|
||||
}
|
||||
catch (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 {
|
||||
console.log('Starting test game session');
|
||||
// Send introduction to client
|
||||
socket.emit('gameIntroduction', {
|
||||
introduction: "Welcome to the Interactive Fiction Test. This is a simplified version that sends predefined paragraphs instead of using an LLM.",
|
||||
initialRoomDescription: TEST_PARAGRAPHS[0],
|
||||
currentRoomId: "test-room"
|
||||
});
|
||||
startDemoGame();
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error starting game:', error);
|
||||
@@ -92,11 +172,9 @@ io.on('connection', (socket) => {
|
||||
socket.on('playerCommand', async (data) => {
|
||||
try {
|
||||
console.log(`Received command: ${data.command}`);
|
||||
// Move to the next paragraph
|
||||
currentParagraphIndex = (currentParagraphIndex + 1) % TEST_PARAGRAPHS.length;
|
||||
// Send narrative response to client
|
||||
socket.emit('narrativeResponse', {
|
||||
text: TEST_PARAGRAPHS[currentParagraphIndex],
|
||||
text: data.command,
|
||||
gameState: {
|
||||
currentRoomId: "test-room"
|
||||
},
|
||||
@@ -120,6 +198,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) {
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user