From fdd1ba8272617c1a7d519989686b8fe49761f8dc Mon Sep 17 00:00:00 2001 From: alexis Date: Thu, 1 Oct 2026 13:18:11 +0200 Subject: [PATCH] Add actualizacion de jugadores dentro de sala cada 5 segundos --- js/buscadorSala.js | 205 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 193 insertions(+), 12 deletions(-) diff --git a/js/buscadorSala.js b/js/buscadorSala.js index e6a97d9..4ec3803 100644 --- a/js/buscadorSala.js +++ b/js/buscadorSala.js @@ -33,22 +33,131 @@ function comprobarSalaUsuario() { } // 2. Cargar únicamente la sala en la que está metido el usuario +let intervaloSala = null; + function cargarSalaUsuario(idPartida) { + // Primera carga completa + cargarSalaCompleta(idPartida); + + // Evitamos crear varios intervalos + if (intervaloSala) { + clearInterval(intervaloSala); + } + + intervaloSala = setInterval(() => { + actualizarSalaUsuario(idPartida); + }, 5000); +} + +function cargarSalaCompleta(idPartida) { $.ajax({ url: "controladores/controladorPartida.php", type: "POST", dataType: "json", - data: { orden: "buscar", idPartida: idPartida }, + data: { + orden: "buscar", + idPartida: idPartida, + }, + success: function (partida) { if (partida.error) { console.error(partida.error); return; } - $("#contenedorSalas").empty(); + + cargarInformacionSalaCompleta(partida); + }, + + error: function (xhr, status, error) { + console.error("Error al obtener la sala:", error); + }, + }); +} + +function cargarInformacionSalaCompleta(partida) { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + + data: { + orden: "getEquipos", + idPartida: partida.idPartida, + }, + + success: function (equipos) { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + + data: { + orden: "getJugadores", + idPartida: partida.idPartida, + }, + + success: function (jugadores) { + // AQUÍ hacemos la primera construcción completa + mostrarSala(partida, equipos, jugadores); + }, + + error: function (xhr, status, error) { + console.error("Error al obtener jugadores:", error); + }, + }); + }, + + error: function (xhr, status, error) { + console.error("Error al obtener equipos:", error); + }, + }); +} + +function actualizarSalaUsuario(idPartida) { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + + data: { + orden: "buscar", + idPartida: idPartida, + }, + + success: function (partida) { + if (partida.error) { + console.error(partida.error); + return; + } + cargarInformacionSala(partida); }, + error: function (xhr, status, error) { - console.error("Error al obtener la sala del usuario:", error); + console.error("Error al actualizar la sala:", error); + }, + }); +} + +function actualizarSalaUsuario(idPartida) { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + data: { + orden: "buscar", + idPartida: idPartida, + }, + success: function (partida) { + if (partida.error) { + console.error(partida.error); + return; + } + + cargarInformacionSala(partida, true); + }, + error: function (xhr, status, error) { + console.error("Error al obtener la sala:", error); }, }); } @@ -74,7 +183,7 @@ function cargarSalas() { } partidas.forEach(function (partida) { - cargarInformacionSala(partida); + cargarInformacionSalaCompleta(partida); }); }, error: function (xhr, status, error) { @@ -89,15 +198,21 @@ function cargarInformacionSala(partida) { url: "controladores/controladorPartida.php", type: "POST", dataType: "json", - data: { orden: "getEquipos", idPartida: partida.idPartida }, + data: { + orden: "getEquipos", + idPartida: partida.idPartida, + }, success: function (equipos) { $.ajax({ url: "controladores/controladorPartida.php", type: "POST", dataType: "json", - data: { orden: "getJugadores", idPartida: partida.idPartida }, + data: { + orden: "getJugadores", + idPartida: partida.idPartida, + }, success: function (jugadores) { - mostrarSala(partida, equipos, jugadores); + mostrarEquipos(partida, equipos, jugadores); }, error: function (xhr, status, error) { console.error("Error al obtener jugadores:", error); @@ -266,7 +381,8 @@ let botonAbandonarReferencia = null; $(document).on("click", ".btn-abandonar-sala", function () { botonAbandonarReferencia = $(this); - const esHost = $(this).data("es-host") === true || $(this).data("es-host") === "true"; + const esHost = + $(this).data("es-host") === true || $(this).data("es-host") === "true"; const titulo = $("#modalAbandonarSalaLabel"); const mensaje = $("#modalAbandonarSalaMensaje"); const botonConfirmar = $("#btnConfirmarAbandonar"); @@ -279,9 +395,7 @@ $(document).on("click", ".btn-abandonar-sala", function () { botonConfirmar.text("Sí, terminar partida"); } else { titulo.text("¿Abandonar la sala?"); - mensaje.text( - "Si sales de la sala, perderás tu lugar en el equipo.", - ); + mensaje.text("Si sales de la sala, perderás tu lugar en el equipo."); botonConfirmar.text("Sí, salir"); } @@ -292,7 +406,6 @@ $(document).on("click", ".btn-abandonar-sala", function () { modal.show(); }); - // B. Al hacer clic en "Sí, salir" dentro del modal Kahoot $(document).on("click", "#btnConfirmarAbandonar", function () { const modalEl = document.getElementById("modalAbandonarSala"); @@ -433,3 +546,71 @@ $(document).on("input", ".input-kahoot", function () { $(this).removeClass("is-invalid"); $(this).next(".mensaje-error-sala").remove(); }); + +function mostrarEquipos(partida, equipos, jugadores) { + let equiposHTML = ""; + + equipos.forEach(function (equipo) { + const jugadoresEquipo = jugadores.filter(function (jugador) { + return jugador.idEquipo == equipo.idEquipo; + }); + + let jugadoresHTML = ""; + + if (jugadoresEquipo.length > 0) { + jugadoresEquipo.forEach(function (jugador) { + const esHost = jugador.idUsuario == partida.idUsuario; + + jugadoresHTML += ` +
+ +
+ + ${ + esHost + ? '' + : '' + } + + + ${escapeHTML(jugador.nombreUsuario)} +
+ + ${ + esHost + ? `HOST` + : "" + } + +
+ `; + }); + } else { + jugadoresHTML = ` +
+ Sin jugadores +
+ `; + } + + equiposHTML += ` +
+ +
+ ${escapeHTML(equipo.nombreEquipo)} +
+ +
+ ${jugadoresHTML} +
+ +
+ `; + }); + + // IMPORTANTE: + // Solo modificamos .equipos + $("#contenedorSalas .equipos").html(equiposHTML); +}