Completed
Pull Request — master (#254)
by
unknown
12:34
created

ManiaExchangeWindowFactory::setMaps()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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