|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\VoteManager\Services; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\VoteManager\Services\VoteFactories\AbstractFactory; |
|
6
|
|
|
use eXpansion\Bundle\VoteManager\Structures\AbstractVote; |
|
7
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer; |
|
8
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification; |
|
9
|
|
|
use eXpansion\Framework\Core\Services\Application\Dispatcher; |
|
10
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
11
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
|
12
|
|
|
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyVote; |
|
13
|
|
|
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap; |
|
14
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
15
|
|
|
use Maniaplanet\DedicatedServer\Structures\Map; |
|
16
|
|
|
|
|
17
|
|
|
class VoteService implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer, ListenerInterfaceMpScriptMap |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Console */ |
|
20
|
|
|
protected $console; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Connection */ |
|
23
|
|
|
protected $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ChatNotification */ |
|
26
|
|
|
protected $chatNotification; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Dispatcher */ |
|
29
|
|
|
protected $dispatcher; |
|
30
|
|
|
|
|
31
|
|
|
/** @var AbstractFactory[] */ |
|
32
|
|
|
protected $voteFactories = []; |
|
33
|
|
|
|
|
34
|
|
|
/** @var array mapping between native MP votes and equivalent expansion votes. */ |
|
35
|
|
|
protected $voteMapping = []; |
|
36
|
|
|
|
|
37
|
|
|
/** @var null|AbstractVote */ |
|
38
|
|
|
protected $currentVote = null; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
protected $votesStarted = []; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* VoteManager constructor. |
|
47
|
|
|
* @param Console $console |
|
48
|
|
|
* @param Connection $connection |
|
49
|
|
|
* @param ChatNotification $chatNotification |
|
50
|
|
|
* @param Dispatcher $dispatcher |
|
51
|
|
|
* @param AbstractFactory[] $voteFactories |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
Console $console, |
|
55
|
|
|
Connection $connection, |
|
56
|
|
|
ChatNotification $chatNotification, |
|
57
|
|
|
Dispatcher $dispatcher, |
|
58
|
|
|
$voteFactories |
|
59
|
|
|
) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->console = $console; |
|
62
|
|
|
$this->connection = $connection; |
|
63
|
|
|
$this->dispatcher = $dispatcher; |
|
64
|
|
|
$this->chatNotification = $chatNotification; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($voteFactories as $voteFactory) { |
|
67
|
|
|
$this->voteFactories[$voteFactory->getVoteCode()] = $voteFactory; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($voteFactory->getReplacementTypes() as $replaces) { |
|
70
|
|
|
$this->voteMapping[$replaces] = $voteFactory->getVoteCode(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* When a new vote is addressed |
|
77
|
|
|
* |
|
78
|
|
|
* @param Player $player |
|
79
|
|
|
* @param string $cmdName |
|
80
|
|
|
* @param string $cmdValue |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function onVoteNew(Player $player, $cmdName, $cmdValue) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!($cmdValue instanceof AbstractVote)) { |
|
87
|
|
|
if (isset($this->voteMapping[$cmdName])) { |
|
88
|
|
|
// disable default vote |
|
89
|
|
|
$this->connection->cancelVote(); |
|
90
|
|
|
$this->startVote($player, $this->voteMapping[$cmdName]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* When vote gets cancelled |
|
97
|
|
|
* |
|
98
|
|
|
* @param Player $player |
|
99
|
|
|
* @param string $cmdName |
|
100
|
|
|
* @param string $cmdValue |
|
101
|
|
|
* |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
public function onVoteCancelled(Player $player, $cmdName, $cmdValue) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($cmdName == null && $cmdValue == null && $this->currentVote instanceof AbstractVote) { |
|
107
|
|
|
$this->currentVote->setStatus(Vote::STATUS_CANCEL); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($cmdValue instanceof AbstractVote) { |
|
111
|
|
|
$this->currentVote = null; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* When vote Passes |
|
117
|
|
|
* @param Player $player |
|
118
|
|
|
* @param string $cmdName |
|
119
|
|
|
* @param string $cmdValue |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
public function onVotePassed(Player $player, $cmdName, $cmdValue) |
|
124
|
|
|
{ |
|
125
|
|
|
if ($cmdValue instanceof AbstractVote) { |
|
126
|
|
|
$this->currentVote = null; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* When vote Fails |
|
132
|
|
|
* @param Player $player |
|
133
|
|
|
* @param string $cmdName |
|
134
|
|
|
* @param string $cmdValue |
|
135
|
|
|
* |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
public function onVoteFailed(Player $player, $cmdName, $cmdValue) |
|
139
|
|
|
{ |
|
140
|
|
|
if ($cmdValue instanceof AbstractVote) { |
|
141
|
|
|
$this->currentVote = null; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function onPreLoop() |
|
146
|
|
|
{ |
|
147
|
|
|
// do nothing |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function onPostLoop() |
|
151
|
|
|
{ |
|
152
|
|
|
//do nothing |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function updateVote($login, $type) |
|
156
|
|
|
{ |
|
157
|
|
|
if ($this->currentVote instanceof AbstractVote) { |
|
158
|
|
|
switch ($type) { |
|
159
|
|
|
case AbstractVote::VOTE_YES: |
|
160
|
|
|
$this->currentVote->castYes($login); |
|
161
|
|
|
break; |
|
162
|
|
|
case AbstractVote::VOTE_NO: |
|
163
|
|
|
$this->currentVote->castNo($login); |
|
164
|
|
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function onEverySecond() |
|
170
|
|
|
{ |
|
171
|
|
|
$vote = $this->currentVote; |
|
172
|
|
|
|
|
173
|
|
|
if ($vote !== null) { |
|
174
|
|
|
$vote->updateVote(time()); |
|
175
|
|
|
|
|
176
|
|
|
switch ($vote->getStatus()) { |
|
177
|
|
View Code Duplication |
case AbstractVote::STATUS_CANCEL: |
|
|
|
|
|
|
178
|
|
|
$this->dispatcher->dispatch("votemanager.votecancelled", |
|
179
|
|
|
[$vote->getPlayer(), $vote->getType(), $this->currentVote]); |
|
180
|
|
|
break; |
|
181
|
|
View Code Duplication |
case AbstractVote::STATUS_FAILED: |
|
|
|
|
|
|
182
|
|
|
$this->dispatcher->dispatch("votemanager.votefailed", |
|
183
|
|
|
[$vote->getPlayer(), $vote->getType(), $this->currentVote]); |
|
184
|
|
|
break; |
|
185
|
|
View Code Duplication |
case AbstractVote::STATUS_PASSED: |
|
|
|
|
|
|
186
|
|
|
$this->dispatcher->dispatch("votemanager.votepassed", |
|
187
|
|
|
[$vote->getPlayer(), $vote->getType(), $this->currentVote]); |
|
188
|
|
|
break; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return AbstractVote |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getCurrentVote() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->currentVote; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
public function startVote(Player $player, $type) |
|
203
|
|
|
{ |
|
204
|
|
|
if ($this->getCurrentVote() !== null) { |
|
205
|
|
|
$this->chatNotification->sendMessage("expansion_votemanager.error.in_progress"); |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if (!isset($this->voteFactories[$type])) { |
|
210
|
|
|
$this->chatNotification->sendMessage("|error| Unknown vote type : $type"); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if (array_key_exists($type, $this->votesStarted) == false) { |
|
|
|
|
|
|
214
|
|
|
$this->votesStarted[$type] = "yep"; |
|
215
|
|
|
$this->currentVote = $this->voteFactories[$type]->create($player); |
|
216
|
|
|
$this->dispatcher->dispatch("votemanager.votenew", [$player, $type, $this->currentVote]); |
|
217
|
|
|
} else { |
|
218
|
|
|
$this->chatNotification->sendMessage("expansion_votemanager.error.already_started"); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Callback sent when the "StartMap" section start. |
|
225
|
|
|
* |
|
226
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
|
227
|
|
|
* @param int $time Server time when the callback was sent |
|
228
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
|
229
|
|
|
* @param Map $map Map started with. |
|
230
|
|
|
* |
|
231
|
|
|
* @return void |
|
232
|
|
|
*/ |
|
233
|
|
|
public function onStartMapStart($count, $time, $restarted, Map $map) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->votesStarted = []; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Callback sent when the "StartMap" section end. |
|
240
|
|
|
* |
|
241
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
|
242
|
|
|
* @param int $time Server time when the callback was sent |
|
243
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
|
244
|
|
|
* @param Map $map Map started with. |
|
245
|
|
|
* |
|
246
|
|
|
* @return void |
|
247
|
|
|
*/ |
|
248
|
|
|
public function onStartMapEnd($count, $time, $restarted, Map $map) |
|
249
|
|
|
{ |
|
250
|
|
|
// Nothing |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Callback sent when the "EndMap" section start. |
|
255
|
|
|
* |
|
256
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
|
257
|
|
|
* @param int $time Server time when the callback was sent |
|
258
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
|
259
|
|
|
* @param Map $map Map started with. |
|
260
|
|
|
* |
|
261
|
|
|
* @return void |
|
262
|
|
|
*/ |
|
263
|
|
|
public function onEndMapStart($count, $time, $restarted, Map $map) |
|
264
|
|
|
{ |
|
265
|
|
|
if ($this->currentVote instanceof Vote) { |
|
|
|
|
|
|
266
|
|
|
$this->currentVote->setStatus(Vote::STATUS_CANCEL); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Callback sent when the "EndMap" section end. |
|
272
|
|
|
* |
|
273
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
|
274
|
|
|
* @param int $time Server time when the callback was sent |
|
275
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
|
276
|
|
|
* @param Map $map Map started with. |
|
277
|
|
|
* |
|
278
|
|
|
* @return void |
|
279
|
|
|
*/ |
|
280
|
|
|
public function onEndMapEnd($count, $time, $restarted, Map $map) |
|
281
|
|
|
{ |
|
282
|
|
|
// Nothing |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.