fix: complete v1.2 account and profile P0 fixes
This commit is contained in:
@@ -1,2 +1,60 @@
|
||||
|
||||
# Create your tests here.
|
||||
import pytest
|
||||
from django.utils import timezone
|
||||
|
||||
from accounts.models import User
|
||||
from contest.models import Contest, Question, RatingHistory, RealtimeMatch
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_profile_返回实时比赛历史及_rating_变化(client):
|
||||
user = User.objects.create_user(
|
||||
username="profile_player",
|
||||
password="StrongPass_2026",
|
||||
nickname="档案玩家",
|
||||
rating=1016,
|
||||
)
|
||||
opponent = User.objects.create_user(
|
||||
username="profile_opponent",
|
||||
password="StrongPass_2026",
|
||||
nickname="对局对手",
|
||||
rating=984,
|
||||
)
|
||||
contest = Contest.objects.create(
|
||||
slug="profile-realtime",
|
||||
title="档案实时赛",
|
||||
kind=Contest.Kind.REALTIME,
|
||||
track=Question.Track.STANDARD,
|
||||
status=Contest.Status.PUBLISHED,
|
||||
)
|
||||
match = RealtimeMatch.objects.create(
|
||||
contest=contest,
|
||||
player_one=user,
|
||||
player_two=opponent,
|
||||
player_one_rating=1000,
|
||||
player_two_rating=1000,
|
||||
winner=user,
|
||||
status=RealtimeMatch.Status.COMPLETED,
|
||||
completed_at=timezone.now(),
|
||||
)
|
||||
RatingHistory.objects.create(
|
||||
user=user,
|
||||
match=match,
|
||||
rating_before=1000,
|
||||
rating_after=1016,
|
||||
delta=16,
|
||||
)
|
||||
client.force_login(user)
|
||||
|
||||
response = client.get("/api/v1/progression/me/")
|
||||
|
||||
assert response.status_code == 200
|
||||
history = response.json()["recent_matches"]
|
||||
assert len(history) == 1
|
||||
assert history[0]["match_id"] == str(match.id)
|
||||
assert history[0]["contest"] == "档案实时赛"
|
||||
assert history[0]["opponent"] == "对局对手"
|
||||
assert history[0]["result"] == "win"
|
||||
assert history[0]["rating_delta"] == 16
|
||||
assert history[0]["rating_after"] == 1016
|
||||
assert history[0]["completed_at"]
|
||||
|
||||
@@ -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