-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub-main.js
More file actions
615 lines (530 loc) · 21.4 KB
/
hub-main.js
File metadata and controls
615 lines (530 loc) · 21.4 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// ========================================
// CHESSARCADE HUB - LÓGICA PRINCIPAL
// Maneja el hub principal y estadísticas globales
// Versión: 1.0.0
// ========================================
// ========================================
// CONFIGURACIÓN DEL HUB
// ========================================
const HUB_CONFIG = {
version: '1.0.0',
games: ['knight-quest', 'vision-blitz', 'square-rush', 'tactic-burst', 'checkmate-countdown', 'memory-matrix'],
availableGames: ['knight-quest'], // Solo juegos implementados
updateInterval: 5000 // 5 segundos para actualizar stats
};
// ========================================
// ESTADO DEL HUB
// ========================================
let hubState = {
userStats: {
totalGames: 0,
bestScore: 0,
totalTime: 0,
totalAchievements: 0
},
gameStats: {},
settings: {
soundEnabled: true,
theme: 'arcade',
language: 'es'
}
};
// ========================================
// INICIALIZACIÓN DEL HUB
// ========================================
function initHub() {
console.log(`Inicializando ChessArcade Hub v${HUB_CONFIG.version}`);
// Cargar configuración
loadHubSettings();
// Cargar estadísticas
loadUserStats();
// Actualizar UI
updateUserStatsDisplay();
updateGameCards();
// Configurar efectos visuales
setupVisualEffects();
// Bind eventos
bindHubEvents();
console.log('Hub inicializado correctamente');
}
// ========================================
// GESTIÓN DE ESTADÍSTICAS
// ========================================
function loadUserStats() {
// Cargar estadísticas globales del usuario
const totalStats = {
totalGames: 0,
bestScore: 0,
totalTime: 0,
totalAchievements: 0
};
// Agregar estadísticas de cada juego
HUB_CONFIG.games.forEach(gameName => {
const gameScores = ChessArcade.getLocalLeaderboard(gameName, 100);
const gameStats = getLocalData(`${gameName}_stats`, {
gamesPlayed: 0,
bestScore: 0,
bestTime: Infinity,
perfectGames: 0
});
totalStats.totalGames += gameStats.gamesPlayed;
totalStats.bestScore = Math.max(totalStats.bestScore, gameStats.bestScore);
// Sumar tiempo total de todas las partidas
gameScores.forEach(score => {
totalStats.totalTime += score.time || 0;
});
// Guardar stats específicos del juego
hubState.gameStats[gameName] = {
...gameStats,
recentScores: gameScores.slice(0, 5)
};
});
// Contar logros totales
const unlockedAchievements = getLocalData('unlocked_achievements', []);
totalStats.totalAchievements = unlockedAchievements.length;
hubState.userStats = totalStats;
}
function updateUserStatsDisplay() {
document.getElementById('totalGames').textContent = hubState.userStats.totalGames;
document.getElementById('bestScore').textContent = ChessArcade.formatScore(hubState.userStats.bestScore);
document.getElementById('totalTime').textContent = ChessArcade.formatTime(hubState.userStats.totalTime);
document.getElementById('totalAchievements').textContent = hubState.userStats.totalAchievements;
}
function updateGameCards() {
// Actualizar Knight Quest card
const knightStats = hubState.gameStats['knight-quest'];
if (knightStats) {
document.getElementById('knightBestScore').textContent = ChessArcade.formatScore(knightStats.bestScore);
document.getElementById('knightBestTime').textContent =
knightStats.bestTime !== Infinity ? ChessArcade.formatTime(knightStats.bestTime) : '--:--';
}
// Agregar indicadores de progreso a juegos disponibles
HUB_CONFIG.availableGames.forEach(gameName => {
const card = document.getElementById(`${gameName.replace('-', '')}Card`);
if (card) {
card.classList.add('available');
card.classList.remove('coming-soon');
// Agregar badge de "nuevo" si nunca jugó
const stats = hubState.gameStats[gameName];
if (stats && stats.gamesPlayed === 0) {
const newBadge = document.createElement('div');
newBadge.className = 'new-badge';
newBadge.textContent = '¡NUEVO!';
card.appendChild(newBadge);
}
}
});
}
// ========================================
// EFECTOS VISUALES
// ========================================
function setupVisualEffects() {
// Animar piezas de ajedrez en el header
const pieces = document.querySelectorAll('.piece');
pieces.forEach((piece, index) => {
piece.addEventListener('click', () => {
piece.style.animation = 'none';
piece.style.transform = `scale(1.5) rotateZ(${360 + Math.random() * 360}deg)`;
setTimeout(() => {
piece.style.animation = '';
piece.style.transform = '';
}, 600);
ChessArcade.playSound('click');
});
// Hover effects
piece.addEventListener('mouseenter', () => {
if (!piece.style.animation.includes('none')) {
piece.style.animationDelay = '0s';
}
});
});
// Efectos de las cards de juegos
const gameCards = document.querySelectorAll('.game-card.available');
gameCards.forEach(card => {
card.addEventListener('mouseenter', () => {
ChessArcade.playSound('click');
});
});
// Efectos de las cards "coming soon"
const comingSoonCards = document.querySelectorAll('.game-card.coming-soon');
comingSoonCards.forEach(card => {
card.addEventListener('click', (e) => {
e.preventDefault();
ChessArcade.shakeElement(card);
ChessArcade.showNotification('🔄 ¡Este juego estará disponible pronto!', 'info', 2000);
ChessArcade.playSound('error');
});
});
}
// ========================================
// CONFIGURACIÓN Y SETTINGS
// ========================================
function loadHubSettings() {
const savedSettings = ChessArcade.getSettings();
hubState.settings = { ...hubState.settings, ...savedSettings };
// Aplicar configuración
applyHubSettings();
}
function applyHubSettings() {
// Aplicar tema
document.body.className = `theme-${hubState.settings.theme}`;
// Actualizar UI de configuración
const soundToggle = document.getElementById('soundToggle');
if (soundToggle) {
soundToggle.checked = hubState.settings.soundEnabled;
}
const themeSelect = document.getElementById('themeSelect');
if (themeSelect) {
themeSelect.value = hubState.settings.theme;
}
}
function showSettings() {
const modal = document.getElementById('settingsModal');
// Actualizar valores en el modal
document.getElementById('soundToggle').checked = ChessArcade.soundEnabled;
document.getElementById('themeSelect').value = hubState.settings.theme;
modal.style.display = 'flex';
setTimeout(() => modal.classList.add('show'), 100);
}
function closeSettings() {
const modal = document.getElementById('settingsModal');
modal.classList.remove('show');
setTimeout(() => modal.style.display = 'none', 300);
}
function toggleGlobalSound() {
const soundToggle = document.getElementById('soundToggle');
ChessArcade.soundEnabled = soundToggle.checked;
hubState.settings.soundEnabled = soundToggle.checked;
ChessArcade.saveSettings(hubState.settings);
ChessArcade.showNotification(
soundToggle.checked ? '🔊 Sonido activado' : '🔇 Sonido desactivado',
'info'
);
if (soundToggle.checked) {
ChessArcade.playSound('success');
}
}
function changeTheme() {
const themeSelect = document.getElementById('themeSelect');
const newTheme = themeSelect.value;
hubState.settings.theme = newTheme;
ChessArcade.saveSettings(hubState.settings);
// Aplicar nuevo tema
document.body.className = `theme-${newTheme}`;
const themeNames = {
arcade: '🕹️ Arcade Clásico',
retro: '📟 Retro 80s',
neon: '💫 Neon Cyber'
};
ChessArcade.showNotification(`🎨 Tema cambiado a ${themeNames[newTheme]}`, 'success');
ChessArcade.playSound('success');
}
function resetAllData() {
if (confirm('⚠️ ¿Estás seguro de que quieres borrar TODOS los datos?\n\nEsto eliminará:\n• Todas las puntuaciones\n• Todos los logros\n• Todo el progreso\n• Configuración\n\nEsta acción NO se puede deshacer.')) {
if (confirm('🚨 ÚLTIMA CONFIRMACIÓN\n\n¿Realmente quieres eliminar TODO tu progreso en ChessArcade?')) {
// Limpiar localStorage
const keysToRemove = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && (key.startsWith('chessarcade_') || key.startsWith('knight_') || key.includes('_scores') || key.includes('_stats'))) {
keysToRemove.push(key);
}
}
keysToRemove.forEach(key => localStorage.removeItem(key));
// Reset estado del hub
hubState.userStats = {
totalGames: 0,
bestScore: 0,
totalTime: 0,
totalAchievements: 0
};
hubState.gameStats = {};
// Actualizar UI
updateUserStatsDisplay();
updateGameCards();
ChessArcade.showNotification('🗑️ Todos los datos han sido eliminados', 'warning', 3000);
ChessArcade.playSound('error');
// Cerrar modal
closeSettings();
}
}
}
// ========================================
// LEADERBOARDS GLOBALES
// ========================================
function showGlobalLeaderboard() {
const modal = document.getElementById('globalLeaderboardModal');
modal.style.display = 'flex';
setTimeout(() => modal.classList.add('show'), 100);
// Mostrar por defecto Knight Quest
showGlobalTab('knight-quest');
}
function closeGlobalLeaderboard() {
const modal = document.getElementById('globalLeaderboardModal');
modal.classList.remove('show');
setTimeout(() => modal.style.display = 'none', 300);
}
function showGlobalTab(tab) {
// Actualizar tabs activos
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
event.target.classList.add('active');
const content = document.getElementById('globalLeaderboardContent');
if (tab === 'knight-quest') {
showKnightQuestLeaderboard(content);
} else if (tab === 'all') {
showAllGamesLeaderboard(content);
} else if (tab === 'achievements') {
showAchievementsLeaderboard(content);
}
}
function showKnightQuestLeaderboard(container) {
const scores = ChessArcade.getLocalLeaderboard('knight-quest', 20);
if (scores.length === 0) {
container.innerHTML = `
<div class="empty-leaderboard">
<div class="empty-icon">🐴</div>
<p>¡Aún no hay puntuaciones en Knight Quest!</p>
<p><a href="games/knight-quest/index.html" class="btn btn-primary">¡Jugar ahora!</a></p>
</div>
`;
return;
}
container.innerHTML = `
<div class="leaderboard-list">
${scores.map((score, index) => `
<div class="leaderboard-item">
<div class="rank ${index === 0 ? 'first' : index === 1 ? 'second' : index === 2 ? 'third' : ''}">#${index + 1}</div>
<div class="player-info">
<div class="player-name">Knight Master ${score.id.toString().slice(-4)}</div>
<div class="player-details">
${score.difficulty} • ${score.moves || 0} movimientos
${score.perfect ? ' • ⭐ Perfecto' : ''}
${score.hintsUsed !== undefined ? ` • ${score.hintsUsed} pistas` : ''}
</div>
</div>
<div class="score-info">
<div class="score">${ChessArcade.formatScore(score.score)}</div>
<div class="time">${ChessArcade.formatTime(score.time)}</div>
<div class="date">${ChessArcade.formatDate(score.date)}</div>
</div>
</div>
`).join('')}
</div>
`;
}
function showAllGamesLeaderboard(container) {
const allScores = [];
HUB_CONFIG.games.forEach(gameName => {
const gameScores = ChessArcade.getLocalLeaderboard(gameName, 10);
gameScores.forEach(score => {
allScores.push({
...score,
game: gameName,
gameName: getGameDisplayName(gameName)
});
});
});
// Ordenar por puntuación
allScores.sort((a, b) => b.score - a.score);
allScores.splice(20); // Top 20
if (allScores.length === 0) {
container.innerHTML = `
<div class="empty-leaderboard">
<div class="empty-icon">🎮</div>
<p>¡Aún no hay puntuaciones en ningún juego!</p>
<p>¡Empieza a jugar para aparecer aquí!</p>
</div>
`;
return;
}
container.innerHTML = `
<div class="leaderboard-list">
${allScores.map((score, index) => `
<div class="leaderboard-item">
<div class="rank ${index === 0 ? 'first' : index === 1 ? 'second' : index === 2 ? 'third' : ''}">#${index + 1}</div>
<div class="player-info">
<div class="player-name">${score.gameName} Master</div>
<div class="player-details">
${score.gameName} • ${ChessArcade.formatDate(score.date)}
</div>
</div>
<div class="score-info">
<div class="score">${ChessArcade.formatScore(score.score)}</div>
<div class="time">${ChessArcade.formatTime(score.time)}</div>
</div>
</div>
`).join('')}
</div>
`;
}
function showAchievementsLeaderboard(container) {
const unlockedAchievements = getLocalData('unlocked_achievements', []);
if (unlockedAchievements.length === 0) {
container.innerHTML = `
<div class="empty-leaderboard">
<div class="empty-icon">🏆</div>
<p>¡Aún no has desbloqueado ningún logro!</p>
<p>Juega para conseguir achievements épicos.</p>
</div>
`;
return;
}
// Obtener información de todos los achievements disponibles
const ACHIEVEMENTS = {
'first_game': { name: 'Primer Paso', description: 'Completa tu primer juego', icon: '🎮' },
'speed_demon': { name: 'Demonio de Velocidad', description: 'Completa un juego en menos de 30 segundos', icon: '⚡' },
'perfectionist': { name: 'Perfeccionista', description: 'Completa un juego sin errores', icon: '💎' },
'persistent': { name: 'Perseverante', description: 'Juega 10 partidas', icon: '🔥' },
'high_scorer': { name: 'Puntuador Elite', description: 'Alcanza 1000 puntos', icon: '🌟' }
};
container.innerHTML = `
<div class="achievements-grid">
${unlockedAchievements.map(achievementId => {
const achievement = ACHIEVEMENTS[achievementId];
if (!achievement) return '';
return `
<div class="achievement-card unlocked">
<div class="achievement-icon">${achievement.icon}</div>
<div class="achievement-info">
<div class="achievement-name">${achievement.name}</div>
<div class="achievement-desc">${achievement.description}</div>
</div>
<div class="achievement-status">✅ Desbloqueado</div>
</div>
`;
}).join('')}
${Object.entries(ACHIEVEMENTS).map(([id, achievement]) => {
if (unlockedAchievements.includes(id)) return '';
return `
<div class="achievement-card locked">
<div class="achievement-icon">🔒</div>
<div class="achievement-info">
<div class="achievement-name">???</div>
<div class="achievement-desc">Sigue jugando para desbloquear</div>
</div>
<div class="achievement-status">🔒 Bloqueado</div>
</div>
`;
}).join('')}
</div>
<div class="achievements-summary">
<p><strong>${unlockedAchievements.length}</strong> de <strong>${Object.keys(ACHIEVEMENTS).length}</strong> logros desbloqueados</p>
<div class="progress-bar">
<div class="progress-fill" style="width: ${(unlockedAchievements.length / Object.keys(ACHIEVEMENTS).length) * 100}%"></div>
</div>
</div>
`;
}
// ========================================
// UTILIDADES
// ========================================
function getGameDisplayName(gameName) {
const names = {
'knight-quest': 'Knight Quest',
'vision-blitz': 'Vision Blitz',
'square-rush': 'Square Rush',
'tactic-burst': 'Tactic Burst',
'checkmate-countdown': 'Checkmate Countdown',
'memory-matrix': 'Memory Matrix'
};
return names[gameName] || gameName;
}
function getLocalData(key, defaultValue = null) {
try {
const chessArcadeData = JSON.parse(localStorage.getItem('chessarcade_data') || '{}');
return chessArcadeData[key] || defaultValue;
} catch (error) {
console.error('Error getting local data:', error);
return defaultValue;
}
}
function refreshStats() {
loadUserStats();
updateUserStatsDisplay();
updateGameCards();
}
// ========================================
// EVENT LISTENERS
// ========================================
function bindHubEvents() {
// Actualizar estadísticas periódicamente
setInterval(refreshStats, HUB_CONFIG.updateInterval);
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey) {
switch(e.key.toLowerCase()) {
case 'k':
e.preventDefault();
window.location.href = 'games/knight-quest/index.html';
break;
case 'l':
e.preventDefault();
showGlobalLeaderboard();
break;
case ',':
e.preventDefault();
showSettings();
break;
}
}
if (e.key === 'Escape') {
// Cerrar modales abiertos
const modals = document.querySelectorAll('.modal-overlay.show');
modals.forEach(modal => {
if (modal.id === 'globalLeaderboardModal') closeGlobalLeaderboard();
if (modal.id === 'settingsModal') closeSettings();
});
}
});
// Enlaces a juegos
document.querySelectorAll('a[href*="games/"]').forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (href.includes('knight-quest')) {
ChessArcade.playSound('success');
}
});
});
// Efectos de click en botones
document.querySelectorAll('.btn').forEach(btn => {
btn.addEventListener('click', () => {
ChessArcade.playSound('click');
});
});
}
// ========================================
// FUNCIONES GLOBALES PARA HTML
// ========================================
window.showGlobalLeaderboard = showGlobalLeaderboard;
window.closeGlobalLeaderboard = closeGlobalLeaderboard;
window.showGlobalTab = showGlobalTab;
window.showSettings = showSettings;
window.closeSettings = closeSettings;
window.toggleGlobalSound = toggleGlobalSound;
window.changeTheme = changeTheme;
window.resetAllData = resetAllData;
// ========================================
// INICIALIZACIÓN
// ========================================
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM loaded, inicializando Hub...');
// Esperar a que shared-utils esté cargado
if (typeof ChessArcade !== 'undefined') {
initHub();
} else {
setTimeout(() => {
if (typeof ChessArcade !== 'undefined') {
initHub();
} else {
console.error('ChessArcade utilities no cargadas');
}
}, 500);
}
});
// Auto-refresh stats cuando la página vuelve a tener foco
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
refreshStats();
}
});
console.log('ChessArcade Hub Main v' + HUB_CONFIG.version + ' cargado');