Manejo de cookies
This commit is contained in:
+21
-95
@@ -2,9 +2,11 @@ $(document).ready(function () {
|
||||
comprobarSesion();
|
||||
});
|
||||
|
||||
// Función auxiliar para escapar texto HTML y evitar XSS
|
||||
// Función auxiliar para escapar texto HTML
|
||||
function escapeHTML(str) {
|
||||
return $("<div>").text(str ?? "").html();
|
||||
return $("<div>")
|
||||
.text(str ?? "")
|
||||
.html();
|
||||
}
|
||||
|
||||
// Comprobar estado de sesión al cargar la página
|
||||
@@ -23,12 +25,16 @@ function comprobarSesion() {
|
||||
contenedor.empty();
|
||||
|
||||
if (respuesta.activa && respuesta.usuario) {
|
||||
// Usuario autenticado: Mostrar Menú Dropdown
|
||||
// Obtenemos la ruta del avatar o la de por defecto
|
||||
const avatarUrl =
|
||||
respuesta.usuario.avatar || "img/avatars/defaultProfile.jpeg";
|
||||
|
||||
// Usuario autenticado: Mostrar Menú Dropdown con el avatar
|
||||
const htmlUsuario = `
|
||||
<div class="dropdown">
|
||||
<button class="btn user-dropdown-btn dropdown-toggle d-flex align-items-center gap-2"
|
||||
type="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<i class="fa-solid fa-circle-user fa-lg text-warning"></i>
|
||||
<img src="${escapeHTML(avatarUrl)}" alt="Avatar" class="rounded-circle" style="width: 32px; height: 32px; object-fit: cover;">
|
||||
<span>${escapeHTML(respuesta.usuario.nombreUsuario)}</span>
|
||||
</button>
|
||||
<ul class="dropdown-menu dropdown-menu-end user-dropdown-menu" aria-labelledby="userDropdown">
|
||||
@@ -51,9 +57,10 @@ function comprobarSesion() {
|
||||
// Si estamos en la página principal, mostramos el botón JUGAR
|
||||
if ($("#colBotonJugar").length) {
|
||||
$("#colBotonJugar").removeClass("d-none");
|
||||
$("#colRanking").removeClass("col-lg-8").addClass("col-lg-7 col-xl-8");
|
||||
$("#colRanking")
|
||||
.removeClass("col-lg-8")
|
||||
.addClass("col-lg-7 col-xl-8");
|
||||
}
|
||||
|
||||
} else {
|
||||
// No hay sesión: Mostrar botones de Login y Registro
|
||||
const htmlInvitado = `
|
||||
@@ -69,7 +76,9 @@ function comprobarSesion() {
|
||||
// Si estamos en la página principal, ocultamos el botón JUGAR
|
||||
if ($("#colBotonJugar").length) {
|
||||
$("#colBotonJugar").addClass("d-none");
|
||||
$("#colRanking").removeClass("col-lg-7 col-xl-8").addClass("col-lg-8");
|
||||
$("#colRanking")
|
||||
.removeClass("col-lg-7 col-xl-8")
|
||||
.addClass("col-lg-8");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -78,7 +87,7 @@ function comprobarSesion() {
|
||||
});
|
||||
}
|
||||
|
||||
// Evento Registro
|
||||
// Registro
|
||||
$("#botonAceptarRegistro").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -131,12 +140,13 @@ $("#botonAceptarRegistro").click(function (event) {
|
||||
});
|
||||
});
|
||||
|
||||
// Evento Login
|
||||
// Login
|
||||
$("#botonAceptarLogin").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
let email = $("#loginEmail").val().trim();
|
||||
let contrasena = $("#loginPassword").val();
|
||||
let recuerdame = $("#recordar").is(":checked");
|
||||
|
||||
$("#loginEmail").removeClass("is-invalid");
|
||||
$("#loginPassword").removeClass("is-invalid");
|
||||
@@ -155,6 +165,7 @@ $("#botonAceptarLogin").click(function (event) {
|
||||
data: {
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
recuerdame: recuerdame,
|
||||
orden: "comprobarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
@@ -170,7 +181,7 @@ $("#botonAceptarLogin").click(function (event) {
|
||||
});
|
||||
});
|
||||
|
||||
// Evento Cerrar sesión (usa delegación de eventos por crearse dinámicamente)
|
||||
// Cerrar sesión
|
||||
$(document).on("click", "#botonCerrarSesion", function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -188,88 +199,3 @@ $(document).on("click", "#botonCerrarSesion", function (event) {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Registro
|
||||
$("#botonAceptarRegistro").click(function (event) {
|
||||
event.preventDefault();
|
||||
let nombreUsuario = $("#regNombre").val().trim();
|
||||
let email = $("#regEmail").val().trim();
|
||||
let contrasena = $("#regPassword").val();
|
||||
let contrasenaConfirmacion = $("#regPasswordConfirm").val();
|
||||
|
||||
$("#regNombre").removeClass("is-invalid");
|
||||
$("#regEmail").removeClass("is-invalid");
|
||||
$("#regPassword").removeClass("is-invalid");
|
||||
$("#regPasswordConfirm").removeClass("is-invalid");
|
||||
$("#errorRegistro").text("");
|
||||
|
||||
if (contrasena !== contrasenaConfirmacion) {
|
||||
$("#regPassword").addClass("is-invalid");
|
||||
$("#regPasswordConfirm").addClass("is-invalid");
|
||||
$("#errorRegistro").text("Las contraseñas no coinciden.");
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
nombreUsuario: nombreUsuario,
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
orden: "registrarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
$("#errorRegistro").text(respuesta.error);
|
||||
$("#regNombre").addClass("is-invalid");
|
||||
$("#regEmail").addClass("is-invalid");
|
||||
} else {
|
||||
$("#errorRegistro")
|
||||
.removeClass("text-danger")
|
||||
.addClass("text-success")
|
||||
.text(respuesta.exito);
|
||||
|
||||
$("#formRegistro")[0].reset();
|
||||
|
||||
setTimeout(function () {
|
||||
location.reload();
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Login
|
||||
$("#botonAceptarLogin").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
let email = $("#loginEmail").val().trim();
|
||||
let contrasena = $("#loginPassword").val();
|
||||
|
||||
$("#loginEmail").removeClass("is-invalid");
|
||||
$("#loginPassword").removeClass("is-invalid");
|
||||
$("#errorLogin").text("");
|
||||
|
||||
if (email === "" || contrasena === "") {
|
||||
$("#errorLogin").text("Todos los campos son obligatorios.");
|
||||
if (email === "") $("#loginEmail").addClass("is-invalid");
|
||||
if (contrasena === "") $("#loginPassword").addClass("is-invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: { orden: "cerrarSesion" },
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
console.log(respuesta.error);
|
||||
} else {
|
||||
window.location.href = "index.php";
|
||||
}
|
||||
});
|
||||
});
|
||||
+45
-39
@@ -1,36 +1,36 @@
|
||||
$(document).ready(function () {
|
||||
cargarRanking();
|
||||
cargarRanking();
|
||||
});
|
||||
|
||||
function cargarRanking() {
|
||||
$.ajax({
|
||||
url: 'controladores/controladorUsuario.php',
|
||||
type: 'POST',
|
||||
data: { orden: 'obtenerRanking' },
|
||||
dataType: 'json',
|
||||
success: function (response) {
|
||||
if (response.exito && response.ranking.length > 0) {
|
||||
renderizarTablaRanking(response.ranking);
|
||||
} else {
|
||||
$('#ranking-container').html(`
|
||||
$.ajax({
|
||||
url: "controladores/controladorUsuario.php",
|
||||
type: "POST",
|
||||
data: { orden: "obtenerRanking" },
|
||||
dataType: "json",
|
||||
success: function (response) {
|
||||
if (response.exito && response.ranking.length > 0) {
|
||||
renderizarTablaRanking(response.ranking);
|
||||
} else {
|
||||
$("#ranking-container").html(`
|
||||
<div class="alert alert-info my-3">
|
||||
<i class="fa-solid fa-info-circle me-2"></i>Aún no hay partidas registradas para mostrar el ranking.
|
||||
</div>
|
||||
`);
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
$('#ranking-container').html(`
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
$("#ranking-container").html(`
|
||||
<div class="alert alert-danger my-3">
|
||||
<i class="fa-solid fa-exclamation-triangle me-2"></i>Error al cargar la clasificación.
|
||||
</div>
|
||||
`);
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function renderizarTablaRanking(jugadores) {
|
||||
let html = `
|
||||
let html = `
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover align-middle mb-0 text-center">
|
||||
<thead class="table-dark">
|
||||
@@ -46,24 +46,29 @@ function renderizarTablaRanking(jugadores) {
|
||||
<tbody>
|
||||
`;
|
||||
|
||||
jugadores.forEach((jugador, index) => {
|
||||
let posicion = index + 1;
|
||||
let medalla = '';
|
||||
jugadores.forEach((jugador, index) => {
|
||||
let posicion = index + 1;
|
||||
let medalla = "";
|
||||
|
||||
// Medallas para el Top 3
|
||||
if (posicion === 1) {
|
||||
medalla = '<i class="fa-solid fa-medal text-warning fa-lg" title="1º Puesto"></i>';
|
||||
} else if (posicion === 2) {
|
||||
medalla = '<i class="fa-solid fa-medal text-secondary fa-lg" title="2º Puesto"></i>';
|
||||
} else if (posicion === 3) {
|
||||
medalla = '<i class="fa-solid fa-medal text-brown fa-lg" style="color: #cd7f32;" title="3º Puesto"></i>';
|
||||
} else {
|
||||
medalla = `<span class="fw-bold text-muted">${posicion}</span>`;
|
||||
}
|
||||
// Medallas para el Top 3
|
||||
if (posicion === 1) {
|
||||
medalla =
|
||||
'<i class="fa-solid fa-medal text-warning fa-lg" title="1º Puesto"></i>';
|
||||
} else if (posicion === 2) {
|
||||
medalla =
|
||||
'<i class="fa-solid fa-medal text-secondary fa-lg" title="2º Puesto"></i>';
|
||||
} else if (posicion === 3) {
|
||||
medalla =
|
||||
'<i class="fa-solid fa-medal text-brown fa-lg" style="color: #cd7f32;" title="3º Puesto"></i>';
|
||||
} else {
|
||||
medalla = `<span class="fw-bold text-muted">${posicion}</span>`;
|
||||
}
|
||||
|
||||
let avatarImg = jugador.avatar ? jugador.avatar : 'img/avatars/defaultProfile.jpeg';
|
||||
let avatarImg = jugador.avatar
|
||||
? jugador.avatar
|
||||
: "img/avatars/defaultProfile.jpeg";
|
||||
|
||||
html += `
|
||||
html += `
|
||||
<tr>
|
||||
<td class="fw-bold fs-5">${medalla}</td>
|
||||
<td class="text-start fw-bold">
|
||||
@@ -78,18 +83,19 @@ function renderizarTablaRanking(jugadores) {
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
});
|
||||
|
||||
html += `
|
||||
html += `
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
`;
|
||||
|
||||
$('#ranking-container').removeClass('text-center p-4 ranking-placeholder').html(html);
|
||||
$("#ranking-container")
|
||||
.removeClass("text-center p-4 ranking-placeholder")
|
||||
.html(html);
|
||||
}
|
||||
|
||||
|
||||
function escapeHtml(texto) {
|
||||
return $('<div>').text(texto).html();
|
||||
}
|
||||
return $("<div>").text(texto).html();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user