// =====================================================
// CARGAR LISTA DE SALAS
// =====================================================
function cargarSalas() {
$.ajax({
url: "controladores/controladorPartida.php",
type: "POST",
dataType: "json",
data: {
orden: "listar",
},
success: function (partidas) {
const contenedor = $("#contenedorSalas");
contenedor.empty();
if (
!Array.isArray(partidas) ||
partidas.length === 0
) {
contenedor.html(`
No hay salas disponibles actualmente.
`);
return;
}
// Cargar cada sala individualmente
partidas.forEach(function (partida) {
cargarSalaBuscador(partida);
});
},
error: function (xhr, status, error) {
console.error(
"Error al obtener las partidas:",
error
);
},
});
}
// =====================================================
// CARGAR INFORMACIÓN DE UNA SALA DEL BUSCADOR
// =====================================================
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) {
mostrarSalaBuscador(
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
);
},
});
}
// =====================================================
// MOSTRAR UNA SALA EN EL BUSCADOR
// =====================================================
function mostrarSalaBuscador(
partida,
equipos,
jugadores
) {
let equiposHTML = "";
equipos.forEach(function (equipo) {
const jugadoresEquipo = jugadores.filter(
function (jugador) {
return (
jugador.idEquipo ==
equipo.idEquipo
);
}
);
let jugadoresHTML = "";
// -----------------------------------------------
// JUGADORES DEL EQUIPO
// -----------------------------------------------
if (jugadoresEquipo.length > 0) {
jugadoresEquipo.forEach(
function (jugador) {
const esHost =
jugador.idUsuario ==
partida.idUsuario;
jugadoresHTML += `
${
esHost
? ''
: ''
}
${escapeHTML(
jugador.nombreUsuario
)}
${
esHost
? `
HOST
`
: ""
}
`;
}
);
} else {
jugadoresHTML = `