Files
sillytavern-rts-mode/index.js
T
2025-08-03 18:48:38 -07:00

171 lines
6.0 KiB
JavaScript

console.log('RTS-MODE: index.js script loading...');
// Test if jQuery is available
console.log('RTS-MODE: jQuery available:', typeof jQuery !== 'undefined');
console.log('RTS-MODE: $ available:', typeof $ !== 'undefined');
import { renderExtensionTemplateAsync } from '../../extensions.js';
import { eventSource, event_types } from '../../events.js';
import { createMapCanvas } from './ui/MapCanvas.js';
import { createResourcePanel } from './ui/ResourcePanel.js';
import { rtsUI } from './ui/RTSUIController.js';
import GameStateManager from './src/GameStateManager.js';
import { sendTurn } from './src/LLMAdapter.js';
import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
import { SlashCommand } from '../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js';
import './js/rts-mode.js';
console.log('RTS-MODE: All imports successful');
const extensionId = 'rts-mode';
const extensionName = 'RTS Chat Mode';
let root;
let topButton;
let uiMounted = false;
async function addTopBarButton() {
console.log('RTS Chat Mode: Adding top bar button...');
if (topButton) {
console.warn('RTS Chat Mode: Top button already exists, skipping creation.');
return;
}
topButton = $(await renderExtensionTemplateAsync('rts-mode', 'button'));
$('#top-settings-holder').append(topButton);
// Set up direct RTS UI toggle functionality
topButton.on('click', async (e) => {
e.stopPropagation();
console.log('RTS Mode button clicked');
try {
// Toggle between fullscreen RTS UI and normal SillyTavern UI
if (rtsUI.isActive()) {
await rtsUI.exitFullscreen();
// Update button to show we're back to normal mode
topButton.find('i').removeClass('fa-eye-slash').addClass('fa-chess-board');
topButton.attr('title', 'Toggle RTS UI').attr('data-i18n', '[title]Toggle RTS UI');
console.log('RTS UI hidden, returned to SillyTavern interface');
} else {
await rtsUI.enterFullscreen();
// Update button to show we're in fullscreen mode
topButton.find('i').removeClass('fa-chess-board').addClass('fa-eye-slash');
topButton.attr('title', 'Hide RTS UI').attr('data-i18n', '[title]Hide RTS UI');
console.log('RTS UI activated');
}
} catch (error) {
console.error('Error toggling RTS UI:', error);
}
});
}
function mountUI() {
if (!document.getElementById('rts-mode-root')) {
root = document.createElement('div');
root.id = 'rts-mode-root';
document.body.appendChild(root);
createMapCanvas(root);
createResourcePanel(root);
console.log('RTS Chat Mode UI mounted.');
uiMounted = true;
// Update button icon to show UI is active
topButton?.find('i').removeClass('fa-chess-board').addClass('fa-eye-slash');
topButton?.attr('title', 'Hide RTS UI').attr('data-i18n', '[title]Hide RTS UI');
}
}
function unmountUI() {
if (root) {
root.remove();
root = null;
console.log('RTS Chat Mode UI unmounted.');
uiMounted = false;
// Update button icon to show UI is hidden
topButton?.find('i').removeClass('fa-eye-slash').addClass('fa-chess-board');
topButton?.attr('title', 'Toggle RTS UI').attr('data-i18n', '[title]Toggle RTS UI');
}
}
function onRtsStartCommand() {
console.log('RTS Start command executed.');
GameStateManager.reset();
// If RTS UI is active, update it
if (rtsUI.isActive()) {
rtsUI.updateUI();
rtsUI.addLogEntry('system', 'Game state reset. New campaign begins!');
}
return '';
}
function onRtsCmdCommand(args, value) {
console.log('RTS Command executed with args:', args, 'value:', value);
if (value) {
// If RTS UI is active, add to log
if (rtsUI.isActive()) {
rtsUI.addLogEntry('action', value);
}
sendTurn(value);
}
return '';
}
async function onRtsUICommand() {
console.log('RTS UI toggle command executed.');
if (rtsUI.isActive()) {
await rtsUI.exitFullscreen();
// Update button state to match
if (topButton) {
topButton.find('i').removeClass('fa-eye-slash').addClass('fa-chess-board');
topButton.attr('title', 'Toggle RTS UI').attr('data-i18n', '[title]Toggle RTS UI');
}
return 'RTS UI hidden. Returned to normal SillyTavern interface.';
} else {
await rtsUI.enterFullscreen();
// Update button state to match
if (topButton) {
topButton.find('i').removeClass('fa-chess-board').addClass('fa-eye-slash');
topButton.attr('title', 'Hide RTS UI').attr('data-i18n', '[title]Hide RTS UI');
}
return 'RTS UI activated. Use ESC key or /rts-ui to return to normal interface.';
}
}
jQuery(async function() {
console.log('RTS Chat Mode: jQuery ready, initializing extension...');
await addTopBarButton();
// Register slash commands
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'rts-start',
callback: onRtsStartCommand,
helpString: 'Resets the RTS game state.',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'rts-cmd',
callback: onRtsCmdCommand,
helpString: 'Sends a command to the RTS game master.',
unnamedArgumentList: [
new SlashCommandArgument('command', [ARGUMENT_TYPE.STRING], true),
],
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'rts-ui',
callback: onRtsUICommand,
aliases: ['rts-toggle', 'rts-fullscreen'],
helpString: 'Toggles the full-screen RTS interface.',
}));
console.log('RTS Chat Mode: Extension initialized successfully');
});