-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-quest-code-fixes.js
More file actions
396 lines (346 loc) · 12.5 KB
/
sql-quest-code-fixes.js
File metadata and controls
396 lines (346 loc) · 12.5 KB
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
// ============================================================================
// FIXED CODE SNIPPETS FOR SQL QUEST
// ============================================================================
// ========== FIX 1: Tailwind Dynamic Classes in Blitz Mode ==========
// Location: Lines 17236-17247
// BEFORE (BROKEN):
{[
{ points: '10 pts', label: 'Easy', color: 'green' },
{ points: '20 pts', label: 'Medium', color: 'yellow' },
{ points: '30 pts', label: 'Hard', color: 'red' }
].map(d => (
<div key={d.label} className={`bg-${d.color}-500/10 border border-${d.color}-500/30 rounded-lg p-3`}>
// AFTER (FIXED):
{[
{ points: '10 pts', label: 'Easy', color: 'green' },
{ points: '20 pts', label: 'Medium', color: 'yellow' },
{ points: '30 pts', label: 'Hard', color: 'red' }
].map(d => (
<div key={d.label} className={`rounded-lg p-3 ${
d.color === 'green' ? 'bg-green-500/10 border border-green-500/30' :
d.color === 'yellow' ? 'bg-yellow-500/10 border border-yellow-500/30' :
'bg-red-500/10 border border-red-500/30'
}`}>
<p className={`font-bold ${
d.color === 'green' ? 'text-green-400' :
d.color === 'yellow' ? 'text-yellow-400' :
'text-red-400'
}`}>{d.points}</p>
<p className="text-xs text-gray-400">{d.label}</p>
</div>
))}
// ========== FIX 2: Tailwind Dynamic Classes in Read/Explain Mode ==========
// Location: Lines 19277-19286
// BEFORE (BROKEN):
{[
{ label: 'Easy', xp: '10 XP', color: 'green', desc: 'Basic SELECT, WHERE, ORDER BY' },
{ label: 'Medium', xp: '20 XP', color: 'yellow', desc: 'GROUP BY, HAVING, subqueries' },
{ label: 'Hard', xp: '30 XP', color: 'red', desc: 'Window functions, CTEs, correlated' }
].map(d => (
<div key={d.label} className={`bg-${d.color}-500/10 border border-${d.color}-500/30 rounded-lg p-3`}>
// AFTER (FIXED):
{[
{ label: 'Easy', xp: '10 XP', color: 'green', desc: 'Basic SELECT, WHERE, ORDER BY' },
{ label: 'Medium', xp: '20 XP', color: 'yellow', desc: 'GROUP BY, HAVING, subqueries' },
{ label: 'Hard', xp: '30 XP', color: 'red', desc: 'Window functions, CTEs, correlated' }
].map(d => (
<div key={d.label} className={`rounded-lg p-3 ${
d.color === 'green' ? 'bg-green-500/10 border border-green-500/30' :
d.color === 'yellow' ? 'bg-yellow-500/10 border border-yellow-500/30' :
'bg-red-500/10 border border-red-500/30'
}`}>
<p className={`font-bold ${
d.color === 'green' ? 'text-green-400' :
d.color === 'yellow' ? 'text-yellow-400' :
'text-red-400'
}`}>{d.xp}</p>
<p className="text-xs text-gray-400">{d.label}</p>
</div>
))}
// ========== FIX 3: Speed Run Timer Cleanup ==========
// Location: Lines 2651-2670
// BEFORE (has dependency issue):
useEffect(() => {
let interval;
if (speedRunActive && speedRunTimer > 0) {
interval = setInterval(() => {
setSpeedRunTimer(prev => {
if (prev <= 1) {
clearInterval(interval);
endSpeedRun();
return 0;
}
return prev - 1;
});
}, 1000);
}
return () => clearInterval(interval);
}, [speedRunActive, speedRunTimer]); // speedRunTimer causes re-render every second
// AFTER (FIXED):
useEffect(() => {
if (!speedRunActive) return;
const interval = setInterval(() => {
setSpeedRunTimer(prev => {
if (prev <= 1) {
endSpeedRun();
return 0;
}
return prev - 1;
});
}, 1000);
return () => {
clearInterval(interval);
};
}, [speedRunActive]); // Only depend on speedRunActive
// ========== FIX 4: Boss Battle Validation ==========
// Location: Around line 4025
// BEFORE (no validation):
const startBossBattle = (skillTopic) => {
const bossQuestions = getChallengesForTopic(skillTopic, 'hard');
const boss = {
...sqlBosses[skillTopic],
currentHP: sqlBosses[skillTopic].maxHP,
totalQuestions: bossQuestions.length,
questionsRemaining: bossQuestions.length
};
setCurrentBoss(boss);
// AFTER (FIXED):
const startBossBattle = (skillTopic) => {
const bossQuestions = getChallengesForTopic(skillTopic, 'hard');
// Validate questions exist
if (!bossQuestions || bossQuestions.length === 0) {
console.error(`No ${skillTopic} questions available for boss battle`);
setShowNotification({
type: 'error',
message: `No ${skillTopic} challenges available. Try a different skill!`
});
return;
}
const boss = {
...sqlBosses[skillTopic],
currentHP: sqlBosses[skillTopic].maxHP,
totalQuestions: bossQuestions.length,
questionsRemaining: bossQuestions.length,
questions: bossQuestions,
skill: skillTopic
};
setCurrentBoss(boss);
setBossBattleMode(true);
playSound('boss-start');
};
// ========== FIX 5: Data Loading Check ==========
// Add near top of component (after state declarations)
const [dataLoaded, setDataLoaded] = useState(false);
const [dataError, setDataError] = useState(null);
// Add this useEffect
useEffect(() => {
const checkData = () => {
try {
// Check if challenge data is loaded
if (!window.challengesData || window.challengesData.length === 0) {
console.warn('Challenge data not found on window object');
setDataError('Challenge data not loaded');
// Try to load from data.js if it exists
const existingScript = document.querySelector('script[src*="data.js"]');
if (!existingScript) {
const script = document.createElement('script');
script.src = '/data.js';
script.async = true;
script.onload = () => {
if (window.challengesData) {
setDataLoaded(true);
setDataError(null);
}
};
script.onerror = () => setDataError('Failed to load challenge data');
document.body.appendChild(script);
}
} else {
setDataLoaded(true);
}
} catch (error) {
console.error('Error checking data:', error);
setDataError(error.message);
}
};
checkData();
}, []);
// ========== FIX 6: Exercise Navigation Safety ==========
// Location: Around line 18680
// BEFORE (potential array access error):
<h3 className="text-lg font-bold mb-4">
{aiLessons[selectedExerciseLesson].exercises[currentExerciseIndex].question}
</h3>
// AFTER (FIXED):
{(() => {
const currentLesson = aiLessons[selectedExerciseLesson];
if (!currentLesson) {
console.error('Lesson not found:', selectedExerciseLesson);
return <p className="text-red-400">Error: Lesson not found</p>;
}
const currentExercise = currentLesson.exercises[currentExerciseIndex];
if (!currentExercise) {
console.error('Exercise not found:', currentExerciseIndex);
return <p className="text-red-400">Error: Exercise not found</p>;
}
return (
<h3 className="text-lg font-bold mb-4">
{currentExercise.question}
</h3>
);
})()}
// ========== FIX 7: Explain Query AI Error Handling ==========
// Location: Lines 2890-2918
// BEFORE (minimal error handling):
try {
const resp = await callAI([
{ role: 'user', content: `SQL Query:\n${explainQuery.query}...` }
], 'You are a SQL teacher...');
if (resp && typeof resp === 'string') {
const jsonMatch = resp.match(/\{[\s\S]*\}/);
if (jsonMatch) {
const parsed = JSON.parse(jsonMatch[0]);
aiScore = parsed.score;
aiFeedback = parsed.feedback + (parsed.missing ? ` Missing: ${parsed.missing}` : '');
}
}
} catch (error) {
console.error('AI eval error:', error);
}
// AFTER (FIXED):
let aiEvaluationFailed = false;
try {
const resp = await callAI([
{ role: 'user', content: `SQL Query:\n${explainQuery.query}\n\nStudent's explanation:\n"${explainAnswer}"\n\nCorrect explanation: "${explainQuery.explanation}"\n\nRate the student's explanation from 0-100 on accuracy and completeness. Respond ONLY with JSON: {"score": <number>, "feedback": "<1-2 sentence feedback>", "missing": "<what they missed, if anything>"}` }
], 'You are a SQL teacher evaluating a student\'s ability to explain what a SQL query does. Be encouraging but accurate. If they got the gist right, give at least 60. Focus on whether they understand the query\'s PURPOSE and KEY operations.');
if (resp && typeof resp === 'string') {
const jsonMatch = resp.match(/\{[\s\S]*\}/);
if (jsonMatch) {
try {
const parsed = JSON.parse(jsonMatch[0]);
// Validate score is a number between 0-100
if (typeof parsed.score === 'number' && parsed.score >= 0 && parsed.score <= 100) {
aiScore = Math.round(parsed.score);
aiFeedback = parsed.feedback || 'AI evaluation completed';
if (parsed.missing && parsed.missing.trim()) {
aiFeedback += ` Missing: ${parsed.missing}`;
}
} else {
console.warn('Invalid AI score:', parsed.score);
aiEvaluationFailed = true;
}
} catch (parseError) {
console.error('Failed to parse AI JSON response:', parseError);
aiEvaluationFailed = true;
}
} else {
console.warn('No JSON found in AI response');
aiEvaluationFailed = true;
}
} else {
console.warn('Invalid AI response format');
aiEvaluationFailed = true;
}
} catch (error) {
console.error('AI evaluation failed:', error);
aiEvaluationFailed = true;
}
// If AI evaluation failed, fall back to keyword scoring only
if (aiEvaluationFailed) {
console.log('Falling back to keyword-only scoring');
aiScore = null;
aiFeedback = '';
}
// ========== FIX 8: Daily Workout Date Comparison ==========
// Location: Around line 17436
// BEFORE:
disabled={lastWorkoutDate === new Date().toDateString() && workoutCompleted}
// AFTER (FIXED):
{(() => {
const today = new Date().toDateString();
const isCompletedToday = lastWorkoutDate === today && workoutCompleted;
return (
<button
onClick={startDailyWorkout}
disabled={isCompletedToday}
className={`w-full py-2 rounded-lg font-medium text-sm ${
isCompletedToday
? 'bg-gray-700 text-gray-500 cursor-not-allowed'
: 'bg-green-600 hover:bg-green-700 text-white'
}`}
>
{isCompletedToday ? '✓ Completed Today' : '▶ Start Workout'}
</button>
);
})()}
// ========== BONUS: Add Loading State for Modes ==========
// Add this before rendering mode content
// For Blitz Mode
{!dataLoaded && practiceSubTab === 'speed-run' && (
<div className="max-w-4xl mx-auto">
<div className="bg-black/30 rounded-xl border border-yellow-500/30 p-8 text-center">
<div className="animate-spin text-6xl mb-4">⏳</div>
<p className="text-gray-400">Loading challenges...</p>
{dataError && (
<p className="text-red-400 text-sm mt-2">{dataError}</p>
)}
</div>
</div>
)}
// For Train Mode
{!dataLoaded && practiceSubTab === 'skill-forge' && (
<div className="max-w-4xl mx-auto">
<div className="bg-black/30 rounded-xl border border-orange-500/30 p-8 text-center">
<div className="animate-spin text-6xl mb-4">⏳</div>
<p className="text-gray-400">Loading training data...</p>
{dataError && (
<p className="text-red-400 text-sm mt-2">{dataError}</p>
)}
</div>
</div>
)}
// ========== BONUS: Error Boundary Component ==========
// Add this near the top of the file, after imports
class ModeErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
console.error('Mode error:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div className="max-w-4xl mx-auto">
<div className="bg-red-500/10 rounded-xl border border-red-500/30 p-8 text-center">
<div className="text-6xl mb-4">⚠️</div>
<h2 className="text-2xl font-bold mb-2 text-red-400">Something went wrong</h2>
<p className="text-gray-400 mb-4">
{this.state.error?.message || 'An error occurred in this mode'}
</p>
<button
onClick={() => {
this.setState({ hasError: false, error: null });
window.location.reload();
}}
className="px-6 py-3 bg-red-600 hover:bg-red-700 rounded-xl font-bold"
>
Reload Page
</button>
</div>
</div>
);
}
return this.props.children;
}
}
// Then wrap each mode in the error boundary:
<ModeErrorBoundary>
{activeTab === 'quests' && practiceSubTab === 'speed-run' && (
// ... speed run content
)}
</ModeErrorBoundary>