1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a form creator for Matches |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BZIon\Form\Creator; |
9
|
|
|
|
10
|
|
|
use BZIon\Form\Type\DatetimeWithTimezoneType; |
11
|
|
|
use BZIon\Form\Type\MatchTeamType; |
12
|
|
|
use BZIon\Form\Type\ModelType; |
13
|
|
|
use Symfony\Component\Form\FormInterface; |
14
|
|
|
use Symfony\Component\Validator\Constraints\LessThan; |
15
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Form creator for matches |
19
|
|
|
*/ |
20
|
|
|
class MatchFormCreator extends ModelFormCreator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
1 |
|
protected function build($builder) |
26
|
|
|
{ |
27
|
1 |
|
$durations = \Service::getParameter('bzion.league.duration'); |
28
|
1 |
|
foreach ($durations as $duration => &$value) { |
29
|
1 |
|
$durations[$duration] = $duration; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $builder |
33
|
1 |
|
->add('first_team', new MatchTeamType(), array( |
34
|
1 |
|
'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
|
|
|
35
|
|
|
)) |
36
|
1 |
|
->add('second_team', new MatchTeamType(), array( |
37
|
1 |
|
'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
|
|
|
38
|
|
|
)) |
39
|
1 |
|
->add('duration', 'choice', array( |
40
|
1 |
|
'choices' => $durations, |
41
|
1 |
|
'constraints' => new NotBlank(), |
42
|
|
|
'expanded' => true |
43
|
|
|
)) |
44
|
1 |
|
->add('server_address', 'text', array( |
45
|
1 |
|
'required' => false, |
46
|
|
|
'attr' => array('placeholder' => 'brad.guleague.org:5100'), |
47
|
|
|
)) |
48
|
1 |
|
->add('time', new DatetimeWithTimezoneType(), array( |
49
|
|
|
'constraints' => array( |
50
|
1 |
|
new NotBlank(), |
51
|
1 |
|
new LessThan(array( |
52
|
1 |
|
'value' => \TimeDate::now()->addMinutes(10), |
53
|
1 |
|
'message' => 'The timestamp of the match must not be in the future' |
54
|
|
|
)) |
55
|
|
|
), |
56
|
1 |
|
'data' => ($this->isEdit()) |
57
|
|
|
? $this->editing->getTimestamp()->setTimezone(\Controller::getMe()->getTimezone()) |
|
|
|
|
58
|
1 |
|
: \TimeDate::now(\Controller::getMe()->getTimezone()), |
59
|
1 |
|
'with_seconds' => $this->isEdit() |
60
|
|
|
)) |
61
|
1 |
|
->add('map', new ModelType('Map'), array( |
62
|
|
|
'required' => false |
63
|
1 |
|
)) |
64
|
1 |
|
->add('type', 'choice', array( |
65
|
|
|
'choices' => array( |
66
|
|
|
\Match::OFFICIAL => 'Official', |
67
|
|
|
\Match::FUN => 'Fun match', |
68
|
|
|
\Match::SPECIAL => 'Special event match', |
69
|
1 |
|
), |
70
|
1 |
|
'disabled' => $this->editing && $this->editing->isOfficial(), |
|
|
|
|
71
|
1 |
|
'label' => 'Match Type' |
72
|
|
|
)) |
73
|
1 |
|
->add('enter', 'submit'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
* |
79
|
|
|
* @param \Match $match |
80
|
|
|
*/ |
81
|
|
|
public function fill($form, $match) |
82
|
|
|
{ |
83
|
|
|
$form->get('first_team')->setData(array( |
84
|
|
|
'team' => $match->getTeamA(), |
85
|
|
|
'participants' => $match->getTeamAPlayers(), |
86
|
|
|
'score' => $match->getTeamAPoints() |
87
|
|
|
)); |
88
|
|
|
$form->get('second_team')->setData(array( |
89
|
|
|
'team' => $match->getTeamB(), |
90
|
|
|
'participants' => $match->getTeamBPlayers(), |
91
|
|
|
'score' => $match->getTeamBPoints() |
92
|
|
|
)); |
93
|
|
|
|
94
|
|
|
$form->get('duration')->setData($match->getDuration()); |
95
|
|
|
$form->get('server_address')->setData($match->getServerAddress()); |
96
|
|
|
$form->get('time')->setData($match->getTimestamp()); |
97
|
|
|
$form->get('map')->setData($match->getMap()); |
98
|
|
|
$form->get('type')->setData($match->getMatchType()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
* |
104
|
|
|
* @param \Match $match |
105
|
|
|
*/ |
106
|
|
|
public function update($form, $match) |
107
|
|
|
{ |
108
|
|
|
if (($match->getDuration() != $form->get('duration')->getData()) |
109
|
|
|
|| $match->getTimestamp()->ne($form->get('time')->getData())) { |
110
|
|
|
// The timestamp of the match was changed, we might need to |
111
|
|
|
// recalculate its ELO |
112
|
|
|
$this->controller->recalculateNeeded = true; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$firstTeam = $form->get('first_team'); |
116
|
|
|
$secondTeam = $form->get('second_team'); |
117
|
|
|
|
118
|
|
|
$serverInfo = $this->getServerInfo($form->get('server_address')); |
119
|
|
|
|
120
|
|
|
if (!$match->isOfficial()) { |
121
|
|
|
$match->setTeamColors( |
122
|
|
|
$firstTeam->get('team')->getData(), |
123
|
|
|
$secondTeam->get('team')->getData() |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$match->setTeamPlayers( |
128
|
|
|
$this->getPlayerList($firstTeam), |
129
|
|
|
$this->getPlayerList($secondTeam) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$match->setTeamPoints( |
133
|
|
|
$firstTeam->get('score')->getData(), |
134
|
|
|
$secondTeam->get('score')->getData() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$match->setDuration($form->get('duration')->getData()) |
138
|
|
|
->setServerAddress($serverInfo[0], $serverInfo[1]) |
139
|
|
|
->setTimestamp($form->get('time')->getData()) |
140
|
|
|
->setMap($form->get('map')->getData()->getId()); |
141
|
|
|
|
142
|
|
|
if (!$match->isEloCorrect()) { |
143
|
|
|
$this->controller->recalculateNeeded = true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
1 |
|
public function enter($form) |
151
|
|
|
{ |
152
|
1 |
|
$firstTeam = $form->get('first_team'); |
153
|
1 |
|
$secondTeam = $form->get('second_team'); |
154
|
|
|
|
155
|
1 |
|
$firstId = $firstTeam->get('team')->getData()->getId(); |
156
|
1 |
|
$secondId = $secondTeam->get('team')->getData()->getId(); |
157
|
|
|
|
158
|
1 |
|
$official = ($form->get('type')->getData() === \Match::OFFICIAL); |
159
|
1 |
|
$serverInfo = $this->getServerInfo($form->get('server_address')); |
160
|
|
|
|
161
|
1 |
|
$match = \Match::enterMatch( |
162
|
1 |
|
$official ? $firstId : null, |
163
|
1 |
|
$official ? $secondId : null, |
164
|
1 |
|
$firstTeam->get('score')->getData(), |
165
|
1 |
|
$secondTeam->get('score')->getData(), |
166
|
1 |
|
$form->get('duration')->getData(), |
167
|
1 |
|
$this->me->getId(), |
168
|
1 |
|
$form->get('time')->getData(), |
169
|
1 |
|
$this->getPlayerList($firstTeam), |
170
|
1 |
|
$this->getPlayerList($secondTeam), |
171
|
1 |
|
$serverInfo[0], |
172
|
1 |
|
$serverInfo[1], |
173
|
1 |
|
null, |
174
|
1 |
|
$form->get('map')->getData()->getId(), |
175
|
1 |
|
$form->get('type')->getData(), |
176
|
1 |
|
$official ? null : $firstId, |
177
|
1 |
|
$official ? null : $secondId |
178
|
|
|
); |
179
|
|
|
|
180
|
1 |
|
return $match; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the player list of a team |
185
|
|
|
* |
186
|
|
|
* @param FormInterface $team A MatchTeamType form |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
1 |
|
private function getPlayerList(FormInterface $team) |
190
|
|
|
{ |
191
|
1 |
|
return array_map($this->getModelToID(), $team->get('participants')->getData()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the server address and port of a match |
196
|
|
|
* |
197
|
|
|
* @param FormInterface $server A text form representing the server |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
1 |
|
private function getServerInfo(FormInterface $server) |
201
|
|
|
{ |
202
|
1 |
|
$serverInfo = explode(':', $server->getData()); |
203
|
1 |
|
if (!isset($serverInfo[1])) { |
204
|
1 |
|
$serverInfo[1] = 5154; |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
return $serverInfo; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get a function which converts models to their IDs |
212
|
|
|
* |
213
|
|
|
* Useful to store the match players into the database |
214
|
|
|
*/ |
215
|
|
|
private static function getModelToID() |
216
|
|
|
{ |
217
|
1 |
|
return function ($model) { |
218
|
1 |
|
return $model->getId(); |
219
|
1 |
|
}; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
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: