Skip to content

Implement new results rules #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/jobs/data/codespellignorefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ nv.d3.min*
composer*
./doc/logos
./m4
./webapp/tests/Unit/Fixtures
177 changes: 105 additions & 72 deletions webapp/src/Controller/Jury/ImportExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Controller\Jury;

use App\Controller\BaseController;
use App\DataTransferObject\ResultRow;
use App\Entity\Clarification;
use App\Entity\Contest;
use App\Entity\ContestProblem;
use App\Entity\Problem;
use App\Entity\TeamCategory;
use App\Form\Type\ContestExportType;
use App\Form\Type\ContestImportType;
use App\Form\Type\ExportResultsType;
use App\Form\Type\ICPCCmsType;
use App\Form\Type\JsonImportType;
use App\Form\Type\ProblemsImportType;
Expand Down Expand Up @@ -45,6 +47,7 @@
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Twig\Environment;

#[Route(path: '/jury/import-export')]
#[IsGranted('ROLE_JURY')]
Expand All @@ -62,6 +65,7 @@ public function __construct(
KernelInterface $kernel,
#[Autowire('%domjudge.version%')]
protected readonly string $domjudgeVersion,
protected readonly Environment $twig,
) {
parent::__construct($em, $eventLogService, $dj, $kernel);
}
Expand Down Expand Up @@ -256,21 +260,73 @@ public function indexAction(Request $request): Response
return $this->redirectToRoute('jury_import_export');
}

