|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\VoteManager\Services; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\VoteManager\Plugins\Votes\AbstractVotePlugin; |
|
6
|
|
|
use eXpansion\Bundle\VoteManager\Structures\Vote; |
|
7
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification; |
|
8
|
|
|
use eXpansion\Framework\Core\Services\Application\Dispatcher; |
|
9
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
10
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
|
11
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
12
|
|
|
|
|
13
|
|
|
class VoteService |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Console */ |
|
16
|
|
|
protected $console; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Connection */ |
|
19
|
|
|
protected $connection; |
|
20
|
|
|
|
|
21
|
|
|
/** @var ChatNotification */ |
|
22
|
|
|
protected $chatNotification; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Dispatcher */ |
|
25
|
|
|
protected $dispatcher; |
|
26
|
|
|
|
|
27
|
|
|
/** @var AbstractVotePlugin[] */ |
|
28
|
|
|
protected $votePlugins = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array mapping between native MP votes and equivalent expansion votes. */ |
|
31
|
|
|
protected $voteMapping = []; |
|
32
|
|
|
|
|
33
|
|
|
/** @var AbstractVotePlugin */ |
|
34
|
|
|
protected $currentVote = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* VoteManager constructor. |
|
38
|
|
|
* @param Console $console |
|
39
|
|
|
* @param Connection $connection |
|
40
|
|
|
* @param ChatNotification $chatNotification |
|
41
|
|
|
* @param Dispatcher $dispatcher |
|
42
|
|
|
* @param AbstractVotePlugin[] $voteFactories |
|
43
|
|
|
*/ |
|
44
|
10 |
|
public function __construct( |
|
45
|
|
|
Console $console, |
|
46
|
|
|
Connection $connection, |
|
47
|
|
|
ChatNotification $chatNotification, |
|
48
|
|
|
Dispatcher $dispatcher, |
|
49
|
|
|
$voteFactories |
|
50
|
|
|
) { |
|
51
|
10 |
|
$this->console = $console; |
|
52
|
10 |
|
$this->connection = $connection; |
|
53
|
10 |
|
$this->dispatcher = $dispatcher; |
|
54
|
10 |
|
$this->chatNotification = $chatNotification; |
|
55
|
|
|
|
|
56
|
10 |
|
foreach ($voteFactories as $voteFactory) { |
|
57
|
10 |
|
$this->votePlugins[$voteFactory->getCode()] = $voteFactory; |
|
58
|
|
|
|
|
59
|
10 |
|
foreach ($voteFactory->getReplacementTypes() as $replaces) { |
|
60
|
10 |
|
$this->voteMapping[$replaces] = $voteFactory->getCode(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
10 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Reset ongoing vote. |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function reset() |
|
69
|
|
|
{ |
|
70
|
4 |
|
if ($this->currentVote) { |
|
71
|
1 |
|
$this->currentVote->reset(); |
|
72
|
|
|
} |
|
73
|
4 |
|
$this->currentVote = null; |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Cast vote |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $login |
|
80
|
|
|
* @param string $type |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function castVote($login, $type) |
|
83
|
|
|
{ |
|
84
|
2 |
|
if ($this->currentVote instanceof AbstractVotePlugin) { |
|
85
|
|
|
switch ($type) { |
|
86
|
2 |
|
case Vote::VOTE_YES: |
|
87
|
1 |
|
$this->currentVote->castYes($login); |
|
88
|
1 |
|
break; |
|
89
|
1 |
|
case Vote::VOTE_NO: |
|
90
|
1 |
|
$this->currentVote->castNo($login); |
|
91
|
1 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Update the status of the vote. |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public function update() |
|
100
|
|
|
{ |
|
101
|
4 |
|
if ($this->currentVote) { |
|
102
|
4 |
|
$vote = $this->currentVote->getCurrentVote(); |
|
103
|
4 |
|
$this->currentVote->update(time()); |
|
104
|
|
|
|
|
105
|
4 |
|
switch ($vote->getStatus()) { |
|
106
|
4 |
View Code Duplication |
case Vote::STATUS_CANCEL: |
|
|
|
|
|
|
107
|
1 |
|
$this->dispatcher->dispatch("votemanager.votecancelled", |
|
108
|
1 |
|
[$vote->getPlayer(), $vote->getType(), $vote]); |
|
109
|
1 |
|
$this->currentVote = null; |
|
110
|
1 |
|
$this->reset(); |
|
111
|
1 |
|
break; |
|
112
|
3 |
View Code Duplication |
case Vote::STATUS_FAILED: |
|
|
|
|
|
|
113
|
1 |
|
$this->dispatcher->dispatch("votemanager.votefailed", |
|
114
|
1 |
|
[$vote->getPlayer(), $vote->getType(), $vote]); |
|
115
|
1 |
|
$this->currentVote = null; |
|
116
|
1 |
|
$this->reset(); |
|
117
|
1 |
|
break; |
|
118
|
2 |
View Code Duplication |
case Vote::STATUS_PASSED: |
|
|
|
|
|
|
119
|
1 |
|
$this->dispatcher->dispatch("votemanager.votepassed", |
|
120
|
1 |
|
[$vote->getPlayer(), $vote->getType(), $vote]); |
|
121
|
1 |
|
$this->currentVote = null; |
|
122
|
1 |
|
$this->reset(); |
|
123
|
1 |
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
4 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Cancel ongoing vote. |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function cancel() |
|
132
|
|
|
{ |
|
133
|
1 |
|
if (!$this->currentVote) { |
|
134
|
1 |
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
$this->currentVote->cancel(); |
|
138
|
1 |
|
$this->update(); |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Pass ongoing vote. |
|
143
|
|
|
*/ |
|
144
|
|
|
public function pass() |
|
145
|
|
|
{ |
|
146
|
|
|
if (!$this->currentVote) { |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$this->currentVote->pass(); |
|
151
|
|
|
$this->update(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return AbstractVotePlugin |
|
157
|
|
|
*/ |
|
158
|
10 |
|
public function getCurrentVote() |
|
159
|
|
|
{ |
|
160
|
10 |
|
return $this->currentVote; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Start a vote. |
|
165
|
|
|
* |
|
166
|
|
|
* @param Player $player |
|
167
|
|
|
* @param string $typeCode |
|
168
|
|
|
* @param array $params |
|
169
|
|
|
*/ |
|
170
|
10 |
|
public function startVote(Player $player, $typeCode, $params) |
|
171
|
|
|
{ |
|
172
|
10 |
|
if ($this->getCurrentVote() !== null) { |
|
173
|
1 |
|
$this->chatNotification->sendMessage("expansion_votemanager.error.in_progress"); |
|
174
|
1 |
|
return; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
10 |
|
if (isset($this->voteMapping[$typeCode])) { |
|
178
|
9 |
|
$typeCode = $this->voteMapping[$typeCode]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
10 |
|
if (!isset($this->votePlugins[$typeCode])) { |
|
182
|
1 |
|
$this->chatNotification->sendMessage("|error| Unknown vote type : $typeCode"); |
|
183
|
1 |
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
9 |
|
$this->currentVote = $this->votePlugins[$typeCode]; |
|
187
|
9 |
|
$this->currentVote->start($player, $params); |
|
188
|
9 |
|
$this->connection->cancelVote(); |
|
189
|
|
|
|
|
190
|
9 |
|
$this->dispatcher->dispatch( |
|
191
|
9 |
|
"votemanager.votenew", |
|
192
|
9 |
|
[$player, $this->currentVote->getCode(), $this->currentVote->getCurrentVote()] |
|
193
|
|
|
); |
|
194
|
9 |
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.