81 lines
2.2 KiB
JavaScript
81 lines
2.2 KiB
JavaScript
$(document).ready(function () {
|
|
// Captura segura del ID de la partida desde el body de la página
|
|
const idPartida =
|
|
$("body").attr("data-id-partida") || $("body").data("id-partida");
|
|
|
|
comprobarEstadoPartida(idPartida);
|
|
|
|
const intervalo = setInterval(function () {
|
|
comprobarEstadoPartida(idPartida, intervalo);
|
|
}, 3000);
|
|
});
|
|
|
|
function comprobarEstadoPartida(idPartida, intervalo = null) {
|
|
if (!idPartida || idPartida <= 0) return;
|
|
|
|
$.ajax({
|
|
url: "controladores/controladorResultados.php",
|
|
type: "POST",
|
|
dataType: "json",
|
|
data: {
|
|
orden: "comprobarEstado",
|
|
idPartida: idPartida,
|
|
},
|
|
success: function (respuesta) {
|
|
if (respuesta.error) {
|
|
console.error("Error del controlador:", respuesta.error);
|
|
return;
|
|
}
|
|
|
|
if (respuesta.terminada === true || respuesta.terminada === "true") {
|
|
if (intervalo !== null) {
|
|
clearInterval(intervalo);
|
|
}
|
|
|
|
$("#pantalla-espera").hide();
|
|
$("#pantalla-resultados").show();
|
|
}
|
|
},
|
|
error: function () {
|
|
console.log("Esperando conexión con el servidor del Trivial...");
|
|
},
|
|
});
|
|
}
|
|
|
|
// INTERCEPTOR DE SALIDA: Saca al usuario y destruye la sala en cascada
|
|
$(document).on("click", "#btnVolverInicio", function (e) {
|
|
e.preventDefault(); // Congelamos un instante la redirección
|
|
|
|
const urlDestino = $(this).attr("href");
|
|
// Leemos el ID de forma científica directamente desde el botón pulsado
|
|
const idPartida = $(this).attr("data-id");
|
|
|
|
if (!idPartida) {
|
|
window.location.href = urlDestino;
|
|
return;
|
|
}
|
|
|
|
$.ajax({
|
|
url: "controladores/controladorResultados.php",
|
|
type: "POST",
|
|
dataType: "json",
|
|
data: {
|
|
orden: "registrarSalidaUsuario",
|
|
idPartida: idPartida,
|
|
},
|
|
success: function (response) {
|
|
console.log(
|
|
"Servidor procesó la salida. Usuarios restantes: " +
|
|
response.usuarios_quedan,
|
|
);
|
|
},
|
|
error: function (error) {
|
|
console.error("Error técnico al salir de la sala:", error);
|
|
},
|
|
complete: function () {
|
|
// Rompemos la pausa y mandamos al jugador al menú de inicio limpio
|
|
window.location.href = urlDestino;
|
|
},
|
|
});
|
|
});
|