포인트 시스템 추가, 프론트 국기 반영
This commit is contained in:
1 parent
6ffe0dfca3
commit
1a803b7fa0
58 files changed
+326
-68
No files matched your search
@@ -1,19 +1,18 @@
|
||||
"""리더보드 API — 누적 포인트 1위 (docs/SCORING.md §3 랭킹 규칙).
|
||||
|
||||
user_points 테이블(채점 시 누적 갱신) 기준 랭킹.
|
||||
1차 정렬: 누적 포인트. 동점: 정확스코어 횟수 → 적중률 → 참여수 → 최초도달.
|
||||
주최측/임직원 배제(P1). 이메일은 마스킹하여 노출.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import UserPrediction
|
||||
from ..scoring import SCORE, is_excluded
|
||||
from ..models import Match, UserPoints
|
||||
from ..scoring import is_excluded
|
||||
from ..schemas import LeaderboardOut, StandingOut
|
||||
|
||||
router = APIRouter(prefix="/api/leaderboard", tags=["leaderboard"])
|
||||
@@ -30,51 +29,36 @@ async def leaderboard(
|
||||
limit: int = Query(50, ge=1, le=200), db: AsyncSession = Depends(get_db)
|
||||
) -> LeaderboardOut:
|
||||
rows = (
|
||||
await db.execute(
|
||||
select(UserPrediction).where(
|
||||
UserPrediction.points.is_not(None),
|
||||
UserPrediction.email.is_not(None),
|
||||
)
|
||||
)
|
||||
await db.execute(select(UserPoints))
|
||||
).scalars().all()
|
||||
|
||||
agg: dict[str, dict] = defaultdict(
|
||||
lambda: {"points": 0, "exact": 0, "played": 0, "first": None}
|
||||
)
|
||||
matches: set[str] = set()
|
||||
for r in rows:
|
||||
email = (r.email or "").strip().lower()
|
||||
if not email or is_excluded(email):
|
||||
continue
|
||||
matches.add(r.match_id)
|
||||
a = agg[email]
|
||||
a["points"] += r.points or 0
|
||||
a["played"] += 1
|
||||
if (r.points or 0) >= SCORE["EXACT"]:
|
||||
a["exact"] += 1
|
||||
ts = r.scored_at or r.created_at
|
||||
if a["first"] is None or (ts and ts < a["first"]):
|
||||
a["first"] = ts
|
||||
|
||||
ranked = sorted(
|
||||
agg.items(),
|
||||
key=lambda kv: (
|
||||
-kv[1]["points"],
|
||||
-kv[1]["exact"],
|
||||
-(kv[1]["points"] / kv[1]["played"] if kv[1]["played"] else 0),
|
||||
-kv[1]["played"],
|
||||
kv[1]["first"].timestamp() if kv[1]["first"] else 0,
|
||||
(r for r in rows if not is_excluded(r.email)),
|
||||
key=lambda r: (
|
||||
-r.total_points,
|
||||
-r.exact_count,
|
||||
-(r.total_points / r.matches_played if r.matches_played else 0),
|
||||
-r.matches_played,
|
||||
r.first_scored_at.timestamp() if r.first_scored_at else 0,
|
||||
),
|
||||
)
|
||||
|
||||
scored_matches = (
|
||||
await db.execute(
|
||||
select(func.count())
|
||||
.select_from(Match)
|
||||
.where(Match.result_outcome.is_not(None))
|
||||
)
|
||||
).scalar() or 0
|
||||
|
||||
standings = [
|
||||
StandingOut(
|
||||
rank=i + 1,
|
||||
emailMasked=_mask(email),
|
||||
totalPoints=v["points"],
|
||||
exactCount=v["exact"],
|
||||
matchesPlayed=v["played"],
|
||||
emailMasked=_mask(r.email),
|
||||
totalPoints=r.total_points,
|
||||
exactCount=r.exact_count,
|
||||
matchesPlayed=r.matches_played,
|
||||
)
|
||||
for i, (email, v) in enumerate(ranked[:limit])
|
||||
for i, r in enumerate(ranked[:limit])
|
||||
]
|
||||
return LeaderboardOut(standings=standings, scoredMatches=len(matches))
|
||||
return LeaderboardOut(standings=standings, scoredMatches=scored_matches)
|
||||
Reference in new issue
Block a user