forked from DOMjudge/domjudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreboardMergeCommand.php
403 lines (373 loc) · 16 KB
/
ScoreboardMergeCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php declare(strict_types=1);
namespace App\Command;
use App\Entity\Contest;
use App\Entity\ContestProblem;
use App\Entity\Problem;
use App\Entity\ScoreCache;
use App\Entity\Team;
use App\Entity\TeamAffiliation;
use App\Entity\TeamCategory;
use App\Service\ConfigurationService;
use App\Service\DOMJudgeService;
use App\Service\ScoreboardService;
use App\Utils\FreezeData;
use App\Utils\Scoreboard\Scoreboard;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use ZipArchive;
/**
* Class ScoreboardMergeCommand
* @package App\Command
*/
class ScoreboardMergeCommand extends Command
{
protected DOMJudgeService $dj;
protected ConfigurationService $config;
protected Environment $twig;
protected HttpClientInterface $client;
protected ScoreboardService $scoreboardService;
protected RouterInterface $router;
protected string $projectDir;
public function __construct(
DOMJudgeService $dj,
ConfigurationService $config,
Environment $twig,
HttpClientInterface $client,
ScoreboardService $scoreboardService,
RouterInterface $router,
string $projectDir,
string $name = null
) {
parent::__construct($name);
$this->dj = $dj;
$this->config = $config;
$this->twig = $twig;
$this->client = $client;
$this->scoreboardService = $scoreboardService;
$this->router = $router;
$this->projectDir = $projectDir;
}
protected function configure(): void
{
$this
->setName('scoreboard:merge')
->setDescription('Merges scoreboards from multiple sites from API endpoints.')
->setHelp(
'Usage example: scoreboard:merge "BAPC preliminaries" ' .
'https://judge.gehack.nl/api/v4/contests/3/ 3 ' .
'http://ragnargrootkoerkamp.nl/upload/uva 2' . PHP_EOL . PHP_EOL .
'This fetches teams and scoreboard data from API endpoints and prints a merged HTML scoreboard. It assumes times in minutes.'
)
->addOption(
'category',
'c',
InputOption::VALUE_REQUIRED,
'Name of the team category to use',
'Participant'
)
->addArgument(
'output-file',
InputArgument::REQUIRED,
'Where to store the ZIP file with the merged scoreboard'
)
->addArgument(
'contest-name',
InputArgument::REQUIRED,
'Title of the merged contest.'
)
->addArgument(
'feed-url',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'Alternating URL location of the scoreboard to merge and a comma separated list of group_ids to include.' . PHP_EOL .
'If an URL and it requires authentication, use username:password@ in the URL' . PHP_EOL .
'URL should have the form https://<domain>/api/v4/contests/<contestid>/ for DOMjudge or point to any ICPC Contest API compatible contest' . PHP_EOL .
'Only the /teams, /organizations, /problems and /scoreboard endpoint are used, so manually putting files in those locations can work as well.' . PHP_EOL .
'Alternatively, you can mount local files directly in the container: add "- /path/to/scoreboards:/scoreboards" to "docker-compose.yml" and use "/scoreboards/eindhoven" as path.'
);
}
/**
* url: "https://judge.gehack.nl/api/v4" or "/path/to/file"
* endpoint: "/teams"
* args: "?public=1" (ignored for files)
*/
protected function getEndpoint(
string $url,
string $endpoint,
string $args = ''
) {
if (str_starts_with($url, 'http')) {
return $this->client
->request('GET', $url . $endpoint . $args)
->toArray();
}
return json_decode(file_get_contents($url . $endpoint . '.json'), true);
}
/**
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$teams = [];
$nextTeamId = 0;
$problems = [];
$problemNameToIdMap = [];
$scoreCache = [];
$affiliations = [];
$firstSolve = [];
$contest = (new Contest())
->setName($input->getArgument('contest-name'));
$freezeData = null;
$category = (new TeamCategory())
->setName($input->getOption('category'))
->setCategoryid(0)
->setColor("#ffffff");
$siteArguments = $input->getArgument('feed-url');
// Convert from flat list to list of (url, groups) pairs
$sites = [];
if (count($siteArguments) % 2 != 0) {
$style->error("Provide an even number of arguments: all pairs of url and comma separated group ids.");
return 1;
}
for ($i = 0; $i < count($siteArguments); $i += 2) {
$site = [];
$site['path'] = $siteArguments[$i];
# Some simple validation to make sure we're actually parsing group ids.
$groupsString = $siteArguments[$i + 1];
$site['group_ids'] = explode(',', $groupsString);
$sites[] = $site;
}
foreach ($sites as $site) {
$path = $site['path'];
// Strip of last /
if (substr($path, -1) === '/') {
$path = substr($path, 0, strlen($path) - 1);
}
$teamData = $this->getEndpoint($path, '/teams');
$organizationData = $this->getEndpoint($path, '/organizations');
$problemData = $this->getEndpoint($path, '/problems');
$organizationMap = [];
foreach ($organizationData as $organization) {
$organizationMap[$organization['id']] = $organization;
}
$problemMap = [];
foreach ($problemData as $problem) {
$problemMap[$problem['id']] = $problem;
}
$teamIdMap = [];
foreach ($teamData as $team) {
// Only include the team if its id is listed in the corresponding groups list.
$include = false;
foreach ($team['group_ids'] as $group_id) {
if (in_array($group_id, $site['group_ids'])) {
$include = true;
break;
}
}
if (!$include) {
continue;
}
$teamObj = (new Team())
->setName($team['name'])
->setDisplayName($team['display_name'] ?? $team['name'])
->setEnabled(true)
->setPublicDescription('')
->setRoom('');
if ($team['organization_id'] !== null &&
isset($organizationMap[$team['organization_id']])) {
$organization = $organizationMap[$team['organization_id']];
$organizationName = $organization['formal_name'] ?? $organization['name'];
if (!array_key_exists($organizationName, $affiliations)) {
$affiliation = (new TeamAffiliation())
->setName($organizationName)
->setAffilid(count($affiliations));
$affiliations[$organizationName] = $affiliation;
}
$teamObj->setAffiliation($affiliations[$organizationName]);
} else {
$teamObj->setAffiliation(null);
}
$teamObj->setCategory($category);
$oldid = $team['id'];
$newid = $nextTeamId++;
$teamObj->setTeamid($newid);
$teams[] = $teamObj;
$teamIdMap[$oldid] = $newid;
}
$scoreboardData = $this->getEndpoint(
$path,
'/scoreboard',
'?public=1'
);
if ($contest->getStarttimeString() === null) {
$state = $scoreboardData['state'];
$endtime = $state['ended'] ?? $state['started'];
// While the contest is running, simply use the start time for everything.
$contest
->setStarttimeString($state['started'])
->setEndtimeString($endtime)
->setFreezetimeString($endtime)
->setUnfreezetimeString($endtime)
->setFinalizetime($endtime)
->setDeactivatetimeString($endtime)
->updateTimes();
}
$freezeData = new FreezeData($contest);
// Add scoreboard data
foreach ($scoreboardData['rows'] as $row) {
// If this this team is not in the teams array (because it's not in the right group),
// ignore this row.
if (!array_key_exists($row['team_id'], $teamIdMap)) {
continue;
}
$team = $teams[$teamIdMap[$row['team_id']]];
foreach ($row['problems'] as $problem) {
// Problems are keyed by name, as that seems to be more consistent.
// Some sites occasionally mix up short_name and id.
$problemId = $problem['problem_id'];
$baseProblem = $problemMap[$problemId];
$label = $baseProblem['label'];
$name = $baseProblem['name'];
if (!array_key_exists($name, $problemNameToIdMap)) {
dump("New NAME $name");
$id = count($problems);
$problemObj = (new Problem())
->setProbid($id)
->setName($name);
$contestProblemObj = (new ContestProblem())
->setProblem($problemObj)
->setColor($baseProblem['rgb'])
->setShortName($label);
$problems[$id] = $contestProblemObj;
$problemNameToIdMap[$name] = $id;
$firstSolve[$name] = null;
} else {
$id = $problemNameToIdMap[$name];
}
$scoreCacheObj = (new scoreCache())
->setProblem($problems[$id]->getProblem())
->setTeam($team);
if (array_key_exists('time', $problem)) {
// TODO: Make this work with input in seconds as well.
$scoreCacheObj
->setSolveTimePublic($problem['time'] * 60)
->setSolveTimeRestricted($problem['time'] * 60);
if (
$firstSolve[$name] === null or
$problem['time'] * 60 < $firstSolve[$name]
) {
$firstSolve[$name] = $problem['time'] * 60;
}
}
$scoreCacheObj
->setSubmissionsPublic($problem['num_judged'])
->setSubmissionsRestricted($problem['num_judged'])
->setIsCorrectPublic($problem['solved'])
->setIsCorrectRestricted($problem['solved']);
$scoreCache[] = $scoreCacheObj;
}
}
}
// Update the first to solve fields.
foreach ($scoreCache as &$scoreCacheObj) {
if ($scoreCacheObj->getSolveTimeRestricted() == $firstSolve[$scoreCacheObj->getProblem()->getName()]) {
$scoreCacheObj->setIsFirstToSolve(true);
}
}
$scoreboard = new Scoreboard(
$contest,
$teams,
[$category],
$problems,
$scoreCache,
$freezeData,
false,
(int)$this->config->get('penalty_time'),
false
);
// Render the scoreboard to HTML and print it.
$data = $this->scoreboardService->getScoreboardTwigData(
null, null, '', false, true, true, $contest, $scoreboard
);
$data['hide_menu'] = true;
$data['current_contest'] = $contest;
$output = $this->twig->render('public/scoreboard.html.twig', $data);
// What files to add to the ZIP file
$filesToAdd = [
'webfonts/*',
'images/*'
];
// Detect other files to add to the ZIP file by scanning the output.
// We need to do this anyway, since we need to rewrite the absolute paths.
$rootUrl = $this->router->generate('root');
// Parts of the output we should match and what to replace it with.
// ROOT_URL will be replaced with the root URL as defined above
$toMatch = [
'/href="ROOT_URL(.*)(?:\?.*)"/' => 'href="$1"',
'/src="ROOT_URL(.*)(?:\?.*)"/' => 'src="$1"',
];
foreach ($toMatch as $pattern => $replace) {
$pattern = str_replace(
'ROOT_URL', preg_quote($rootUrl, '/'), $pattern
);
preg_match_all($pattern, $output, $matches);
$filesToAdd = array_merge($filesToAdd, $matches[1]);
$output = preg_replace($pattern, $replace, $output);
}
$zip = new ZipArchive();
$result = $zip->open($input->getArgument('output-file'),
ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($result !== true) {
$style->error('Can not open output file to write ZIP to: ' . $result);
return 1;
}
$zip->addFromString('index.html', $output);
// Now add all files we need
$publicDir = realpath(sprintf('%s/public/', $this->projectDir));
foreach ($filesToAdd as $fileToAdd) {
$finder = new Finder();
$lastSlash = strrpos($fileToAdd, '/');
if ($lastSlash === false) {
$path = '';
$file = $fileToAdd;
} else {
$path = substr($fileToAdd, 0, $lastSlash);
$file = substr($fileToAdd, $lastSlash + 1);
}
$pathRegex = sprintf('/^%s/', preg_quote($path, '/'));
/** @var SplFileInfo $fileInfo */
foreach ($finder->followLinks()->in($publicDir)->path($pathRegex)->name($file)->files() as $fileInfo) {
$zip->addFile($fileInfo->getRealPath(),
$fileInfo->getRelativePathname());
}
}
$zip->close();
$style->success(sprintf('Merged scoreboard data written to %s',
$input->getArgument('output-file')));
return static::SUCCESS;
}
}