fix: complete v1.2 account and profile P0 fixes
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
from django.db.models import Prefetch, Q
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from contest.models import RatingHistory, RealtimeMatch
|
||||
|
||||
from .models import UserAbility, UserPet
|
||||
|
||||
|
||||
@@ -21,6 +24,54 @@ class ProgressionProfileView(APIView):
|
||||
"fragments": ability.fragments,
|
||||
}
|
||||
)
|
||||
matches = (
|
||||
RealtimeMatch.objects.filter(
|
||||
Q(player_one=request.user) | Q(player_two=request.user),
|
||||
status=RealtimeMatch.Status.COMPLETED,
|
||||
)
|
||||
.select_related("contest", "player_one", "player_two", "winner")
|
||||
.prefetch_related(
|
||||
Prefetch(
|
||||
"rating_changes",
|
||||
queryset=RatingHistory.objects.filter(user=request.user),
|
||||
to_attr="viewer_rating_changes",
|
||||
)
|
||||
)
|
||||
.order_by("-completed_at")[:10]
|
||||
)
|
||||
recent_matches = []
|
||||
for match in matches:
|
||||
opponent = (
|
||||
match.player_two
|
||||
if match.player_one_id == request.user.id
|
||||
else match.player_one
|
||||
)
|
||||
rating_change = (
|
||||
match.viewer_rating_changes[0]
|
||||
if match.viewer_rating_changes
|
||||
else None
|
||||
)
|
||||
recent_matches.append(
|
||||
{
|
||||
"match_id": match.id,
|
||||
"contest": match.contest.title,
|
||||
"opponent": opponent.nickname if opponent else "未知对手",
|
||||
"result": (
|
||||
"draw"
|
||||
if match.winner_id is None
|
||||
else "win"
|
||||
if match.winner_id == request.user.id
|
||||
else "loss"
|
||||
),
|
||||
"rating_delta": rating_change.delta if rating_change else 0,
|
||||
"rating_after": (
|
||||
rating_change.rating_after
|
||||
if rating_change
|
||||
else request.user.rating
|
||||
),
|
||||
"completed_at": match.completed_at,
|
||||
}
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"pet": {
|
||||
@@ -52,5 +103,6 @@ class ProgressionProfileView(APIView):
|
||||
}
|
||||
for attempt in request.user.math_game_attempts.all()[:10]
|
||||
],
|
||||
"recent_matches": recent_matches,
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user