1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\MxKarma\Plugins; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use eXpansion\Bundle\MxKarma\DataProviders\Listeners\ListenerInterfaceMxKarma; |
7
|
|
|
use eXpansion\Bundle\MxKarma\Entity\MxRating; |
8
|
|
|
use eXpansion\Bundle\MxKarma\Entity\MxVote; |
9
|
|
|
use eXpansion\Bundle\MxKarma\Services\MxKarmaService; |
10
|
|
|
use eXpansion\Framework\Config\Model\ConfigInterface; |
11
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication; |
12
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification; |
13
|
|
|
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface; |
14
|
|
|
use eXpansion\Framework\Core\Services\Application\Dispatcher; |
15
|
|
|
use eXpansion\Framework\Core\Services\Console; |
16
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
17
|
|
|
use eXpansion\Framework\Core\Storage\GameDataStorage; |
18
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
19
|
|
|
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyChat; |
20
|
|
|
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap; |
21
|
|
|
use Maniaplanet\DedicatedServer\Structures\Map; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class MxKarma implements StatusAwarePluginInterface, |
25
|
|
|
ListenerInterfaceMpScriptMap, |
26
|
|
|
ListenerInterfaceMpLegacyChat, |
27
|
|
|
ListenerInterfaceExpApplication, |
28
|
|
|
ListenerInterfaceMxKarma |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** @var MxVote[] */ |
33
|
|
|
protected $changedVotes = []; |
34
|
|
|
|
35
|
|
|
/** @var int */ |
36
|
|
|
private $startTime; |
37
|
|
|
/** |
38
|
|
|
* @var object |
39
|
|
|
*/ |
40
|
|
|
protected $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Console |
44
|
|
|
*/ |
45
|
|
|
protected $console; |
46
|
|
|
/** |
47
|
|
|
* @var Dispatcher |
48
|
|
|
*/ |
49
|
|
|
protected $dispatcher; |
50
|
|
|
/** |
51
|
|
|
* @var ChatNotification |
52
|
|
|
*/ |
53
|
|
|
protected $chatNotification; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var MxKarmaService |
57
|
|
|
*/ |
58
|
|
|
private $mxKarma; |
59
|
|
|
|
60
|
|
|
/** @var MxRating */ |
61
|
|
|
private $mxRating; |
62
|
|
|
|
63
|
|
|
/** @var MxVote[] */ |
64
|
|
|
private $votes; |
65
|
|
|
/** |
66
|
|
|
* @var PlayerStorage |
67
|
|
|
*/ |
68
|
|
|
private $playerStorage; |
69
|
|
|
/** |
70
|
|
|
* @var ConfigInterface |
71
|
|
|
*/ |
72
|
|
|
private $apikey; |
73
|
|
|
/** |
74
|
|
|
* @var ConfigInterface |
75
|
|
|
*/ |
76
|
|
|
private $serverLogin; |
77
|
|
|
/** |
78
|
|
|
* @var GameDataStorage |
79
|
|
|
*/ |
80
|
|
|
private $gameDataStorage; |
81
|
|
|
/** |
82
|
|
|
* @var ConfigInterface |
83
|
|
|
*/ |
84
|
|
|
private $enabled; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* MxKarma constructor. |
88
|
|
|
* @param ConfigInterface $enabled |
89
|
|
|
* @param ConfigInterface $apikey |
90
|
|
|
* @param ConfigInterface $serverLogin |
91
|
|
|
* @param MxKarmaService $mxKarma |
92
|
|
|
* @param Console $console |
93
|
|
|
* @param ChatNotification $chatNotification |
94
|
|
|
* @param Dispatcher $dispatcher |
95
|
|
|
* @param PlayerStorage $playerStorage |
96
|
|
|
* @param GameDataStorage $gameDataStorage |
97
|
|
|
*/ |
98
|
|
|
public function __construct( |
99
|
|
|
ConfigInterface $enabled, |
100
|
|
|
ConfigInterface $apikey, |
101
|
|
|
ConfigInterface $serverLogin, |
102
|
|
|
MxKarmaService $mxKarma, |
103
|
|
|
Console $console, |
104
|
|
|
ChatNotification $chatNotification, |
105
|
|
|
Dispatcher $dispatcher, |
106
|
|
|
PlayerStorage $playerStorage, |
107
|
|
|
GameDataStorage $gameDataStorage |
108
|
|
|
) { |
109
|
|
|
|
110
|
|
|
$this->console = $console; |
111
|
|
|
$this->dispatcher = $dispatcher; |
112
|
|
|
$this->chatNotification = $chatNotification; |
113
|
|
|
$this->mxKarma = $mxKarma; |
114
|
|
|
$this->playerStorage = $playerStorage; |
115
|
|
|
$this->apikey = $apikey; |
116
|
|
|
$this->serverLogin = $serverLogin; |
117
|
|
|
$this->gameDataStorage = $gameDataStorage; |
118
|
|
|
$this->enabled = $enabled; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* sets vote value |
123
|
|
|
* @param $login |
124
|
|
|
* @param $vote |
125
|
|
|
*/ |
126
|
|
|
public function setVote($login, $vote) |
127
|
|
|
{ |
128
|
|
|
$player = $this->playerStorage->getPlayerInfo($login); |
129
|
|
|
|
130
|
|
|
$obj = [ |
131
|
|
|
"login" => $login, |
132
|
|
|
"nickname" => $player->getNickName(), |
133
|
|
|
"vote" => $vote, |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
$this->changedVotes[$player->getLogin()] = new MxVote((object)$obj); |
137
|
|
|
$this->chatNotification->sendMessage('expansion_mxkarma.chat.votechanged', $login); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the status of the plugin |
143
|
|
|
* |
144
|
|
|
* @param boolean $status |
145
|
|
|
* |
146
|
|
|
* @return null |
147
|
|
|
*/ |
148
|
|
|
public function setStatus($status) |
149
|
|
|
{ |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Called when a player chats. |
155
|
|
|
* |
156
|
|
|
* @param Player $player |
157
|
|
|
* @param $text |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function onPlayerChat(Player $player, $text) |
162
|
|
|
{ |
163
|
|
|
if ($player->getPlayerId() === 0) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
if (substr($text, 0, 1) == "/") { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($text == "++") { |
171
|
|
|
$this->setVote($player->getLogin(), 100); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($text == "--") { |
175
|
|
|
$this->setVote($player->getLogin(), 0); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* called at eXpansion init |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function onApplicationInit() |
187
|
|
|
{ |
188
|
|
|
// do nothing |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* called when init is done and callbacks are enabled |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function onApplicationReady() |
197
|
|
|
{ |
198
|
|
|
$this->startTime = time(); |
199
|
|
|
$this->connect(); |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* called when requesting application stop |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
|
|
public function onApplicationStop() |
209
|
|
|
{ |
210
|
|
|
// do nothing |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* |
215
|
|
|
*/ |
216
|
|
|
public function onMxKarmaConnect() |
217
|
|
|
{ |
218
|
|
|
$this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param MxRating $mxRating |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
public function onMxKarmaVoteLoad(MxRating $mxRating) |
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
$this->mxRating = $mxRating; |
229
|
|
|
$this->changedVotes = []; |
230
|
|
|
$this->votes = $mxRating->getVotes(); |
231
|
|
|
|
232
|
|
|
$total = $mxRating->getVoteAverage(); |
233
|
|
|
|
234
|
|
|
$yes = 0; |
235
|
|
|
$no = 0; |
236
|
|
|
if ($mxRating->getVoteAverage() > 0) { |
237
|
|
|
$yes = round(($mxRating->getVoteAverage() / 100) * $mxRating->getVoteCount()); |
238
|
|
|
|
239
|
|
|
$no = round(((100 - $mxRating->getVoteAverage()) / 100) * $mxRating->getVoteCount()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->chatNotification->sendMessage('expansion_mxkarma.chat.votesloaded', null, |
243
|
|
|
["%total%" => round($total, 2), "%positive%" => $yes, "%negative%" => $no]); |
244
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param MxVote[] $updatedVotes |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function onMxKarmaVoteSave($updatedVotes) |
252
|
|
|
{ |
253
|
|
|
// do nothing |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function onMxKarmaDisconnect() |
257
|
|
|
{ |
258
|
|
|
// do nothing |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Callback sent when the "StartMap" section start. |
263
|
|
|
* |
264
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
265
|
|
|
* @param int $time Server time when the callback was sent |
266
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
267
|
|
|
* @param Map $map Map started with. |
268
|
|
|
* |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
|
public function onStartMapStart($count, $time, $restarted, Map $map) |
272
|
|
|
{ |
273
|
|
|
if ($this->enabled->get()) { |
274
|
|
|
if ($this->mxKarma->isConnected() == false) { |
|
|
|
|
275
|
|
|
$this->console->writeln("> MxKarma is at disconnected state, trying to establish connection."); |
276
|
|
|
$this->connect(); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Callback sent when the "StartMap" section end. |
283
|
|
|
* |
284
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
285
|
|
|
* @param int $time Server time when the callback was sent |
286
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
287
|
|
|
* @param Map $map Map started with. |
288
|
|
|
* |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
public function onStartMapEnd($count, $time, $restarted, Map $map) |
292
|
|
|
{ |
293
|
|
|
$this->startTime = time(); |
294
|
|
|
$this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Callback sent when the "EndMap" section start. |
299
|
|
|
* |
300
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
301
|
|
|
* @param int $time Server time when the callback was sent |
302
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
303
|
|
|
* @param Map $map Map started with. |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function onEndMapStart($count, $time, $restarted, Map $map) |
308
|
|
|
{ |
309
|
|
|
// do nothing |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Callback sent when the "EndMap" section end. |
314
|
|
|
* |
315
|
|
|
* @param int $count Each time this section is played, this number is incremented by one |
316
|
|
|
* @param int $time Server time when the callback was sent |
317
|
|
|
* @param boolean $restarted true if the map was restarted, false otherwise |
318
|
|
|
* @param Map $map Map started with. |
319
|
|
|
* |
320
|
|
|
* @return void |
321
|
|
|
*/ |
322
|
|
|
public function onEndMapEnd($count, $time, $restarted, Map $map) |
323
|
|
|
{ |
324
|
|
|
|
325
|
|
|
if (!empty($this->changedVotes)) { |
326
|
|
|
$votes = []; |
327
|
|
|
foreach ($this->changedVotes as $vote) { |
328
|
|
|
$votes[] = $vote; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$this->mxKarma->saveVotes($map, (time() - $this->startTime), $votes); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
protected function connect() |
336
|
|
|
{ |
337
|
|
|
if ($this->enabled->get()) { |
338
|
|
|
if (empty($this->apikey->get())) { |
339
|
|
|
$this->console->writeln('> MxKarma: api key not set, $f00can\'t connect.'); |
340
|
|
|
|
341
|
|
|
return; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
if (empty($this->serverLogin->get())) { |
345
|
|
|
$this->console->writeln('> MxKarma: server login not set, $f00can\'t connect.'); |
346
|
|
|
|
347
|
|
|
return; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if ($this->gameDataStorage->getSystemInfo()->serverLogin !== $this->serverLogin->get()) { |
351
|
|
|
$this->console->writeln("> MxKarma: server login doesn't match configured server login."); |
352
|
|
|
|
353
|
|
|
return; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$this->mxKarma->connect($this->serverLogin->get(), $this->apikey->get()); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.