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