Dividido buscadorSala.js en dos archivos para facilitar la modificacion
This commit is contained in:
+4
-1
@@ -22,7 +22,7 @@
|
|||||||
<div class="mb-4">
|
<div class="mb-4">
|
||||||
<a href="index.php" class="btn btn-outline-secondary d-inline-flex align-items-center gap-2">
|
<a href="index.php" class="btn btn-outline-secondary d-inline-flex align-items-center gap-2">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
|
||||||
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8"/>
|
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8" />
|
||||||
</svg>
|
</svg>
|
||||||
Volver al Inicio
|
Volver al Inicio
|
||||||
</a>
|
</a>
|
||||||
@@ -100,7 +100,10 @@
|
|||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
<script src="js/jquery-4.0.0.min.js"></script>
|
<script src="js/jquery-4.0.0.min.js"></script>
|
||||||
|
<script src="js/sala.js"></script>
|
||||||
<script src="js/buscadorSala.js"></script>
|
<script src="js/buscadorSala.js"></script>
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
+380
-527
@@ -1,592 +1,171 @@
|
|||||||
let usuarioYaEstaEnEquipo = false;
|
// =====================================================
|
||||||
|
// CARGAR LISTA DE SALAS
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
$(document).ready(function () {
|
|
||||||
comprobarSalaUsuario();
|
|
||||||
});
|
|
||||||
|
|
||||||
// 1. Comprobar si el usuario actual ya pertenece a una sala
|
|
||||||
function comprobarSalaUsuario() {
|
|
||||||
$("#cargandoSalas").remove();
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: "controladores/controladorEquipoxUsuario.php",
|
|
||||||
type: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
data: { orden: "obtenerSalaUsuario" },
|
|
||||||
success: function (respuesta) {
|
|
||||||
if (respuesta.error || !respuesta.asociado) {
|
|
||||||
usuarioYaEstaEnEquipo = false;
|
|
||||||
$("#contenedorCrearSala").show();
|
|
||||||
cargarSalas();
|
|
||||||
} else {
|
|
||||||
usuarioYaEstaEnEquipo = true;
|
|
||||||
$("#contenedorCrearSala").hide(); // Ocultar "Crear Sala" si ya pertenece a una
|
|
||||||
cargarSalaUsuario(respuesta.idPartida);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function (xhr, status, error) {
|
|
||||||
console.error("Error al comprobar la sala del usuario:", error);
|
|
||||||
usuarioYaEstaEnEquipo = false;
|
|
||||||
cargarSalas();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
},
|
|
||||||
|
|
||||||
success: function (partida) {
|
|
||||||
if (partida.error) {
|
|
||||||
console.error(partida.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Cargar la lista completa de todas las salas
|
|
||||||
function cargarSalas() {
|
function cargarSalas() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "controladores/controladorPartida.php",
|
url: "controladores/controladorPartida.php",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
data: { orden: "listar" },
|
|
||||||
|
data: {
|
||||||
|
orden: "listar",
|
||||||
|
},
|
||||||
|
|
||||||
success: function (partidas) {
|
success: function (partidas) {
|
||||||
const contenedor = $("#contenedorSalas");
|
const contenedor = $("#contenedorSalas");
|
||||||
|
|
||||||
contenedor.empty();
|
contenedor.empty();
|
||||||
|
|
||||||
if (!Array.isArray(partidas) || partidas.length === 0) {
|
if (
|
||||||
|
!Array.isArray(partidas) ||
|
||||||
|
partidas.length === 0
|
||||||
|
) {
|
||||||
contenedor.html(`
|
contenedor.html(`
|
||||||
<div class="col-12 text-center">
|
<div class="col-12 text-center">
|
||||||
<p class="text-muted">No hay salas disponibles actualmente.</p>
|
<p class="text-muted">
|
||||||
|
No hay salas disponibles actualmente.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
`);
|
`);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cargar cada sala individualmente
|
||||||
partidas.forEach(function (partida) {
|
partidas.forEach(function (partida) {
|
||||||
cargarInformacionSalaCompleta(partida);
|
cargarSalaBuscador(partida);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error("Error al obtener las partidas:", error);
|
console.error(
|
||||||
|
"Error al obtener las partidas:",
|
||||||
|
error
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Obtener equipos y jugadores asociados a una sala concreta
|
|
||||||
function cargarInformacionSala(partida) {
|
// =====================================================
|
||||||
|
// CARGAR INFORMACIÓN DE UNA SALA DEL BUSCADOR
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function cargarSalaBuscador(partida) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "controladores/controladorPartida.php",
|
url: "controladores/controladorPartida.php",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
orden: "getEquipos",
|
orden: "getEquipos",
|
||||||
idPartida: partida.idPartida,
|
idPartida: partida.idPartida,
|
||||||
},
|
},
|
||||||
|
|
||||||
success: function (equipos) {
|
success: function (equipos) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "controladores/controladorPartida.php",
|
url: "controladores/controladorPartida.php",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
orden: "getJugadores",
|
orden: "getJugadores",
|
||||||
idPartida: partida.idPartida,
|
idPartida: partida.idPartida,
|
||||||
},
|
},
|
||||||
|
|
||||||
success: function (jugadores) {
|
success: function (jugadores) {
|
||||||
mostrarEquipos(partida, equipos, jugadores);
|
mostrarSalaBuscador(
|
||||||
|
partida,
|
||||||
|
equipos,
|
||||||
|
jugadores
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error("Error al obtener jugadores:", error);
|
console.error(
|
||||||
|
"Error al obtener jugadores:",
|
||||||
|
error
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error("Error al obtener equipos:", error);
|
console.error(
|
||||||
|
"Error al obtener equipos:",
|
||||||
|
error
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Renderizar el HTML de la sala y botones según estado
|
|
||||||
function mostrarSala(partida, equipos, jugadores) {
|
// =====================================================
|
||||||
const contenedor = $("#contenedorSalas");
|
// MOSTRAR UNA SALA EN EL BUSCADOR
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function mostrarSalaBuscador(
|
||||||
|
partida,
|
||||||
|
equipos,
|
||||||
|
jugadores
|
||||||
|
) {
|
||||||
let equiposHTML = "";
|
let equiposHTML = "";
|
||||||
|
|
||||||
equipos.forEach(function (equipo) {
|
equipos.forEach(function (equipo) {
|
||||||
const jugadoresEquipo = jugadores.filter(function (jugador) {
|
const jugadoresEquipo = jugadores.filter(
|
||||||
return jugador.idEquipo == equipo.idEquipo;
|
function (jugador) {
|
||||||
});
|
return (
|
||||||
|
jugador.idEquipo ==
|
||||||
let jugadoresHTML = "";
|
equipo.idEquipo
|
||||||
if (jugadoresEquipo.length > 0) {
|
);
|
||||||
jugadoresEquipo.forEach(function (jugador) {
|
|
||||||
const esHost = jugador.idUsuario == partida.idUsuario;
|
|
||||||
|
|
||||||
jugadoresHTML += `
|
|
||||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
|
||||||
esHost ? "jugador-host" : ""
|
|
||||||
}">
|
|
||||||
<div>
|
|
||||||
<span class="avatar">${esHost ? '<i class="bi bi-award-fill"></i>' : '<i class="bi bi-person-fill"></i>'}</span>
|
|
||||||
${escapeHTML(jugador.nombreUsuario)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
${
|
|
||||||
esHost
|
|
||||||
? `<span class="badge bg-warning text-dark ms-2">HOST</span>`
|
|
||||||
: ""
|
|
||||||
}
|
}
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
jugadoresHTML = `<div class="text-muted small">Sin jugadores</div>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Botón "Unirse" solo visible si NO pertenece a ningún equipo
|
|
||||||
let botonUnirseHTML = "";
|
|
||||||
if (!usuarioYaEstaEnEquipo) {
|
|
||||||
botonUnirseHTML = `
|
|
||||||
<button type="button" class="btn-unirse w-100 btn-unirse-equipo mt-2"
|
|
||||||
data-id-partida="${partida.idPartida}"
|
|
||||||
data-id-equipo="${equipo.idEquipo}">
|
|
||||||
Unirse
|
|
||||||
</button>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
equiposHTML += `
|
|
||||||
<div class="equipo me-2 ms-2 p-3 border rounded">
|
|
||||||
<h6>${escapeHTML(equipo.nombreEquipo)}</h6>
|
|
||||||
<div class="jugadores mb-2">
|
|
||||||
${jugadoresHTML}
|
|
||||||
</div>
|
|
||||||
${botonUnirseHTML}
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
|
||||||
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
|
||||||
let accionesSalaHTML = "";
|
|
||||||
|
|
||||||
if (usuarioYaEstaEnEquipo) {
|
|
||||||
const esHost = partida.idUsuario == idUsuarioSesion;
|
|
||||||
let botonIniciarHTML = "";
|
|
||||||
|
|
||||||
// Solo el host puede iniciar la partida
|
|
||||||
if (esHost) {
|
|
||||||
botonIniciarHTML = `
|
|
||||||
<a href="juego.php?idPartida=${partida.idPartida}"
|
|
||||||
class="btn btn-success btn-lg px-4">
|
|
||||||
<i class="bi bi-controller"></i> ¡Iniciar Partida!
|
|
||||||
</a>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
accionesSalaHTML = `
|
|
||||||
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
|
|
||||||
|
|
||||||
<button type="button"
|
|
||||||
class="btn btn-outline-danger btn-abandonar-sala"
|
|
||||||
data-id-partida="${partida.idPartida}"
|
|
||||||
data-es-host="${esHost}">
|
|
||||||
Abandonar sala
|
|
||||||
</button>
|
|
||||||
|
|
||||||
${botonIniciarHTML}
|
|
||||||
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const salaHTML = `
|
|
||||||
<div class="col-12 col-md-10 col-lg-8">
|
|
||||||
<div class="sala card p-3 shadow-sm mb-4">
|
|
||||||
<div class="sala-header h4 text-center mb-3 text-white">
|
|
||||||
${escapeHTML(partida.nombrePartida)}
|
|
||||||
</div>
|
|
||||||
<div class="sala-body">
|
|
||||||
<div class="equipos d-flex justify-content-around">
|
|
||||||
${equiposHTML}
|
|
||||||
</div>
|
|
||||||
${accionesSalaHTML}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
|
|
||||||
contenedor.append(salaHTML);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. Evento: Unirse a un Equipo
|
|
||||||
$(document).on("click", ".btn-unirse-equipo", function () {
|
|
||||||
const boton = $(this);
|
|
||||||
const idEquipo = boton.data("id-equipo");
|
|
||||||
const idPartida = boton.data("id-partida");
|
|
||||||
|
|
||||||
boton.prop("disabled", true).text("Uniéndose...");
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: "controladores/controladorEquipoxUsuario.php",
|
|
||||||
type: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
data: {
|
|
||||||
orden: "registrarUsuarioEnEquipo",
|
|
||||||
idEquipo: idEquipo,
|
|
||||||
},
|
|
||||||
success: function (respuesta) {
|
|
||||||
if (respuesta.error) {
|
|
||||||
alert(respuesta.error);
|
|
||||||
boton.prop("disabled", false).text("Unirse");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
usuarioYaEstaEnEquipo = true;
|
|
||||||
$("#contenedorCrearSala").hide();
|
|
||||||
$("#contenedorSalas").empty();
|
|
||||||
cargarSalaUsuario(idPartida);
|
|
||||||
},
|
|
||||||
error: function (xhr, status, error) {
|
|
||||||
console.error("Error al unirse al equipo:", error);
|
|
||||||
alert("No se ha podido unir al equipo.");
|
|
||||||
boton.prop("disabled", false).text("Unirse");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// 7. Evento: Abandonar Sala
|
|
||||||
// Guardamos temporalmente el botón presionado
|
|
||||||
let botonAbandonarReferencia = null;
|
|
||||||
|
|
||||||
// A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal
|
|
||||||
$(document).on("click", ".btn-abandonar-sala", function () {
|
|
||||||
botonAbandonarReferencia = $(this);
|
|
||||||
|
|
||||||
const esHost =
|
|
||||||
$(this).data("es-host") === true || $(this).data("es-host") === "true";
|
|
||||||
const titulo = $("#modalAbandonarSalaLabel");
|
|
||||||
const mensaje = $("#modalAbandonarSalaMensaje");
|
|
||||||
const botonConfirmar = $("#btnConfirmarAbandonar");
|
|
||||||
|
|
||||||
if (esHost) {
|
|
||||||
titulo.text("¿Abandonar la sala?");
|
|
||||||
mensaje.text(
|
|
||||||
"Si abandonas la sala como anfitrión, la partida terminará para todos los jugadores.",
|
|
||||||
);
|
);
|
||||||
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.");
|
|
||||||
botonConfirmar.text("Sí, salir");
|
|
||||||
}
|
|
||||||
|
|
||||||
const modal = new bootstrap.Modal(
|
|
||||||
document.getElementById("modalAbandonarSala"),
|
|
||||||
);
|
|
||||||
|
|
||||||
modal.show();
|
|
||||||
});
|
|
||||||
|
|
||||||
// B. Al hacer clic en "Sí, salir" dentro del modal Kahoot
|
|
||||||
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
|
||||||
const modalEl = document.getElementById("modalAbandonarSala");
|
|
||||||
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
|
||||||
|
|
||||||
const btnConfirmar = $(this);
|
|
||||||
btnConfirmar.prop("disabled", true).text("Saliendo...");
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: "controladores/controladorEquipoxUsuario.php",
|
|
||||||
type: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
data: { orden: "salirDeSala" },
|
|
||||||
success: function (respuesta) {
|
|
||||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
|
||||||
|
|
||||||
if (modalInstance) {
|
|
||||||
modalInstance.hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (respuesta.error) {
|
|
||||||
alert(respuesta.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restablecemos la interfaz a la lista completa de salas
|
|
||||||
usuarioYaEstaEnEquipo = false;
|
|
||||||
$("#contenedorCrearSala").show();
|
|
||||||
cargarSalas();
|
|
||||||
},
|
|
||||||
error: function (xhr, status, error) {
|
|
||||||
console.error("Error al salir de la sala:", error);
|
|
||||||
alert("No se pudo salir de la sala.");
|
|
||||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
|
||||||
if (modalInstance) {
|
|
||||||
modalInstance.hide();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
// 8. Evento: Crear Sala
|
|
||||||
$("#btnCrearSala").on("click", function () {
|
|
||||||
const nombreSala = $("#nombreSala").val().trim();
|
|
||||||
const nombreEquipo1 = $("#nombreEquipo1").val().trim();
|
|
||||||
const nombreEquipo2 = $("#nombreEquipo2").val().trim();
|
|
||||||
|
|
||||||
let valido = true;
|
|
||||||
$(".mensaje-error-sala").remove();
|
|
||||||
$(".input-kahoot").removeClass("is-invalid");
|
|
||||||
|
|
||||||
if (nombreSala === "" || nombreSala.length < 3) {
|
|
||||||
mostrarError(
|
|
||||||
"#nombreSala",
|
|
||||||
"El nombre de la sala debe tener al menos 3 caracteres.",
|
|
||||||
);
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nombreEquipo1 === "" || nombreEquipo1.length < 2) {
|
|
||||||
mostrarError(
|
|
||||||
"#nombreEquipo1",
|
|
||||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
|
||||||
);
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nombreEquipo2 === "" || nombreEquipo2.length < 2) {
|
|
||||||
mostrarError(
|
|
||||||
"#nombreEquipo2",
|
|
||||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
|
||||||
);
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
nombreEquipo1 !== "" &&
|
|
||||||
nombreEquipo2 !== "" &&
|
|
||||||
nombreEquipo1.toLowerCase() === nombreEquipo2.toLowerCase()
|
|
||||||
) {
|
|
||||||
mostrarError(
|
|
||||||
"#nombreEquipo2",
|
|
||||||
"Los dos equipos deben tener nombres diferentes.",
|
|
||||||
);
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!valido) return;
|
|
||||||
|
|
||||||
const boton = $("#btnCrearSala");
|
|
||||||
boton.prop("disabled", true).text("Creando sala...");
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: "controladores/controladorPartida.php",
|
|
||||||
type: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
data: {
|
|
||||||
orden: "crear",
|
|
||||||
nombrePartida: nombreSala,
|
|
||||||
nombreEquipo1: nombreEquipo1,
|
|
||||||
nombreEquipo2: nombreEquipo2,
|
|
||||||
},
|
|
||||||
success: function (respuesta) {
|
|
||||||
if (respuesta.error) {
|
|
||||||
alert(respuesta.error);
|
|
||||||
boton.prop("disabled", false).text("Crear sala");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (respuesta.exito) {
|
|
||||||
$("#modalCrearSala").modal("hide");
|
|
||||||
location.reload();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function (xhr, status, error) {
|
|
||||||
console.error("Error al crear la sala:", error);
|
|
||||||
alert("No se ha podido crear la sala.");
|
|
||||||
boton.prop("disabled", false).text("Crear sala");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Auxiliares
|
|
||||||
function escapeHTML(texto) {
|
|
||||||
return $("<div>")
|
|
||||||
.text(texto ?? "")
|
|
||||||
.html();
|
|
||||||
}
|
|
||||||
|
|
||||||
function mostrarError(selector, mensaje) {
|
|
||||||
const input = $(selector);
|
|
||||||
input.addClass("is-invalid");
|
|
||||||
input.after(
|
|
||||||
`<div class="mensaje-error-sala text-danger mt-1 small">${mensaje}</div>`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$(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 = "";
|
let jugadoresHTML = "";
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// JUGADORES DEL EQUIPO
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
if (jugadoresEquipo.length > 0) {
|
if (jugadoresEquipo.length > 0) {
|
||||||
jugadoresEquipo.forEach(function (jugador) {
|
jugadoresEquipo.forEach(
|
||||||
const esHost = jugador.idUsuario == partida.idUsuario;
|
function (jugador) {
|
||||||
|
const esHost =
|
||||||
|
jugador.idUsuario ==
|
||||||
|
partida.idUsuario;
|
||||||
|
|
||||||
jugadoresHTML += `
|
jugadoresHTML += `
|
||||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
<div class="jugador border border-2 d-flex align-items-center justify-content-between">
|
||||||
esHost ? "jugador-host" : ""
|
|
||||||
}">
|
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<span class="avatar">
|
<span class="avatar">
|
||||||
${
|
${
|
||||||
esHost
|
esHost
|
||||||
? '<i class="bi bi-award-fill"></i>'
|
? '<i class="bi bi-award-fill"></i>'
|
||||||
: '<i class="bi bi-person-fill"></i>'
|
: '<i class="bi bi-person-fill"></i>'
|
||||||
}
|
}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
${escapeHTML(
|
||||||
|
jugador.nombreUsuario
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? `
|
||||||
|
<span class="badge bg-warning text-dark ms-2">
|
||||||
|
HOST
|
||||||
|
</span>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
|
||||||
${escapeHTML(jugador.nombreUsuario)}
|
|
||||||
</div>
|
</div>
|
||||||
|
`;
|
||||||
${
|
}
|
||||||
esHost
|
);
|
||||||
? `<span class="badge bg-warning text-dark ms-2">HOST</span>`
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
jugadoresHTML = `
|
jugadoresHTML = `
|
||||||
<div class="text-muted small">
|
<div class="text-muted small">
|
||||||
@@ -595,22 +174,296 @@ function mostrarEquipos(partida, equipos, jugadores) {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// BOTÓN UNIRSE
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
let botonUnirseHTML = "";
|
||||||
|
|
||||||
|
if (!usuarioYaEstaEnEquipo) {
|
||||||
|
botonUnirseHTML = `
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn-unirse w-100 btn-unirse-equipo mt-2"
|
||||||
|
data-id-partida="${partida.idPartida}"
|
||||||
|
data-id-equipo="${equipo.idEquipo}">
|
||||||
|
|
||||||
|
Unirse
|
||||||
|
|
||||||
|
</button>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// EQUIPO
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
equiposHTML += `
|
equiposHTML += `
|
||||||
<div class="equipo me-2 ms-2 p-3 border rounded">
|
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||||
|
|
||||||
<h6>
|
<h6>
|
||||||
${escapeHTML(equipo.nombreEquipo)}
|
${escapeHTML(
|
||||||
|
equipo.nombreEquipo
|
||||||
|
)}
|
||||||
</h6>
|
</h6>
|
||||||
|
|
||||||
<div class="jugadores mb-2">
|
<div class="jugadores mb-2">
|
||||||
${jugadoresHTML}
|
${jugadoresHTML}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
${botonUnirseHTML}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
|
|
||||||
// IMPORTANTE:
|
|
||||||
// Solo modificamos .equipos
|
// =================================================
|
||||||
$("#contenedorSalas .equipos").html(equiposHTML);
|
// TARJETA DE LA SALA
|
||||||
|
// =================================================
|
||||||
|
|
||||||
|
const salaHTML = `
|
||||||
|
<div class="col-12 col-md-10 col-lg-8">
|
||||||
|
|
||||||
|
<div class="sala card p-3 shadow-sm mb-4">
|
||||||
|
|
||||||
|
<div class="sala-header h4 text-center mb-3 text-white">
|
||||||
|
${escapeHTML(
|
||||||
|
partida.nombrePartida
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="sala-body">
|
||||||
|
|
||||||
|
<div class="equipos d-flex justify-content-around">
|
||||||
|
${equiposHTML}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
|
||||||
|
// =================================================
|
||||||
|
// IMPORTANTE
|
||||||
|
// =================================================
|
||||||
|
//
|
||||||
|
// NO usamos .html() aquí porque eso eliminaría
|
||||||
|
// las salas que ya se hayan mostrado.
|
||||||
|
//
|
||||||
|
// Usamos .append() para añadir cada nueva sala.
|
||||||
|
//
|
||||||
|
|
||||||
|
$("#contenedorSalas").append(
|
||||||
|
salaHTML
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// CREAR SALA
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
$("#btnCrearSala").on(
|
||||||
|
"click",
|
||||||
|
function () {
|
||||||
|
const nombreSala =
|
||||||
|
$("#nombreSala").val().trim();
|
||||||
|
|
||||||
|
const nombreEquipo1 =
|
||||||
|
$("#nombreEquipo1").val().trim();
|
||||||
|
|
||||||
|
const nombreEquipo2 =
|
||||||
|
$("#nombreEquipo2").val().trim();
|
||||||
|
|
||||||
|
let valido = true;
|
||||||
|
|
||||||
|
// Limpiar errores anteriores
|
||||||
|
$(".mensaje-error-sala").remove();
|
||||||
|
|
||||||
|
$(".input-kahoot")
|
||||||
|
.removeClass("is-invalid");
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// VALIDAR NOMBRE DE SALA
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
if (
|
||||||
|
nombreSala === "" ||
|
||||||
|
nombreSala.length < 3
|
||||||
|
) {
|
||||||
|
mostrarError(
|
||||||
|
"#nombreSala",
|
||||||
|
"El nombre de la sala debe tener al menos 3 caracteres."
|
||||||
|
);
|
||||||
|
|
||||||
|
valido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// VALIDAR EQUIPO 1
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
if (
|
||||||
|
nombreEquipo1 === "" ||
|
||||||
|
nombreEquipo1.length < 2
|
||||||
|
) {
|
||||||
|
mostrarError(
|
||||||
|
"#nombreEquipo1",
|
||||||
|
"El nombre del equipo debe tener al menos 2 caracteres."
|
||||||
|
);
|
||||||
|
|
||||||
|
valido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// VALIDAR EQUIPO 2
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
if (
|
||||||
|
nombreEquipo2 === "" ||
|
||||||
|
nombreEquipo2.length < 2
|
||||||
|
) {
|
||||||
|
mostrarError(
|
||||||
|
"#nombreEquipo2",
|
||||||
|
"El nombre del equipo debe tener al menos 2 caracteres."
|
||||||
|
);
|
||||||
|
|
||||||
|
valido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// LOS EQUIPOS NO PUEDEN TENER EL MISMO NOMBRE
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
if (
|
||||||
|
nombreEquipo1 !== "" &&
|
||||||
|
nombreEquipo2 !== "" &&
|
||||||
|
nombreEquipo1.toLowerCase() ===
|
||||||
|
nombreEquipo2.toLowerCase()
|
||||||
|
) {
|
||||||
|
mostrarError(
|
||||||
|
"#nombreEquipo2",
|
||||||
|
"Los dos equipos deben tener nombres diferentes."
|
||||||
|
);
|
||||||
|
|
||||||
|
valido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Si hay errores, no continuar
|
||||||
|
if (!valido) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// DESACTIVAR BOTÓN
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
const boton = $("#btnCrearSala");
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", true)
|
||||||
|
.text("Creando sala...");
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------
|
||||||
|
// CREAR SALA
|
||||||
|
// -----------------------------------------------
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorPartida.php",
|
||||||
|
type: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
|
||||||
|
data: {
|
||||||
|
orden: "crear",
|
||||||
|
nombrePartida: nombreSala,
|
||||||
|
nombreEquipo1: nombreEquipo1,
|
||||||
|
nombreEquipo2: nombreEquipo2,
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
if (respuesta.error) {
|
||||||
|
alert(respuesta.error);
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Crear sala");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (respuesta.exito) {
|
||||||
|
$("#modalCrearSala").modal(
|
||||||
|
"hide"
|
||||||
|
);
|
||||||
|
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
console.error(
|
||||||
|
"Error al crear la sala:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
alert(
|
||||||
|
"No se ha podido crear la sala."
|
||||||
|
);
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Crear sala");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// MOSTRAR ERROR DE VALIDACIÓN
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function mostrarError(
|
||||||
|
selector,
|
||||||
|
mensaje
|
||||||
|
) {
|
||||||
|
const input = $(selector);
|
||||||
|
|
||||||
|
input.addClass("is-invalid");
|
||||||
|
|
||||||
|
input.after(`
|
||||||
|
<div class="mensaje-error-sala text-danger mt-1 small">
|
||||||
|
${mensaje}
|
||||||
|
</div>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// LIMPIAR ERRORES AL ESCRIBIR
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
$(document).on(
|
||||||
|
"input",
|
||||||
|
".input-kahoot",
|
||||||
|
function () {
|
||||||
|
$(this)
|
||||||
|
.removeClass("is-invalid");
|
||||||
|
|
||||||
|
$(this)
|
||||||
|
.next(".mensaje-error-sala")
|
||||||
|
.remove();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
+665
@@ -0,0 +1,665 @@
|
|||||||
|
let usuarioYaEstaEnEquipo = false;
|
||||||
|
let intervaloSala = null;
|
||||||
|
let botonAbandonarReferencia = null;
|
||||||
|
|
||||||
|
$(document).ready(function () {
|
||||||
|
comprobarSalaUsuario();
|
||||||
|
});
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// COMPROBAR SI EL USUARIO YA ESTÁ EN UNA SALA
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function comprobarSalaUsuario() {
|
||||||
|
$("#cargandoSalas").remove();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorEquipoxUsuario.php",
|
||||||
|
type: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
data: {
|
||||||
|
orden: "obtenerSalaUsuario",
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
if (respuesta.error || !respuesta.asociado) {
|
||||||
|
usuarioYaEstaEnEquipo = false;
|
||||||
|
|
||||||
|
$("#contenedorCrearSala").show();
|
||||||
|
|
||||||
|
cargarSalas();
|
||||||
|
} else {
|
||||||
|
usuarioYaEstaEnEquipo = true;
|
||||||
|
|
||||||
|
$("#contenedorCrearSala").hide();
|
||||||
|
|
||||||
|
cargarSalaUsuario(respuesta.idPartida);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
console.error(
|
||||||
|
"Error al comprobar la sala del usuario:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
usuarioYaEstaEnEquipo = false;
|
||||||
|
|
||||||
|
$("#contenedorCrearSala").show();
|
||||||
|
|
||||||
|
cargarSalas();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// CARGAR SALA DEL USUARIO
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function cargarSalaUsuario(idPartida) {
|
||||||
|
// Primera carga completa
|
||||||
|
cargarSalaCompleta(idPartida);
|
||||||
|
|
||||||
|
// Evitar intervalos duplicados
|
||||||
|
if (intervaloSala) {
|
||||||
|
clearInterval(intervaloSala);
|
||||||
|
}
|
||||||
|
|
||||||
|
intervaloSala = setInterval(function () {
|
||||||
|
actualizarSalaUsuario(idPartida);
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// CARGA COMPLETA DE LA SALA
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function cargarSalaCompleta(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
cargarInformacionSalaCompleta(partida);
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
console.error(
|
||||||
|
"Error al obtener la sala:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// OBTENER EQUIPOS Y JUGADORES
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
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) {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// ACTUALIZAR SALA CADA 5 SEGUNDOS
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
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 actualizar la sala:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// OBTENER INFORMACIÓN PARA ACTUALIZACIÓN
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function cargarInformacionSala(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) {
|
||||||
|
mostrarEquipos(
|
||||||
|
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 SALA COMPLETA
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function mostrarSala(partida, equipos, jugadores) {
|
||||||
|
const contenedor = $("#contenedorSalas");
|
||||||
|
|
||||||
|
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 += `
|
||||||
|
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
||||||
|
esHost ? "jugador-host" : ""
|
||||||
|
}">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<span class="avatar">
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? '<i class="bi bi-award-fill"></i>'
|
||||||
|
: '<i class="bi bi-person-fill"></i>'
|
||||||
|
}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
${escapeHTML(jugador.nombreUsuario)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? `<span class="badge bg-warning text-dark ms-2">
|
||||||
|
HOST
|
||||||
|
</span>`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
jugadoresHTML = `
|
||||||
|
<div class="text-muted small">
|
||||||
|
Sin jugadores
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
equiposHTML += `
|
||||||
|
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||||
|
|
||||||
|
<h6>
|
||||||
|
${escapeHTML(equipo.nombreEquipo)}
|
||||||
|
</h6>
|
||||||
|
|
||||||
|
<div class="jugadores mb-2">
|
||||||
|
${jugadoresHTML}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
const esHost =
|
||||||
|
partida.idUsuario == idUsuarioSesion;
|
||||||
|
|
||||||
|
let botonIniciarHTML = "";
|
||||||
|
|
||||||
|
if (esHost) {
|
||||||
|
botonIniciarHTML = `
|
||||||
|
<a
|
||||||
|
href="juego.php?idPartida=${partida.idPartida}"
|
||||||
|
class="btn btn-success btn-lg px-4">
|
||||||
|
|
||||||
|
<i class="bi bi-controller"></i>
|
||||||
|
¡Iniciar Partida!
|
||||||
|
|
||||||
|
</a>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const salaHTML = `
|
||||||
|
<div class="col-12 col-md-10 col-lg-8">
|
||||||
|
|
||||||
|
<div class="sala card p-3 shadow-sm mb-4">
|
||||||
|
|
||||||
|
<div class="sala-header h4 text-center mb-3 text-white">
|
||||||
|
${escapeHTML(partida.nombrePartida)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="sala-body">
|
||||||
|
|
||||||
|
<div class="equipos d-flex justify-content-around">
|
||||||
|
${equiposHTML}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-outline-danger btn-abandonar-sala"
|
||||||
|
data-id-partida="${partida.idPartida}"
|
||||||
|
data-es-host="${esHost}">
|
||||||
|
|
||||||
|
Abandonar sala
|
||||||
|
|
||||||
|
</button>
|
||||||
|
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? botonIniciarHTML
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
// IMPORTANTE:
|
||||||
|
// El usuario está dentro de una única sala.
|
||||||
|
// Por eso aquí SÍ queremos reemplazar.
|
||||||
|
contenedor.html(salaHTML);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// UNIRSE A UN EQUIPO
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
$(document).on(
|
||||||
|
"click",
|
||||||
|
".btn-unirse-equipo",
|
||||||
|
function () {
|
||||||
|
const boton = $(this);
|
||||||
|
|
||||||
|
const idEquipo = boton.data("id-equipo");
|
||||||
|
const idPartida = boton.data("id-partida");
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", true)
|
||||||
|
.text("Uniéndose...");
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorEquipoxUsuario.php",
|
||||||
|
type: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
|
||||||
|
data: {
|
||||||
|
orden: "registrarUsuarioEnEquipo",
|
||||||
|
idEquipo: idEquipo,
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
if (respuesta.error) {
|
||||||
|
alert(respuesta.error);
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Unirse");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usuarioYaEstaEnEquipo = true;
|
||||||
|
|
||||||
|
$("#contenedorCrearSala").hide();
|
||||||
|
$("#contenedorSalas").empty();
|
||||||
|
|
||||||
|
cargarSalaUsuario(idPartida);
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
console.error(
|
||||||
|
"Error al unirse al equipo:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
alert(
|
||||||
|
"No se ha podido unir al equipo."
|
||||||
|
);
|
||||||
|
|
||||||
|
boton
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Unirse");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// ABANDONAR SALA - ABRIR MODAL
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
$(document).on(
|
||||||
|
"click",
|
||||||
|
".btn-abandonar-sala",
|
||||||
|
function () {
|
||||||
|
botonAbandonarReferencia = $(this);
|
||||||
|
|
||||||
|
const esHost =
|
||||||
|
$(this).data("es-host") === true ||
|
||||||
|
$(this).data("es-host") === "true";
|
||||||
|
|
||||||
|
const titulo =
|
||||||
|
$("#modalAbandonarSalaLabel");
|
||||||
|
|
||||||
|
const mensaje =
|
||||||
|
$("#modalAbandonarSalaMensaje");
|
||||||
|
|
||||||
|
const botonConfirmar =
|
||||||
|
$("#btnConfirmarAbandonar");
|
||||||
|
|
||||||
|
if (esHost) {
|
||||||
|
titulo.text("¿Abandonar la sala?");
|
||||||
|
|
||||||
|
mensaje.text(
|
||||||
|
"Si abandonas la sala como anfitrión, la partida terminará para todos los jugadores."
|
||||||
|
);
|
||||||
|
|
||||||
|
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."
|
||||||
|
);
|
||||||
|
|
||||||
|
botonConfirmar.text("Sí, salir");
|
||||||
|
}
|
||||||
|
|
||||||
|
const modal = new bootstrap.Modal(
|
||||||
|
document.getElementById(
|
||||||
|
"modalAbandonarSala"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
modal.show();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// CONFIRMAR ABANDONAR SALA
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
$(document).on(
|
||||||
|
"click",
|
||||||
|
"#btnConfirmarAbandonar",
|
||||||
|
function () {
|
||||||
|
const modalEl =
|
||||||
|
document.getElementById(
|
||||||
|
"modalAbandonarSala"
|
||||||
|
);
|
||||||
|
|
||||||
|
const modalInstance =
|
||||||
|
bootstrap.Modal.getInstance(modalEl);
|
||||||
|
|
||||||
|
const btnConfirmar = $(this);
|
||||||
|
|
||||||
|
btnConfirmar
|
||||||
|
.prop("disabled", true)
|
||||||
|
.text("Saliendo...");
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorEquipoxUsuario.php",
|
||||||
|
type: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
|
||||||
|
data: {
|
||||||
|
orden: "salirDeSala",
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
btnConfirmar
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Sí, salir");
|
||||||
|
|
||||||
|
if (modalInstance) {
|
||||||
|
modalInstance.hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (respuesta.error) {
|
||||||
|
alert(respuesta.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usuarioYaEstaEnEquipo = false;
|
||||||
|
|
||||||
|
if (intervaloSala) {
|
||||||
|
clearInterval(intervaloSala);
|
||||||
|
intervaloSala = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#contenedorCrearSala").show();
|
||||||
|
|
||||||
|
cargarSalas();
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
console.error(
|
||||||
|
"Error al salir de la sala:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
alert(
|
||||||
|
"No se pudo salir de la sala."
|
||||||
|
);
|
||||||
|
|
||||||
|
btnConfirmar
|
||||||
|
.prop("disabled", false)
|
||||||
|
.text("Sí, salir");
|
||||||
|
|
||||||
|
if (modalInstance) {
|
||||||
|
modalInstance.hide();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// AUXILIARES
|
||||||
|
// =====================================================
|
||||||
|
|
||||||
|
function escapeHTML(texto) {
|
||||||
|
return $("<div>")
|
||||||
|
.text(texto ?? "")
|
||||||
|
.html();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 += `
|
||||||
|
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
||||||
|
esHost ? "jugador-host" : ""
|
||||||
|
}">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<span class="avatar">
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? '<i class="bi bi-award-fill"></i>'
|
||||||
|
: '<i class="bi bi-person-fill"></i>'
|
||||||
|
}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
${escapeHTML(
|
||||||
|
jugador.nombreUsuario
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
${
|
||||||
|
esHost
|
||||||
|
? `<span class="badge bg-warning text-dark ms-2">
|
||||||
|
HOST
|
||||||
|
</span>`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
jugadoresHTML = `
|
||||||
|
<div class="text-muted small">
|
||||||
|
Sin jugadores
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
equiposHTML += `
|
||||||
|
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||||
|
|
||||||
|
<h6>
|
||||||
|
${escapeHTML(
|
||||||
|
equipo.nombreEquipo
|
||||||
|
)}
|
||||||
|
</h6>
|
||||||
|
|
||||||
|
<div class="jugadores mb-2">
|
||||||
|
${jugadoresHTML}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
$("#contenedorSalas .equipos")
|
||||||
|
.html(equiposHTML);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user