test contest

This commit is contained in:
2026-08-09 15:33:07 +08:00
parent ebd6070aae
commit ee006fba18
4 changed files with 356 additions and 8 deletions
+21 -7
View File
@@ -435,6 +435,11 @@ def match_payload(match, user):
if reveal_results and opponent_attempt
else None
),
"duration_ms": (
opponent_attempt.duration_ms
if reveal_results and opponent_attempt
else None
),
}
if opponent
else None
@@ -481,12 +486,13 @@ def refresh_match_state(match_id):
elif match.status == RealtimeMatch.Status.ACTIVE and match.started_at:
deadline = match.started_at + timedelta(seconds=match.contest.duration_seconds)
if timezone.now() >= deadline:
elapsed_ms = match.contest.duration_seconds * 1000
match.attempts.filter(status=ContestAttempt.Status.ACTIVE).update(
status=ContestAttempt.Status.EXPIRED,
duration_ms=elapsed_ms,
submitted_at=timezone.now(),
)
now = timezone.now()
for attempt in match.attempts.filter(status=ContestAttempt.Status.ACTIVE):
elapsed_ms = max(0, int((now - attempt.started_at).total_seconds() * 1000))
attempt.status = ContestAttempt.Status.EXPIRED
attempt.duration_ms = elapsed_ms
attempt.submitted_at = now
attempt.save(update_fields=["status", "duration_ms", "submitted_at"])
match = finalize_match(match.id)
return match
@@ -516,7 +522,15 @@ def finalize_match(match_id):
first_result, second_result = 0.0, 1.0
match.winner_id = second.user_id
else:
first_result = second_result = 0.5
diff = first.duration_ms - second.duration_ms
if abs(diff) <= 100:
first_result = second_result = 0.5
elif diff < 0:
first_result, second_result = 1.0, 0.0
match.winner_id = first.user_id
else:
first_result, second_result = 0.0, 1.0
match.winner_id = second.user_id
users = {
user.id: user