Completed
Push — master ( 151512...fa81e5 )
by De Cramer
08:19 queued 03:57
created

ManiaExchangeWindowFactory::updateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\Maps\Plugins\Gui;
4
5
use eXpansion\Bundle\Maps\Plugins\ManiaExchange;
6
use eXpansion\Bundle\Maps\Structure\MxInfo;
7
use eXpansion\Framework\Core\Helpers\Http;
8
use eXpansion\Framework\Core\Helpers\Structures\HttpResult;
9
use eXpansion\Framework\Core\Helpers\Time;
10
use eXpansion\Framework\Core\Helpers\TMString;
11
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollection;
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\Plugins\Gui\GridWindowFactory;
18
use eXpansion\Framework\Core\Services\Console;
19
use eXpansion\Framework\Core\Storage\GameDataStorage;
20
use eXpansion\Framework\Gui\Components\Button;
21
use eXpansion\Framework\Gui\Components\Dropdown;
22
use eXpansion\Framework\Gui\Components\Label;
23
use FML\Controls\Quad;
24
use Psr\Log\LoggerInterface;
25
26
class ManiaExchangeWindowFactory extends GridWindowFactory
27
{
28
    /** @var  Dropdown */
29
    public $lengthBox;
30
    /** @var  Dropdown */
31
    public $stylebox;
32
    /** @var  Dropdown */
33
    public $sitebox;
34
    /** @var  Dropdown */
35
    public $difficultiesBox;
36
    /** @var  Dropdown */
37
    public $tpackBox;
38
39
    public $tpack = [
40
        "Server Titlepack" => "!server",
41
        "All" => "",
42
    ];
43
44
    /** @var  Dropdown */
45
    private $orderbox;
46
    /** @var  Dropdown */
47
    private $opbox;
48
    /** @var  Dropdown */
49
    private $modebox;
50
51
    /** @var  array */
52
    private $tracksearch;
53
54
    /** @var GridBuilderFactory */
55
    protected $gridBuilderFactory;
56
57
    /** @var DataCollectionFactory */
58
    protected $dataCollectionFactory;
59
60
    /** @var Time */
61
62
    protected $timeFormatter;
63
    /**
64
     * @var  ManiaExchange $mxPlugin
65
     */
66
    private $mxPlugin;
67
    /**
68
     * @var array
69
     */
70
    private $order;
71
    /**
72
     * @var array
73
     */
74
    private $length;
75
    /**
76
     * @var array
77
     */
78
    private $mapStylesTm;
79
    /**
80
     * @var array
81
     */
82
    private $mapStylesSm;
83
    /**
84
     * @var array
85
     */
86
    private $difficulties;
87
    /**
88
     * @var array
89
     */
90
    private $operator;
91
92
    /**
93
     * @var Http
94
     */
95
    private $http;
96
    /**
97
     * @var GameDataStorage
98
     */
99
    private $gameDataStorage;
100
    /**
101
     * @var Console
102
     */
103
    private $console;
104
    /**
105
     * @var LoggerInterface
106
     */
107
    private $logger;
108
109
    /**
110
     * ManiaExchangeWindowFactory constructor.
111
     *
112
     * @param                       $name
113
     * @param                       $sizeX
114
     * @param                       $sizeY
115
     * @param null                  $posX
116
     * @param null                  $posY
117
     * @param WindowFactoryContext  $context
118
     * @param GridBuilderFactory    $gridBuilderFactory
119
     * @param DataCollectionFactory $dataCollectionFactory
120
     * @param Time                  $time
121
     * @param ManiaExchange         $mxPlugin
122
     * @param Http                  $http
123
     * @param GameDataStorage       $gameDataStorage
124
     * @param Console               $console
125
     * @param LoggerInterface       $logger
126
     * @param                       $tracksearch
127
     * @param                       $order
128
     * @param                       $length
129
     * @param                       $mapStylesTm
130
     * @param                       $mapStylesSm
131
     * @param                       $difficulties
132
     * @param                       $operator
133
     */
134
    public function __construct(
135
        $name,
136
        $sizeX,
137
        $sizeY,
138
        $posX,
139
        $posY,
140
        WindowFactoryContext $context,
141
        GridBuilderFactory $gridBuilderFactory,
142
        DataCollectionFactory $dataCollectionFactory,
143
        Time $time,
144
        ManiaExchange $mxPlugin,
145
        Http $http,
146
        GameDataStorage $gameDataStorage,
147
        Console $console,
148
        LoggerInterface $logger,
149
        $tracksearch,
150
        $order,
151
        $length,
152
        $mapStylesTm,
153
        $mapStylesSm,
154
        $difficulties,
155
        $operator
156
157
    ) {
158
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
159
160
        $this->gridBuilderFactory = $gridBuilderFactory;
161
        $this->dataCollectionFactory = $dataCollectionFactory;
162
        $this->timeFormatter = $time;
163
        $this->mxPlugin = $mxPlugin;
164
        $this->http = $http;
165
        $this->gameDataStorage = $gameDataStorage;
166
167
        $this->tracksearch = array_flip($tracksearch);
168
        $this->order = array_flip($order);
169
        $this->length = array_flip($length);
170
        $this->mapStylesTm = array_flip($mapStylesTm);
171
        $this->mapStylesSm = array_flip($mapStylesSm);
172
        $this->difficulties = array_flip($difficulties);
173
        $this->operator = array_flip($operator);
174
175
        $this->console = $console;
176
        $this->logger = $logger;
177
    }
178
179
    /**
180
     * @param ManialinkInterface $manialink
181
     */
182
    protected function createGrid(ManialinkInterface $manialink)
183
    {
184
        $x = 0;
185
186
        $tooltip = $this->uiFactory->createTooltip();
187
        $manialink->addChild($tooltip);
188
189
        $this->modebox = $this->uiFactory->createDropdown("mode", $this->tracksearch, 0);
190
        $this->modebox->setPosition($x, -4, 2);
191
        $manialink->addChild($this->modebox);
192
193
        $label = $this->uiFactory->createLabel("Sort by", Label::TYPE_HEADER);
194
        $label->setPosition($x, 0);
195
        $manialink->addChild($label);
196
197
        $x += 31;
198
        $this->orderbox = $this->uiFactory->createDropdown("order", $this->order, 0);
199
        $this->orderbox->setPosition($x, -4, 2);
200
        $manialink->addChild($this->orderbox);
201
202
        $label = $this->uiFactory->createLabel("Order", Label::TYPE_HEADER);
203
        $label->setPosition($x, 0);
204
        $manialink->addChild($label);
205
206
        $x += 31;
207
        $this->opbox = $this->uiFactory->createDropdown("operator", $this->operator, 0);
208
        $this->opbox->setPosition($x, -4, 2);
209
        $manialink->addChild($this->opbox);
210
211
        $label = $this->uiFactory->createLabel("Operator", Label::TYPE_HEADER);
212
        $label->setPosition($x, 0);
213
        $manialink->addChild($label);
214
215
        $x += 31;
216
        $this->lengthBox = $this->uiFactory->createDropdown("length", $this->length, 0);
217
        $this->lengthBox->setPosition($x, -4, 2);
218
        $manialink->addChild($this->lengthBox);
219
220
        $label = $this->uiFactory->createLabel("Length", Label::TYPE_HEADER);
221
        $label->setPosition($x, 0);
222
        $manialink->addChild($label);
223
224
        $x += 31;
225
        $this->stylebox = $this->uiFactory->createDropdown("style", $this->mapStylesTm, 0);
226
        $this->stylebox->setPosition($x, -4, 2);
227
        $manialink->addChild($this->stylebox);
228
229
        $label = $this->uiFactory->createLabel("Style", Label::TYPE_HEADER);
230
        $label->setPosition($x, 0);
231
        $manialink->addChild($label);
232
233
        $x += 31;
234
        $this->difficultiesBox = $this->uiFactory->createDropdown("difficulties", $this->difficulties, 0);
235
        $this->difficultiesBox->setPosition($x, -4, 2);
236
        $manialink->addChild($this->difficultiesBox);
237
238
        $label = $this->uiFactory->createLabel("Difficulty", Label::TYPE_HEADER);
239
        $label->setPosition($x, 0);
240
        $manialink->addChild($label);
241
242
        /// second line
243
        $idx = 0;
244
        if ($this->gameDataStorage->getTitle() == "SM") {
245
            $idx = 1;
246
        }
247
        $this->sitebox = $this->uiFactory->createDropdown("site", ["Trackmania" => "tm", "Storm" => "sm"], $idx);
248
        $this->sitebox->setPosition(0, -10, 2);
249
        $manialink->addChild($this->sitebox);
250
251
        $this->tpackBox = $this->uiFactory->createDropdown("tpack", $this->tpack, 0);
252
        $this->tpackBox->setPosition(31, -10, 2);
253
        $manialink->addChild($this->tpackBox);
254
255
        $mapname = $this->uiFactory->createInput("map");
256
257
        $author = $this->uiFactory->createInput("author");
258
259
260
        $search = $this->uiFactory->createButton('🔍 Search', Button::TYPE_DECORATED);
261
        $search->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackSearch'],
262
            null, true));
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...
263
264
        $all = $this->uiFactory->createConfirmButton('Install view', Button::TYPE_DEFAULT);
