Completed
Pull Request — master (#194)
by
unknown
04:05
created

PlayersWindow::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 1
eloc 20
nc 1
nop 12
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\Players\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Helpers\Countries;
7
use eXpansion\Framework\Core\Helpers\TMString;
8
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\Window;
12
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
13
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
14
use eXpansion\Framework\Core\Storage\PlayerStorage;
15
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
16
use eXpansion\Framework\Gui\Components\uiButton;
17
use eXpansion\Framework\Gui\Components\uiLabel;
18
use eXpansion\Framework\Gui\Layouts\layoutRow;
19
use FML\Controls\Frame;
20
use Maniaplanet\DedicatedServer\Connection;
21
22
class PlayersWindow extends GridWindowFactory
23
{
24
    /**
25
     * @var PlayerStorage
26
     */
27
    private $playerStorage;
28
    /**
29
     * @var ChatCommandDataProvider
30
     */
31
    private $chatCommandDataProvider;
32
    /**
33
     * @var Connection
34
     */
35
    private $connection;
36
    /**
37
     * @var AdminGroups
38
     */
39
    private $adminGroups;
40
41
    /**
42
     * PlayersWindow constructor.
43
     * @param                         $name
44
     * @param                         $sizeX
45
     * @param                         $sizeY
46
     * @param null                    $posX
47
     * @param null                    $posY
48
     * @param WindowFactoryContext    $context
49
     * @param PlayerStorage           $playerStorage
50
     * @param DataCollectionFactory   $dataCollectionFactory
51
     * @param GridBuilderFactory      $gridBuilderFactory
52
     * @param ChatCommandDataProvider $chatCommandDataProvider
53
     * @param Connection              $connection
54
     * @param AdminGroups             $adminGroups
55
     */
56 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
        $name,
58
        $sizeX,
59
        $sizeY,
60
        $posX = null,
61
        $posY = null,
62
        WindowFactoryContext $context,
63
        PlayerStorage $playerStorage,
64
        DataCollectionFactory $dataCollectionFactory,
65
        GridBuilderFactory $gridBuilderFactory,
66
        ChatCommandDataProvider $chatCommandDataProvider,
67
        Connection $connection,
68
        AdminGroups $adminGroups
69
70
    ) {
71
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
72
73
        $this->playerStorage = $playerStorage;
74
        $this->dataCollectionFactory = $dataCollectionFactory;
75
        $this->gridBuilderFactory = $gridBuilderFactory;
76
        $this->chatCommandDataProvider = $chatCommandDataProvider;
77
        $this->connection = $connection;
78
        $this->adminGroups = $adminGroups;
79
    }
80
81
    protected function createContent(ManialinkInterface $manialink)
82
    {
83
        parent::createContent($manialink);
84
85
        $manialink->setData('playerActions', []);
86
        $this->setPlayer($manialink, $manialink->getUserGroup()->getLogins()[0]);
87
88
        $recipient = $manialink->getUserGroup()->getLogins()[0];
89
90
        if ($this->adminGroups->isAdmin($recipient)) {
91
92
            $ignoreList = $this->uiFactory->createButton("expansion_players.gui.players.window.ignorelist")
93
                ->setTranslate(true)
94
                ->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackChatCommand"],
95
                    ["action" => "//ignorelist"]));
96
97
            $guestList = $this->uiFactory->createButton("expansion_players.gui.players.window.guestlist")
98
                ->setTranslate(true)
99
                ->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackChatCommand"],
100
                    ["action" => "//guestlist"]));
101
102
            $banList = $this->uiFactory->createButton("expansion_players.gui.players.window.banlist")
103
                ->setTranslate(true)
104
                ->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackChatCommand"],
105
                    ["action" => "//banlist"]));
106
107
            $blackList = $this->uiFactory->createButton("expansion_players.gui.players.window.blacklist")
108
                ->setTranslate(true)
109
                ->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackChatCommand"],
110
                    ["action" => "//blacklist"]));
