Skip to content

Commit

Permalink
feat(judge)!: 评测采用生成器形式
Browse files Browse the repository at this point in the history
评测采用了生成器形式,使得WebUI可以即时展示评测出的结果,而无需长时间等待
  • Loading branch information
XYCode-Kerman committed Apr 8, 2024
1 parent 31ac7f9 commit f59293d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions judge/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
from pathlib import Path
from typing import *
from typing import List

from ccf_parser import CCF
Expand All @@ -10,7 +11,7 @@
from .player import Player


def start_judging(ccf: CCF):
def start_judging(ccf: CCF) -> Generator[List[JudgingResult], Any, Any]:
start = datetime.datetime.now()
judging_results: List[JudgingResult] = []
judge_logger.info(f'开始评测比赛 {ccf.header.name},当前时间:{start}')
Expand All @@ -31,6 +32,8 @@ def start_judging(ccf: CCF):
for player in players:
result = player.judging(ccf)
judging_results.append(result)
yield result
judge_logger.info(f'选手 {player.order} 评测完成。')

# 保存评测数据
Path(ccf.header.path).joinpath('./judging_results.json').write_text(
Expand All @@ -45,4 +48,3 @@ def start_judging(ccf: CCF):
end = datetime.datetime.now()
judge_logger.info(
f'评测比赛 {ccf.header.name} 完成,当前时间:{end},总用时:{end - start}')
return judging_results
13 changes: 12 additions & 1 deletion manager/api/routers/contest/judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@

router = APIRouter(prefix='/judge', tags=['评测管理'])
trackIds2Results: Dict[uuid.UUID, List[ccf_parser.JudgingResult]] = {}
judging: List[uuid.UUID] = []


def start_judging_task(ccf: ccf_parser.CCF, trackId: uuid.UUID):
trackIds2Results[trackId] = judge.start_judging(ccf)
# trackIds2Results[trackId] = judge.start_judging(ccf)
trackIds2Results[trackId] = []
judging.append(trackId)
for result in judge.start_judging(ccf):
trackIds2Results[trackId].append(result)
judging.remove(trackId)


@router.post('/start', name='开始评测', response_model=Dict[str, str], responses={
Expand Down Expand Up @@ -60,3 +66,8 @@ async def get_judging_result(trackId: uuid.UUID):
raise fastapi.HTTPException(status_code=404, detail='trackId 不存在')

return trackIds2Results[trackId]


@router.get('/is_judging/{trackId}', name='获取某个比赛是否处于评测状态', response_model=bool)
async def get_contest_is_judging(trackId: uuid.UUID):
return trackId in judging

0 comments on commit f59293d

Please sign in to comment.