diff --git a/clases/ClaseUsuario.php b/clases/ClaseUsuario.php index a180469..47a8a1f 100644 --- a/clases/ClaseUsuario.php +++ b/clases/ClaseUsuario.php @@ -383,4 +383,37 @@ class Usuario ]; } } + + public static function obtenerRanking($limite = 10) { + try { + $conexion = conexionBD(); + + if (is_array($conexion)) { + return []; + } + + + $sql = "SELECT + idUsuario, + nombreUsuario, + avatar, + aciertos, + victorias, + derrotas, + CASE + WHEN (victorias + derrotas) > 0 + THEN ROUND((victorias / (victorias + derrotas)) * 100, 0) + ELSE 0 + END AS porcentajeVictoria + FROM usuario + ORDER BY victorias DESC, aciertos DESC + LIMIT " . (int)$limite; + + $stmt = $conexion->prepare($sql); + $stmt->execute(); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + return []; + } +} } diff --git a/controladores/ControladorUsuario.php b/controladores/ControladorUsuario.php index cfc5206..98daa4f 100644 --- a/controladores/ControladorUsuario.php +++ b/controladores/ControladorUsuario.php @@ -221,4 +221,12 @@ switch ($_POST['orden']) { } break; + + case 'obtenerRanking': + $ranking = Usuario::obtenerRanking(5); //Cambiar para mostrar X jugadores en index + echo json_encode([ + 'exito' => true, + 'ranking' => $ranking + ]); + break; } diff --git a/index.php b/index.php index 8a52c84..b879e8d 100644 --- a/index.php +++ b/index.php @@ -12,6 +12,7 @@ include 'cabecera.php';