arreglado crear sala
This commit is contained in:
+103
-220
@@ -1,28 +1,25 @@
|
||||
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);
|
||||
|
||||
if (respuesta.error) {
|
||||
|
||||
console.error(respuesta.error);
|
||||
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
@@ -33,15 +30,12 @@ $.ajax({
|
||||
}
|
||||
|
||||
if (respuesta.asociado) {
|
||||
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
$("#contenedorCrearSala").hide();
|
||||
|
||||
cargarSalaUsuario(respuesta.idPartida);
|
||||
|
||||
} else {
|
||||
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
cargarSalas();
|
||||
@@ -49,37 +43,28 @@ $.ajax({
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
|
||||
console.error(
|
||||
"Error al comprobar la sala del usuario:",
|
||||
error
|
||||
);
|
||||
console.error("Error al comprobar la sala del usuario:", error);
|
||||
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
cargarSalas();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function cargarSalaUsuario(idPartida) {
|
||||
|
||||
$.ajax({
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "buscar",
|
||||
idPartida: idPartida
|
||||
idPartida: idPartida,
|
||||
},
|
||||
|
||||
success: function (partida) {
|
||||
|
||||
if (partida.error) {
|
||||
|
||||
console.error(partida.error);
|
||||
|
||||
return;
|
||||
@@ -91,37 +76,28 @@ $.ajax({
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
|
||||
console.error(
|
||||
"Error al obtener la sala del usuario:",
|
||||
error
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
console.error("Error al obtener la sala del usuario:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function cargarSalas() {
|
||||
|
||||
$.ajax({
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "listar"
|
||||
orden: "listar",
|
||||
},
|
||||
|
||||
success: function (partidas) {
|
||||
|
||||
const contenedor = $("#contenedorSalas");
|
||||
|
||||
contenedor.empty();
|
||||
contenedor.empty();
|
||||
|
||||
if (!Array.isArray(partidas) || partidas.length === 0) {
|
||||
|
||||
contenedor.html(`
|
||||
<div class="col-12 text-center">
|
||||
<p class="text-muted">
|
||||
@@ -134,38 +110,28 @@ $.ajax({
|
||||
}
|
||||
|
||||
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
|
||||
idPartida: partida.idPartida,
|
||||
},
|
||||
|
||||
success: function (equipos) {
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
@@ -173,60 +139,39 @@ $.ajax({
|
||||
|
||||
data: {
|
||||
orden: "getJugadores",
|
||||
idPartida: partida.idPartida
|
||||
idPartida: partida.idPartida,
|
||||
},
|
||||
|
||||
success: function (jugadores) {
|
||||
|
||||
mostrarSala(
|
||||
partida,
|
||||
equipos,
|
||||
jugadores
|
||||
);
|
||||
mostrarSala(partida, equipos, jugadores);
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
|
||||
console.error(
|
||||
"Error al obtener jugadores:",
|
||||
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;
|
||||
|
||||
});
|
||||
|
||||
let jugadoresHTML = "";
|
||||
|
||||
if (jugadoresEquipo.length > 0) {
|
||||
|
||||
jugadoresEquipo.forEach(function (jugador) {
|
||||
|
||||
jugadoresHTML += `
|
||||
<div class="jugador">
|
||||
|
||||
@@ -239,9 +184,7 @@ equipos.forEach(function (equipo) {
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
jugadoresHTML = `
|
||||
<div class="text-muted small">
|
||||
Sin jugadores
|
||||
@@ -249,11 +192,9 @@ equipos.forEach(function (equipo) {
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
let botonUnirseHTML = "";
|
||||
|
||||
if (!usuarioYaEstaEnEquipo) {
|
||||
|
||||
botonUnirseHTML = `
|
||||
<button
|
||||
type="button"
|
||||
@@ -267,7 +208,6 @@ equipos.forEach(function (equipo) {
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
equiposHTML += `
|
||||
<div class="equipo">
|
||||
|
||||
@@ -285,10 +225,9 @@ equipos.forEach(function (equipo) {
|
||||
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
const salaHTML = `
|
||||
const salaHTML = `
|
||||
<div class="col-12 col-md-10 col-lg-8">
|
||||
|
||||
<div class="sala">
|
||||
@@ -314,35 +253,25 @@ const salaHTML = `
|
||||
</div>
|
||||
`;
|
||||
|
||||
|
||||
contenedor.append(salaHTML);
|
||||
|
||||
|
||||
contenedor.append(salaHTML);
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// UNIRSE A UN EQUIPO
|
||||
// ======================================================
|
||||
|
||||
$(document).on(
|
||||
"click",
|
||||
".btn-unirse-equipo",
|
||||
function () {
|
||||
|
||||
$(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);
|
||||
|
||||
boton.text("Uniéndose...");
|
||||
|
||||
|
||||
$.ajax({
|
||||
|
||||
url: "controladores/ControladorEquipoxUsuario.php",
|
||||
|
||||
type: "POST",
|
||||
@@ -351,20 +280,13 @@ function () {
|
||||
|
||||
data: {
|
||||
orden: "registrarUsuarioEnEquipo",
|
||||
idEquipo: idEquipo
|
||||
idEquipo: idEquipo,
|
||||
},
|
||||
|
||||
|
||||
success: function (respuesta) {
|
||||
|
||||
console.log(
|
||||
"Respuesta al unirse:",
|
||||
respuesta
|
||||
);
|
||||
|
||||
console.log("Respuesta al unirse:", respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
|
||||
alert(respuesta.error);
|
||||
|
||||
boton.prop("disabled", false);
|
||||
@@ -374,233 +296,194 @@ function () {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// ==========================================
|
||||
// UNIÓN CORRECTA
|
||||
// ==========================================
|
||||
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
|
||||
// Ocultamos el botón "Crear sala"
|
||||
|
||||
$("#contenedorCrearSala").hide();
|
||||
|
||||
|
||||
// Eliminamos las salas actuales
|
||||
|
||||
$("#contenedorSalas").empty();
|
||||
|
||||
|
||||
// Cargamos directamente la sala
|
||||
// donde acaba de entrar el usuario
|
||||
|
||||
cargarSalaUsuario(idPartida);
|
||||
},
|
||||
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al unirse al equipo:", error);
|
||||
|
||||
console.error(
|
||||
"Error al unirse al equipo:",
|
||||
error
|
||||
);
|
||||
|
||||
console.error(
|
||||
"Respuesta del servidor:",
|
||||
xhr.responseText
|
||||
);
|
||||
|
||||
|
||||
alert(
|
||||
"No se ha podido unir al equipo."
|
||||
);
|
||||
console.error("Respuesta del servidor:", xhr.responseText);
|
||||
|
||||
alert("No se ha podido unir al equipo.");
|
||||
|
||||
boton.prop("disabled", false);
|
||||
|
||||
boton.text("Unirse");
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
);
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
// ESCAPAR HTML
|
||||
// ======================================================
|
||||
|
||||
function escapeHTML(texto) {
|
||||
|
||||
return $("<div>")
|
||||
return $("<div>")
|
||||
.text(texto ?? "")
|
||||
.html();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// CREAR SALA
|
||||
// ======================================================
|
||||
|
||||
$("#btnCrearSala").on(
|
||||
"click",
|
||||
function () {
|
||||
$("#btnCrearSala").on("click", function () {
|
||||
const nombreSala = $("#nombreSala").val().trim();
|
||||
|
||||
const nombreSala =
|
||||
$("#nombreSala").val().trim();
|
||||
|
||||
const nombreEquipo1 =
|
||||
$("#nombreEquipo1").val().trim();
|
||||
|
||||
const nombreEquipo2 =
|
||||
$("#nombreEquipo2").val().trim();
|
||||
const nombreEquipo1 = $("#nombreEquipo1").val().trim();
|
||||
|
||||
const nombreEquipo2 = $("#nombreEquipo2").val().trim();
|
||||
|
||||
let valido = true;
|
||||
let valido = true;
|
||||
|
||||
|
||||
$(".mensaje-error-sala").remove();
|
||||
|
||||
$(".input-kahoot").removeClass(
|
||||
"is-invalid"
|
||||
);
|
||||
|
||||
$(".input-kahoot").removeClass("is-invalid");
|
||||
|
||||
if (nombreSala === "") {
|
||||
|
||||
mostrarError(
|
||||
"#nombreSala",
|
||||
"El nombre de la sala es obligatorio."
|
||||
);
|
||||
mostrarError("#nombreSala", "El nombre de la sala es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
|
||||
} else if (nombreSala.length < 3) {
|
||||
|
||||
mostrarError(
|
||||
"#nombreSala",
|
||||
"El nombre de la sala debe tener al menos 3 caracteres."
|
||||
"El nombre de la sala debe tener al menos 3 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
|
||||
if (nombreEquipo1 === "") {
|
||||
|
||||
mostrarError(
|
||||
"#nombreEquipo1",
|
||||
"El nombre del equipo 1 es obligatorio."
|
||||
);
|
||||
mostrarError("#nombreEquipo1", "El nombre del equipo 1 es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
|
||||
} else if (nombreEquipo1.length < 2) {
|
||||
|
||||
mostrarError(
|
||||
"#nombreEquipo1",
|
||||
"El nombre del equipo debe tener al menos 2 caracteres."
|
||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
|
||||
if (nombreEquipo2 === "") {
|
||||
|
||||
mostrarError(
|
||||
"#nombreEquipo2",
|
||||
"El nombre del equipo 2 es obligatorio."
|
||||
);
|
||||
mostrarError("#nombreEquipo2", "El nombre del equipo 2 es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
|
||||
} else if (nombreEquipo2.length < 2) {
|
||||
|
||||
mostrarError(
|
||||
"#nombreEquipo2",
|
||||
"El nombre del equipo debe tener al menos 2 caracteres."
|
||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
|
||||
if (
|
||||
nombreEquipo1 !== "" &&
|
||||
nombreEquipo2 !== "" &&
|
||||
nombreEquipo1.toLowerCase() ===
|
||||
nombreEquipo2.toLowerCase()
|
||||
nombreEquipo1.toLowerCase() === nombreEquipo2.toLowerCase()
|
||||
) {
|
||||
|
||||
mostrarError(
|
||||
"#nombreEquipo2",
|
||||
"Los dos equipos deben tener nombres diferentes."
|
||||
"Los dos equipos deben tener nombres diferentes.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
|
||||
if (!valido) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Deshabilitamos el botón mientras se crea la sala
|
||||
const boton = $("#btnCrearSala");
|
||||
|
||||
boton.prop("disabled", true);
|
||||
boton.text("Creando sala...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php", // <-- cambia esto por la ruta de tu controlador
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: {
|
||||
orden: "crear",
|
||||
nombrePartida: nombreSala,
|
||||
nombreEquipo1: nombreEquipo1,
|
||||
nombreEquipo2: nombreEquipo2,
|
||||
},
|
||||
|
||||
success: function (respuesta) {
|
||||
console.log("Respuesta del servidor:", respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
|
||||
boton.prop("disabled", false);
|
||||
boton.text("Crear sala");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (respuesta.exito) {
|
||||
$("#modalCrearSala").modal("hide");
|
||||
location.reload();
|
||||
}
|
||||
},
|
||||
|
||||
console.log("Datos válidos:");
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al crear la sala:", error);
|
||||
console.error("Respuesta:", xhr.responseText);
|
||||
|
||||
console.log({
|
||||
nombreSala: nombreSala,
|
||||
nombreEquipo1: nombreEquipo1,
|
||||
nombreEquipo2: nombreEquipo2
|
||||
alert("No se ha podido crear la sala.");
|
||||
|
||||
boton.prop("disabled", false);
|
||||
boton.text("Crear sala");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
);
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
// MOSTRAR ERROR
|
||||
// ======================================================
|
||||
|
||||
function mostrarError(selector, mensaje) {
|
||||
const input = $(selector);
|
||||
|
||||
const input = $(selector);
|
||||
input.addClass("is-invalid");
|
||||
|
||||
input.addClass("is-invalid");
|
||||
|
||||
|
||||
input.after(`
|
||||
input.after(`
|
||||
<div class="mensaje-error-sala text-danger mt-1 small">
|
||||
|
||||
${mensaje}
|
||||
|
||||
</div>
|
||||
`);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// QUITAR ERROR AL ESCRIBIR
|
||||
// ======================================================
|
||||
|
||||
$(".input-kahoot").on(
|
||||
"input",
|
||||
function () {
|
||||
$(".input-kahoot").on("input", function () {
|
||||
$(this).removeClass("is-invalid");
|
||||
|
||||
$(this).removeClass(
|
||||
"is-invalid"
|
||||
);
|
||||
|
||||
$(this)
|
||||
.next(".mensaje-error-sala")
|
||||
.remove();
|
||||
}
|
||||
|
||||
|
||||
);
|
||||
$(this).next(".mensaje-error-sala").remove();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user