Spaces:
Running
Running
Download js/engine.js from ballcoman/CosmoMente: direct link, hf CLI and curl.
- Browser
- Download file 7.36 kB
-
https://huggingface.co/spaces/ballcoman/CosmoMente/resolve/main/js/engine.js
- Command line
-
hf download hf://spaces/ballcoman/CosmoMente/js/engine.js
-
curl -L -o engine.js https://huggingface.co/spaces/ballcoman/CosmoMente/resolve/main/js/engine.js
7.36 kB
| import { app } from './app.js'; | |
| import { ui } from './ui.js'; | |
| export const gameEngine = { | |
| data: null, | |
| currentLevelIdx: 0, | |
| currentActivityIdx: 0, | |
| score: 0, | |
| maxScore: 0, | |
| errors: 0, | |
| startTime: 0, | |
| timerInterval: null, | |
| start: (tallerData) => { | |
| clearInterval(gameEngine.timerInterval); | |
| gameEngine.data = tallerData; | |
| gameEngine.score = 0; | |
| gameEngine.errors = 0; | |
| gameEngine.currentLevelIdx = 0; | |
| gameEngine.currentActivityIdx = 0; | |
| gameEngine.startTime = Date.now(); | |
| gameEngine.maxScore = tallerData.niveles.reduce((total, nivel) => { | |
| return total + nivel.actividades.reduce((nivelTotal, actividad) => { | |
| return nivelTotal + (actividad.puntos || 10); | |
| }, 0); | |
| }, 0); | |
| if (tallerData.intro_contenido) { | |
| gameEngine.showIntro(); | |
| } else { | |
| gameEngine.startLevel(0); | |
| } | |
| }, | |
| showIntro: () => { | |
| const intro = gameEngine.data.intro_contenido; | |
| const canvas = document.getElementById('canvas-reto'); | |
| let videoUrl = intro.url; | |
| if (videoUrl.includes("watch?v=")) { | |
| videoUrl = videoUrl.replace("watch?v=", "embed/"); | |
| } | |
| canvas.innerHTML = ` | |
| <div style="text-align:center; color:white; height:100%; display:flex; flex-direction:column; justify-content:center; align-items:center;"> | |
| <h2 class="game-title" style="margin-bottom:10px; font-size:1.8rem;">${intro.titulo}</h2> | |
| <p style="max-width:600px; margin-bottom:20px; color:#ccc;">${intro.texto}</p> | |
| <div style="width: 100%; max-width: 320px; aspect-ratio: 9/16; margin: 0 auto 20px auto; border-radius: 20px; overflow: hidden; box-shadow: 0 0 20px var(--neon-blue); border: 2px solid var(--neon-blue); background: #000;"> | |
| <iframe src="${videoUrl}" width="100%" height="100%" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> | |
| </div> | |
| <button class="btn-primary" onclick="gameEngine.startLevel(0)">COMENZAR MISIÓN</button> | |
| </div> | |
| `; | |
| }, | |
| startLevel: (levelIdx) => { | |
| if (!gameEngine.data.niveles[levelIdx]) return; | |
| gameEngine.currentLevelIdx = levelIdx; | |
| gameEngine.currentActivityIdx = 0; | |
| gameEngine.render(); | |
| }, | |
| render: () => { | |
| clearInterval(gameEngine.timerInterval); | |
| const timerBar = document.getElementById('timer-bar'); | |
| if(timerBar) timerBar.style.width = '100%'; | |
| const nivel = gameEngine.data.niveles[gameEngine.currentLevelIdx]; | |
| const actividad = nivel.actividades[gameEngine.currentActivityIdx]; | |
| const canvas = document.getElementById('canvas-reto'); | |
| canvas.innerHTML = ui.renderCard(actividad, nivel.nombre_planeta); | |
| const optsArea = document.getElementById('card-options-area'); | |
| let listaOpciones = actividad.opciones || actividad.options || []; | |
| listaOpciones.forEach((opt, idx) => { | |
| const btn = document.createElement('button'); | |
| btn.className = "btn-primary"; | |
| btn.innerText = opt; | |
| btn.onclick = () => gameEngine.validate(idx); | |
| optsArea.appendChild(btn); | |
| }); | |
| if (actividad.tiempo_limite) { | |
| gameEngine.startTimer(actividad.tiempo_limite); | |
| } else { | |
| document.getElementById('timer-display').innerText = ""; | |
| } | |
| }, | |
| validate: (idxUsuario) => { | |
| clearInterval(gameEngine.timerInterval); | |
| const nivel = gameEngine.data.niveles[gameEngine.currentLevelIdx]; | |
| const actividad = nivel.actividades[gameEngine.currentActivityIdx]; | |
| const modal = document.getElementById('modal-feedback'); | |
| modal.classList.remove('hidden'); | |
| if (idxUsuario === actividad.correcta) { | |
| document.getElementById('fb-title').innerText = "¡Correcto!"; | |
| document.getElementById('fb-msg').innerText = actividad.feedback; | |
| gameEngine.score += (actividad.puntos || 10); | |
| } else { | |
| document.getElementById('fb-title').innerText = "¡Sigue intentando!"; | |
| document.getElementById('fb-msg').innerText = "Respuesta incorrecta."; | |
| gameEngine.errors++; | |
| } | |
| }, | |
| nextStep: () => { | |
| const nivel = gameEngine.data.niveles[gameEngine.currentLevelIdx]; | |
| if (gameEngine.currentActivityIdx < nivel.actividades.length - 1) { | |
| gameEngine.currentActivityIdx++; | |
| gameEngine.render(); | |
| } else if (gameEngine.currentLevelIdx < gameEngine.data.niveles.length - 1) { | |
| gameEngine.startLevel(gameEngine.currentLevelIdx + 1); | |
| } else { | |
| gameEngine.checkCompletion(); | |
| } | |
| }, | |
| checkCompletion: () => { | |
| const isLastLevel = gameEngine.currentLevelIdx === gameEngine.data.niveles.length - 1; | |
| const isLastActivity = gameEngine.currentActivityIdx === gameEngine.data.niveles[gameEngine.currentLevelIdx].actividades.length - 1; | |
| if (isLastLevel && isLastActivity) { | |
| const canvas = document.getElementById('canvas-reto'); | |
| canvas.innerHTML = ` | |
| <div style="text-align:center; color:white; padding:20px;"> | |
| <h2>¡Felicidades!</h2> | |
| <p>Has completado el taller. ¿Listo para el siguiente?</p> | |
| <button class="btn-primary" onclick="gameEngine.finish()">Ir al siguiente taller</button> | |
| </div> | |
| `; | |
| } else { | |
| gameEngine.finish(); | |
| } | |
| }, | |
| startTimer: (seconds) => { | |
| let timeLeft = seconds; | |
| const timerDisplay = document.getElementById('timer-display'); | |
| const timerBar = document.getElementById('timer-bar'); | |
| timerDisplay.innerText = `⏳ ${timeLeft}s`; | |
| gameEngine.timerInterval = setInterval(() => { | |
| timeLeft--; | |
| timerDisplay.innerText = `⏳ ${timeLeft}s`; | |
| if(timerBar) timerBar.style.width = (timeLeft / seconds) * 100 + "%"; | |
| if (timeLeft <= 0) { | |
| clearInterval(gameEngine.timerInterval); | |
| alert("Tiempo agotado"); | |
| gameEngine.errors++; | |
| gameEngine.nextStep(); | |
| } | |
| }, 1000); | |
| }, | |
| finish: () => { | |
| clearInterval(gameEngine.timerInterval); | |
| const duration = Math.floor((Date.now() - gameEngine.startTime) / 1000); | |
| const totalActivities = gameEngine.data.niveles.reduce((acc, nivel) => acc + nivel.actividades.length, 0); | |
| const aciertos = totalActivities - gameEngine.errors; | |
| // --- LÓGICA DE PUNTUACIÓN MEJORADA --- | |
| // El puntaje es el porcentaje de aciertos. Cada pregunta vale lo mismo. | |
| const percentageScore = (totalActivities > 0) ? Math.round((aciertos * 100) / totalActivities) : 0; | |
| app.finishWorkshop({ | |
| taller_id: gameEngine.data.id, | |
| aciertos: aciertos, | |
| errores: gameEngine.errors, | |
| tiempo: duration, | |
| puntaje: percentageScore | |
| }); | |
| } | |
| }; | |
| window.gameEngine = gameEngine; |