Spaces:
Running
Running
File size: 12,070 Bytes
5dd5427 6f3b96e b88ba0c 6f3b96e b88ba0c 6f3b96e b48eb70 6f3b96e eeb2654 6f3b96e eeb2654 6f3b96e 1df4d55 6f3b96e 1505067 6f3b96e eeb2654 07a676b eeb2654 6f3b96e 1505067 6f3b96e 5dd5427 6f3b96e 440ff08 6f3b96e 106bc8b 6f3b96e 3ca518f 106bc8b 1df4d55 3ca518f 1df4d55 3ca518f 6f3b96e 106bc8b 6f3b96e 5dd5427 6f3b96e 5dd5427 6f3b96e afac52e 5dd5427 6f3b96e b48eb70 6f3b96e b48eb70 5dd5427 6f3b96e 5dd5427 6f3b96e 5dd5427 6f3b96e af00564 6f3b96e 5dd5427 |
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 |
// Main application entry point
import ClozeGame from './clozeGameEngine.js';
import ChatUI from './chatInterface.js';
import WelcomeOverlay from './welcomeOverlay.js';
class App {
constructor() {
this.game = new ClozeGame();
this.chatUI = new ChatUI(this.game);
this.welcomeOverlay = new WelcomeOverlay();
this.elements = {
loading: document.getElementById('loading'),
gameArea: document.getElementById('game-area'),
stickyControls: document.getElementById('sticky-controls'),
bookInfo: document.getElementById('book-info'),
roundInfo: document.getElementById('round-info'),
contextualization: document.getElementById('contextualization'),
passageContent: document.getElementById('passage-content'),
hintsSection: document.getElementById('hints-section'),
hintsList: document.getElementById('hints-list'),
submitBtn: document.getElementById('submit-btn'),
nextBtn: document.getElementById('next-btn'),
hintBtn: document.getElementById('hint-btn'),
result: document.getElementById('result')
};
this.currentResults = null;
this.setupEventListeners();
}
async initialize() {
try {
this.showLoading(true);
await this.game.initialize();
await this.startNewGame();
this.showLoading(false);
} catch (error) {
console.error('Failed to initialize app:', error);
this.showError('Failed to load the game. Please refresh and try again.');
}
}
setupEventListeners() {
this.elements.submitBtn.addEventListener('click', () => this.handleSubmit());
this.elements.nextBtn.addEventListener('click', () => this.handleNext());
this.elements.hintBtn.addEventListener('click', () => this.toggleHints());
// Allow Enter key to submit when focused on an input
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && e.target.classList.contains('cloze-input')) {
this.handleSubmit();
}
});
}
async startNewGame() {
try {
const roundData = await this.game.startNewRound();
this.displayRound(roundData);
this.resetUI();
} catch (error) {
console.error('Error starting new game:', error);
this.showError('Could not load a new passage. Please try again.');
}
}
displayRound(roundData) {
// Show book information
this.elements.bookInfo.innerHTML = `
<strong>${roundData.title}</strong> by ${roundData.author}
`;
// Show level information
const blanksCount = roundData.blanks.length;
const levelInfo = `Level ${this.game.currentLevel} β’ ${blanksCount} blank${blanksCount > 1 ? 's' : ''}`;
this.elements.roundInfo.innerHTML = levelInfo;
// Show contextualization from AI agent
this.elements.contextualization.innerHTML = `
<div class="flex items-start gap-2">
<span class="text-blue-600">π</span>
<span>${roundData.contextualization || 'Loading context...'}</span>
</div>
`;
// Render the cloze text with input fields and chat buttons
const clozeHtml = this.game.renderClozeTextWithChat();
this.elements.passageContent.innerHTML = `<p>${clozeHtml}</p>`;
// Store hints for later display
this.currentHints = roundData.hints || [];
this.populateHints();
// Hide hints initially
this.elements.hintsSection.style.display = 'none';
// Set up input field listeners
this.setupInputListeners();
// Set up chat buttons
this.chatUI.setupChatButtons();
}
setupInputListeners() {
const inputs = this.elements.passageContent.querySelectorAll('.cloze-input');
inputs.forEach((input, index) => {
input.addEventListener('input', () => {
// Remove any previous styling
input.classList.remove('correct', 'incorrect');
this.updateSubmitButton();
});
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
// Move to next input or submit if last
const nextInput = inputs[index + 1];
if (nextInput) {
nextInput.focus();
} else {
this.handleSubmit();
}
}
});
});
// Focus first input
if (inputs.length > 0) {
inputs[0].focus();
}
}
updateSubmitButton() {
const inputs = this.elements.passageContent.querySelectorAll('.cloze-input');
const allFilled = Array.from(inputs).every(input => input.value.trim() !== '');
this.elements.submitBtn.disabled = !allFilled;
}
handleSubmit() {
const inputs = this.elements.passageContent.querySelectorAll('.cloze-input');
const answers = Array.from(inputs).map(input => input.value.trim());
// Check if all fields are filled
if (answers.some(answer => answer === '')) {
alert('Please fill in all blanks before submitting.');
return;
}
// Submit answers and get results
this.currentResults = this.game.submitAnswers(answers);
this.displayResults(this.currentResults);
}
displayResults(results) {
let message = `Score: ${results.correct}/${results.total} (${results.percentage}%)`;
// Show "Required" information at all levels for consistency
message += ` - Required: ${results.requiredCorrect}/${results.total}`;
if (results.passed) {
// Check if this completes the requirements for level advancement
const roundsCompleted = this.game.roundsPassedAtCurrentLevel + 1; // +1 for this round
if (roundsCompleted >= 2) {
message += ` - Excellent! Advancing to Level ${this.game.currentLevel + 1}! π`;
} else {
message += ` - Great job! ${roundsCompleted}/2 rounds completed for Level ${this.game.currentLevel}`;
}
this.elements.result.className = 'mt-4 text-center font-semibold text-green-600';
} else {
message += ` - Need ${results.requiredCorrect} correct to advance. Keep practicing! πͺ`;
this.elements.result.className = 'mt-4 text-center font-semibold text-red-600';
}
this.elements.result.textContent = message;
// Always reveal answers at the end of each round
this.revealAnswersInPlace(results.results);
// Show next button and hide submit button
this.elements.submitBtn.style.display = 'none';
this.elements.nextBtn.classList.remove('hidden');
}
highlightAnswers(results) {
const inputs = this.elements.passageContent.querySelectorAll('.cloze-input');
results.forEach((result, index) => {
const input = inputs[index];
if (input) {
if (result.isCorrect) {
input.classList.add('correct');
} else {
input.classList.add('incorrect');
// Show correct answer as placeholder or title
input.title = `Correct answer: ${result.correctAnswer}`;
}
input.disabled = true;
}
});
}
async handleNext() {
try {
// Show loading immediately with specific message
this.showLoading(true, 'Loading passages...');
// Clear chat history when starting new passage/round
this.chatUI.clearChatHistory();
// Always show loading for at least 1 second for smooth UX
const startTime = Date.now();
// Check if we should load next passage or next round
let roundData;
if (this.game.currentPassageIndex === 0) {
// Load second passage in current round
roundData = await this.game.nextPassage();
} else {
// Load next round (two new passages)
roundData = await this.game.nextRound();
}
// Ensure loading is shown for at least half a second
const elapsedTime = Date.now() - startTime;
if (elapsedTime < 500) {
await new Promise(resolve => setTimeout(resolve, 500 - elapsedTime));
}
this.displayRound(roundData);
this.resetUI();
this.showLoading(false);
} catch (error) {
console.error('Error loading next passage:', error);
this.showError('Could not load next passage. Please try again.');
}
}
// Reveal correct answers immediately after submission
revealAnswersInPlace(results) {
const inputs = this.elements.passageContent.querySelectorAll('.cloze-input');
results.forEach((result, index) => {
const input = inputs[index];
if (input) {
if (result.isCorrect) {
input.classList.add('correct');
input.style.backgroundColor = '#dcfce7'; // Light green
input.style.borderColor = '#16a34a'; // Green border
} else {
input.classList.add('incorrect');
input.style.backgroundColor = '#fef2f2'; // Light red
input.style.borderColor = '#dc2626'; // Red border
// Show correct answer below the input (only if not already shown)
const existingAnswer = input.parentNode.querySelector('.correct-answer-reveal');
if (!existingAnswer) {
const correctAnswerSpan = document.createElement('span');
correctAnswerSpan.className = 'correct-answer-reveal text-sm text-green-600 font-semibold ml-2';
correctAnswerSpan.textContent = `β ${result.correctAnswer}`;
input.parentNode.appendChild(correctAnswerSpan);
}
}
input.disabled = true;
}
});
}
populateHints() {
if (!this.currentHints || this.currentHints.length === 0) {
this.elements.hintsList.innerHTML = '<div class="text-yellow-600">No hints available for this passage.</div>';
return;
}
const hintsHtml = this.currentHints.map((hintData, index) =>
`<div class="flex items-start gap-2">
<span class="font-semibold text-yellow-800">${index + 1}.</span>
<span>${hintData.hint}</span>
</div>`
).join('');
this.elements.hintsList.innerHTML = hintsHtml;
}
toggleHints() {
const isHidden = this.elements.hintsSection.style.display === 'none';
this.elements.hintsSection.style.display = isHidden ? 'block' : 'none';
this.elements.hintBtn.textContent = isHidden ? 'Hide Hints' : 'Show Hints';
}
resetUI() {
this.elements.result.textContent = '';
this.elements.submitBtn.style.display = 'inline-block';
this.elements.submitBtn.disabled = true;
this.elements.nextBtn.classList.add('hidden');
this.elements.hintsSection.style.display = 'none';
this.elements.hintBtn.textContent = 'Show Hints';
this.currentResults = null;
this.currentHints = [];
}
showLoading(show, message = 'Loading passages...') {
if (show) {
this.elements.loading.innerHTML = `
<div class="text-center py-8">
<p class="text-lg loading-text">${message}</p>
</div>
`;
this.elements.loading.classList.remove('hidden');
this.elements.gameArea.classList.add('hidden');
this.elements.stickyControls.classList.add('hidden');
} else {
this.elements.loading.classList.add('hidden');
this.elements.gameArea.classList.remove('hidden');
this.elements.stickyControls.classList.remove('hidden');
}
}
showError(message) {
this.elements.loading.innerHTML = `
<div class="text-center py-8">
<p class="text-lg text-red-600 mb-4">${message}</p>
<button onclick="location.reload()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">
Reload
</button>
</div>
`;
this.elements.loading.classList.remove('hidden');
this.elements.gameArea.classList.add('hidden');
}
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
const app = new App();
// Show welcome overlay immediately before any loading
app.welcomeOverlay.show();
app.initialize();
// Expose API key setter for browser console
window.setOpenRouterKey = (key) => {
app.game.chatService.aiService.setApiKey(key);
console.log('OpenRouter API key updated');
};
}); |