265
        $tooltip->addTooltip($all, "Install all maps from the view");
266
        $all->setBackgroundColor("f00");
267
        $all->setAction($this->actionFactory->createManialinkAction($manialink, [$this, 'callbackInstallAll'], 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...
268
            true));
269
270
        $spacer = Quad::create();
271
        $spacer->setSize(7, 3)->setOpacity(0);
272
273
        $line = $this->uiFactory->createLayoutLine(62, -10, [$mapname, $author, $search, $spacer, $all], 1);
274
        $manialink->addChild($line);
275
276
        $addButton = $this->uiFactory->createConfirmButton('Install', Button::TYPE_DEFAULT);
277
        $addButton->setSize(20, 4);
278
279
        $gridBuilder = $this->gridBuilderFactory->create();
280
        $gridBuilder->setManialink($manialink)
281
            ->setDataCollection($manialink->getData('dataCollection'))
282
            ->setManialinkFactory($this)
283
            ->addTextColumn(
284
                'index',
285
                'expansion_mx.gui.mxsearch.column.index',
286
                1,
287
                true,
288
                false
289
            )->addTextColumn(
290
                'name',
291
                'expansion_mx.gui.mxsearch.column.name',
292
                5,
293
                true,
294
                false
295
            )->addTextColumn(
296
                'author',
297
                'expansion_mx.gui.mxsearch.column.author',
298
                3,
299
                true
300
            );
