Add se actualizan las salas automaticamente
This commit is contained in:
+147
-242
@@ -27,7 +27,7 @@ function comprobarSalaUsuario() {
|
||||
|
||||
$("#contenedorCrearSala").show();
|
||||
|
||||
cargarSalas();
|
||||
iniciarActualizacionSalas();
|
||||
} else {
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
@@ -38,10 +38,7 @@ function comprobarSalaUsuario() {
|
||||
},
|
||||
|
||||
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;
|
||||
|
||||
@@ -95,10 +92,7 @@ function cargarSalaCompleta(idPartida) {
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error(
|
||||
"Error al obtener la sala:",
|
||||
error
|
||||
);
|
||||
console.error("Error al obtener la sala:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -134,19 +128,13 @@ function cargarInformacionSalaCompleta(partida) {
|
||||
},
|
||||
|
||||
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);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -176,10 +164,7 @@ function actualizarSalaUsuario(idPartida) {
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error(
|
||||
"Error al actualizar la sala:",
|
||||
error
|
||||
);
|
||||
console.error("Error al actualizar la sala:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -211,27 +196,17 @@ function cargarInformacionSala(partida) {
|
||||
},
|
||||
|
||||
success: function (jugadores) {
|
||||
mostrarEquipos(
|
||||
partida,
|
||||
equipos,
|
||||
jugadores
|
||||
);
|
||||
mostrarEquipos(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);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -241,33 +216,32 @@ function cargarInformacionSala(partida) {
|
||||
// =====================================================
|
||||
|
||||
function mostrarSala(partida, equipos, jugadores) {
|
||||
const contenedor = $("#contenedorSalas");
|
||||
const contenedor = $("#contenedorSalas");
|
||||
|
||||
let equiposHTML = "";
|
||||
let equiposHTML = "";
|
||||
|
||||
equipos.forEach(function (equipo) {
|
||||
const jugadoresEquipo = jugadores.filter(function (jugador) {
|
||||
return jugador.idEquipo == equipo.idEquipo;
|
||||
});
|
||||
equipos.forEach(function (equipo) {
|
||||
const jugadoresEquipo = jugadores.filter(function (jugador) {
|
||||
return jugador.idEquipo == equipo.idEquipo;
|
||||
});
|
||||
|
||||
let jugadoresHTML = "";
|
||||
let jugadoresHTML = "";
|
||||
|
||||
if (jugadoresEquipo.length > 0) {
|
||||
jugadoresEquipo.forEach(function (jugador) {
|
||||
const esHost =
|
||||
jugador.idUsuario == partida.idUsuario;
|
||||
if (jugadoresEquipo.length > 0) {
|
||||
jugadoresEquipo.forEach(function (jugador) {
|
||||
const esHost = jugador.idUsuario == partida.idUsuario;
|
||||
|
||||
jugadoresHTML += `
|
||||
jugadoresHTML += `
|
||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
||||
esHost ? "jugador-host" : ""
|
||||
esHost ? "jugador-host" : ""
|
||||
}">
|
||||
|
||||
<div>
|
||||
<span class="avatar">
|
||||
${
|
||||
esHost
|
||||
? '<i class="bi bi-award-fill"></i>'
|
||||
: '<i class="bi bi-person-fill"></i>'
|
||||
esHost
|
||||
? '<i class="bi bi-award-fill"></i>'
|
||||
: '<i class="bi bi-person-fill"></i>'
|
||||
}
|
||||
</span>
|
||||
|
||||
@@ -275,25 +249,25 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
</div>
|
||||
|
||||
${
|
||||
esHost
|
||||
? `<span class="badge bg-warning text-dark ms-2">
|
||||
esHost
|
||||
? `<span class="badge bg-warning text-dark ms-2">
|
||||
HOST
|
||||
</span>`
|
||||
: ""
|
||||
: ""
|
||||
}
|
||||
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
} else {
|
||||
jugadoresHTML = `
|
||||
});
|
||||
} else {
|
||||
jugadoresHTML = `
|
||||
<div class="text-muted small">
|
||||
Sin jugadores
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
equiposHTML += `
|
||||
equiposHTML += `
|
||||
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||
|
||||
<h6>
|
||||
@@ -306,15 +280,14 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
});
|
||||
|
||||
const esHost =
|
||||
partida.idUsuario == idUsuarioSesion;
|
||||
const esHost = partida.idUsuario == idUsuarioSesion;
|
||||
|
||||
let botonIniciarHTML = "";
|
||||
let botonIniciarHTML = "";
|
||||
|
||||
if (esHost) {
|
||||
botonIniciarHTML = `
|
||||
if (esHost) {
|
||||
botonIniciarHTML = `
|
||||
<a
|
||||
href="juego.php?idPartida=${partida.idPartida}"
|
||||
class="btn btn-success btn-lg px-4">
|
||||
@@ -324,9 +297,9 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
|
||||
</a>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
const salaHTML = `
|
||||
const salaHTML = `
|
||||
<div class="col-12 col-md-10 col-lg-8">
|
||||
|
||||
<div class="sala card p-3 shadow-sm mb-4">
|
||||
@@ -353,11 +326,7 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
|
||||
</button>
|
||||
|
||||
${
|
||||
esHost
|
||||
? botonIniciarHTML
|
||||
: ""
|
||||
}
|
||||
${esHost ? botonIniciarHTML : ""}
|
||||
|
||||
</div>
|
||||
|
||||
@@ -368,208 +337,159 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
</div>
|
||||
`;
|
||||
|
||||
// IMPORTANTE:
|
||||
// El usuario está dentro de una única sala.
|
||||
// Por eso aquí SÍ queremos reemplazar.
|
||||
contenedor.html(salaHTML);
|
||||
// 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);
|
||||
$(document).on("click", ".btn-unirse-equipo", function () {
|
||||
const boton = $(this);
|
||||
|
||||
const idEquipo = boton.data("id-equipo");
|
||||
const idPartida = boton.data("id-partida");
|
||||
const idEquipo = boton.data("id-equipo");
|
||||
const idPartida = boton.data("id-partida");
|
||||
|
||||
boton
|
||||
.prop("disabled", true)
|
||||
.text("Uniéndose...");
|
||||
boton.prop("disabled", true).text("Uniéndose...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/controladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
$.ajax({
|
||||
url: "controladores/controladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "registrarUsuarioEnEquipo",
|
||||
idEquipo: idEquipo,
|
||||
},
|
||||
data: {
|
||||
orden: "registrarUsuarioEnEquipo",
|
||||
idEquipo: idEquipo,
|
||||
},
|
||||
|
||||
success: function (respuesta) {
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
success: function (respuesta) {
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
|
||||
boton
|
||||
.prop("disabled", false)
|
||||
.text("Unirse");
|
||||
boton.prop("disabled", false).text("Unirse");
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
$("#contenedorCrearSala").hide();
|
||||
$("#contenedorSalas").empty();
|
||||
$("#contenedorCrearSala").hide();
|
||||
$("#contenedorSalas").empty();
|
||||
|
||||
cargarSalaUsuario(idPartida);
|
||||
},
|
||||
cargarSalaUsuario(idPartida);
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error(
|
||||
"Error al unirse al equipo:",
|
||||
error
|
||||
);
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al unirse al equipo:", error);
|
||||
|
||||
alert(
|
||||
"No se ha podido unir al equipo."
|
||||
);
|
||||
alert("No se ha podido unir al equipo.");
|
||||
|
||||
boton
|
||||
.prop("disabled", false)
|
||||
.text("Unirse");
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
boton.prop("disabled", false).text("Unirse");
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// =====================================================
|
||||
// ABANDONAR SALA - ABRIR MODAL
|
||||
// =====================================================
|
||||
|
||||
$(document).on(
|
||||
"click",
|
||||
".btn-abandonar-sala",
|
||||
function () {
|
||||
botonAbandonarReferencia = $(this);
|
||||
$(document).on("click", ".btn-abandonar-sala", function () {
|
||||
botonAbandonarReferencia = $(this);
|
||||
|
||||
const esHost =
|
||||
$(this).data("es-host") === true ||
|
||||
$(this).data("es-host") === "true";
|
||||
const esHost =
|
||||
$(this).data("es-host") === true || $(this).data("es-host") === "true";
|
||||
|
||||
const titulo =
|
||||
$("#modalAbandonarSalaLabel");
|
||||
const titulo = $("#modalAbandonarSalaLabel");
|
||||
|
||||
const mensaje =
|
||||
$("#modalAbandonarSalaMensaje");
|
||||
const mensaje = $("#modalAbandonarSalaMensaje");
|
||||
|
||||
const botonConfirmar =
|
||||
$("#btnConfirmarAbandonar");
|
||||
const botonConfirmar = $("#btnConfirmarAbandonar");
|
||||
|
||||
if (esHost) {
|
||||
titulo.text("¿Abandonar la sala?");
|
||||
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"
|
||||
)
|
||||
mensaje.text(
|
||||
"Si abandonas la sala como anfitrión, la partida terminará para todos los jugadores.",
|
||||
);
|
||||
|
||||
modal.show();
|
||||
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"
|
||||
);
|
||||
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
||||
const modalEl = document.getElementById("modalAbandonarSala");
|
||||
|
||||
const modalInstance =
|
||||
bootstrap.Modal.getInstance(modalEl);
|
||||
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
||||
|
||||
const btnConfirmar = $(this);
|
||||
const btnConfirmar = $(this);
|
||||
|
||||
btnConfirmar
|
||||
.prop("disabled", true)
|
||||
.text("Saliendo...");
|
||||
btnConfirmar.prop("disabled", true).text("Saliendo...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/controladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
$.ajax({
|
||||
url: "controladores/controladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "salirDeSala",
|
||||
},
|
||||
data: {
|
||||
orden: "salirDeSala",
|
||||
},
|
||||
|
||||
success: function (respuesta) {
|
||||
btnConfirmar
|
||||
.prop("disabled", false)
|
||||
.text("Sí, salir");
|
||||
success: function (respuesta) {
|
||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
return;
|
||||
}
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
return;
|
||||
}
|
||||
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
if (intervaloSala) {
|
||||
clearInterval(intervaloSala);
|
||||
intervaloSala = null;
|
||||
}
|
||||
if (intervaloSala) {
|
||||
clearInterval(intervaloSala);
|
||||
intervaloSala = null;
|
||||
}
|
||||
|
||||
$("#contenedorCrearSala").show();
|
||||
$("#contenedorCrearSala").show();
|
||||
|
||||
cargarSalas();
|
||||
},
|
||||
cargarSalas();
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error(
|
||||
"Error al salir de la sala:",
|
||||
error
|
||||
);
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al salir de la sala:", error);
|
||||
|
||||
alert(
|
||||
"No se pudo salir de la sala."
|
||||
);
|
||||
alert("No se pudo salir de la sala.");
|
||||
|
||||
btnConfirmar
|
||||
.prop("disabled", false)
|
||||
.text("Sí, salir");
|
||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// =====================================================
|
||||
// AUXILIARES
|
||||
@@ -581,29 +501,19 @@ function escapeHTML(texto) {
|
||||
.html();
|
||||
}
|
||||
|
||||
function mostrarEquipos(
|
||||
partida,
|
||||
equipos,
|
||||
jugadores
|
||||
) {
|
||||
function mostrarEquipos(partida, equipos, jugadores) {
|
||||
let equiposHTML = "";
|
||||
|
||||
equipos.forEach(function (equipo) {
|
||||
const jugadoresEquipo =
|
||||
jugadores.filter(function (jugador) {
|
||||
return (
|
||||
jugador.idEquipo ==
|
||||
equipo.idEquipo
|
||||
);
|
||||
});
|
||||
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;
|
||||
const esHost = jugador.idUsuario == partida.idUsuario;
|
||||
|
||||
jugadoresHTML += `
|
||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
||||
@@ -619,9 +529,7 @@ function mostrarEquipos(
|
||||
}
|
||||
</span>
|
||||
|
||||
${escapeHTML(
|
||||
jugador.nombreUsuario
|
||||
)}
|
||||
${escapeHTML(jugador.nombreUsuario)}
|
||||
</div>
|
||||
|
||||
${
|
||||
@@ -647,9 +555,7 @@ function mostrarEquipos(
|
||||
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||
|
||||
<h6>
|
||||
${escapeHTML(
|
||||
equipo.nombreEquipo
|
||||
)}
|
||||
${escapeHTML(equipo.nombreEquipo)}
|
||||
</h6>
|
||||
|
||||
<div class="jugadores mb-2">
|
||||
@@ -660,6 +566,5 @@ function mostrarEquipos(
|
||||
`;
|
||||
});
|
||||
|
||||
$("#contenedorSalas .equipos")
|
||||
.html(equiposHTML);
|
||||
$("#contenedorSalas .equipos").html(equiposHTML);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user