Completed
Pull Request — master (#194)
by De Cramer
24:15 queued 02:45
created

ListWindow::callbackRemovePlayer()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
ccs 0
cts 20
cp 0
cc 6
eloc 24
nc 15
nop 4
crap 42
1
<?php
2
3
namespace eXpansion\Bundle\Players\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Helpers\ChatNotification;
7
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Model\Gui\Window;
11
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
12
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
13
use eXpansion\Framework\Core\Storage\PlayerStorage;
14
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
15
use eXpansion\Framework\Gui\Components\uiButton;
16
use Maniaplanet\DedicatedServer\Connection;
17
18
class ListWindow extends GridWindowFactory
19
{
20
    /**
21
     * @var PlayerStorage
22
     */
23
    private $playerStorage;
24
    /**
25
     * @var ChatCommandDataProvider
26
     */
27
    private $chatCommandDataProvider;
28
    /**
29
     * @var Connection
30
     */
31
    private $connection;
32
    /**
33
     * @var AdminGroups
34
     */
35
    private $adminGroups;
36
37
    protected $mode;
38
    /**
39
     * @var ChatNotification
40
     */
41
    private $chatNotification;
42
43
    /**
44
     * PlayersWindow constructor.
45
     * @param                         $name
46
     * @param                         $sizeX
47
     * @param                         $sizeY
48
     * @param null                    $posX
49
     * @param null                    $posY
50
     * @param WindowFactoryContext    $context
51
     * @param PlayerStorage           $playerStorage
52
     * @param DataCollectionFactory   $dataCollectionFactory
53
     * @param GridBuilderFactory      $gridBuilderFactory
54
     * @param ChatCommandDataProvider $chatCommandDataProvider
55
     * @param Connection              $connection
56
     * @param AdminGroups             $adminGroups
57
     * @param ChatNotification        $chatNotification
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
        ChatNotification $chatNotification
73
74
    ) {
75
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
76
77
        $this->playerStorage = $playerStorage;
78
        $this->dataCollectionFactory = $dataCollectionFactory;
79
        $this->gridBuilderFactory = $gridBuilderFactory;
80
        $this->chatCommandDataProvider = $chatCommandDataProvider;
81
        $this->connection = $connection;
82
        $this->adminGroups = $adminGroups;
83
        $this->chatNotification = $chatNotification;
84
    }
85
86
    /**
87
     * @param mixed $mode
88
     */
89
    public function setMode($mode)
90
    {
91
        $this->mode = $mode;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function getMode()
98
    {
99
        return $this->mode;
100
    }
101
102
    protected function createContent(
103
        ManialinkInterface $manialink
104
    ) {
105
        parent::createContent($manialink);
106
        $manialink->setData('mode', $this->getMode());
107
        $this->updateData($manialink);
108
    }
109
110
    protected function updateContent(
111
        ManialinkInterface $manialink
112
    ) {
113
        parent::updateContent($manialink);
114
    }
115
116
    /**
117
     * @param ManialinkInterface $manialink
118
     */
119
    protected function createGrid(
120
        ManialinkInterface $manialink
121
    ) {
122
123
        $selectButton = $this->uiFactory->createButton('expansion_players.gui.list.window.column.remove',
124
            uiButton::TYPE_DEFAULT)->setSize(10, 5)->setTranslate(true);
125
126
        $gridBuilder = $this->gridBuilderFactory->create();
127
        $gridBuilder->setManialink($manialink)
128
            ->setDataCollection($manialink->getData('dataCollection'))
129
            ->setManialinkFactory($this)
130
            ->addTextColumn(
131
                'login',
132
                'expansion_players.gui.players.window.column.login',
133
                4,
134
                true,
135
                false
136
137
            )
138
            ->addActionColumn('login', 'expansion_players.gui.list.window.column.remove',
139
                3, [$this, "callbackRemovePlayer"], $selectButton);
140
141
142
        $manialink->setData('grid', $gridBuilder);
143
144
        $frame = $manialink->getContentFrame();
145
        $this->setGridSize($frame->getWidth(), $frame->getHeight() - 10);
146
147
        $this->setGridSize(60, 90);
148
        $this->setGridPosition(0, 0);
149
150
    }
151
152
    /**
153
     * @param Window|ManialinkInterface $manialink
154
     * @throws \Maniaplanet\DedicatedServer\InvalidArgumentException
155
     */
156
    public function updateData(
157
        $manialink
158
    ) {
159
        $dataset = [];
160
        switch ($manialink->getData("mode")) {
161
            case "ignorelist":
162
                $dataset = $this->connection->getIgnoreList();
163
                break;
164
            case "guestlist":
165
                $dataset = $this->connection->getGuestList();
166
                break;
167
            case "banlist":
168
                $dataset = $this->connection->getBanList();
169
                break;
170
            case "blacklist":
171
                $dataset = $this->connection->getBlackList();
172
                break;
173
        }
174
175
        $listData = [];
176
        foreach ($dataset as $player) {
177
            $listData[] = ["login" => $player->login];
178
        }
179
180
        $this->setData($manialink, $listData);
181
    }
182
183
184
    /**
185
     * @param ManialinkInterface $manialink
186
     * @param $login
187
     * @param $entries
188
     * @param $args
189
     */
190
    public function callbackRemovePlayer(
191
        $manialink,
192
        $login,
193
        $entries,
194
        $args
195
    ) {
196
        try {
197
            switch ($manialink->getData("mode")) {
198
                case "ignorelist":
199
                    $this->connection->unIgnore($args['login']);
200
                    break;
201
                case "guestlist":
202
                    $this->connection->removeGuest($args['login']);
203
                    break;
204
                case "banlist":
205
                    $this->connection->unBan($args['login']);
206
                    break;
207
                case "blacklist":
208
                    $this->connection->unBlackList($args['login']);
209
                    break;
210
            }
211
212
            $this->updateData($manialink);
213
        } catch (\Exception $e) {
214
            $this->chatNotification->sendMessage('expansion_players.chat.exception',
215
                $login, ["%message%" => $e->getMessage()]);
216
        }
217
        $this->update($manialink->getUserGroup());
218
    }
219
220
    /**
221
     * @param $login
222
     * @param $command
223
     */
224
    public function callChatCommand($login, $command)
225
    {
226
        $this->chatCommandDataProvider->onPlayerChat($login, $login, $command, true);
227
    }
228
229
}
230
231