diff --git a/js/buscadorSala.js b/js/buscadorSala.js index daa9c84..953587a 100644 --- a/js/buscadorSala.js +++ b/js/buscadorSala.js @@ -1,128 +1,104 @@ let usuarioYaEstaEnEquipo = false; $(document).ready(function () { -comprobarSalaUsuario(); + comprobarSalaUsuario(); }); function comprobarSalaUsuario() { + $("#cargandoSalas").remove(); -$("#cargandoSalas").remove(); - -$.ajax({ + $.ajax({ url: "controladores/ControladorEquipoxUsuario.php", type: "POST", dataType: "json", data: { - orden: "obtenerSalaUsuario" + orden: "obtenerSalaUsuario", }, success: function (respuesta) { + console.log("Sala del usuario:", respuesta); - console.log("Sala del usuario:", respuesta); - - if (respuesta.error) { - - console.error(respuesta.error); - - usuarioYaEstaEnEquipo = false; - - cargarSalas(); - - return; - } - - if (respuesta.asociado) { - - usuarioYaEstaEnEquipo = true; - - $("#contenedorCrearSala").hide(); - - cargarSalaUsuario(respuesta.idPartida); - - } else { - - usuarioYaEstaEnEquipo = false; - - cargarSalas(); - } - }, - - error: function (xhr, status, error) { - - console.error( - "Error al comprobar la sala del usuario:", - error - ); + if (respuesta.error) { + console.error(respuesta.error); usuarioYaEstaEnEquipo = false; cargarSalas(); - } -}); + return; + } -} + if (respuesta.asociado) { + usuarioYaEstaEnEquipo = true; -function cargarSalaUsuario(idPartida) { + $("#contenedorCrearSala").hide(); -$.ajax({ - url: "controladores/controladorPartida.php", - type: "POST", - dataType: "json", + cargarSalaUsuario(respuesta.idPartida); + } else { + usuarioYaEstaEnEquipo = false; - data: { - orden: "buscar", - idPartida: idPartida - }, - - success: function (partida) { - - if (partida.error) { - - console.error(partida.error); - - return; - } - - $("#contenedorSalas").empty(); - - cargarInformacionSala(partida); + cargarSalas(); + } }, error: function (xhr, status, error) { + console.error("Error al comprobar la sala del usuario:", error); - console.error( - "Error al obtener la sala del usuario:", - error - ); - } -}); - + usuarioYaEstaEnEquipo = false; + cargarSalas(); + }, + }); } -function cargarSalas() { - -$.ajax({ +function cargarSalaUsuario(idPartida) { + $.ajax({ url: "controladores/controladorPartida.php", type: "POST", dataType: "json", data: { - orden: "listar" + orden: "buscar", + idPartida: idPartida, + }, + + success: function (partida) { + if (partida.error) { + console.error(partida.error); + + return; + } + + $("#contenedorSalas").empty(); + + cargarInformacionSala(partida); + }, + + error: function (xhr, status, error) { + console.error("Error al obtener la sala del usuario:", error); + }, + }); +} + +function cargarSalas() { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", + + data: { + orden: "listar", }, success: function (partidas) { + const contenedor = $("#contenedorSalas"); - const contenedor = $("#contenedorSalas"); + contenedor.empty(); + contenedor.empty(); - contenedor.empty(); - contenedor.empty(); - - if (!Array.isArray(partidas) || partidas.length === 0) { - - contenedor.html(` + if (!Array.isArray(partidas) || partidas.length === 0) { + contenedor.html(`

No hay salas disponibles. @@ -130,104 +106,73 @@ $.ajax({

`); - return; - } + return; + } - partidas.forEach(function (partida) { - - cargarInformacionSala(partida); - - }); + partidas.forEach(function (partida) { + cargarInformacionSala(partida); + }); }, error: function (xhr, status, error) { - - console.error( - "Error al obtener las partidas:", - error - ); - } -}); - - + console.error("Error al obtener las partidas:", error); + }, + }); } function cargarInformacionSala(partida) { - -$.ajax({ + $.ajax({ url: "controladores/controladorPartida.php", type: "POST", dataType: "json", data: { - orden: "getEquipos", - idPartida: partida.idPartida + orden: "getEquipos", + idPartida: partida.idPartida, }, success: function (equipos) { + $.ajax({ + url: "controladores/controladorPartida.php", + type: "POST", + dataType: "json", - $.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); + }, - success: function (jugadores) { - - 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 jugadores:", error); + }, + }); }, error: function (xhr, status, error) { - - console.error( - "Error al obtener equipos:", - error - ); - } -}); - - + console.error("Error al obtener equipos:", error); + }, + }); } function mostrarSala(partida, equipos, jugadores) { + const contenedor = $("#contenedorSalas"); -const contenedor = $("#contenedorSalas"); - -let equiposHTML = ""; - -equipos.forEach(function (equipo) { + let equiposHTML = ""; + equipos.forEach(function (equipo) { const jugadoresEquipo = jugadores.filter(function (jugador) { - - return jugador.idEquipo == equipo.idEquipo; - + return jugador.idEquipo == equipo.idEquipo; }); let jugadoresHTML = ""; if (jugadoresEquipo.length > 0) { - - jugadoresEquipo.forEach(function (jugador) { - - jugadoresHTML += ` + jugadoresEquipo.forEach(function (jugador) { + jugadoresHTML += `
@@ -238,23 +183,19 @@ equipos.forEach(function (equipo) {
`; - }); - + }); } else { - - jugadoresHTML = ` + jugadoresHTML = `
Sin jugadores
`; } - let botonUnirseHTML = ""; if (!usuarioYaEstaEnEquipo) { - - botonUnirseHTML = ` + botonUnirseHTML = `