Completed
Pull Request — master (#194)
by De Cramer
18:20 queued 02:58
created

PlayersWindow::createContent()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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