fix: complete v1.2 account and profile P0 fixes

This commit is contained in:
2026-08-10 00:12:53 +08:00
parent 587962f9c9
commit 97ca20413e
14 changed files with 346 additions and 12 deletions
+52
View File
@@ -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,
}
)