@@ -453,193 +350,132 @@ function generarEquiposHTML(
`;
});
-
return equiposHTML;
}
+$("#btnCrearSala").on("click", function () {
+ const nombreSala =
+ $("#nombreSala").val().trim();
-// =====================================================
-// CREAR SALA
-// =====================================================
+ const nombreEquipo1 =
+ $("#nombreEquipo1").val().trim();
-$("#btnCrearSala").on(
- "click",
- function () {
- const nombreSala =
- $("#nombreSala").val().trim();
+ const nombreEquipo2 =
+ $("#nombreEquipo2").val().trim();
- const nombreEquipo1 =
- $("#nombreEquipo1").val().trim();
+ let valido = true;
- const nombreEquipo2 =
- $("#nombreEquipo2").val().trim();
+ $(".mensaje-error-sala").remove();
+ $(".input-kahoot")
+ .removeClass("is-invalid");
- let valido = true;
+ if (
+ nombreSala === "" ||
+ nombreSala.length < 3
+ ) {
+ mostrarError(
+ "#nombreSala",
+ "El nombre de la sala debe tener al menos 3 caracteres."
+ );
+ valido = false;
+ }
- // -----------------------------------------------
- // LIMPIAR ERRORES
- // -----------------------------------------------
+ if (
+ nombreEquipo1 === "" ||
+ nombreEquipo1.length < 2
+ ) {
+ mostrarError(
+ "#nombreEquipo1",
+ "El nombre del equipo debe tener al menos 2 caracteres."
+ );
- $(".mensaje-error-sala").remove();
+ valido = false;
+ }
- $(".input-kahoot")
- .removeClass("is-invalid");
+ if (
+ nombreEquipo2 === "" ||
+ nombreEquipo2.length < 2
+ ) {
+ mostrarError(
+ "#nombreEquipo2",
+ "El nombre del equipo debe tener al menos 2 caracteres."
+ );
+ valido = false;
+ }
- // -----------------------------------------------
- // VALIDAR NOMBRE DE SALA
- // -----------------------------------------------
+ if (
+ nombreEquipo1 !== "" &&
+ nombreEquipo2 !== "" &&
+ nombreEquipo1.toLowerCase() ===
+ nombreEquipo2.toLowerCase()
+ ) {
+ mostrarError(
+ "#nombreEquipo2",
+ "Los dos equipos deben tener nombres diferentes."
+ );
- if (
- nombreSala === "" ||
- nombreSala.length < 3
- ) {
- mostrarError(
- "#nombreSala",
- "El nombre de la sala debe tener al menos 3 caracteres."
- );
+ valido = false;
+ }
- valido = false;
- }
+ if (!valido) {
+ return;
+ }
+ const boton = $("#btnCrearSala");
- // -----------------------------------------------
- // VALIDAR EQUIPO 1
- // -----------------------------------------------
+ boton
+ .prop("disabled", true)
+ .text("Creando sala...");
- if (
- nombreEquipo1 === "" ||
- nombreEquipo1.length < 2
- ) {
- mostrarError(
- "#nombreEquipo1",
- "El nombre del equipo debe tener al menos 2 caracteres."
- );
+ $.ajax({
+ url: "controladores/controladorPartida.php",
+ type: "POST",
+ dataType: "json",
- valido = false;
- }
+ data: {
+ orden: "crear",
+ nombrePartida: nombreSala,
+ nombreEquipo1: nombreEquipo1,
+ nombreEquipo2: nombreEquipo2,
+ },
-
- // -----------------------------------------------
- // VALIDAR EQUIPO 2
- // -----------------------------------------------
-
- if (
- nombreEquipo2 === "" ||
- nombreEquipo2.length < 2
- ) {
- mostrarError(
- "#nombreEquipo2",
- "El nombre del equipo debe tener al menos 2 caracteres."
- );
-
- valido = false;
- }
-
-
- // -----------------------------------------------
- // COMPROBAR NOMBRES IGUALES
- // -----------------------------------------------
-
- if (
- nombreEquipo1 !== "" &&
- nombreEquipo2 !== "" &&
- nombreEquipo1.toLowerCase() ===
- nombreEquipo2.toLowerCase()
- ) {
- mostrarError(
- "#nombreEquipo2",
- "Los dos equipos deben tener nombres diferentes."
- );
-
- valido = false;
- }
-
-
- // -----------------------------------------------
- // SI HAY ERRORES
- // -----------------------------------------------
-
- if (!valido) {
- return;
- }
-
-
- // -----------------------------------------------
- // BOTÓN
- // -----------------------------------------------
-
- const boton = $("#btnCrearSala");
-
- boton
- .prop("disabled", true)
- .text("Creando sala...");
-
-
- // -----------------------------------------------
- // PETICIÓN
- // -----------------------------------------------
-
- $.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."
- );
+ success: function (respuesta) {
+ if (respuesta.error) {
+ alert(respuesta.error);
boton
.prop("disabled", false)
.text("Crear sala");
- },
- });
- }
-);
+ return;
+ }
-// =====================================================
-// MOSTRAR ERROR
-// =====================================================
+ 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");
+ },
+ });
+});
function mostrarError(
selector,
@@ -659,11 +495,6 @@ function mostrarError(
`);
}
-
-// =====================================================
-// LIMPIAR ERRORES AL ESCRIBIR
-// =====================================================
-
$(document).on(
"input",
".input-kahoot",
diff --git a/js/sala.js b/js/sala.js
index 758f3c1..42c0d8f 100644
--- a/js/sala.js
+++ b/js/sala.js
@@ -6,10 +6,6 @@ $(document).ready(function () {
comprobarSalaUsuario();
});
-// =====================================================
-// COMPROBAR SI EL USUARIO YA ESTÁ EN UNA SALA
-// =====================================================
-
function comprobarSalaUsuario() {
$("#cargandoSalas").remove();
@@ -38,26 +34,25 @@ function comprobarSalaUsuario() {
},
error: function (xhr, status, error) {
- console.error("Error al comprobar la sala del usuario:", error);
-
usuarioYaEstaEnEquipo = false;
+ if (intervaloSala) {
+ clearInterval(intervaloSala);
+ intervaloSala = null;
+ }
+
+ $("#contenedorSalas").empty();
+
$("#contenedorCrearSala").show();
- cargarSalas();
+ iniciarActualizacionSalas();
},
});
}
-// =====================================================
-// CARGAR SALA DEL USUARIO
-// =====================================================
-
function cargarSalaUsuario(idPartida) {
- // Primera carga completa
cargarSalaCompleta(idPartida);
- // Evitar intervalos duplicados
if (intervaloSala) {
clearInterval(intervaloSala);
}
@@ -67,10 +62,6 @@ function cargarSalaUsuario(idPartida) {
}, 5000);
}
-// =====================================================
-// CARGA COMPLETA DE LA SALA
-// =====================================================
-
function cargarSalaCompleta(idPartida) {
$.ajax({
url: "controladores/controladorPartida.php",
@@ -97,10 +88,6 @@ function cargarSalaCompleta(idPartida) {
});
}
-// =====================================================
-// OBTENER EQUIPOS Y JUGADORES
-// =====================================================
-
function cargarInformacionSalaCompleta(partida) {
$.ajax({
url: "controladores/controladorPartida.php",
@@ -139,10 +126,6 @@ function cargarInformacionSalaCompleta(partida) {
});
}
-// =====================================================
-// ACTUALIZAR SALA CADA 5 SEGUNDOS
-// =====================================================
-
function actualizarSalaUsuario(idPartida) {
$.ajax({
url: "controladores/controladorPartida.php",
@@ -169,10 +152,6 @@ function actualizarSalaUsuario(idPartida) {
});
}
-// =====================================================
-// OBTENER INFORMACIÓN PARA ACTUALIZACIÓN
-// =====================================================
-
function cargarInformacionSala(partida) {
$.ajax({
url: "controladores/controladorPartida.php",
@@ -211,299 +190,11 @@ function cargarInformacionSala(partida) {
});
}
-// =====================================================
-// 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 += `
-
-
-
-
- ${
- esHost
- ? ''
- : ''
- }
-
-
- ${escapeHTML(jugador.nombreUsuario)}
-
-
- ${
- esHost
- ? `
- HOST
- `
- : ""
- }
-
-
- `;
- });
- } else {
- jugadoresHTML = `
-
- Sin jugadores
-
- `;
- }
-
- equiposHTML += `
-
-
-
- ${escapeHTML(equipo.nombreEquipo)}
-
-
-
- ${jugadoresHTML}
-
-
-
- `;
- });
-
- const esHost = partida.idUsuario == idUsuarioSesion;
-
- let botonIniciarHTML = "";
-
- if (esHost) {
- botonIniciarHTML = `
-
-
-
- ¡Iniciar Partida!
-
-
- `;
- }
-
- const salaHTML = `
-
-
-
-
-
-
-
-
-
- ${equiposHTML}
-
-
-
-
-
-
- ${esHost ? botonIniciarHTML : ""}
-
-
-
-
-
-
-
-
- `;
-
- // 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 $("
")
- .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;
@@ -566,5 +257,294 @@ function mostrarEquipos(partida, equipos, jugadores) {
`;
});
- $("#contenedorSalas .equipos").html(equiposHTML);
+ const esHost = partida.idUsuario == idUsuarioSesion;
+
+ let botonIniciarHTML = "";
+
+ if (esHost) {
+ botonIniciarHTML = `
+
+
+
+ ¡Iniciar Partida!
+
+
+ `;
+ }
+
+ const salaHTML = `
+
+
+
+
+
+
+
+
+
+ ${equiposHTML}
+
+
+
+
+
+
+ ${esHost ? botonIniciarHTML : ""}
+
+
+
+
+
+
+
+
+ `;
+
+ contenedor.html(salaHTML);
}
+
+$(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");
+ },
+ });
+});
+
+$(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();
+});
+
+$(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;
+ }
+
+ $("#contenedorSalas").empty();
+
+ $("#contenedorCrearSala").show();
+
+ iniciarActualizacionSalas();
+ },
+
+ 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();
+ }
+ },
+ });
+});
+
+function escapeHTML(texto) {
+ return $("
")
+ .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 += `
+
+
+
+
+ ${
+ esHost
+ ? ''
+ : ''
+ }
+
+
+ ${escapeHTML(
+ jugador.nombreUsuario
+ )}
+
+
+ ${
+ esHost
+ ? `
+ HOST
+ `
+ : ""
+ }
+
+
+ `;
+ });
+ } else {
+ jugadoresHTML = `
+
+ Sin jugadores
+
+ `;
+ }
+
+ equiposHTML += `
+
+
+
+ ${escapeHTML(equipo.nombreEquipo)}
+
+
+
+ ${jugadoresHTML}
+
+
+
+ `;
+ });
+
+ $("#contenedorSalas .equipos").html(equiposHTML);
+}
\ No newline at end of file