// =====================================================
// VARIABLES
// =====================================================
let intervaloBuscadorSalas = null;
let cargandoSalas = false;
// =====================================================
// INICIAR ACTUALIZACIÓN AUTOMÁTICA
// =====================================================
function iniciarActualizacionSalas() {
// Evitar crear varios intervalos
if (intervaloBuscadorSalas) {
clearInterval(intervaloBuscadorSalas);
}
// Primera carga
cargarSalas();
// Actualizar cada 5 segundos
intervaloBuscadorSalas = setInterval(function () {
// Si el usuario está dentro de una sala,
// no necesitamos actualizar el buscador.
if (!usuarioYaEstaEnEquipo) {
cargarSalas();
}
}, 5000);
}
// =====================================================
// CARGAR LISTA DE SALAS
// =====================================================
function cargarSalas() {
// Evitar peticiones simultáneas
if (cargandoSalas) {
return;
}
cargandoSalas = true;
$.ajax({
url: "controladores/controladorPartida.php",
type: "POST",
dataType: "json",
data: {
orden: "listar",
},
success: function (partidas) {
const contenedor = $("#contenedorSalas");
// =================================================
// NO HAY SALAS
// =================================================
if (
!Array.isArray(partidas) ||
partidas.length === 0
) {
// Ocultamos/eliminamos únicamente las salas
$(".sala-item").remove();
if ($("#mensajeSinSalas").length === 0) {
contenedor.append(`
No hay salas disponibles actualmente.
`);
}
return;
}
// =================================================
// HAY SALAS
// =================================================
// Eliminar mensaje de "no hay salas"
$("#mensajeSinSalas").remove();
// =================================================
// OBTENER IDS DE LAS SALAS ACTUALES
// =================================================
const idsPartidas = partidas.map(function (partida) {
return String(partida.idPartida);
});
// =================================================
// ELIMINAR SALAS QUE YA NO EXISTEN
// =================================================
$(".sala-item").each(function () {
const idPartida = String(
$(this).data("id-partida")
);
if (!idsPartidas.includes(idPartida)) {
$(this).remove();
}
});
// =================================================
// ACTUALIZAR / CREAR CADA SALA
// =================================================
partidas.forEach(function (partida) {
cargarSalaBuscador(partida);
});
},
error: function (xhr, status, error) {
console.error(
"Error al obtener las partidas:",
error
);
},
complete: function () {
cargandoSalas = false;
},
});
}
// =====================================================
// CARGAR INFORMACIÓN DE UNA SALA
// =====================================================
function cargarSalaBuscador(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) {
actualizarSalaBuscador(
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
);
},
});
}
// =====================================================
// ACTUALIZAR UNA SALA
// =====================================================
function actualizarSalaBuscador(
partida,
equipos,
jugadores
) {
const selectorSala =
`.sala-item[data-id-partida="${partida.idPartida}"]`;
const salaExistente = $(selectorSala);
// ===================================================
// SI LA SALA NO EXISTE → CREARLA
// ===================================================
if (salaExistente.length === 0) {
crearSalaBuscador(
partida,
equipos,
jugadores
);
return;
}
// ===================================================
// LA SALA YA EXISTE
// ===================================================
//
// NO TOCAMOS LA TARJETA COMPLETA.
//
// Solo actualizamos .equipos
//
const equiposHTML =
generarEquiposHTML(
partida,
equipos,
jugadores
);
salaExistente
.find(".equipos")
.html(equiposHTML);
}
// =====================================================
// CREAR UNA SALA NUEVA
// =====================================================
function crearSalaBuscador(
partida,
equipos,
jugadores
) {
const equiposHTML =
generarEquiposHTML(
partida,
equipos,
jugadores
);
const salaHTML = `
${
esHost
? ''
: ''
}
${escapeHTML(
jugador.nombreUsuario
)}
${
esHost
? `
HOST
`
: ""
}
`;
}
);
} else {
jugadoresHTML = `