-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/mcpt/ctf
- Loading branch information
Showing
12 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,75 @@ | ||
from django.db.models import F, OuterRef, Subquery | ||
from django.db.models import F, OuterRef, Subquery, Case, When, Q | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from gameserver.models.cache import ContestScore | ||
from gameserver.models.contest import ContestProblem, ContestSubmission | ||
from gameserver.models.contest import ContestProblem, ContestSubmission, Contest | ||
from ninja import NinjaAPI, Schema | ||
from typing import List, Any | ||
|
||
|
||
def unicode_safe(string): | ||
return string.encode("unicode_escape").decode() | ||
|
||
|
||
|
||
api = NinjaAPI() | ||
|
||
|
||
class CTFSchema(Schema): | ||
pos: int | ||
team: str | ||
team: Any = None | ||
score: int | ||
lastAccept: Any | ||
|
||
lastAccept: Any = None | ||
|
||
@staticmethod | ||
def resolve_lastAccept(obj) -> int: | ||
"""Turns a datetime object into a timestamp.""" | ||
if obj["lastAccept"] is None: | ||
return 0 | ||
return int(obj["lastAccept"].timestamp()) | ||
|
||
@staticmethod | ||
def resolve_team(obj): | ||
return unicode_safe(obj["team"]) | ||
|
||
|
||
class CTFTimeSchema(Schema): | ||
standings: List[CTFSchema] | ||
tasks: List[str] | ||
|
||
@api.get("/ctftime", response=CTFTimeSchema) | ||
def add(request, contest_id: int): | ||
last_sub_time = ContestSubmission.objects.filter( | ||
participation=OuterRef("pk"), submission__is_correct=True | ||
).values("submission__date_created") | ||
standings = ContestScore.ranks(contest=contest_id).annotate(pos=F("rank"), score=F("points"), team=F("participation__team__name"), lastAccept=Subquery(last_sub_time)) | ||
# .only("pos", "team", "score") | ||
task_names = ContestProblem.objects.filter(contest_id=contest_id).prefetch_related("problem__name").values_list("problem__name") | ||
|
||
return {"standings": standings, "tasks": task_names} | ||
|
||
|
||
@api.get("/ctftime/{contest_name}", response=CTFTimeSchema) | ||
def ctftime_standings(request, contest_name: str): | ||
contest_id = get_object_or_404(Contest, name__iexact=contest_name).id | ||
"""Get the standings for a contest in CTFTime format.""" | ||
last_sub_time = ( | ||
ContestSubmission.objects.filter( | ||
participation_id=OuterRef("participation_id"), submission__is_correct=True | ||
) | ||
.prefetch_related("submission") | ||
.order_by("-submission__date_created") | ||
.values("submission__date_created") | ||
) | ||
standings = ( | ||
ContestScore.ranks(contest=contest_id) | ||
.annotate( | ||
pos=F("rank"), | ||
score=F("points"), | ||
team=F("participation__team__name"), | ||
# team=Coalesce(F("participation__team__name"), F("participation__participants__username")), | ||
# Using Coalesce and indexing | ||
# team=Case( | ||
# When(F("participation__team__isnull")==True, then=Q(("participation__participants")[0]["username"])), | ||
# default=F("team_name"), | ||
# output_field=TextField(), | ||
# ), | ||
lastAccept=Subquery(last_sub_time), | ||
) | ||
.values("pos", "score", "team", "lastAccept") | ||
) | ||
task_names = ( | ||
ContestProblem.objects.filter(contest_id=contest_id) | ||
.prefetch_related("problem__name") | ||
.values_list("problem__name", flat=True) | ||
) | ||
|
||
return {"standings": standings, "tasks": task_names} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,5 +112,5 @@ | |
views.add_comment, | ||
name="add_comment", | ||
), | ||
path("api/", api.urls) | ||
path("api/", api.urls), | ||
] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ def get_mocked_response(self): | |
} | ||
} | ||
""", | ||
) # noqa | ||
) # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters