diff --git a/statsservice/model/stats-getter.js b/statsservice/model/stats-getter.js
index cf47411d..04d2ccae 100644
--- a/statsservice/model/stats-getter.js
+++ b/statsservice/model/stats-getter.js
@@ -58,16 +58,39 @@ class StatsForUser {
ratioCorrect: newRatioCorrect,
avgTime: newAvgTime
};
- }
+ };
- async getRanking(gamemode) {
+ async getRanking(gamemode, filterBy) {
try {
- var stats = await Stats.find({gamemode:gamemode}).sort({ avgPoints: -1 }).limit(10);
+ let sortBy, displayField;
+
+ switch (filterBy) {
+ case "avgPoints":
+ sortBy = { avgPoints: -1 };
+ displayField = "avgPoints";
+ break;
+ case "totalPoints":
+ sortBy = { totalPoints: -1 };
+ displayField = "totalPoints";
+ break;
+ case "ratioCorrect":
+ sortBy = { ratioCorrect: -1 };
+ displayField = "ratioCorrect";
+ break;
+ case "avgTime":
+ sortBy = { avgTime: -1 };
+ displayField = "avgTime";
+ break;
+ default:
+ return null;
+ }
+
+ const stats = await Stats.find({ gamemode: gamemode }).sort(sortBy).limit(10);
if (stats && stats.length > 0) {
return stats.map(stat => ({
username: stat.username,
- avgPoints: stat.avgPoints
+ [displayField]: stat[displayField]
}));
} else {
return null;
@@ -78,6 +101,7 @@ class StatsForUser {
}
}
+
}
module.exports = StatsForUser;
diff --git a/statsservice/stats-service.js b/statsservice/stats-service.js
index 88ee82be..b0c3c2e9 100644
--- a/statsservice/stats-service.js
+++ b/statsservice/stats-service.js
@@ -88,7 +88,7 @@ app.get("/stats", async (req, res) => {
app.get("/ranking", async (req, res) => {
try {
- var data = await statsGetter.getRanking(req.query.gamemode);
+ var data = await statsGetter.getRanking(req.query.gamemode,req.query.filterBy);
res.json(data);
} catch (error) {
diff --git a/webapp/src/App.js b/webapp/src/App.js
index dee6ef63..08eab810 100644
--- a/webapp/src/App.js
+++ b/webapp/src/App.js
@@ -5,6 +5,7 @@ import Clasico from "./pages/Clasico/Clasico.js";
import Bateria from "./pages/Bateria/Bateria.js";
import WrongRoute from "./pages/WrongRoute/WrongRoute.js";
import Stats from "./pages/Stats/Stats.js";
+import Ranking from "./pages/Ranking/Ranking.js";
import "./App.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { ProtectedRoute } from "./routers/ProtectedRoute.js";
@@ -26,6 +27,7 @@ function App() {
Se está consultando el ranking, espere unos instantes.
+Ha ocurrido un error al obtener el ranking
+Usuario | +{getDisplayedField()} | +
---|---|
{stat.username} | +{getDisplayValue(stat)} | +
Usuario | -Puntos promedio | -
---|---|
{stat.username} | -{stat.avgPoints.toFixed(2)} | -