-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathBalloonService.php
231 lines (206 loc) · 9.15 KB
/
BalloonService.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
<?php declare(strict_types=1);
namespace App\Service;
use App\Entity\Balloon;
use App\Entity\Contest;
use App\Entity\ContestProblem;
use App\Entity\Judging;
use App\Entity\Problem;
use App\Entity\ScoreCache;
use App\Entity\Submission;
use App\Entity\Team;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BalloonService
{
public function __construct(
protected readonly EntityManagerInterface $em,
protected readonly ConfigurationService $config
) {}
/**
* Update the balloons table after a correct submission.
*
* This function double checks that the judging is correct and confirmed.
*
* @throws NoResultException
* @throws NonUniqueResultException
* @throws ORMException
*/
public function updateBalloons(
Contest $contest,
Submission $submission,
Judging $judging
): void {
// Balloon processing disabled for contest.
if (!$contest->getProcessBalloons()) {
return;
}
// Make sure judging is correct.
if ($judging->getResult() !== Judging::RESULT_CORRECT) {
return;
}
// Also make sure it is verified if this is required.
if (!$judging->getVerified() &&
$this->config->get('verification_required')) {
return;
}
// Prevent duplicate balloons in case of multiple correct submissions.
$numCorrect = $this->em->createQueryBuilder()
->from(Balloon::class, 'b')
->select('COUNT(b.submission) AS numBalloons')
->andWhere('b.problem = :probid')
->andWhere('b.team = :teamid')
->andWhere('b.contest = :cid')
->setParameter('probid', $submission->getProblem())
->setParameter('teamid', $submission->getTeam())
->setParameter('cid', $submission->getContest())
->getQuery()
->getSingleScalarResult();
if ($numCorrect == 0) {
$balloon = new Balloon();
$balloon->setSubmission($this->em->getReference(Submission::class, $submission->getSubmitid()));
$balloon->setTeam($this->em->getReference(Team::class, $submission->getTeamId()));
$balloon->setContest(
$this->em->getReference(Contest::class, $submission->getContest()->getCid()));
$balloon->setProblem($this->em->getReference(Problem::class, $submission->getProblemId()));
$this->em->persist($balloon);
try {
$this->em->flush();
} catch (UniqueConstraintViolationException $e) {
}
}
}
/**
* @return array<array{data: array{balloonid: int, time: string, problem: string, contestproblem: ContestProblem,
* team: Team, teamid: int, location: string|null, affiliation: string|null,
* affiliationid: int, category: string, categoryid: int, total: array<string, ContestProblem>,
* awards: string, done: bool}}>
*/
public function collectBalloonTable(Contest $contest, bool $todo = false): array
{
$em = $this->em;
$showPostFreeze = (bool)$this->config->get('show_balloons_postfreeze');
if (!$showPostFreeze) {
$freezetime = $contest->getFreezeTime();
}
// Build a list of teams and the problems they solved first.
$firstSolved = $em->getRepository(ScoreCache::class)->findBy(['is_first_to_solve' => 1]);
$firstSolvers = [];
foreach ($firstSolved as $scoreCache) {
$firstSolvers[$scoreCache->getTeam()->getTeamId()][] = $scoreCache->getProblem()->getProbid();
}
$query = $em->createQueryBuilder()
->select('b', 's.submittime', 'p.probid',
't.teamid', 's', 't', 't.location',
'c.categoryid AS categoryid', 'c.name AS catname',
'co.cid', 'co.shortname',
'cp.shortname AS probshortname', 'cp.color',
'a.affilid AS affilid', 'a.shortname AS affilshort')
->from(Balloon::class, 'b')
->leftJoin('b.submission', 's')
->leftJoin('b.problem', 'p')
->leftJoin('b.contest', 'co')
->leftJoin('p.contest_problems', 'cp', Join::WITH, 'co.cid = cp.contest AND p.probid = cp.problem')
->leftJoin('b.team', 't')
->leftJoin('t.category', 'c')
->leftJoin('t.affiliation', 'a')
->andWhere('co.cid = :cid')
->setParameter('cid', $contest->getCid())
->orderBy('b.done', 'ASC')
->addOrderBy('s.submittime', 'DESC');
$balloons = $query->getQuery()->getResult();
// Loop once over the results to get totals and awards.
$TOTAL_BALLOONS = $AWARD_BALLOONS = [];
foreach ($balloons as $balloonsData) {
if ($balloonsData['color'] === null) {
continue;
}
$TOTAL_BALLOONS[$balloonsData['teamid']][$balloonsData['probshortname']] = $balloonsData[0]->getSubmission()->getContestProblem();
// Keep a list of balloons that were first to solve this problem;
// can be multiple, one for each sortorder.
if (in_array($balloonsData['probid'], $firstSolvers[$balloonsData['teamid']] ?? [], true)) {
$AWARD_BALLOONS['problem'][$balloonsData['probid']][] = $balloonsData[0]->getBalloonId();
}
// Keep overwriting this - in the end it'll
// contain the ID of the first balloon in this contest.
$submittime = $balloonsData[0]->getSubmission()->getSubmittime();
$AWARD_BALLOONS['contest'] = $balloonsData[0]->getBalloonId();
}
$countBefore = $em->createQueryBuilder()
->from(Judging::class, 'j')
->leftJoin('j.submission', 's')
->select('COUNT(s.submitid)')
->where('s.contest = :cid')
->andWhere('s.submittime < :submittime')
->andWhere('s.valid = 1')
->andWhere('j.valid = 1')
->andWhere('j.result IS NULL')
->setParameter('cid', $contest->getCid())
->setParameter('submittime', $submittime)
->getQuery()->getSingleScalarResult();
if ($countBefore > 0) {
// We don't have complete information about 'first in contest' yet as there are unjudged submissions that
// could influence the result.
$AWARD_BALLOONS['contest'] = -1;
}
// Loop again to construct table.
$balloons_table = [];
foreach ($balloons as $balloonsData) {
if ($balloonsData['color'] === null) {
continue;
}
/** @var Balloon $balloon */
$balloon = $balloonsData[0];
$done = $balloon->getDone();
if ($todo && $done) {
continue;
}
$balloonId = $balloon->getBalloonId();
$stime = $balloonsData['submittime'];
if (isset($freezetime) && $stime >= $freezetime) {
continue;
}
$balloondata = [];
$balloondata['balloonid'] = $balloonId;
$balloondata['time'] = $stime;
$balloondata['problem'] = $balloonsData['probshortname'];
$balloondata['contestproblem'] = $balloon->getSubmission()->getContestProblem();
$balloondata['team'] = $balloon->getSubmission()->getTeam();
$balloondata['teamid'] = $balloonsData['teamid'];
$balloondata['location'] = $balloonsData['location'];
$balloondata['affiliation'] = $balloonsData['affilshort'];
$balloondata['affiliationid'] = $balloonsData['affilid'];
$balloondata['category'] = $balloonsData['catname'];
$balloondata['categoryid'] = $balloonsData['categoryid'];
ksort($TOTAL_BALLOONS[$balloonsData['teamid']]);
$balloondata['total'] = $TOTAL_BALLOONS[$balloonsData['teamid']];
$comments = [];
if ($AWARD_BALLOONS['contest'] == $balloonId) {
$comments[] = 'first in contest';
} elseif (isset($AWARD_BALLOONS['problem'][$balloonsData['probid']])
&& in_array($balloonId, $AWARD_BALLOONS['problem'][$balloonsData['probid']], true)) {
$comments[] = 'first for problem';
}
$balloondata['awards'] = implode('; ', $comments);
$balloondata['done'] = $done;
$balloons_table[] = [
'data' => $balloondata,
];
}
return $balloons_table;
}
public function setDone(int $balloonId): void
{
$em = $this->em;
$balloon = $em->getRepository(Balloon::class)->find($balloonId);
if (!$balloon) {
throw new NotFoundHttpException('balloon not found');
}
$balloon->setDone(true);
$em->flush();
}
}