111
112
            $row = $this->uiFactory->createLayoutLine(125, 0,
113
                [$guestList, $ignoreList, $banList, $blackList], 2);
114
            $manialink->addChild($row);
115
116
117
        }
118
119
120
        $frame = Frame::create();;
121
        $frame->setPosition(120, -16);
122
123
        $manialink->setData("playerFrame", $frame);
124
        $manialink->addChild($frame);
125
126
    }
127
128
    protected function updateContent(ManialinkInterface $manialink)
129
    {
130
131
        parent::updateContent($manialink);
132
        $width = 60;
133
        $recipient = $manialink->getUserGroup()->getLogins()[0];
134
135
        /** @var Frame $frame */
136
        $login = $manialink->getData('playerLogin');
137
        $player = $this->playerStorage->getPlayerInfo($login);
138
139
        $frame = $manialink->getData('playerFrame');
140
        $frame->removeAllChildren();
141
142
        $row = $this->uiFactory->createLayoutRow(0, 0, [], -2);
143
144
145
        $element = $this->uiFactory->createLabel($player->getNickName(), uiLabel::TYPE_HEADER);
146
        $element->setTextSize(5)->setSize($width, 10)->setAlign("center", "top")
147
            ->setPosition($width / 2, 0);
148
        $row->addChild($element);
149
150
        $elem = [
151
            $this->uiFactory->createLabel("expansion_players.gui.players.window.column.from")
152
                ->setSize(20, 5)->setTranslate(true),
153
            $this->uiFactory->createLabel($player->getPath()),
154
        ];
155
        $line = $this->uiFactory->createLayoutLine(0, 0, $elem, 2);
156
        $row->addChild($line);
157
158
159
        $elem = [
160
            $this->uiFactory->createLabel("expansion_players.gui.players.window.language")
161
                ->setSize(20, 5)->setTranslate(true),
162
            $this->uiFactory->createLabel($player->getLanguage()),
163
        ];
164
        $line = $this->uiFactory->createLayoutLine(0, 0, $elem, 2);
165
        $row->addChild($line);
166
167
        $elem = [
168
            $this->uiFactory->createLabel("expansion_players.gui.players.window.gameversion")
169
                ->setSize(20, 5)->setTranslate(true),
170
            $this->uiFactory->createLabel($player->getClientVersion()),
171
        ];
172
        $line = $this->uiFactory->createLayoutLine(0, 0, $elem, 2);
173
        $row->addChild($line);
174
175
        $elem = [
176
            $this->uiFactory->createLabel("expansion_players.gui.players.window.ladder")
177
                ->setSize(20, 5)->setTranslate(true),
178
            $this->uiFactory->createLabel(floor($player->getLadderScore())),
179
        ];
180
181
        $line = $this->uiFactory->createLayoutLine(0, 0, $elem, 1);
182
        $row->addChild($line);
183
184
185
        if ($this->adminGroups->isAdmin($recipient)) {
186
            $this->createAdminControls($manialink, $row);
187
        }
188
189
        $frame->addChild($row);
190
    }
191
192
193
    /**
194
     * @param ManialinkInterface $manialink
195
     * @param layoutRow          $row
196
     */
197
    private function createAdminControls($manialink, $row)
