Completed
Pull Request — master (#280)
by
unknown
03:52
created

MxKarma::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 9.2
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 9
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
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->mxKarma->isConnected() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
274
            $this->console->writeln("> MxKarma is at disconnected state, trying to establish connection.");
275
            $this->connect();
276
        }
277
    }
278
279
    /**
280
     * Callback sent when the "StartMap" section end.
281
     *
282
     * @param int     $count Each time this section is played, this number is incremented by one
283
     * @param int     $time Server time when the callback was sent
284
     * @param boolean $restarted true if the map was restarted, false otherwise
285
     * @param Map     $map Map started with.
286
     *
287
     * @return void
288
     */
289
    public function onStartMapEnd($count, $time, $restarted, Map $map)
290
    {
291
        $this->startTime = time();
292
        $this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false);
293
    }
294
295
    /**
296
     * Callback sent when the "EndMap" section start.
297
     *
298
     * @param int     $count Each time this section is played, this number is incremented by one
299
     * @param int     $time Server time when the callback was sent
300
     * @param boolean $restarted true if the map was restarted, false otherwise
301
     * @param Map     $map Map started with.
302
     *
303
     * @return void
304
     */
305
    public function onEndMapStart($count, $time, $restarted, Map $map)
306
    {
307
        // do nothing
308
    }
309
310
    /**
311
     * Callback sent when the "EndMap" section end.
312
     *
313
     * @param int     $count Each time this section is played, this number is incremented by one
314
     * @param int     $time Server time when the callback was sent
315
     * @param boolean $restarted true if the map was restarted, false otherwise
316
     * @param Map     $map Map started with.
317
     *
318
     * @return void
319
     */
320
    public function onEndMapEnd($count, $time, $restarted, Map $map)
321
    {
322
323
        if (!empty($this->changedVotes)) {
324
            $votes = [];
325
            foreach ($this->changedVotes as $vote) {
326
                $votes[] = $vote;
327
            }
328
329
            $this->mxKarma->saveVotes($map, (time() - $this->startTime), $votes);
330
        }
331
    }
332
333
    protected function connect()
334
    {
335
        if ($this->enabled->get()) {
336
            if (empty($this->apikey->get())) {
337
                $this->console->writeln('> MxKarma: api key not set, $f00can\'t connect.');
338
339
                return;
340
            }
341
342
            if (empty($this->serverLogin->get())) {
343
                $this->console->writeln('> MxKarma: server login not set, $f00can\'t connect.');
344
345
                return;
346
            }
347
348
            if ($this->gameDataStorage->getSystemInfo()->serverLogin !== $this->serverLogin->get()) {
349
                $this->console->writeln("> MxKarma: server login doesn't match configured server login.");
350
351
                return;
352
            }
353
354
            $this->mxKarma->connect($this->serverLogin->get(), $this->apikey->get());
355
        }
356
    }
357
358
359
}
360