40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""채점 서비스 — 결과 입력 시 해당 경기 유저 픽 전수 채점.
|
|
|
|
관리자 setResult 와 워커가 공용으로 사용. scoring.score_prediction 적용.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from ..domain import now_utc
|
|
from ..models import Match, UserPrediction
|
|
from ..scoring import outcome_of, score_prediction
|
|
|
|
log = logging.getLogger("triplepick.grading")
|
|
|
|
|
|
async def apply_result(db: AsyncSession, match: Match, score_a: int, score_b: int) -> int:
|
|
"""결과 기록 + 해당 경기 픽 전수 채점. 채점된 픽 수 반환."""
|
|
match.result_score_a = score_a
|
|
match.result_score_b = score_b
|
|
match.result_outcome = outcome_of(score_a, score_b)
|
|
match.status = "finished"
|
|
match.finished_at = now_utc()
|
|
|
|
picks = (
|
|
await db.execute(
|
|
select(UserPrediction).where(UserPrediction.match_id == match.match_id)
|
|
)
|
|
).scalars().all()
|
|
|
|
for p in picks:
|
|
p.points = score_prediction(p.score_a, p.score_b, score_a, score_b)
|
|
p.scored_at = now_utc()
|
|
|
|
await db.commit()
|
|
log.info("graded %d picks for %s (%d-%d)", len(picks), match.match_id, score_a, score_b)
|
|
return len(picks)
|