Skip to content

Commit 9fbf309

Browse files
Use Usernames when team name isn't defined
1 parent cced0a4 commit 9fbf309

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

gameserver/api/routes.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import datetime
22
from typing import Any, List
33

4-
from django.db.models import F, OuterRef, Max
4+
from django.db.models import F, OuterRef, Max, Subquery, Case, When, Value, BooleanField, TextField
55
from django.shortcuts import get_object_or_404
66
from ninja import NinjaAPI, Schema
77

88
from gameserver.models.cache import ContestScore
9-
from gameserver.models.contest import Contest, ContestProblem, ContestSubmission
9+
from gameserver.models.profile import User
10+
from gameserver.models.contest import Contest, ContestProblem, ContestSubmission, ContestParticipation
1011

1112

1213
def unicode_safe(string):
@@ -33,11 +34,12 @@ class CTFSchema(Schema):
3334
lastAccept: Any = None
3435

3536
@staticmethod
36-
def resolve_lastAccept(obj) -> int:
37+
def resolve_lastAccept(obj: dict) -> int:
3738
"""Turns a datetime object into a timestamp."""
38-
if obj["lastAccept"] is None:
39+
print(obj, ' - DEBUG PRINT')
40+
if obj['lastAccept'] is None:
3941
return 0
40-
return int(obj["lastAccept"].timestamp())
42+
return int(obj['lastAccept'].timestamp())
4143

4244
@staticmethod
4345
def resolve_team(obj):
@@ -61,20 +63,26 @@ def ctftime_standings(request, contest_name: str):
6163
.order_by("-submission__date_created")
6264
.values("submission__date_created")
6365
)
66+
6467
standings = (
6568
ContestScore.ranks(contest=contest_id)
6669
.annotate(
6770
pos=F("rank"),
6871
score=F("points"),
69-
team=F("participation__team__name"),
72+
is_solo=Case(
73+
When(participation__team_id=None, then=Value(False)),
74+
default=Value(True),
75+
output_field=BooleanField(),
76+
),
77+
team=Case(
78+
When(participation__team_id=None, then=Subquery( # If the team is None, use the username of the participant ( solo player )
79+
User.objects.filter(contest_participations=OuterRef("participation_id")).values(
80+
"username")[:1]
81+
),),
82+
default=F("participation__team__name"),
83+
output_field=TextField(),
84+
),
7085
lastAccept=Max("participation__submission__submission__date_created"),
71-
# team=Coalesce(F("participation__team__name"), F("participation__participants__username")),
72-
# Using Coalesce and indexing
73-
# team=Case(
74-
# When(F("participation__team__isnull")==True, then=Q(("participation__participants")[0]["username"])),
75-
# default=F("team_name"),
76-
# output_field=TextField(),
77-
# ),
7886
)
7987
.values("pos", "score", "team", "lastAccept")
8088
)

0 commit comments

Comments
 (0)