198
    {
199
        $actions = $manialink->getData('playerActions');
200
        $login = $manialink->getData('playerLogin');
201
202
        if ($this->getIgnoredStatus($login)) {
203
            $muteText = "Allow";
204
            $color = uiButton::COLOR_SUCCESS;
205
        } else {
206
            $muteText = "Mute";
207
            $color = uiButton::COLOR_WARNING;
208
        }
209
210
        $elem = [
211
            $this->uiFactory->createConfirmButton($muteText, uiButton::TYPE_DEFAULT)
212
                ->setAction($actions['mute'])
213
                ->setBackgroundColor($color),
214
            $this->uiFactory->createConfirmButton("Guest", uiButton::TYPE_DEFAULT)
215
                ->setAction($actions['guest'])
216
                ->setBackgroundColor(UiButton::COLOR_DEFAULT),
217
        ];
218
        $line = $this->uiFactory->createLayoutLine(10, 0, $elem, 2);
219
        $row->addChild($line);
220
221
        $separator = $this->uiFactory->createLine(0, 0)->setLength(60)->setStroke(0.5);
222
        $row->addChild($separator);
223
224
225
        $elem = $this->uiFactory->createLabel("expansion_players.gui.players.window.reason")
226
            ->setSize(20, 5)
227
            ->setTranslate(true);
228
        $row->addChild($elem);
229
        $elem = $this->uiFactory->createInput('reason', "", 60);
230
        $row->addChild($elem);
231
232
233
        $elem = [
234
            $this->uiFactory->createConfirmButton("Kick", uiButton::TYPE_DEFAULT)
235
                ->setAction($actions['kick'])
236
                ->setBackgroundColor(UiButton::COLOR_DEFAULT),
237
            $this->uiFactory->createConfirmButton("Ban", uiButton::TYPE_DEFAULT)
238
                ->setAction($actions['ban'])
239
                ->setBackgroundColor(UiButton::COLOR_DEFAULT),
240
            $this->uiFactory->createConfirmButton("Black", uiButton::TYPE_DEFAULT)
241
                ->setAction($actions['black'])
242
                ->setBackgroundColor(UiButton::COLOR_SECONDARY),
243
244
245
        ];
246
        $line = $this->uiFactory->createLayoutLine(10, 0, $elem, 2);
247
        $row->addChild($line);
248
    }
249
250
251
    /**
252
     * @param ManialinkInterface $manialink
253
     */
254
    protected function createGrid(ManialinkInterface $manialink)
255
    {
256
257
        $this->updateData($manialink);
258
259
        $selectButton = $this->uiFactory->createButton('expansion_players.gui.players.window.column.select',
260
            uiButton::TYPE_DEFAULT)->setSize(10, 5)->setTranslate(true);
261
262
        $gridBuilder = $this->gridBuilderFactory->create();
263
        $gridBuilder->setManialink($manialink)
264
            ->setDataCollection($manialink->getData('dataCollection'))
265
            ->setManialinkFactory($this)
266
            ->addIconColumn('zoneIcon', '#', 1)
267
            ->addTextColumn(
268
                'country',
269
                'expansion_players.gui.players.window.column.from',
270
                4,
271
                true,
272
                true
273
274
            )
275
            ->addTextColumn(
276
                'login',
277
                'expansion_players.gui.players.window.column.login',
278
                4,
279
                true,
280
                true
281
282
            )
283
            ->addTextColumn(
284
                'nickname',
285
                'expansion_players.gui.players.window.column.nickname',
286
                6,
287
                true,
288
                true
289
290
            )
291
            ->addActionColumn('login', "expansion_players.gui.players.window.column.select",
292
                3, [$this, "callbackSetPlayer"], $selectButton);
293
294
295
        $manialink->setData('grid', $gridBuilder);
296
297
        $frame = $manialink->getContentFrame();
298
        $this->setGridSize($frame->getWidth(), $frame->getHeight() - 10);
299
300
        $this->setGridSize(100, 90);
301
        $this->setGridPosition(0, 0);
302
303
    }
304
305
306
    /**
307
     * @param ManialinkInterface|Window $manialink
308
     * @param string                    $login
309
     * @param array                     $entries
310
     * @param array                     $args
311
     */
312
    public function callbackSetPlayer($manialink, $login, $entries, $args)
313
    {
314
        $this->setPlayer($manialink, $args['login']);
315
        $this->update($manialink->getUserGroup());
316
    }
317
318
    /**
319
     * @param ManialinkInterface|Window $manialink
320
     * @param string                    $login
321
     */
322
    public function setPlayer($manialink, $login)