/** @var TeamCategory[] $teamCategories */
$teamCategories = $this->em->createQueryBuilder()
->from(TeamCategory::class, 'c', 'c.categoryid')
->select('c.sortorder, c.name')
->where('c.visible = 1')
->orderBy('c.sortorder')
->getQuery()
->getResult();
$sortOrders = [];
foreach ($teamCategories as $teamCategory) {
$sortOrder = $teamCategory['sortorder'];
if (!array_key_exists($sortOrder, $sortOrders)) {
$sortOrders[$sortOrder] = [];
$exportResultsForm = $this->createForm(ExportResultsType::class);

$exportResultsForm->handleRequest($request);

if ($exportResultsForm->isSubmitted() && $exportResultsForm->isValid()) {
$contest = $this->dj->getCurrentContest();
if ($contest === null) {
throw new BadRequestHttpException('No current contest');
}
$sortOrders[$sortOrder][] = $teamCategory['name'];

$data = $exportResultsForm->getData();
$format = $data['format'];
$sortOrder = $data['sortorder'];
$individuallyRanked = $data['individually_ranked'];
$honors = $data['honors'];

$extension = match ($format) {
'html_inline', 'html_download' => 'html',
'tsv' => 'tsv',
default => throw new BadRequestHttpException('Invalid format'),
};
$contentType = match ($format) {
'html_inline' => 'text/html',
'html_download' => 'text/html',
'tsv' => 'text/csv',
default => throw new BadRequestHttpException('Invalid format'),
};
$contentDisposition = match ($format) {
'html_inline' => 'inline',
'html_download', 'tsv' => 'attachment',
default => throw new BadRequestHttpException('Invalid format'),
};
$filename = 'results.' . $extension;

$response = new StreamedResponse();
$response->setCallback(function () use (
$format,
$sortOrder,
$individuallyRanked,
$honors
) {
if ($format === 'tsv') {
$data = $this->importExportService->getResultsData(
$sortOrder->sort_order,
$individuallyRanked,
$honors,
);

echo "results\t1\n";
foreach ($data as $row) {
echo implode("\t", array_map(fn($field) => Utils::toTsvField((string)$field), $row->toArray())) . "\n";
}
} else {
echo $this->getResultsHtml(
$sortOrder->sort_order,
$individuallyRanked,
$honors,
);
}
});
$response->headers->set('Content-Type', $contentType);
$response->headers->set('Content-Disposition', "$contentDisposition; filename=\"$filename\"");
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Connection', 'Keep-Alive');
$response->headers->set('Accept-Ranges', 'bytes');

return $response;
}

return $this->render('jury/import_export.html.twig', [
Expand All @@ -281,16 +337,13 @@ public function indexAction(Request $request): Response
'contest_export_form' => $contestExportForm,
'contest_import_form' => $contestImportForm,
'problems_import_form' => $problemsImportForm,
'sort_orders' => $sortOrders,
'export_results_form' => $exportResultsForm,
]);
}

#[Route(path: '/export/{type<groups|teams|wf_results|full_results>}.tsv', name: 'jury_tsv_export')]
public function exportTsvAction(
string $type,
#[MapQueryParameter(name: 'sort_order')]
?int $sortOrder,
): Response {
public function exportTsvAction(string $type): Response
{
$data = [];
$tsvType = $type;
try {
Expand All @@ -301,14 +354,6 @@ public function exportTsvAction(
case 'teams':
$data = $this->importExportService->getTeamData();
break;
case 'wf_results':
$data = $this->importExportService->getResultsData($sortOrder);
$tsvType = 'results';
break;
case 'full_results':
$data = $this->importExportService->getResultsData($sortOrder, full: true);
$tsvType = 'results';
break;
}
} catch (BadRequestHttpException $e) {
$this->addFlash('danger', $e->getMessage());
Expand All @@ -331,29 +376,22 @@ public function exportTsvAction(
return $response;
}

#[Route(path: '/export/{type<wf_results|full_results|clarifications>}.html', name: 'jury_html_export')]
public function exportHtmlAction(Request $request, string $type): Response
#[Route(path: '/export/clarifications.html', name: 'jury_html_export_clarifications')]
public function exportClarificationsHtmlAction(): Response
{
try {
switch ($type) {
case 'wf_results':
return $this->getResultsHtml($request);
case 'full_results':
return $this->getResultsHtml($request, full: true);
case 'clarifications':
return $this->getClarificationsHtml();
default:
$this->addFlash('danger', "Unknown export type '" . $type . "' requested.");
return $this->redirectToRoute('jury_import_export');
}
return $this->getClarificationsHtml();
} catch (BadRequestHttpException $e) {
$this->addFlash('danger', $e->getMessage());
return $this->redirectToRoute('jury_import_export');
}
}

protected function getResultsHtml(Request $request, bool $full = false): Response
{
protected function getResultsHtml(
int $sortOrder,
bool $individuallyRanked,
bool $honors
): string {
/** @var TeamCategory[] $categories */
$categories = $this->em->createQueryBuilder()
->from(TeamCategory::class, 'c', 'c.categoryid')
Expand Down Expand Up @@ -388,37 +426,35 @@ protected function getResultsHtml(Request $request, bool $full = false): Respons
$regionWinners = [];
$rankPerTeam = [];

$sortOrder = $request->query->getInt('sort_order');

foreach ($this->importExportService->getResultsData($sortOrder, full: $full) as $row) {
$team = $teamNames[$row[0]];
$rankPerTeam[$row[0]] = $row[1];
foreach ($this->importExportService->getResultsData($sortOrder, $individuallyRanked, $honors) as $row) {
$team = $teamNames[$row->teamId];
$rankPerTeam[$row->teamId] = $row->rank;

if ($row[6] !== '') {
if ($row->groupWinner) {
$regionWinners[] = [
'group' => $row[6],
'group' => $row->groupWinner,
'team' => $team,
'rank' => $row[1] ?: '-',
'rank' => $row->rank ?? '-',
];
}

$row = [
'team' => $team,
'rank' => $row[1],
'award' => $row[2],
'solved' => $row[3],
'total_time' => $row[4],
'max_time' => $row[5],
'rank' => $row->rank,
'award' => $row->award,
'solved' => $row->numSolved,
'total_time' => $row->totalTime,
'max_time' => $row->timeOfLastSubmission,
];
if (preg_match('/^(.*) Medal$/', $row['award'], $matches)) {
$row['class'] = strtolower($matches[1]);
} else {
$row['class'] = '';
}
if ($row['rank'] === '') {
if ($row['rank'] === null) {
$honorable[] = $row['team'];
} elseif ($row['award'] === 'Ranked') {
$ranked[] = $row;
} elseif (in_array($row['award'], ['Ranked', 'Highest Honors', 'High Honors', 'Honors'], true)) {
$ranked[$row['award']][] = $row;
} else {
$awarded[] = $row;
}
Expand All @@ -428,13 +464,16 @@ protected function getResultsHtml(Request $request, bool $full = false): Respons

$collator = new Collator('en_US');
$collator->sort($honorable);
usort($ranked, function (array $a, array $b) use ($collator): int {
if ($a['rank'] !== $b['rank']) {
return $a['rank'] <=> $b['rank'];
}
foreach ($ranked as &$rankedTeams) {
usort($rankedTeams, function (array $a, array $b) use ($collator): int {
if ($a['rank'] !== $b['rank']) {
return $a['rank'] <=> $b['rank'];
}

return $collator->compare($a['team'], $b['team']);
});
return $collator->compare($a['team'], $b['team']);
});
}
unset($rankedTeams);

$problems = $scoreboard->getProblems();
$matrix = $scoreboard->getMatrix();
Expand Down Expand Up @@ -487,16 +526,10 @@ protected function getResultsHtml(Request $request, bool $full = false): Respons
'firstToSolve' => $firstToSolve,
'domjudgeVersion' => $this->domjudgeVersion,
'title' => sprintf('Results for %s', $contest->getName()),
'download' => $request->query->getBoolean('download'),
'sortOrder' => $sortOrder,
];
$response = $this->render('jury/export/results.html.twig', $data);

if ($request->query->getBoolean('download')) {
$response->headers->set('Content-disposition', 'attachment; filename=results.html');
}

return $response;
return $this->twig->render('jury/export/results.html.twig', $data);
}

protected function getClarificationsHtml(): Response
Expand Down
40 changes: 40 additions & 0 deletions webapp/src/DataTransferObject/ResultRow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace App\DataTransferObject;

class ResultRow
{
public function __construct(
public readonly string $teamId,
public readonly ?int $rank,
public readonly string $award,
public readonly int $numSolved,
public readonly int $totalTime,
public readonly int $timeOfLastSubmission,
public readonly ?string $groupWinner = null,
) {}

/**
* @return array{
* team_id: string,
* rank: int|null,
* award: string,
* num_solved: int,
* total_time: int,
* time_of_last_submission: int,
* group_winner: string|null,
* }
*/
public function toArray(): array
{
return [
'team_id' => $this->teamId,
'rank' => $this->rank,
'award' => $this->award,
'num_solved' => $this->numSolved,
'total_time' => $this->totalTime,
'time_of_last_submission' => $this->timeOfLastSubmission,
'group_winner' => $this->groupWinner,
];
}
}
Loading
Loading