fix ranking order + display winning gifs
This commit is contained in:
@@ -8,10 +8,8 @@ from django.shortcuts import get_object_or_404
|
||||
from accounts.models import CustomUser
|
||||
from animations.schemas import (
|
||||
RankingSchema,
|
||||
TournamentWinnersOut,
|
||||
TournamentSubmissionsOut,
|
||||
TournamentPuzzleResultsOut,
|
||||
PuzzleWinnerOut,
|
||||
PuzzleSubmissionsOut,
|
||||
PuzzleResultsOut,
|
||||
PuzzleSubmissionWithRankOut,
|
||||
@@ -62,72 +60,6 @@ def results(request: HttpRequest) -> dict:
|
||||
return data
|
||||
|
||||
|
||||
@router.get("winners", response=TournamentWinnersOut)
|
||||
def winners(request: HttpRequest) -> TournamentWinnersOut:
|
||||
"""Get tournament winners with submission file URLs. Only available when tournament is closed."""
|
||||
collection = get_object_or_404(SteamCollection, is_active=True)
|
||||
|
||||
# Only allow access when tournament is closed
|
||||
if collection.accepting_submissions:
|
||||
raise HttpError(403, "Tournament is still accepting submissions")
|
||||
|
||||
# Get all puzzles
|
||||
puzzles = SteamCollectionItem.objects.filter(collection=collection).order_by("order_index")
|
||||
|
||||
# Get best response for each puzzle
|
||||
winners_by_puzzle = {}
|
||||
for puzzle in puzzles:
|
||||
# Get the best response for this puzzle (ranked by points)
|
||||
best_response = (
|
||||
PuzzleResponse.objects
|
||||
.filter(puzzle=puzzle, needs_manual_validation=False)
|
||||
.filter_user_best_response()
|
||||
.annotate_rank_points()
|
||||
.order_by("rank_points")
|
||||
.first()
|
||||
)
|
||||
|
||||
if best_response:
|
||||
winners_by_puzzle[puzzle.id] = best_response
|
||||
|
||||
# Build response
|
||||
winners_list = []
|
||||
for puzzle in puzzles:
|
||||
best_response = winners_by_puzzle.get(puzzle.id)
|
||||
|
||||
winner_data = None
|
||||
if best_response:
|
||||
# Get submission files
|
||||
files = best_response.files.all()
|
||||
winner_files = [
|
||||
WinnerFileOut(
|
||||
file_url=file.file_url or "",
|
||||
original_filename=file.original_filename
|
||||
)
|
||||
for file in files
|
||||
]
|
||||
|
||||
winner_data = WinnerResponseOut(
|
||||
user_id=best_response.submission.user.id if best_response.submission.user else 0,
|
||||
username=best_response.submission.user.username if best_response.submission.user else "Anonymous",
|
||||
final_cost=best_response.final_cost,
|
||||
final_cycles=best_response.final_cycles,
|
||||
final_area=best_response.final_area,
|
||||
rank_points=best_response.rank_points,
|
||||
files=winner_files,
|
||||
)
|
||||
|
||||
winners_list.append(
|
||||
PuzzleWinnerOut(
|
||||
puzzle_id=puzzle.id,
|
||||
puzzle_title=puzzle.title,
|
||||
winner=winner_data,
|
||||
)
|
||||
)
|
||||
|
||||
return TournamentWinnersOut(winners=winners_list)
|
||||
|
||||
|
||||
@router.get("top-submissions", response=TournamentSubmissionsOut)
|
||||
def top_submissions(request: HttpRequest, limit: int = 5) -> TournamentSubmissionsOut:
|
||||
"""Get tournament top submissions for each puzzle. Only available when tournament is closed."""
|
||||
@@ -143,13 +75,13 @@ def top_submissions(request: HttpRequest, limit: int = 5) -> TournamentSubmissio
|
||||
# Build response
|
||||
submissions_list = []
|
||||
for puzzle in puzzles:
|
||||
# Get the top N responses for this puzzle (ranked by points)
|
||||
# Get the top N responses for this puzzle (ranked by points, highest first)
|
||||
top_responses = (
|
||||
PuzzleResponse.objects
|
||||
.filter(puzzle=puzzle, needs_manual_validation=False)
|
||||
.filter_user_best_response()
|
||||
.annotate_rank_points()
|
||||
.order_by("rank_points")[:limit]
|
||||
.order_by("-rank_points")[:limit]
|
||||
)
|
||||
|
||||
# Build submission list for this puzzle
|
||||
@@ -218,7 +150,7 @@ def puzzle_results(request: HttpRequest, limit: int = 5) -> TournamentPuzzleResu
|
||||
.filter(puzzle=puzzle, needs_manual_validation=False)
|
||||
.filter_user_best_response()
|
||||
.annotate_rank_points()
|
||||
.order_by("rank_points")[:limit]
|
||||
.order_by("-rank_points")[:limit]
|
||||
)
|
||||
|
||||
# Build submission list for this puzzle with rank
|
||||
|
||||
@@ -64,14 +64,6 @@ class WinnerResponseOut(Schema):
|
||||
files: List[WinnerFileOut]
|
||||
|
||||
|
||||
class PuzzleWinnerOut(Schema):
|
||||
"""Schema for puzzle with winner info"""
|
||||
|
||||
puzzle_id: int
|
||||
puzzle_title: str
|
||||
winner: Optional[WinnerResponseOut]
|
||||
|
||||
|
||||
class PuzzleSubmissionsOut(Schema):
|
||||
"""Schema for puzzle with all top submissions"""
|
||||
|
||||
@@ -80,12 +72,6 @@ class PuzzleSubmissionsOut(Schema):
|
||||
submissions: List[WinnerResponseOut]
|
||||
|
||||
|
||||
class TournamentWinnersOut(Schema):
|
||||
"""Schema for tournament winners results"""
|
||||
|
||||
winners: List[PuzzleWinnerOut]
|
||||
|
||||
|
||||
class TournamentSubmissionsOut(Schema):
|
||||
"""Schema for tournament top submissions results"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user