301
302
303
        if ($this->gameDataStorage->getTitle() == "TM") {
304
            $gridBuilder
305
                ->addTextColumn(
306
                    'envir',
307
                    'expansion_mx.gui.mxsearch.column.envir',
308
                    2,
309
                    true,
310
                    false
311
                )->addTextColumn(
312
                    'awards',
313
                    'expansion_mx.gui.mxsearch.column.awards',
314
                    1,
315
                    true,
316
                    false
317
                )->addTextColumn(
318
                    'length',
319
                    'expansion_mx.gui.mxsearch.column.length',
320
                    2,
321
                    true,
322
                    false
323
                )->addTextColumn(
324
                    'style',
325
                    'expansion_mx.gui.mxsearch.column.style',
326
                    2,
327
                    true,
328
                    false
329
                );
330
        }
331
        if ($this->gameDataStorage->getTitle() == "SM") {
332
            $gridBuilder->addTextColumn(
333
                'maptype',
334
                'expansion_mx.gui.mxsearch.column.maptype',
335
                2,
336
                true,
337
                false
338
            )->addTextColumn(
339
                'awards',
340
                'expansion_mx.gui.mxsearch.column.awards',
341
                1,
342
                true,
343
                false
344
            )->addTextColumn(
345
                'difficulty',
346
                'expansion_mx.gui.mxsearch.column.difficulty',
347
                2,
348
                true,
349
                false
350
            );
351
        }
352
353
        $gridBuilder->addActionColumn('add', 'expansion_mx.gui.mxsearch.column.add', 2, array($this, 'callbackAdd'),
354
            $addButton);
355
        $this->setGridPosition(0, -18);
356
357
        $content = $manialink->getContentFrame();
358
        $this->setGridSize($content->getWidth(), $content->getHeight() - 18);
359
        $manialink->setData('grid', $gridBuilder);
360
        $this->gridBuilder = $gridBuilder;
361
        $params = [
362
            "mode" => 0,
363
            "order" => 0,
364
            "operator" => 0,
365
            "length" => -1,
366
            "style" => -1,
367
            "difficulties" => -1,
368
            "tpack" => -1,
369
        ];
370
        $manialink->setData("params", (object)$params);
371
372
    }
373
374
    protected function updateContent(ManialinkInterface $manialink)
