Skip to content

Commit

Permalink
Added route for round 1 leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
KreativeThinker committed Jan 9, 2024
1 parent 2980eda commit 4fd8f6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pwncore/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import APIRouter

from pwncore.routes import ctf, team, auth, admin
from pwncore.routes import ctf, team, auth, admin, leaderboard
from pwncore.config import config

# Main router (all routes go under /api)
Expand All @@ -10,5 +10,6 @@
router.include_router(auth.router)
router.include_router(ctf.router)
router.include_router(team.router)
router.include_router(leaderboard.router)
if config.development:
router.include_router(admin.router)
4 changes: 4 additions & 0 deletions src/pwncore/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PreEventProblem,
)
from pwncore.config import config
from pwncore.models.ctf import SolvedProblem

metadata = {
"name": "admin",
Expand Down Expand Up @@ -146,3 +147,6 @@ async def init_db():
await Hint.create(order=2, problem_id=1, text="This is the third hint")
await Hint.create(order=0, problem_id=2, text="This is the first hint")
await Hint.create(order=1, problem_id=2, text="This is the second hint")
await SolvedProblem.create(team_id=2, problem_id=1)
await SolvedProblem.create(team_id=2, problem_id=2)
await SolvedProblem.create(team_id=1, problem_id=2)
24 changes: 24 additions & 0 deletions src/pwncore/routes/leaderboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations
from fastapi import APIRouter

from pwncore.models import SolvedProblem, Team, Team_Pydantic

# Metadata at the top for instant accessibility
metadata = {"name": "leaderboard", "description": "Operations on the leaderboard"}

router = APIRouter(prefix="/leaderboard", tags=["leaderboard"])


@router.get("/fetch_leaderboard")
async def fetch_leaderboard():
teams = await Team_Pydantic.from_queryset(Team.all())
# points = await SolvedProblem.filter(team=2).values("problem__points")
leaderboard = {}
for team in teams:
problems_solved_by_team = await SolvedProblem.filter(
team__id=team.id
).values_list("problem__points", flat=True)
leaderboard[team.name] = sum(problems_solved_by_team)
# Uncomment to return sorted leaderboard
# leaderboard = dict(sorted(leaderboard.items(), key=lambda x: x[0], reverse=True))
return leaderboard

0 comments on commit 4fd8f6c

Please sign in to comment.