Subir archivos a "js"
This commit is contained in:
+72
-30
@@ -1,38 +1,80 @@
|
||||
$(document).ready(function () {
|
||||
const idPartida = $("body").data("id-partida");
|
||||
|
||||
comprobarEstadoPartida(idPartida);
|
||||
// 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");
|
||||
|
||||
const intervalo = setInterval(function () {
|
||||
comprobarEstadoPartida(idPartida, intervalo);
|
||||
}, 3000);
|
||||
comprobarEstadoPartida(idPartida);
|
||||
|
||||
const intervalo = setInterval(function () {
|
||||
comprobarEstadoPartida(idPartida, intervalo);
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
function comprobarEstadoPartida(idPartida, intervalo = null) {
|
||||
$.ajax({
|
||||
url: "controladores/controladorResultados.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: {
|
||||
orden: "comprobarEstado",
|
||||
idPartida: idPartida
|
||||
},
|
||||
success: function (respuesta) {
|
||||
if (respuesta.error) {
|
||||
console.error("Error:", respuesta.error);
|
||||
return;
|
||||
}
|
||||
if (!idPartida || idPartida <= 0) return;
|
||||
|
||||
if (respuesta.terminada) {
|
||||
if (intervalo !== null) clearInterval(intervalo);
|
||||
|
||||
$("#pantalla-espera").fadeOut(400, function() {
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
console.log("Error de comunicación con el controlador.");
|
||||
$.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;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user