File size: 7,355 Bytes
889c7bb
146ae5a
889c7bb
 
 
 
 
 
afd6f11
889c7bb
 
 
 
 
5f10a09
889c7bb
 
 
 
 
 
 
afd6f11
 
 
 
 
 
889c7bb
 
 
 
 
 
 
 
 
 
 
146ae5a
 
 
 
 
889c7bb
146ae5a
 
 
 
 
889c7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146ae5a
 
889c7bb
146ae5a
889c7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146ae5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
889c7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f10a09
889c7bb
146ae5a
 
afd6f11
 
 
 
146ae5a
889c7bb
 
146ae5a
889c7bb
 
afd6f11
889c7bb
 
 
 
 
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
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;