Spaces:
Sleeping
Sleeping
File size: 15,942 Bytes
1e98ab1 9fdd425 1e98ab1 9fdd425 1e98ab1 9fdd425 1e98ab1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
/**
* Natural Language Command Interface for RTS Game
* Allows players to control the game using natural language in EN/FR/ZH
*/
class NLInterface {
constructor(gameClient) {
this.gameClient = gameClient;
this.translator = null;
this.translatorReady = false;
this.lastCommand = null;
this.commandHistory = [];
this.maxHistorySize = 50;
this.initializeUI();
this.checkTranslatorStatus();
}
initializeUI() {
// Create NL interface container
const nlContainer = document.createElement('div');
nlContainer.id = 'nl-interface';
nlContainer.className = 'nl-interface';
nlContainer.innerHTML = `
<div class="nl-header">
<span class="nl-icon">🎤</span>
<span class="nl-title">Natural Language Commands</span>
<button id="nl-toggle" class="nl-toggle-btn" title="Toggle NL Interface">
<span class="toggle-icon">▼</span>
</button>
</div>
<div class="nl-body" id="nl-body">
<div class="nl-status" id="nl-status">
<span class="status-dot loading"></span>
<span id="nl-status-text">Checking translator...</span>
</div>
<div class="nl-input-container">
<input type="text"
id="nl-input"
class="nl-input"
placeholder="Type command in English, French, or Chinese..."
disabled>
<button id="nl-send" class="nl-send-btn" disabled>
<span>Send</span>
</button>
</div>
<div class="nl-examples" id="nl-examples">
<div class="examples-header">📝 Example Commands:</div>
<div class="examples-list" id="examples-list">
Loading examples...
</div>
</div>
<div class="nl-confirmation" id="nl-confirmation" style="display: none;">
<div class="confirmation-header">Parsed Command:</div>
<div class="confirmation-content">
<div class="confirmation-tool">
<strong>Action:</strong> <span id="confirm-tool"></span>
</div>
<div class="confirmation-params">
<strong>Parameters:</strong>
<pre id="confirm-params"></pre>
</div>
</div>
<div class="confirmation-buttons">
<button id="confirm-execute" class="confirm-btn execute">Execute</button>
<button id="confirm-cancel" class="confirm-btn cancel">Cancel</button>
</div>
</div>
</div>
`;
// Insert into left sidebar (after existing sections)
const leftSidebar = document.getElementById('left-sidebar');
if (leftSidebar) {
leftSidebar.appendChild(nlContainer);
} else {
// Fallback: insert before right sidebar if left sidebar not found
const rightSidebar = document.getElementById('right-sidebar');
if (rightSidebar) {
rightSidebar.parentElement.insertBefore(nlContainer, rightSidebar);
} else {
document.getElementById('game-container').appendChild(nlContainer);
}
}
this.setupEventListeners();
}
setupEventListeners() {
// Toggle button
const toggleBtn = document.getElementById('nl-toggle');
const nlBody = document.getElementById('nl-body');
if (toggleBtn && nlBody) {
toggleBtn.addEventListener('click', () => {
nlBody.classList.toggle('collapsed');
const icon = toggleBtn.querySelector('.toggle-icon');
icon.textContent = nlBody.classList.contains('collapsed') ? '▶' : '▼';
});
}
// Input field
const nlInput = document.getElementById('nl-input');
if (nlInput) {
nlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !nlInput.disabled) {
this.sendCommand();
}
});
}
// Send button
const sendBtn = document.getElementById('nl-send');
if (sendBtn) {
sendBtn.addEventListener('click', () => this.sendCommand());
}
// Confirmation buttons
const executeBtn = document.getElementById('confirm-execute');
const cancelBtn = document.getElementById('confirm-cancel');
if (executeBtn) {
executeBtn.addEventListener('click', () => this.executeCommand());
}
if (cancelBtn) {
cancelBtn.addEventListener('click', () => this.hideConfirmation());
}
}
async checkTranslatorStatus() {
try {
const response = await fetch('/api/nl/status');
const data = await response.json();
if (data.available) {
this.translatorReady = true;
this.updateStatus('ready', 'Ready');
this.enableInput();
await this.loadExamples();
} else {
this.translatorReady = false;
const errorMsg = data.last_error || 'Translator loading...';
this.updateStatus('loading', errorMsg);
this.disableInput();
// Retry after 3 seconds if not available
console.log('[NL] Translator not ready, will retry in 3s...');
setTimeout(() => this.checkTranslatorStatus(), 3000);
}
} catch (error) {
console.error('[NL] Failed to check translator status:', error);
this.updateStatus('error', 'Failed to connect to translator');
this.disableInput();
// Retry after 5 seconds on error
setTimeout(() => this.checkTranslatorStatus(), 5000);
}
}
async loadExamples() {
try {
const lang = this.gameClient.currentLanguage || 'en';
const response = await fetch(`/api/nl/examples?language=${lang}`);
const data = await response.json();
const examplesList = document.getElementById('examples-list');
if (examplesList && data.examples) {
examplesList.innerHTML = data.examples
.map(cmd => `<div class="example-item" data-command="${cmd}">💬 ${cmd}</div>`)
.join('');
// Add click handlers
examplesList.querySelectorAll('.example-item').forEach(item => {
item.addEventListener('click', () => {
const cmd = item.getAttribute('data-command');
document.getElementById('nl-input').value = cmd;
});
});
}
} catch (error) {
console.error('[NL] Failed to load examples:', error);
}
}
updateStatus(state, text) {
const statusDot = document.querySelector('#nl-status .status-dot');
const statusText = document.getElementById('nl-status-text');
if (statusDot) {
statusDot.className = 'status-dot';
statusDot.classList.add(state);
}
if (statusText) {
statusText.textContent = text;
}
}
enableInput() {
const nlInput = document.getElementById('nl-input');
const sendBtn = document.getElementById('nl-send');
if (nlInput) nlInput.disabled = false;
if (sendBtn) sendBtn.disabled = false;
}
disableInput() {
const nlInput = document.getElementById('nl-input');
const sendBtn = document.getElementById('nl-send');
if (nlInput) nlInput.disabled = true;
if (sendBtn) sendBtn.disabled = true;
}
async sendCommand() {
const nlInput = document.getElementById('nl-input');
const command = nlInput.value.trim();
if (!command) {
return;
}
// Update status
this.updateStatus('loading', 'Translating...');
this.disableInput();
try {
const response = await fetch('/api/nl/translate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
command: command,
language: this.gameClient.currentLanguage
})
});
const result = await response.json();
if (result.success && result.json_command) {
// Show confirmation
this.showConfirmation(command, result.json_command);
this.updateStatus('ready', `Ready (${result.response_time.toFixed(2)}s)`);
// Add to history
this.addToHistory(command, result.json_command);
} else {
// Show error
const errorMsg = result.error || 'Failed to translate command';
this.updateStatus('error', errorMsg);
this.gameClient.showNotification(`NL Error: ${errorMsg}`, 'error');
setTimeout(() => {
this.updateStatus('ready', 'Ready');
this.enableInput();
}, 3000);
}
} catch (error) {
console.error('[NL] Translation error:', error);
this.updateStatus('error', 'Network error');
this.gameClient.showNotification('Failed to translate command', 'error');
setTimeout(() => {
this.updateStatus('ready', 'Ready');
this.enableInput();
}, 3000);
}
}
showConfirmation(originalCommand, jsonCommand) {
const confirmation = document.getElementById('nl-confirmation');
const toolSpan = document.getElementById('confirm-tool');
const paramsSpan = document.getElementById('confirm-params');
if (confirmation && toolSpan && paramsSpan) {
// Store for execution
this.lastCommand = { original: originalCommand, json: jsonCommand };
// Display
toolSpan.textContent = jsonCommand.tool;
paramsSpan.textContent = jsonCommand.params
? JSON.stringify(jsonCommand.params, null, 2)
: 'None';
confirmation.style.display = 'block';
}
}
hideConfirmation() {
const confirmation = document.getElementById('nl-confirmation');
if (confirmation) {
confirmation.style.display = 'none';
}
this.lastCommand = null;
this.enableInput();
// Clear input
const nlInput = document.getElementById('nl-input');
if (nlInput) {
nlInput.value = '';
}
}
async executeCommand() {
if (!this.lastCommand) {
return;
}
const { json } = this.lastCommand;
// Execute via MCP or direct game command
try {
// Convert MCP command to game command
const gameCommand = this.convertMCPToGameCommand(json);
if (gameCommand) {
// Send via WebSocket
this.gameClient.ws.send(JSON.stringify(gameCommand));
this.gameClient.showNotification('Command executed', 'success');
} else {
this.gameClient.showNotification('Unsupported command', 'error');
}
} catch (error) {
console.error('[NL] Execution error:', error);
this.gameClient.showNotification('Failed to execute command', 'error');
}
this.hideConfirmation();
}
convertMCPToGameCommand(mcpCommand) {
const { tool, params } = mcpCommand;
// Map MCP tools to game commands
switch (tool) {
case 'get_game_state':
// This just shows notification with game state
const state = this.gameClient.gameState;
if (state) {
const player = state.players[0];
const msg = `Credits: ${player.credits} | Power: ${player.power}/${player.power_max} | Units: ${Object.keys(state.units).length}`;
this.gameClient.showNotification(msg, 'info');
}
return null;
case 'move_units':
// Move selected units or units by ID
if (params && params.target_x !== undefined && params.target_y !== undefined) {
return {
type: 'move_units',
player_id: 0,
unit_ids: params.unit_ids || Array.from(this.gameClient.selectedUnits),
x: params.target_x,
y: params.target_y
};
}
break;
case 'attack_unit':
// Attack target
if (params && params.target_id) {
return {
type: 'attack',
player_id: 0,
attacker_ids: params.attacker_ids || Array.from(this.gameClient.selectedUnits),
target_id: params.target_id
};
}
break;
case 'build_unit':
// Build unit
if (params && params.unit_type) {
return {
type: 'build_unit',
player_id: 0,
unit_type: params.unit_type,
building_id: this.gameClient.selectedProductionBuilding || null
};
}
break;
case 'build_building':
// Build building
if (params && params.building_type && params.x !== undefined && params.y !== undefined) {
return {
type: 'build_building',
player_id: 0,
building_type: params.building_type,
x: params.x,
y: params.y
};
}
break;
}
return null;
}
addToHistory(command, jsonCommand) {
this.commandHistory.unshift({
timestamp: Date.now(),
command: command,
json: jsonCommand
});
if (this.commandHistory.length > this.maxHistorySize) {
this.commandHistory.pop();
}
}
}
// Initialize NL interface when game is ready
window.addEventListener('load', () => {
// Wait for game client to be ready
const checkGameReady = setInterval(() => {
if (window.gameClient && window.gameClient.ws) {
clearInterval(checkGameReady);
// Initialize NL interface
window.nlInterface = new NLInterface(window.gameClient);
console.log('[NL] Interface initialized');
}
}, 100);
});
|