375
    {
376
        $params = $manialink->getData("params");
377
        $this->modebox->setSelectedByValue($params->mode);
378
        $this->orderbox->setSelectedByValue($params->order);
379
        $this->opbox->setSelectedByValue($params->operator);
380
        $this->lengthBox->setSelectedByValue($params->length);
381
        $this->stylebox->setSelectedByValue($params->style);
382
        $this->difficultiesBox->setSelectedByValue($params->difficulties);
383
        $this->tpackBox->setSelectedByValue($params->tpack);
384
385
        parent::updateContent($manialink);
386
    }
387
388
    /**
389
     * @param ManialinkInterface $manialink
390
     * @param                    $login
391
     * @param                    $params
392
     * @param                    $args
393
     */
394
    public function callbackAdd(ManialinkInterface $manialink, $login, $params, $args)
395
    {
396
        if ($params['site'] == "") {
397
            $params['site'] = strtolower($this->gameDataStorage->getTitle());
398
        }
399
        $this->mxPlugin->addMapToQueue($login, $args['mxid'], $params['site']);
400
    }
401
402
    public function callbackInstallAll(ManialinkInterface $manialink, $login, $params, $arguments)
403
    {
404
        /** @var DataCollection $collection */
405
        $collection = $manialink->getData('dataCollection');
406
407
        /** @var GridBuilder $grid */
408
        $grid = $manialink->getData('grid');
409
410
        $data = $collection->getData($grid->getCurrentPage());
411
        $this->mxPlugin->addAllMaps($login, $data);
412
    }
413
414
    /**
415
     * @param ManialinkInterface $manialink
416
     * @param                    $login
417
     * @param                    $params
418
     * @param                    $arguments
419
     */
420
    public function callbackSearch(ManialinkInterface $manialink, $login, $params, $arguments)
421
    {
422
423
        $params = (object)$params;
424
        $manialink->setData('params', $params);
425
426
        $options = "";
427
428
        if ($params->tpack) {
429
            $title = $params->tpack;
430
            if ($params->tpack === "!server") {
431
                $title = explode("@", $this->gameDataStorage->getVersion()->titleId);
432
                $title = $title[0];
433
            }
434
            $options .= "&tpack=".$title;
435
        }
436
        if ($params->operator != -1) {
437
            $options .= "&lengthop=".$params->operator;
438
        }
439
        $map = "";
440
        if ($params->map) {
441
            $map = "&trackname=".urlencode($params->map);
442
        }
443
444
        $args = "&mode=".$params->mode.$map."&anyauthor=".urlencode($params->author).
445
            "&style=".$params->style."&priord=".$params->order."&length=".$params->length.
446
            "&limit=100&gv=1".$options;
447
448
        $query = 'https://'.$params->site.'.mania-exchange.com/tracksearch2/search?api=on'.$args;
449
450
        $this->setBusy($manialink, "Searching, please wait...");
451
        $this->http->get($query, [$this, 'setMaps'], ['login' => $login, 'params' => $params, 'ml' => $manialink]);
452
453
    }
454
455
456
    /**
457
     * @param HttpResult $result
458
     */
459
    public function setMaps(HttpResult $result)
460
    {
461
        $manialink = $result->getAdditionalData()['ml'];
462
        $params = $result->getAdditionalData()['params'];
463
464
        $this->gridBuilder->goToFirstPage($manialink);
465
466
        if ($result->hasError()) {
467
            $this->console->writeln('$d00Error: '.$result->getError().' while searching maps from MX, see logs for more details.');
468
            $this->logger->error("error while searching maps from mx: ".$result->getError());
469
470
            return;
471
        }
472
473
        $json = json_decode($result->getResponse(), true);
474
        $data = [];
475
        foreach ($json['results'] as $idx => $mxInfo) {
476
            $map = new MxInfo($mxInfo);
477
            $data[] = [
478
                "index" => $idx + 1,
479
                "name" => TMString::trimControls($map->gbxMapName),
480
                "author" => $map->username,
481
                "envir" => $map->environmentName,
482
                "awards" => $map->awardCount ? '$ff0🏆 $fff'.$map->awardCount : "",
483
                "length" => $map->lengthName,
484
                "difficulty" => $map->difficultyName,
485
                "maptype" => $map->mapType,
486
                "style" => $map->styleName,
487
                "mxid" => $map->trackID,
488
                "mxsite" => $params->site,
489
            ];
490
        }
491
492
        $this->setData($manialink, $data);
493
        $this->update($result->getAdditionalData()['login']);
494
    }
495
}
496