|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\VoteManager\Plugins\Votes; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\VoteManager\Structures\Vote; |
|
6
|
|
|
use eXpansion\Framework\Core\Services\Application\DispatcherInterface; |
|
7
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
|
8
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AbstractVotePlugin |
|
12
|
|
|
* |
|
13
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
14
|
|
|
* @copyright 2017 eXpansion |
|
15
|
|
|
* @package eXpansion\Bundle\VoteManager\Plugins\Votes |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractVotePlugin |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var DispatcherInterface */ |
|
20
|
|
|
protected $dispatcher; |
|
21
|
|
|
|
|
22
|
|
|
/** @var PlayerStorage */ |
|
23
|
|
|
protected $playerStorage; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
protected $duration; |
|
27
|
|
|
|
|
28
|
|
|
/** @var float */ |
|
29
|
|
|
protected $ratio; |
|
30
|
|
|
|
|
31
|
|
|
/** @var Vote|null */ |
|
32
|
|
|
protected $currentVote = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AbstractVotePlugin constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param DispatcherInterface $dispatcher |
|
38
|
|
|
* @param PlayerStorage $playerStorage |
|
39
|
|
|
* @param int $duration |
|
40
|
|
|
* @param float $ratio |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public function __construct( |
|
43
|
|
|
DispatcherInterface $dispatcher, |
|
44
|
|
|
PlayerStorage $playerStorage, |
|
45
|
|
|
int $duration, |
|
46
|
|
|
float $ratio |
|
47
|
|
|
) { |
|
48
|
10 |
|
$this->dispatcher = $dispatcher; |
|
49
|
10 |
|
$this->playerStorage = $playerStorage; |
|
50
|
10 |
|
$this->duration = $duration; |
|
51
|
10 |
|
$this->ratio = $ratio; |
|
52
|
10 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Start a new vote. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Player $player |
|
58
|
|
|
* |
|
59
|
|
|
* @return Vote|null |
|
60
|
|
|
*/ |
|
61
|
7 |
|
public function start(Player $player, $params) |
|
62
|
|
|
{ |
|
63
|
7 |
|
$this->currentVote = new Vote($player, $this->getCode(), $params); |
|
64
|
7 |
|
return $this->currentVote; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Reset current vote session. |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function reset() |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->currentVote = null; |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* User votes yes |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $login |
|
79
|
|
|
*/ |
|
80
|
5 |
View Code Duplication |
public function castYes($login) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
5 |
|
if ($this->currentVote) { |
|
83
|
5 |
|
$this->currentVote->castYes($login); |
|
84
|
|
|
|
|
85
|
5 |
|
$player = $this->playerStorage->getPlayerInfo($login); |
|
86
|
5 |
|
$this->dispatcher->dispatch("votemanager.voteyes", [$player, $this->currentVote]); |
|
87
|
|
|
} |
|
88
|
5 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* User votes no |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $login |
|
94
|
|
|
*/ |
|
95
|
3 |
View Code Duplication |
public function castNo($login) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
3 |
|
if ($this->currentVote) { |
|
98
|
3 |
|
$this->currentVote->castNo($login); |
|
99
|
|
|
|
|
100
|
3 |
|
$player = $this->playerStorage->getPlayerInfo($login); |
|
101
|
3 |
|
$this->dispatcher->dispatch("votemanager.voteno", [$player, $this->currentVote]); |
|
102
|
|
|
} |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Cancel ongoing vote. |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function cancel() |
|
109
|
|
|
{ |
|
110
|
1 |
|
if (!$this->currentVote) { |
|
111
|
1 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$this->currentVote->setStatus(Vote::STATUS_CANCEL); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Update the status of the vote, and execute actions if vote passed. |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $time |
|
121
|
|
|
*/ |
|
122
|
8 |
|
public function update($time = null) |
|
123
|
|
|
{ |
|
124
|
8 |
|
if (!$this->currentVote || $this->currentVote->getStatus() == Vote::STATUS_CANCEL) { |
|
125
|
1 |
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
7 |
|
if (is_null($time)) { |
|
129
|
2 |
|
$time = time(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
7 |
|
$playerCount = count($this->playerStorage->getOnline()); |
|
133
|
|
|
|
|
134
|
|
|
// Check if vote passes when we suppose that all palyers that didn't vote would vote NO. |
|
135
|
7 |
View Code Duplication |
if ($playerCount > 0 && ($this->currentVote->getYes() / $playerCount) > $this->ratio) { |
|
|
|
|
|
|
136
|
2 |
|
$this->votePassed(); |
|
137
|
2 |
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// If the vote is still not decided wait for the end to decide. |
|
141
|
5 |
|
if (($time - $this->currentVote->getStartTime()) > $this->duration) { |
|
142
|
3 |
|
$totalVotes = $this->currentVote->getYes() + $this->currentVote->getNo() * 1.0; |
|
143
|
|
|
|
|
144
|
3 |
View Code Duplication |
if ($totalVotes >= 1 && ($this->currentVote->getYes() / $totalVotes) > $this->ratio) { |
|
|
|
|
|
|
145
|
1 |
|
$this->votePassed(); |
|
146
|
|
|
} else { |
|
147
|
2 |
|
$this->voteFailed(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
5 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return Vote|null |
|
154
|
|
|
*/ |
|
155
|
6 |
|
public function getCurrentVote() |
|
156
|
|
|
{ |
|
157
|
6 |
|
return $this->currentVote; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Pass a vote |
|
162
|
|
|
*/ |
|
163
|
|
|
public function pass() |
|
164
|
|
|
{ |
|
165
|
|
|
$this->currentVote->setStatus(Vote::STATUS_PASSED); |
|
166
|
|
|
$this->executeVotePassed(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Called when a vote passed. |
|
171
|
|
|
*/ |
|
172
|
3 |
|
protected function votePassed() |
|
173
|
|
|
{ |
|
174
|
3 |
|
$this->currentVote->setStatus(Vote::STATUS_PASSED); |
|
175
|
3 |
|
$this->executeVotePassed(); |
|
176
|
3 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Called when a vote failed. |
|
180
|
|
|
*/ |
|
181
|
2 |
|
protected function voteFailed() |
|
182
|
|
|
{ |
|
183
|
2 |
|
$this->currentVote->setStatus(Vote::STATUS_FAILED); |
|
184
|
2 |
|
$this->executeVoteFailed(); |
|
185
|
2 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return int |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getDuration(): int |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->duration; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return float |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getRatio(): float |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->ratio; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return int |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getElapsedTime() : int |
|
207
|
|
|
{ |
|
208
|
|
|
return time() - $this->currentVote->getStartTime(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get question text to display for this vote. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
abstract public function getQuestion(): string; |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get type code of this vote. |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
abstract public function getCode(): string; |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get native votes this votes replaces. |
|
227
|
|
|
* |
|
228
|
|
|
* @return string[] |
|
229
|
|
|
*/ |
|
230
|
|
|
abstract public function getReplacementTypes(): array; |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Called when vote is passed. |
|
234
|
|
|
* |
|
235
|
|
|
* @return void |
|
236
|
|
|
*/ |
|
237
|
|
|
abstract protected function executeVotePassed(); |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Called when vote is failed. |
|
241
|
|
|
*/ |
|
242
|
|
|
abstract protected function executeVoteFailed(); |
|
243
|
|
|
} |
|
244
|
|
|
|
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.