Add highscores page

This commit is contained in:
2023-01-05 01:41:09 +01:00
parent c8c33c9aa0
commit 0f1f00c9eb
8 changed files with 187 additions and 8 deletions
+32
View File
@@ -32,6 +32,7 @@ import {
ProblemId,
Solution as SolutionModel,
SolutionId,
SolutionStat,
User as UserModel,
UserId,
} from '../shared/model'
@@ -208,6 +209,37 @@ export async function createApiRouter() {
res.json(id)
})
// questa route ritorna la lista di utenti con associato numero di soluzioni inviate, corrette.
r.get('/api/stats', async (req, res) => {
let queryUser = (req.query.user ?? null) as UserId | null
let solutions = await getSolutions(db)
if (queryUser !== null) {
solutions = solutions.filter(s => s.sentBy === queryUser)
}
const stats: Record<string, SolutionStat> = {}
solutions.forEach(s => {
if (!stats[s.sentBy]) {
stats[s.sentBy] = {
sentSolutionsCount: s.status !== "pending" ? 1 : 0,
correctSolutionsCount: s.status === "correct" ? 1 : 0
}
} else {
if (s.status !== "pending") {
stats[s.sentBy].sentSolutionsCount += 1
}
if (s.status === "correct") {
stats[s.sentBy].correctSolutionsCount += 1
}
}
})
return res.json(stats)
})
r.get('/api/solutions', async (req, res) => {
let queryUser = (req.query.user ?? null) as UserId | null
let queryProblem = (req.query.problem ?? null) as ProblemId | null