|
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
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $builder |
|
33
|
1 |
|
->add('first_team', new MatchTeamType(), array( |
|
34
|
1 |
|
'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
|
|
|
|
|
35
|
1 |
|
)) |
|
36
|
1 |
|
->add('second_team', new MatchTeamType(), array( |
|
37
|
1 |
|
'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
|
|
|
|
|
38
|
1 |
|
)) |
|
39
|
1 |
|
->add('duration', 'choice', array( |
|
40
|
1 |
|
'choices' => $durations, |
|
41
|
1 |
|
'constraints' => new NotBlank(), |
|
42
|
|
|
'expanded' => true |
|
43
|
1 |
|
)) |
|
44
|
1 |
|
->add('server_address', 'text', array( |
|
45
|
1 |
|
'required' => false, |
|
46
|
1 |
|
'attr' => array('placeholder' => 'brad.guleague.org:5100'), |
|
47
|
1 |
|
)) |
|
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
|
|
|
'message' => 'The timestamp of the match must not be in the future' |
|
54
|
1 |
|
)) |
|
55
|
1 |
|
), |
|
56
|
1 |
|
'data' => ($this->isEdit()) |
|
57
|
1 |
|
? $this->editing->getTimestamp()->setTimezone(\Controller::getMe()->getTimezone()) |
|
|
|
|
|
|
58
|
1 |
|
: \TimeDate::now(\Controller::getMe()->getTimezone()), |
|
59
|
1 |
|
'with_seconds' => $this->isEdit() |
|
60
|
1 |
|
)) |
|
61
|
1 |
|
->add('map', new ModelType('Map'), array( |
|
62
|
|
|
'required' => false |
|
63
|
1 |
|
)) |
|
64
|
1 |
|
->add('type', 'choice', array( |
|
65
|
|
|
'choices' => array( |
|
66
|
1 |
|
\Match::OFFICIAL => 'Official', |
|
67
|
1 |
|
\Match::FUN => 'Fun match', |
|
68
|
1 |
|
\Match::SPECIAL => 'Special event match', |
|
69
|
1 |
|
), |
|
70
|
1 |
|
'disabled' => $this->editing && $this->editing->isOfficial(), |
|
|
|
|
|
|
71
|
|
|
'label' => 'Match Type' |
|
72
|
1 |
|
)) |
|
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
|
|
|
if (!$match->isOfficial()) { |
|
119
|
|
|
$match->setTeamColors( |
|
120
|
|
|
$firstTeam->get('team')->getData(), |
|
121
|
|
|
$secondTeam->get('team')->getData() |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$match->setTeamPlayers( |
|
126
|
|
|
$this->getPlayerList($firstTeam), |
|
127
|
|
|
$this->getPlayerList($secondTeam) |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$match->setTeamPoints( |
|
131
|
|
|
$firstTeam->get('score')->getData(), |
|
132
|
|
|
$secondTeam->get('score')->getData() |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
$match->setDuration($form->get('duration')->getData()) |
|
136
|
|
|
->setServerAddress($form->get('server_address')->getData()) |
|
137
|
|
|
->setTimestamp($form->get('time')->getData()) |
|
138
|
|
|
->setMap($form->get('map')->getData()->getId()); |
|
139
|
|
|
|
|
140
|
|
|
if (!$match->isEloCorrect()) { |
|
141
|
|
|
$this->controller->recalculateNeeded = true; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function enter($form) |
|
149
|
|
|
{ |
|
150
|
1 |
|
$firstTeam = $form->get('first_team'); |
|
151
|
|
|
$secondTeam = $form->get('second_team'); |
|
152
|
1 |
|
|
|
153
|
1 |
|
$firstId = $firstTeam->get('team')->getData()->getId(); |
|
154
|
|
|
$secondId = $secondTeam->get('team')->getData()->getId(); |
|
155
|
1 |
|
|
|
156
|
1 |
|
$official = ($form->get('type')->getData() === \Match::OFFICIAL); |
|
157
|
|
|
|
|
158
|
1 |
|
$match = \Match::enterMatch( |
|
159
|
1 |
|
$official ? $firstId : null, |
|
160
|
|
|
$official ? $secondId : null, |
|
161
|
1 |
|
$firstTeam->get('score')->getData(), |
|
162
|
1 |
|
$secondTeam->get('score')->getData(), |
|
163
|
1 |
|
$form->get('duration')->getData(), |
|
164
|
1 |
|
$this->me->getId(), |
|
165
|
1 |
|
$form->get('time')->getData(), |
|
166
|
1 |
|
$this->getPlayerList($firstTeam), |
|
167
|
1 |
|
$this->getPlayerList($secondTeam), |
|
168
|
1 |
|
$form->get('server_address')->getData(), |
|
169
|
1 |
|
null, |
|
170
|
1 |
|
$form->get('map')->getData()->getId(), |
|
171
|
1 |
|
$form->get('type')->getData(), |
|
172
|
1 |
|
$official ? null : $firstId, |
|
173
|
1 |
|
$official ? null : $secondId |
|
174
|
1 |
|
); |
|
175
|
1 |
|
|
|
176
|
1 |
|
return $match; |
|
177
|
1 |
|
} |
|
178
|
1 |
|
|
|
179
|
|
|
/** |
|
180
|
1 |
|
* Get the player list of a team |
|
181
|
|
|
* |
|
182
|
|
|
* @param FormInterface $team A MatchTeamType form |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
|
|
private function getPlayerList(FormInterface $team) |
|
186
|
|
|
{ |
|
187
|
|
|
return array_map($this->getModelToID(), $team->get('participants')->getData()); |
|
188
|
|
|
} |
|
189
|
1 |
|
|
|
190
|
|
|
/** |
|
191
|
1 |
|
* Get a function which converts models to their IDs |
|
192
|
|
|
* |
|
193
|
|
|
* Useful to store the match players into the database |
|
194
|
|
|
*/ |
|
195
|
|
|
private static function getModelToID() |
|
196
|
|
|
{ |
|
197
|
|
|
return function ($model) { |
|
198
|
|
|
return $model->getId(); |
|
199
|
|
|
}; |
|
200
|
1 |
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
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: