From 7dbd2743d0a5775a8069ecedccbc30998c474e13 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Tue, 19 Mar 2024 10:44:54 +0100 Subject: [PATCH 01/11] =?UTF-8?q?A=C3=B1adiendo=20distintos=20parametros?= =?UTF-8?q?=20al=20ranking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- statsservice/model/stats-getter.js | 28 ++++++++++-- statsservice/stats-service.js | 2 +- webapp/src/pages/Stats/Stats.js | 70 +++++++++++++++++++++--------- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/statsservice/model/stats-getter.js b/statsservice/model/stats-getter.js index cf47411d..2e1981d8 100644 --- a/statsservice/model/stats-getter.js +++ b/statsservice/model/stats-getter.js @@ -58,16 +58,35 @@ 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; + 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 +97,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/pages/Stats/Stats.js b/webapp/src/pages/Stats/Stats.js index c5560ce6..b63a6718 100644 --- a/webapp/src/pages/Stats/Stats.js +++ b/webapp/src/pages/Stats/Stats.js @@ -10,6 +10,7 @@ const Stats = () => { const [stats, setStats] = useState(null); const [ranking, setRanking] = useState(null); const [gamemode, setGamemode] = useState("clasico"); + const [filterBy, setFilterby] = useState("avgPoints"); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); @@ -30,7 +31,7 @@ const Stats = () => { const fetchRanking = () => { setIsLoading(true); - fetch(gatewayUrl+`/ranking?gamemode=${gamemode}`) + fetch(gatewayUrl+`/ranking?gamemode=${gamemode}&filterBy=${filterBy}`) .then((response) => response.json()) .then((data) => { setRanking(data); @@ -62,6 +63,32 @@ const Stats = () => { fetchStats(); }; + const getDisplayedField = () => { + switch (filterBy) { + case "avgPoints": + return "Puntos promedio"; + case "totalPoints": + return "Puntos totales"; + case "ratioCorrect": + return "Ratio de aciertos"; + default: + return ""; + } + } + +const getDisplayValue = (stat) => { + switch (filterBy) { + case "avgPoints": + return stat.avgPoints.toFixed(2); + case "totalPoints": + return stat.totalPoints; + case "ratioCorrect": + return stat.ratioCorrect.toFixed(2); + default: + return ""; + } +} + const getModeName = () => { if(gamemode=="clasico"){ return "Clásico"; @@ -176,26 +203,27 @@ const Stats = () => { )} {ranking && ranking.length > 0 && ( -
-

Ranking - Modo {getModeName()}

- - - - - - - - - {ranking.map((stat, index) => ( - - - - - ))} - -
UsuarioPuntos promedio
{stat.username}{stat.avgPoints.toFixed(2)}
-
- )} +
+

Ranking - Modo {getModeName()}

+ + + + + + + + + {ranking.map((stat, index) => ( + + + + + ))} + +
Usuario{getDisplayedField()}
{stat.username}{getDisplayValue(stat)}
+
+ )} +