$(document).ready(function () {
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(`
Aún no hay partidas registradas para mostrar el ranking.
`);
}
},
error: function () {
$('#ranking-container').html(`
Error al cargar la clasificación.
`);
}
});
}
function renderizarTablaRanking(jugadores) {
let html = `
| # |
Jugador |
Vic |
Der |
Aciertos |
% Vic |
`;
jugadores.forEach((jugador, index) => {
let posicion = index + 1;
let medalla = '';
// Medallas para el Top 3
if (posicion === 1) {
medalla = '';
} else if (posicion === 2) {
medalla = '';
} else if (posicion === 3) {
medalla = '';
} else {
medalla = `${posicion}`;
}
let avatarImg = jugador.avatar ? jugador.avatar : 'img/avatars/defaultProfile.jpeg';
html += `
| ${medalla} |
${escapeHtml(jugador.nombreUsuario)}
|
${jugador.victorias} |
${jugador.derrotas} |
${jugador.aciertos} |
${jugador.porcentajeVictoria}%
|
`;
});
html += `
`;
$('#ranking-container').removeClass('text-center p-4 ranking-placeholder').html(html);
}
function escapeHtml(texto) {
return $('').text(texto).html();
}