Skip to content

Commit

Permalink
Merge pull request #154 from Arquisoft/ranking-fixes
Browse files Browse the repository at this point in the history
Ranking fixes
  • Loading branch information
iyanfdezz authored Apr 27, 2024
2 parents 30e84bc + 2c83683 commit 8655622
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
14 changes: 7 additions & 7 deletions statsservice/stats-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ app.post("/saveGame", async (req, res) => {
);
}

res.status(200).json({ message: "Partida guardada exitosamente" });
res.status(200).json({ message: "Game saved successfully" });
} else {
throw new Error("Invalid game mode");
}
} catch (error) {
res.status(400).json({ error: "Error al guardar juego: " + error.message });
res.status(400).json({ error: "Error while saving game: " + error.message });
}
});

Expand All @@ -121,7 +121,7 @@ app.get("/stats", async (req, res) => {
} catch (error) {
res
.status(400)
.json({ error: "Error al obtener las estadísticas:" + error.message });
.json({ error: "Error getting stats:" + error.message });
}
});

Expand All @@ -131,23 +131,23 @@ app.get("/ranking", async (req, res) => {
let gamemode = req.query.gamemode;

let data = await Stats.find({ gamemode: gamemode })
.sort(sortBy)
.limit(10);
.sort(sortBy === 'avgTime' ? { [sortBy]: 1 } : { [sortBy]: -1 })
.limit(10);

if (data && data.length > 0) {
data = data.map((stat) => ({
username: stat.username,
[sortBy]: stat[sortBy],
}));
} else {
throw new Error("No se encontraron estadísticas");
throw new Error("No stats found");
}

res.status(200).json(data);
} catch (error) {
res
.status(400)
.json({ error: "Error al obtener el ranking: " + error.message });
.json({ error: "Error getting ranking: " + error.message });
}
});

Expand Down
3 changes: 2 additions & 1 deletion webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
"ratioCorrect": "Correct Ratio (%)",
"avgTime": "Time per Question (s):",
"reboot": "Reset to Default",
"errorText": "An error occurred while retrieving the ranking"
"errorText": "An error occurred while retrieving the ranking",
"noStats":"No stats found for this gamemode"
},
"about": {
"title": "WIQ_es1a Team",
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
"ratioCorrect": "Ratio de aciertos (%)",
"avgTime": "Tiempo por pregunta (s):",
"reboot": "Restablecer por defecto",
"errorText": "Ha ocurrido un error al obtener el ranking"
"errorText": "Ha ocurrido un error al obtener el ranking",
"noStats":"No hay estadísticas para este modo de juego"
},
"about": {
"title": "Equipo WIQ_es1a",
Expand Down
28 changes: 19 additions & 9 deletions webapp/src/pages/Ranking/Ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const Ranking = () => {
fetch(gatewayUrl + `/ranking?gamemode=${gamemode}&filterBy=${filterBy}`)
.then((response) => response.json())
.then((data) => {

setRanking(data);
setIsLoading(false);
})
Expand Down Expand Up @@ -110,13 +109,13 @@ const Ranking = () => {
if (error) {
return (
<>
<Nav/>
<div>
<Heading as="h2">{t('pages.ranking.error')} {error}</Heading>
<p>{t('pages.ranking.errorLabel')}</p>
</div>
<Footer/>
</>
<Nav/>
<div>
<Heading as="h2">{t('pages.ranking.errorText')}</Heading>
<p>{error}</p>
</div>
<Footer/>
</>
);
}

Expand Down Expand Up @@ -154,7 +153,9 @@ const Ranking = () => {
>
{t('pages.ranking.humancalculator')}
</Button>
<Table>
{ranking && ranking.length>0 ? (
<>
<Table>
<Thead>
<Tr>
<Th>{t('pages.ranking.user')}</Th>
Expand All @@ -170,6 +171,15 @@ const Ranking = () => {
))}
</Tbody>
</Table>
</>
):
<>
<div>
<Heading as="h2">{t('pages.ranking.errorText')}</Heading>
<p>{t('pages.ranking.noStats')}</p>
</div>
</>}

</Flex>
<Footer/>
</>
Expand Down

0 comments on commit 8655622

Please sign in to comment.