323
    {
324
        foreach ($manialink->getData('playerActions') as $key => $action) {
325
            $this->actionFactory->destroyAction($action);
326
        }
327
328
        $actions = [
329
            "mute" => (string)$this->actionFactory->createManialinkAction($manialink, [$this, 'callbackIgnore'],
330
                [
331
                    "login" => $login,
332
                ]),
333
            "kick" => (string)$this->actionFactory->createManialinkAction($manialink, [$this, 'callbackKick'],
334
                [
335
                    "login" => $login,
336
                ]),
337
            "ban" => (string)$this->actionFactory->createManialinkAction($manialink, [$this, 'callbackBan'],
338
                [
339
                    "login" => $login,
340
                ]),
341
            "black" => (string)$this->actionFactory->createManialinkAction($manialink, [$this, 'callbackBlack'],
342
                [
343
                    "login" => $login,
344
                ]),
345
            "guest" => (string)$this->actionFactory->createManialinkAction($manialink, [$this, 'callbackGuest'],
346
                [
347
                    "login" => $login,
348
                ]),
349
        ];
350
351
        $manialink->setData('playerActions', $actions);
352
        $manialink->setData("playerLogin", $login);
353
    }
354
355
    public function callbackIgnore($manialink, $login, $entries, $args)
356
    {
357
        $status = $this->getIgnoredStatus($login);
358
359
        if ($status) {
360
            $this->callChatCommand($login, "//unignore ".$args['login']);
361
        } else {
362
            $this->callChatCommand($login, "//ignore ".$args['login']);
363
        }
364
        $this->updateData($manialink);
365
        $this->update($manialink->getUserGroup());
366
    }
367
368
    public function callbackKick($manialink, $login, $entries, $args)
369
    {
370
        $this->callChatCommand($login, "//kick ".$args['login'].' "'.$entries['reason'].'"');
371
        $this->updateData($manialink);
372
        $this->update($manialink->getUserGroup());
373
    }
374
375
    public function callbackGuest($manialink, $login, $entries, $args)
376
    {
377
        $this->callChatCommand($login, "//addguest ".$args['login']);
378
        $this->updateData($manialink);
379
        $this->update($manialink->getUserGroup());
380
    }
381
382
    public function callbackBan($manialink, $login, $entries, $args)
383
    {
384
        $this->callChatCommand($login, "//ban ".$args['login'].' "'.$entries['reason'].'"');
385
        $this->updateData($manialink);
386
        $this->update($manialink->getUserGroup());
387
    }
388
389
    public function callbackBlack($manialink, $login, $entries, $args)
390
    {
391
        $this->callChatCommand($login, "//black ".$args['login'].' "'.$entries['reason'].'"');
392
        $this->updateData($manialink);
393
        $this->update($manialink->getUserGroup());
394
    }
395
396
    public function callbackChatCommand($manialink, $login, $entries, $args)
397
    {
398
        $this->callChatCommand($login, $args['action']);
399
    }
400
401
402
    public function updateData($manialink)
403
    {
404
        $players = $this->playerStorage->getOnline();
405
        $data = [];
406
        foreach ($players as $login => $player) {
407
            $country = Countries::parseCountryFromPath($player->getPath());
408
            $data[] = [
409
                "login" => $player->getLogin(),
410
                "nickname" => TMString::trimLinks($player->getNickName()),
411
                "country" => $country,
412
                "zoneIcon" => "file://Media/Flags/".Countries::getCodeFromCountry($country).".dds",
413
            ];
414
        }
415
        $this->setData($manialink, $data);
416
    }
417
418
419
    /**
420
     * @param $login
421
     * @param $command
422
     */
423
    public function callChatCommand($login, $command)
424
    {
425
        $this->chatCommandDataProvider->onPlayerChat($login, $login, $command, true);
426
    }
427
428
    /** Get ignore status for a player;
429
     *
430
     * @param string $login
431
     * @return bool
432
     */
433
    private function getIgnoredStatus($login)
434
    {
435
        try {
436
            $ignoreList = $this->connection->getIgnoreList();
437
            foreach ($ignoreList as $player) {
438
                if ($player->login === $login) {
439
                    return true;
440
                }
441
            }
442
443
            return false;
444
        } catch (\Exception $e) {
445
            return false;
446
        }
447
    }
448
449
}
450
451