1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Form\FormError; |
4
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
7
|
|
|
|
8
|
|
|
class MatchController extends CRUDController |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Whether the last edited match has had its ELO changed, requiring an ELO |
12
|
|
|
* recalculation |
13
|
|
|
* |
14
|
|
|
* This is useful so that a confirmation form is shown, asking the user if |
15
|
|
|
* they want to recalculate ELOs |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public $recalculateNeeded = false; |
20
|
|
|
|
21
|
|
|
public function listAction(Request $request, Player $me, Team $team = null, $type = null) |
22
|
|
|
{ |
23
|
|
|
$qb = $this->getQueryBuilder(); |
24
|
|
|
|
25
|
|
|
$currentPage = $request->query->get('page', 1); |
26
|
|
|
|
27
|
|
|
$query = $qb->sortBy('time')->reverse() |
|
|
|
|
28
|
|
|
->with($team, $type) |
29
|
|
|
->limit(50)->fromPage($currentPage); |
30
|
|
|
|
31
|
|
|
$matches = $query->getModels($fast = true); |
32
|
|
|
|
33
|
|
|
foreach ($matches as $match) { |
34
|
|
|
// Don't show wrong labels for matches |
35
|
|
|
$match->getOriginalTimestamp()->setTimezone($me->getTimezone()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return array( |
39
|
|
|
"matches" => $matches, |
40
|
|
|
"team" => $team, |
41
|
|
|
"currentPage" => $currentPage, |
42
|
|
|
"totalPages" => $qb->countPages() |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function showAction(Match $match) |
47
|
|
|
{ |
48
|
1 |
|
return array("match" => $match); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
View Code Duplication |
public function createAction(Player $me) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return $this->create($me, function (Match $match) use ($me) { |
54
|
1 |
|
if ($me->canEdit($match) |
55
|
1 |
|
&& $match->isOfficial() |
56
|
1 |
|
&& (!$match->getTeamA()->isLastMatch($match) |
|
|
|
|
57
|
1 |
|
|| !$match->getTeamB()->isLastMatch($match)) |
|
|
|
|
58
|
|
|
) { |
59
|
|
|
$url = Service::getGenerator()->generate('match_recalculate', array( |
60
|
|
|
'match' => $match->getId(), |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
return new RedirectResponse($url); |
64
|
|
|
} |
65
|
1 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function deleteAction(Player $me, Match $match) |
69
|
|
|
{ |
70
|
|
View Code Duplication |
return $this->delete($match, $me, function () use ($match, $me) { |
|
|
|
|
71
|
|
|
if ($match->getTeamA()->isLastMatch($match) |
|
|
|
|
72
|
|
|
&& $match->getTeamB()->isLastMatch($match)) { |
|
|
|
|
73
|
|
|
$match->resetELOs(); |
74
|
|
|
} elseif ($me->canEdit($match)) { |
75
|
|
|
$url = Service::getGenerator()->generate('match_recalculate', array( |
76
|
|
|
'match' => $match->getId(), |
77
|
|
|
)); |
78
|
|
|
|
79
|
|
|
return new RedirectResponse($url); |
80
|
|
|
} |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function editAction(Player $me, Match $match) |
85
|
|
|
{ |
86
|
|
|
// TODO: Generating this response is unnecessary |
87
|
|
|
$response = $this->edit($match, $me, "match"); |
88
|
|
|
|
89
|
|
View Code Duplication |
if ($this->recalculateNeeded && $match->isOfficial()) { |
|
|
|
|
90
|
|
|
// Redirect to a confirmation form if we are assigning a new leader |
91
|
|
|
$url = Service::getGenerator()->generate('match_recalculate', array( |
92
|
|
|
'match' => $match->getId(), |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
return new RedirectResponse($url); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function recalculateAction(Player $me, $match) |
102
|
|
|
{ |
103
|
|
|
$match = Match::get($match); // get a match even if it's deleted |
104
|
|
|
|
105
|
|
|
if (!$me->canEdit($match)) { |
106
|
|
|
throw new ForbiddenException("You are not allowed to edit that match."); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$match->isOfficial()) { |
110
|
|
|
throw new BadRequestException("You can't recalculate ELO history for a special match."); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->showConfirmationForm(function () use ($match) { |
114
|
|
|
$response = new StreamedResponse(); |
115
|
|
|
$response->headers->set('Content-Type', 'text/plain'); |
116
|
|
|
$response->setCallback(function () use ($match) { |
117
|
|
|
$this->recalculate($match); |
118
|
|
|
}); |
119
|
|
|
$response->send(); |
120
|
|
|
}, "Do you want to recalculate ELO history for all teams and matches after the specified match?", |
121
|
|
|
"ELO history recalculated", |
122
|
|
|
"Recalculate ELOs", |
123
|
|
|
function () use ($match) { |
124
|
|
|
if ($match->isDeleted()) { |
125
|
|
|
return new RedirectResponse($match->getURL('list')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new RedirectResponse($match->getURL('show')); |
129
|
|
|
}, |
130
|
|
|
"Match/recalculate.html.twig", |
131
|
|
|
$noButton = true |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Recalculates match history for all teams and matches |
137
|
|
|
* |
138
|
|
|
* Recalculation is done as follows: |
139
|
|
|
* 1. A match is chosen as a starting point - it's stored old team ELOs are |
140
|
|
|
* considered correct |
141
|
|
|
* 2. Team ELOs are reset to their values at the starting point |
142
|
|
|
* 3. Each match that occurred since the first specified match has its ELO |
143
|
|
|
* recalculated based on the current team values, and the new match data |
144
|
|
|
* and team ELOs are stored in the database |
145
|
|
|
* |
146
|
|
|
* @param Match $match The first match |
147
|
|
|
*/ |
148
|
|
|
private function recalculate(Match $match) |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
|
|
// Commented out to prevent ridiculously large recalculations |
152
|
|
|
//set_time_limit(0); |
153
|
|
|
|
154
|
|
|
$query = Match::getQueryBuilder() |
155
|
|
|
->where('status')->notEquals('deleted') |
156
|
|
|
->where('type')->equals(Match::OFFICIAL) |
157
|
|
|
->where('time')->isAfter($match->getTimestamp(), $inclusive = true) |
158
|
|
|
->sortBy('time'); |
159
|
|
|
|
160
|
|
|
/** @var Match[] $matches */ |
161
|
|
|
$matches = $query->getModels($fast = true); |
162
|
|
|
|
163
|
|
|
// Send the total count to client-side javascript |
164
|
|
|
$this->log(count($matches) . "\n"); |
165
|
|
|
|
166
|
|
|
// Start a transaction so tables are locked and we don't stay with |
167
|
|
|
// messed up data if something goes wrong |
168
|
|
|
Database::getInstance()->startTransaction(); |
169
|
|
|
|
170
|
|
|
$teamsReset = []; |
171
|
|
|
|
172
|
|
|
// Reset match teams, in case the selected match is deleted and does |
173
|
|
|
// not show up in the list of matches to recalculate |
174
|
|
|
$match->getTeamA()->setElo($match->getTeamAEloOld()); |
|
|
|
|
175
|
|
|
$match->getTeamB()->setElo($match->getTeamBEloOld()); |
|
|
|
|
176
|
|
|
$teamsReset[ $match->getTeamA()->getId() ] = true; |
177
|
|
|
$teamsReset[ $match->getTeamB()->getId() ] = true; |
178
|
|
|
|
179
|
|
|
foreach ($matches as $i => $match) { |
180
|
|
|
// Reset teams' ELOs if they haven't been reset already |
181
|
|
View Code Duplication |
if (!isset($teamsReset[ $match->getTeamA()->getId() ])) { |
|
|
|
|
182
|
|
|
$teamsReset[ $match->getTeamA()->getId() ] = true; |
183
|
|
|
$match->getTeamA()->setElo($match->getTeamAEloOld()); |
|
|
|
|
184
|
|
|
} |
185
|
|
View Code Duplication |
if (!isset($teamsReset[ $match->getTeamB()->getId() ])) { |
|
|
|
|
186
|
|
|
$teamsReset[ $match->getTeamB()->getId() ] = true; |
187
|
|
|
$match->getTeamB()->setElo($match->getTeamBEloOld()); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$match->recalculateElo(); |
191
|
|
|
|
192
|
|
|
// Send an update to the client-side javascript, so that a |
193
|
|
|
// progress bar can be updated |
194
|
|
|
$this->log("m"); |
195
|
|
|
} |
196
|
|
|
} catch (Exception $e) { |
197
|
|
|
Database::getInstance()->rollback(); |
198
|
|
|
Database::getInstance()->finishTransaction(); |
199
|
|
|
throw $e; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
Database::getInstance()->finishTransaction(); |
203
|
|
|
|
204
|
|
|
$this->log("\n\nCalculation successful\n"); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Echo a string and flush the buffers |
209
|
|
|
* |
210
|
|
|
* Useful for streamed AJAX responses |
211
|
|
|
* |
212
|
|
|
* @param string $string The string to echo |
213
|
|
|
*/ |
214
|
|
|
private function log($string) |
215
|
|
|
{ |
216
|
|
|
echo $string; |
217
|
|
|
ob_flush(); |
218
|
|
|
flush(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
1 |
|
protected function getMessages($type, $name = '') |
225
|
|
|
{ |
226
|
1 |
|
$messages = parent::getMessages($type, $name); |
227
|
|
|
|
228
|
|
|
// Don't show the match info on the successful create/edit message |
229
|
1 |
|
foreach ($messages as &$action) { |
230
|
1 |
|
foreach ($action as &$status) { |
231
|
1 |
|
if (isset($status['named'])) { |
232
|
1 |
|
$status['named'] = $status['unnamed']; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
return $messages; |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
protected function validate($form) |
241
|
|
|
{ |
242
|
|
|
// Make sure that two different teams participated in a match, i.e. a team |
243
|
|
|
// didn't match against itself |
244
|
1 |
|
$firstTeam = $form->get('first_team')->get('team')->getData(); |
245
|
1 |
|
$secondTeam = $form->get('second_team')->get('team')->getData(); |
246
|
|
|
|
247
|
1 |
|
if (!$firstTeam || !$secondTeam) { |
248
|
|
|
return; |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
if ($firstTeam->isSameAs($secondTeam)) { |
252
|
1 |
|
$message = "You can't report a match where a team played against itself!"; |
253
|
1 |
|
$form->addError(new FormError($message)); |
254
|
|
|
} |
255
|
|
|
|
256
|
1 |
|
foreach (array('first_team', 'second_team') as $team) { |
257
|
1 |
|
$input = $form->get($team)->get('team'); |
258
|
|
|
|
259
|
1 |
|
if ($form->get('type')->getData() == Match::OFFICIAL) { |
260
|
1 |
|
if ($input->getData() instanceof ColorTeam) { |
261
|
|
|
$message = "Please enter a valid team for an official match."; |
262
|
1 |
|
$input->addError(new FormError($message)); |
263
|
|
|
} |
264
|
|
|
} else { |
265
|
1 |
|
if (!$input->getData() instanceof ColorTeam) { |
266
|
|
|
$message = "Please enter a team color for fun and special matches."; |
267
|
1 |
|
$input->addError(new FormError($message)); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
1 |
|
} |
272
|
|
|
|
273
|
|
|
protected function validateEdit($form, $match) |
274
|
|
|
{ |
275
|
|
|
if ($match->isOfficial() && $form->get('type')->getData() !== Match::OFFICIAL) { |
276
|
|
|
$message = "You cannot change this match's type."; |
277
|
|
|
$form->get('type')->addError(new FormError($message)); |
278
|
|
|
} elseif (!$match->isOfficial() && $form->get('type')->getData() === Match::OFFICIAL) { |
279
|
|
|
$message = "You can't make this an official match."; |
280
|
|
|
$form->get('type')->addError(new FormError($message)); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: