-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
308 lines (261 loc) · 10.3 KB
/
Copy pathscript.js
File metadata and controls
308 lines (261 loc) · 10.3 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
let gameIsActive = true;
let lives = 10;
let correctCategories = [];
let mangaList = [];
// cargar datos json
fetch('./top100film.json')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
mangaList = data;// actualizar mangaList
initGame();
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
function initGame() {
populateDataList();
resetGame();
}
//// función vista de vidas o partidas //////
function displayLives() {
const livesElement = document.getElementById("lives");
let hearts = '';
for (let i = 0; i < lives; i++) {
hearts += '❤️';
}
for (let i = 0; i < (10 - lives); i++) {
hearts += '🤍';
}
livesElement.innerHTML = hearts;
}
displayLives();
//// función mostrar opciones de dentro del json //////
function populateDataList() {
const dataList = document.getElementById("manga-titles");
mangaList.forEach(manga => {
const option = document.createElement("option");
option.value = manga.title;
dataList.appendChild(option);
});
}
populateDataList();
//// función obtener el objeto aleatorio del json //////
function getRandomManga() {
const randomIndex = Math.floor(Math.random() * mangaList.length);
return mangaList[randomIndex];
}
let mangaToGuess = getRandomManga();
//// función reiniciar el juego //////
function resetGame() {
gameIsActive = true;
mangaToGuess = getRandomManga(); // generar un nuevo objeto aleatorio para adivinar
const input = document.getElementById("guess");
const guessButton = document.getElementById("guessButton");
input.disabled = false;
guessButton.disabled = false;
lives = 10; // reinicia vidas
displayLives(); // actualiza info de vidas mostradas
// limpia la tabla de info previa del juego
const tableBody = document.getElementById("infoTable").getElementsByTagName('tbody')[0];
tableBody.innerHTML = "";
// Mostrar el botón de hint-pista
document.getElementById("hintButton").style.display = "inline-block";
}
//// función dar una pista //////
function giveHint() {
if (!gameIsActive) return; // no dar pistas si es game over
// esconde el boton hint
document.getElementById("hintButton").style.display = "none";
// Calcular la lista de categorías que aún no se han adivinado correctamente
const unguessedCategories = Object.keys(mangaToGuess).filter(category => {
return !correctCategories.includes(category) && category !== 'image' && category !== 'title' && category !== 'Director' && category !== 'Artists';
});
// Elegir una categoría aleatoria no adivinada para proporcionar una pista.
const randomCategoryIndex = Math.floor(Math.random() * unguessedCategories.length);
const hintCategory = unguessedCategories[randomCategoryIndex];
// dar una pista basado en la categoria
const tableBody = document.getElementById("infoTable").getElementsByTagName('tbody')[0];
const newRow = tableBody.insertRow();
const imgCell = newRow.insertCell(0);
const imgElement = document.createElement("img");
imgElement.src = "./img/img-hint.JPG"; // Usa la URL de la imagen del hint
imgElement.alt = "Hint Image";
imgElement.style.width = "50px"; // Ajusta el tamaño
imgElement.style.height = "auto"; // Ajusta el tamaño según sea necesario
imgCell.appendChild(imgElement);
Object.keys(mangaToGuess).forEach((key, i) => {
if (key !== 'image') {
const cell = newRow.insertCell(i);
imgCell.style.border = '1px solid blue';
imgCell.style.borderRadius = '6px';
imgCell.style.padding = '5px';
if (key === hintCategory) {
cell.innerText = mangaToGuess[hintCategory];
cell.style.backgroundColor = '#00FF00';
cell.style.padding = '5px 2px 5px 2px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
} else {
cell.innerText = " "; // celdas vacias para resto
cell.style.backgroundColor = '#FF3728';
}
}
});
}
//// función chequeo de seleccion del usuario //////
function checkGuess() {
if (!gameIsActive) return;
const input = document.getElementById("guess");
const guessButton = document.getElementById("guessButton");
const guess = input.value.toLowerCase();
const validTitles = mangaList.map(manga => manga.title.toLowerCase());
if (!validTitles.includes(guess)) {
alert("Please enter a valid title from the list");
return;
}
const guessedManga = mangaList.find(manga => manga.title.toLowerCase() === guess);
const tableBody = document.getElementById("infoTable").getElementsByTagName('tbody')[0];
const newRow = tableBody.insertRow();
// Manejo de cada categoría //
// Inserta la image en la celda inicial
const imgCell = newRow.insertCell(0);
imgCell.style.border = '1px solid blue';
imgCell.style.borderRadius = '6px';
imgCell.style.padding = '5px';
const imgElement = document.createElement("img");
imgElement.src = guessedManga.image;
imgElement.alt = guessedManga.title;
imgElement.style.width = "50px"; // Ajusta el tamaño según sea necesario
imgElement.style.height = "auto"; // Ajusta el tamaño según sea necesario
imgCell.appendChild(imgElement);
Object.keys(mangaToGuess).forEach((key, i) => {
if (key !== 'image') {
const cell = newRow.insertCell(i);
cell.innerText = guessedManga[key];
// celda 'year'
if (key === 'year') {
const currentYear = new Date().getFullYear();
const correctYear = parseInt(mangaToGuess[key] === "Ongoing" ? currentYear : mangaToGuess[key]);
const guessedYear = parseInt(guessedManga[key] === "Ongoing" ? currentYear : guessedManga[key]);
if (guessedYear === correctYear) {
cell.style.backgroundColor = '#00FF00';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
} else if (typeof guessedYear === "number" && typeof correctYear === "number") {
const diff = guessedYear - correctYear;
if (Math.abs(diff) <= 55) {
cell.style.backgroundColor = 'yellow';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
cell.innerHTML += ` ${diff < 0 ? '▲' : '▼'}`;
} else {
cell.style.backgroundColor = '#FF3728';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
cell.innerHTML += ` ${diff < 0 ? '▲' : '▼'}`;
}
} else {
cell.style.backgroundColor = '#FF3728';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
}
return; // Salimos de la iteración actual
}
// celda 'genre'
if (key === 'genre') {
const correctGenres = mangaToGuess.genre.split(', ');
const guessedGenres = guessedManga.genre.split(', ');
const intersection = guessedGenres.filter(g => correctGenres.includes(g));
if (intersection.length === guessedGenres.length && guessedGenres.length === correctGenres.length) {
cell.style.backgroundColor = '#00FF00';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
} else if (intersection.length > 0) {
cell.style.backgroundColor = 'yellow';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
} else {
cell.style.backgroundColor = '#FF3728';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
}
return; // Salimos de la iteración actual
}
// celda 'serie'
if (key === 'serie') {
if (guessedManga[key].toLowerCase() === mangaToGuess[key].toLowerCase()) {
cell.style.backgroundColor = '#00FF00'; // Verde si coincide
} else {
cell.style.backgroundColor = '#FF3728'; // Rojo si no coincide
}
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
return; // Salimos de la iteración actual
}
// resto de celdas
if (guessedManga[key] === mangaToGuess[key]) {
cell.style.backgroundColor = '#00FF00';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
// Add the category to the list of correct categories if it's not already there
if (!correctCategories.includes(key)) {
correctCategories.push(key);
}
} else {
cell.style.backgroundColor = '#FF3728';
cell.style.padding = '5px 3px 5px 3px';
cell.style.border = '1px solid blue';
cell.style.borderRadius = '6px';
}
}
});
if (guess === mangaToGuess.title.toLowerCase()) {
setTimeout(function () {
alert("Felicidades ¡Has adivinado la peli!");
gameIsActive = false;
input.disabled = true;
guessButton.disabled = true;
// Optionally reset the game or proceed to the next round
}, 300);
} else {
lives--;
displayLives();
if (lives <= 0) {
setTimeout(function () {
alert("¡Game over! La peli era " + mangaToGuess.title);
gameIsActive = false;
input.disabled = true;
guessButton.disabled = true;
}, 300);
}
}
input.value = "";
}
window.onload = function () {
// Esto ahora está vacío porque se llama a initGame después de cargar los datos.
};
resetGame();
// texto explicación desplazamiento
const indicadoresDezplaza = document.querySelector('.move-r-l');
const closex1 = document.querySelector('.close-move');
closex1.addEventListener('click',
() => { indicadoresDezplaza.style.display = 'none' });
// indicadores significado de colores y flechas
const indicadoresColor = document.getElementById('indicadores');
const closex2 = document.querySelector('.close-colores');
closex2.addEventListener('click',
() => { indicadoresColor.style.display = 'none' });