Completed
Push — master ( 220ce5...be682d )
by
unknown
17s
created

ManiaExchangeWindowFactory   B

Complexity

Total Complexity 10

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 17

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 17
dl 0
loc 360
ccs 0
cts 206
cp 0
rs 7.8571

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 37 1
B createGrid() 0 143 1
A callbackAdd() 0 4 1
B callbackSearch() 0 29 3
B setMaps() 0 31 4
1
<?php
2
3
namespace eXpansion\Bundle\Maps\Plugins\Gui;
4
5
use eXpansion\Bundle\Maps\Plugins\Jukebox;
6
use eXpansion\Bundle\Maps\Plugins\ManiaExchange;
7
use eXpansion\Bundle\Maps\Structure\MxInfo;
8
use eXpansion\Framework\Core\Helpers\Http;
9
use eXpansion\Framework\Core\Helpers\Structures\HttpResult;
10
use eXpansion\Framework\Core\Helpers\Time;
11
use eXpansion\Framework\Core\Helpers\TMString;
12
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
13
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
14
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
15
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
16
use eXpansion\Framework\Core\Plugins\Gui\GridWindowFactory;
17
use eXpansion\Framework\Gui\Components\uiButton;
18
use eXpansion\Framework\Gui\Components\uiDropdown;
19
use eXpansion\Framework\Gui\Components\uiLabel;
20
21
class ManiaExchangeWindowFactory extends GridWindowFactory
22
{
23
    /** @var  uiDropdown */
24
    public $lengthBox;
25
    /** @var  uiDropdown */
26
    public $stylebox;
27
    /** @var  uiDropdown */
28
    public $sitebox;
29
    /** @var  uiDropdown */
30
    public $difficultiesBox;
31
    /** @var  uiDropdown */
32
    public $tpackBox;
33
34
    public $tpack = [
35
        "All" => "",
36
        "Stadium" => "Stadium,TMStadium",
37
        "Canyon" => "Canyon,TmCanyon",
38
        "Valley" => "Valley,TmValley",
39
        "Lagoon" => "Lagoon,TmLagoon",
40
    ];
41
42
    /** @var  uiDropdown */
43
    private $orderbox;
44
    /** @var  uiDropdown */
45
    private $opbox;
46
    /** @var  uiDropdown */
47
    private $modebox;
48
49
    /** @var  array */
50
    private $tracksearch;
51
52
    /** @var GridBuilderFactory */
53
    protected $gridBuilderFactory;
54
55
    /** @var DataCollectionFactory */
56
    protected $dataCollectionFactory;
57
58
    /** @var Time */
59
60
    protected $timeFormatter;
61
    /**
62
     * @var  ManiaExchange $mxPlugin
63
     */
64
    private $mxPlugin;
65
    /**
66
     * @var array
67
     */
68
    private $order;
69
    /**
70
     * @var array
71
     */
72
    private $length;
73
    /**
74
     * @var array
75
     */
76
    private $map_styles_tm;
77
    /**
78
     * @var array
79
     */
80
    private $map_style_sm;
81
    /**
82
     * @var array
83
     */
84
    private $difficulties;
85
    /**
86
     * @var array
87
     */
88
    private $operator;
89
90
    /**
91
     * @var Http
92
     */
93
    private $http;
94
95
    /**
96
     * ManiaExchangeWindowFactory constructor.
97
     * @param $name
98
     * @param $sizeX
99
     * @param $sizeY
100
     * @param null $posX
101
     * @param null $posY
102
     * @param WindowFactoryContext $context
103
     * @param GridBuilderFactory $gridBuilderFactory
104
     * @param DataCollectionFactory $dataCollectionFactory
105
     * @param Time $time
106
     * @param ManiaExchange $mxPlugin
107
     * @param Http $http
108
     * @param $tracksearch
109
     * @param $order
110
     * @param $length
111
     * @param $map_styles_tm
112
     * @param $map_style_sm
113
     * @param $difficulties
114
     * @param $operator
115
     */
116
    public function __construct(
117
        $name,
118
        $sizeX,
119
        $sizeY,
120
        $posX,
121
        $posY,
122
        WindowFactoryContext $context,
123
        GridBuilderFactory $gridBuilderFactory,
124
        DataCollectionFactory $dataCollectionFactory,
125
        Time $time,
126
        ManiaExchange $mxPlugin,
127
        Http $http,
128
        $tracksearch,
129
        $order,
130
        $length,
131
        $map_styles_tm,
132
        $map_style_sm,
133
        $difficulties,
134
        $operator
135
136
    ) {
137
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
138
139
        $this->gridBuilderFactory = $gridBuilderFactory;
140
        $this->dataCollectionFactory = $dataCollectionFactory;
141
        $this->timeFormatter = $time;
142
        $this->mxPlugin = $mxPlugin;
143
        $this->tracksearch = array_flip($tracksearch);
144
145
        $this->order = array_flip($order);
146
        $this->length = array_flip($length);
147
        $this->map_styles_tm = array_flip($map_styles_tm);
148
        $this->map_style_sm = array_flip($map_style_sm);
149
        $this->difficulties = array_flip($difficulties);
150
        $this->operator = array_flip($operator);
151
        $this->http = $http;
152
    }
153
154
    /**
155
     * @param ManialinkInterface $manialink
156
     */
157
    protected function createGrid(ManialinkInterface $manialink)
158
    {
159
        $collection = $this->dataCollectionFactory->create($this->getData());
160
        $collection->setPageSize(20);
161
162
        $x = 0;
163
164
        $tooltip = $this->uiFactory->createTooltip();
165
        $manialink->addChild($tooltip);
166
167
        $this->modebox = $this->uiFactory->createDropdown("mode", $this->tracksearch, 0);
168
        $this->modebox->setPosition($x, -6, 2);
169
        $manialink->addChild($this->modebox);
170
171
        $label = $this->uiFactory->createLabel("Sort by", uiLabel::TYPE_HEADER);
172
        $label->setPosition($x, 0);
173
        $manialink->addChild($label);
174
175
        $x += 32;
176
        $this->orderbox = $this->uiFactory->createDropdown("order", $this->order, 0);
177
        $this->orderbox->setPosition($x, -6, 2);
178
        $manialink->addChild($this->orderbox);
179
180
        $label = $this->uiFactory->createLabel("Order", uiLabel::TYPE_HEADER);
181
        $label->setPosition($x, 0);
182
        $manialink->addChild($label);
183
184
        $x += 32;
185
        $this->opbox = $this->uiFactory->createDropdown("operator", $this->operator, 0);
186
        $this->opbox->setPosition($x, -6, 2);
187
        $manialink->addChild($this->opbox);
188
189
        $label = $this->uiFactory->createLabel("Operator", uiLabel::TYPE_HEADER);
190
        $label->setPosition($x, 0);
191
        $manialink->addChild($label);
192
193
        $x += 32;
194
        $this->lengthBox = $this->uiFactory->createDropdown("length", $this->length, 0);
195
        $this->lengthBox->setPosition($x, -6, 2);
196
        $manialink->addChild($this->lengthBox);
197
198
        $label = $this->uiFactory->createLabel("Length", uiLabel::TYPE_HEADER);
199
        $label->setPosition($x, 0);
200
        $manialink->addChild($label);
201
202
        $x += 32;
203
        $this->stylebox = $this->uiFactory->createDropdown("style", $this->map_styles_tm, 0);
204
        $this->stylebox->setPosition($x, -6, 2);
205
        $manialink->addChild($this->stylebox);
206
207
        $label = $this->uiFactory->createLabel("Style", uiLabel::TYPE_HEADER);
208
        $label->setPosition($x, 0);
209
        $manialink->addChild($label);
210
211
        $x += 32;
212
        $this->difficultiesBox = $this->uiFactory->createDropdown("difficulties", $this->difficulties, 0);
213
        $this->difficultiesBox->setPosition($x, -6, 2);
214
        $manialink->addChild($this->difficultiesBox);
215
216
        $label = $this->uiFactory->createLabel("Difficulty", uiLabel::TYPE_HEADER);
217
        $label->setPosition($x, 0);
218
        $manialink->addChild($label);
219
220
        /// second line
221
222
        $this->sitebox = $this->uiFactory->createDropdown("site", ["Trackmania" => "tm", "Storm" => "sm"], 0);
223
        $this->sitebox->setPosition(0, -14, 2);
224
        $manialink->addChild($this->sitebox);
225
226
        $this->tpackBox = $this->uiFactory->createDropdown("tpack", $this->tpack, 0);
227
        $this->tpackBox->setPosition(32, -14, 2);
228
        $manialink->addChild($this->tpackBox);
229
230
        $mapname = $this->uiFactory->createInput("map");
231
        $mapname->setHeight(6);
232
        $author = $this->uiFactory->createInput("author");
233
        $author->setHeight(6);
234
235
        $search = $this->uiFactory->createButton('🔍 Search', uiButton::TYPE_DECORATED);
236
        $search->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackSearch'], []));
237
        $search->setHeight(6);
238
239
        $line = $this->uiFactory->createLayoutLine(64, -14, [$mapname, $author, $search], 2);
240
        $manialink->addChild($line);
241
242
        $addButton = $this->uiFactory->createButton('Install', uiButton::TYPE_DEFAULT);
243
        $addButton->setSize(20, 5);
244
245
        $gridBuilder = $this->gridBuilderFactory->create();
246
        $gridBuilder->setManialink($manialink)
247
            ->setDataCollection($collection)
248
            ->setManialinkFactory($this)
249
            ->addTextColumn(
250
                'index',
251
                'expansion_mx.gui.mxsearch.column.index',
252
                1,
253
                true,
254
                false
255
            )->addTextColumn(
256
                'name',
257
                'expansion_mx.gui.mxsearch.column.name',
258
                5,
259
                true,
260
                false
261
            )->addTextColumn(
262
                'author',
263
                'expansion_mx.gui.mxsearch.column.author',
264
                3,
265
                false
266
            )->addTextColumn(
267
                'envir',
268
                'expansion_mx.gui.mxsearch.column.envir',
269
                2,
270
                true,
271
                false
272
            )->addTextColumn(
273
                'awards',
274
                'expansion_mx.gui.mxsearch.column.awards',
275
                1,
276
                true,
277
                false
278
            )->addTextColumn(
279
                'length',
280
                'expansion_mx.gui.mxsearch.column.length',
281
                2,
282
                true,
283
                false
284
            )->addTextColumn(
285
                'style',
286
                'expansion_mx.gui.mxsearch.column.style',
287
                2,
288
                true,
289
                false
290
            )
291
            ->addActionColumn('add', 'expansion_mx.gui.mxsearch.column.add', 2, array($this, 'callbackAdd'),
292
                $addButton);
293
        $this->setGridPosition(0, -24);
294
295
        $content = $manialink->getContentFrame();
296
        $this->setGridSize($content->getWidth(), $content->getHeight() - 24);
297
        $manialink->setData('grid', $gridBuilder);
298
        $this->gridBuilder = $gridBuilder;
299
    }
300
301
    /**
302
     * @param $login
303
     * @param $params
304
     * @param $args
305
     */
306
    public function callbackAdd($login, $params, $args)
307
    {
308
        $this->mxPlugin->addMap($login, $args['mxid'], $params['site']);
309
    }
310
311
    /**
312
     * @param $login
313
     * @param $params
314
     */
315
    public function callbackSearch($login, $params)
316
    {
317
        $params = (object)$params;
318
319
        $this->modebox->setSelectedByValue($params->mode);
320
        $this->orderbox->setSelectedByValue($params->order);
321
        $this->opbox->setSelectedByValue($params->operator);
322
        $this->lengthBox->setSelectedByValue($params->length);
323
        $this->stylebox->setSelectedByValue($params->style);
324
        $this->difficultiesBox->setSelectedByValue($params->difficulties);
325
        $this->tpackBox->setSelectedByValue($params->tpack);
326
327
        $options = "";
328
329
        if ($params->tpack) {
330
            $options .= "&tpack=".$params->tpack;
331
        }
332
        if ($params->operator != -1) {
333
            $options .= "&lengthop=".$params->operator;
334
        }
335
336
        $args = "&mode=".$params->mode."&trackname=".urlencode($params->map)."&anyauthor=".urlencode($params->author).
337
            "&style=".$params->style."&priord=".$params->order."&length=".$params->length.
338
            "&limit=100&gv=1".$options;
339
340
        $query = 'https://'.$params->site.'.mania-exchange.com/tracksearch2/search?api=on'.$args;
341
        $this->http->get($query, [$this, 'setMaps'], ['login' => $login]);
342
343
    }
344
345
346
    /**
347
     * @param HttpResult $result
348
     */
349
    public function setMaps(HttpResult $result)
350
    {
351
        $this->gridBuilder->goToFirstPage();
352
353
        if ($result->hasError()) {
354
            echo $result->getError();
355
356
            return;
357
        }
358
359
        $json = json_decode($result->getResponse(), true);
360
        $data = [];
361
        foreach ($json['results'] as $idx => $mxInfo) {
362
            $map = new MxInfo($mxInfo);
363
            $data[] = [
364
                "index" => $idx + 1,
365
                "name" => TMString::trimControls($map->GbxMapName),
366
                "author" => $map->Username,
367
                "envir" => $map->EnvironmentName,
368
                "awards" => $map->AwardCount ? '$ff0🏆 $fff'.$map->AwardCount : "",
369
                "length" => $map->LengthName,
370
                "style" => $map->StyleName,
371
                "mxid" => $map->TrackID,
372
            ];
373
        }
374
375
        $this->setData($data);
376
377
        $group = $this->groupFactory->createForPlayer($result->getAdditionalData()['login']);
378
        $this->update($group);
379
    }
380
}
381