Subir archivos a "js"
This commit is contained in:
+70
-28
@@ -1,38 +1,80 @@
|
|||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
const idPartida = $("body").data("id-partida");
|
// 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);
|
comprobarEstadoPartida(idPartida);
|
||||||
|
|
||||||
const intervalo = setInterval(function () {
|
const intervalo = setInterval(function () {
|
||||||
comprobarEstadoPartida(idPartida, intervalo);
|
comprobarEstadoPartida(idPartida, intervalo);
|
||||||
}, 3000);
|
}, 3000);
|
||||||
});
|
});
|
||||||
|
|
||||||
function comprobarEstadoPartida(idPartida, intervalo = null) {
|
function comprobarEstadoPartida(idPartida, intervalo = null) {
|
||||||
$.ajax({
|
if (!idPartida || idPartida <= 0) return;
|
||||||
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 (respuesta.terminada) {
|
$.ajax({
|
||||||
if (intervalo !== null) clearInterval(intervalo);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
$("#pantalla-espera").fadeOut(400, function() {
|
if (respuesta.terminada === true || respuesta.terminada === "true") {
|
||||||
location.reload();
|
if (intervalo !== null) {
|
||||||
});
|
clearInterval(intervalo);
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function () {
|
|
||||||
console.log("Error de comunicación con el controlador.");
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
$("#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