Completed
Branch master (220ce5)
by De Cramer
16:11
created

JukeboxWindowFactory::createGrid()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 42
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 39
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace eXpansion\Bundle\Maps\Plugins\Gui;
5
6
use eXpansion\Bundle\Acme\Plugins\Gui\WindowFactory;
7
use eXpansion\Bundle\LocalRecords\Entity\Record;
8
use eXpansion\Bundle\Maps\Plugins\Jukebox;
9
use eXpansion\Bundle\Maps\Services\JukeboxService;
10
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
11
use eXpansion\Framework\Core\Helpers\Time;
12
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
13
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
14
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
15
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
16
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
17
use eXpansion\Framework\Core\Model\UserGroups\Group;
18
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
19
use eXpansion\Framework\Gui\Components\uiButton;
20
use FML\Controls\Frame;
21
use Maniaplanet\DedicatedServer\Structures\Map;
22
23
24
/**
25
 * Class RecordsWindowFactory
26
 *
27
 * @package eXpansion\Bundle\LocalRecords\Plugins\Gui;
28
 * @author  reaby
29
 */
30
class JukeboxWindowFactory extends GridWindowFactory
31
{
32
    public $sizeX;
33
    public $sizeY;
34
35
    /** @var GridBuilderFactory */
36
    protected $gridBuilderFactory;
37
38
    /** @var DataCollectionFactory */
39
    protected $dataCollectionFactory;
40
41
    /** @var Time */
42
    protected $timeFormatter;
43
    /**
44
     * @var Jukebox
45
     */
46
    private $jukeboxPlugin;
47
    /**
48
     * @var JukeboxService
49
     */
50
    private $jukeboxService;
51
    /**
52
     * @var AdminGroups
53
     */
54
    private $adminGroups;
55
56
    /**
57
     * MapsWindowFactory constructor.
58
     * @param $name
59
     * @param $sizeX
60
     * @param $sizeY
61
     * @param null $posX
62
     * @param null $posY
63
     * @param WindowFactoryContext $context
64
     * @param GridBuilderFactory $gridBuilderFactory
65
     * @param DataCollectionFactory $dataCollectionFactory
66
     * @param Time $time
67
     * @param JukeboxService $jukeboxService
68
     * @param AdminGroups $adminGroups
69
     */
70
    public function __construct(
71
        $name,
72
        $sizeX,
73
        $sizeY,
74
        $posX,
75
        $posY,
76
        WindowFactoryContext $context,
77
        GridBuilderFactory $gridBuilderFactory,
78
        DataCollectionFactory $dataCollectionFactory,
79
        Time $time,
80
        JukeboxService $jukeboxService,
81
        AdminGroups $adminGroups
82
    ) {
83
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
84
85
        $this->gridBuilderFactory = $gridBuilderFactory;
86
        $this->dataCollectionFactory = $dataCollectionFactory;
87
        $this->timeFormatter = $time;
88
        $this->sizeX = $sizeX;
89
        $this->sizeY = $sizeY;
90
        $this->jukeboxService = $jukeboxService;
91
        $this->adminGroups = $adminGroups;
92
    }
93
94
    public function setJukeboxPlugin(Jukebox $plugin)
95
    {
96
        $this->jukeboxPlugin = $plugin;
97
    }
98
99
    /**
100
     * @param ManialinkInterface $manialink
101
     * @return void
102
     */
103
    protected function createGrid(ManialinkInterface $manialink)
104
    {
105
        $collection = $this->dataCollectionFactory->create($this->getData());
106
        $collection->setPageSize(20);
107
108
        $queueButton = $this->uiFactory->createButton('drop', uiButton::TYPE_DEFAULT);
109
        $queueButton->setTextColor("000")->setSize(12, 5)->setTranslate(true);
110
111
        $gridBuilder = $this->gridBuilderFactory->create();
112
        $gridBuilder->setManialink($manialink)
113
            ->setDataCollection($collection)
114
            ->setManialinkFactory($this)
115
            ->addTextColumn(
116
                'index',
117
                'expansion_maps.gui.window.column.index',
118
                1,
119
                true,
120
                false
121
            )->addTextColumn(
122
                'name',
123
                'expansion_maps.gui.window.column.name',
124
                3,
125
                true,
126
                false
127
            )->addTextColumn(
128
                'time',
129
                'expansion_maps.gui.window.column.goldtime',
130
                2,
131
                true,
132
                false
133
            )->addTextColumn(
134
                'nickname',
135
                'expansion_maps.gui.window.column.nickname',
136
                3,
137
                true,
138
                false
139
            )
140
            ->addActionColumn('map', 'expansion_maps.gui.window.column.drop', 2, array($this, 'callbackDropMap'),
141
                $queueButton);
142
        $contentFrame = $manialink->getContentFrame();
143
        $this->setGridSize($contentFrame->getWidth(), $contentFrame->getHeight() - 12);
144
        $this->setGridPosition(0, -10);
145
        $manialink->setData('grid', $gridBuilder);
146
    }
147
148
    public function callbackClear($login)
149
    {
150
        $this->jukeboxPlugin->clear($login);
151
        $group = $this->groupFactory->createForPlayer($login);
152
        $this->update($group);
153
    }
154
155
    public function callbackDrop($login)
156
    {
157
158
        $this->jukeboxPlugin->drop($login, null);
159
        $group = $this->groupFactory->createForPlayer($login);
160
        $this->update($group);
161
    }
162
163
    public function callbackDropMap($login, $params, $args)
164
    {
165
        $this->jukeboxPlugin->drop($login, $args['map']);
166
        $group = $this->groupFactory->createForPlayer($login);
167
        $this->update($group);
168
    }
169
170
    public function createContent(ManialinkInterface $manialink)
171
    {
172
        parent::createContent($manialink);
173
174
        $line = $this->uiFactory->createLayoutLine(0, 0, [], 2);
175
176
        $dropButton = $this->uiFactory->createButton("expansion_maps.gui.button.drop", uiButton::TYPE_DECORATED);
177
        $dropButton->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackDrop'], null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
        $line->addChild($dropButton);
179
180
        $clearButton = $this->uiFactory->createButton("expansion_maps.gui.button.clear");
181
        $clearButton->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClear'],
182
            null))
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
            ->setFocusColor('f00')
184
            ->setBorderColor('d00')
185
            ->setTextColor('fff')
186
            ->setTranslate(true);
187
188
        if ($this->adminGroups->hasPermission($manialink->getUserGroup(), "jukebox")) {
189
            $line->addChild($clearButton);
190
        }
191
        $manialink->addChild($line);
192
    }
193
194
    protected function updateContent(ManialinkInterface $manialink)
195
    {
196
        $this->updateMaps();
197
        parent::updateContent($manialink);
198
    }
199
200
    public function updateMaps()
201
    {
202
        /**
203
         * @var string $i
204
         * @var Map $map
205
         */
206
        $this->setData([]);
207
208
        $i = 1;
209
        $data = [];
210
        foreach ($this->jukeboxService->getMapQueue() as $idx => $jbMap) {
211
            $map = $jbMap->getMap();
212
            $data[] = [
213
                'index' => $i++,
214
                'name' => $map->name,
215
                'nickname' => $jbMap->getPlayer()->getNickName(),
216
                'time' => $this->timeFormatter->timeToText($map->goldTime, true),
217
                'map' => $jbMap,
218
            ];
219
        }
220
        $this->setData($data);
221
    }
222
223
224
}
225