CurrentMapWidgetFactory::createContent()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 144

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 144
ccs 0
cts 113
cp 0
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace eXpansion\Bundle\WidgetCurrentMap\Plugins\Gui;
4
5
use eXpansion\Bundle\LocalMapRatings\Model\Maprating;
6
use eXpansion\Bundle\LocalMapRatings\Services\MapRatingsService;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
8
use eXpansion\Framework\Core\Model\Gui\Widget;
9
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
10
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
11
use eXpansion\Framework\Core\Storage\GameDataStorage;
12
use eXpansion\Framework\GameManiaplanet\DataProviders\ChatCommandDataProvider;
13
use eXpansion\Framework\Gui\Components\Label;
14
use FML\Script\ScriptLabel;
15
16
class CurrentMapWidgetFactory extends WidgetFactory
17
{
18
19
    /** @var Label */
20
    public $lblNo;
21
22
    /** @var Label */
23
    public $lblYes;
24
    /**
25
     * @var GameDataStorage
26
     */
27
    private $gameDataStorage;
28
    /**
29
     * @var MapRatingsService
30
     */
31
    private $mapRatingsService;
32
    /**
33
     * @var ChatCommandDataProvider
34
     */
35
    private $chatCommandDataProvider;
36
37
    /***
38
     * MenuFactory constructor.
39
     *
40
     * @param                         $name
41
     * @param                         $sizeX
42
     * @param                         $sizeY
43
     * @param null                    $posX
44
     * @param null                    $posY
45
     * @param WidgetFactoryContext    $context
46
     * @param GameDataStorage         $gameDataStorage
47
     * @param MapRatingsService       $mapRatingsService
48
     * @param ChatCommandDataProvider $chatCommandDataProvider
49
     */
50
    public function __construct(
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX,
55
        $posY,
56
        WidgetFactoryContext $context,
57
        GameDataStorage $gameDataStorage,
58
        MapRatingsService $mapRatingsService,
59
        ChatCommandDataProvider $chatCommandDataProvider
60
61
    ) {
62
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
63
64
        $this->gameDataStorage = $gameDataStorage;
65
        $this->mapRatingsService = $mapRatingsService;
66
        $this->chatCommandDataProvider = $chatCommandDataProvider;
67
    }
68
69
    /**
70
     * @param Widget|ManialinkInterface $manialink
71
     */
72
    protected function createContent(ManialinkInterface $manialink)
73
    {
74
        parent::createContent($manialink);
75
76
        $tooltip = $this->uiFactory->createTooltip();
77
        $manialink->addChild($tooltip);
78
79
        /* second row */
80
        $line = $this->uiFactory->createLayoutLine(0, -4.45, [], 0.5);
81
        $line->setAlign("left", "top");
82
        $manialink->addChild($line);
83
        $div = ((60 - 1) / 3);
84
85
        $lbl = $this->uiFactory->createLabel("0 / 0", Label::TYPE_NORMAL, "Players");
86
        $lbl->setTextPrefix("👥  ");
87
        $lbl->setAlign("center", "center2");
88
        $lbl->setTextSize(1)->setSize($div, 4);
89
        $lbl->setAreaColor("0017")->setAreaFocusColor("0013")->setScriptEvents(true);
90
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackShowPlayers"], [],
91
            true));
92
        $line->addChild($lbl);
93
94
        $lbl = $this->uiFactory->createLabel("0 / 0", Label::TYPE_NORMAL, "Spectators");
95
        $lbl->setTextPrefix("🎥  ");
96
        $lbl->setAlign("center", "center2");
97
        $lbl->setTextSize(1)->setSize($div, 4);
98
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
99
        $line->addChild($lbl);
100
101
        $lbl = $this->uiFactory->createLabel("", Label::TYPE_NORMAL, "LocalTime");
102
        $lbl->setAlign("center", "center2");
103
        $lbl->setTextSize(1)->setSize($div, 4);
104
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
105
        $line->addChild($lbl);
106
107
        /* third row */
108
        $line2 = $this->uiFactory->createLayoutLine(0, -9.0, [], 0.5);
109
        $manialink->addChild($line2);
110
        $div = ((60 - 1.5) / 4);
111
112
        $lbl = $this->uiFactory->createLabel("Recs", Label::TYPE_NORMAL);
113
        $lbl->setAlign("center", "center2");
114
        $lbl->setTextSize(1)->setSize($div, 4);
115
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
116
        $lbl->setAction($this->actionFactory->createManialinkAction(
117
            $manialink, [$this, "callbackShowRecs"], [], true)
118
        );
119
120
        $line2->addChild($lbl);
121
122
        $lbl = $this->uiFactory->createLabel("Maps", Label::TYPE_NORMAL);
123
        $lbl->setAlign("center", "center2");
124
        $lbl->setTextSize(1)->setSize($div, 4);
125
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
126
        $lbl->setAction($this->actionFactory->createManialinkAction(
127
            $manialink, [$this, "callbackShowMapList"], [], true)
128
        );
129
130
        $line2->addChild($lbl);
131
132
        $lbl = $this->uiFactory->createLabel("", Label::TYPE_NORMAL);
133
        $lbl->setTextPrefix("  ");
134
        $lbl->setAlign("center", "center2");
135
        $lbl->setTextSize(1)->setSize($div, 4);
136
        $lbl->setAreaColor("0017")->setAreaFocusColor("0707")->setScriptEvents(true);
137
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteYes"], [], true));
138
        $this->lblYes = $lbl;
139
        $line2->addChild($this->lblYes);
140
141
        $lbl = $this->uiFactory->createLabel("", Label::TYPE_NORMAL);
142
        $lbl->setTextPrefix("  ");
143
        $lbl->setAlign("center", "center2");
144
        $lbl->setTextSize(1)->setSize($div, 4);
145
        $lbl->setAreaColor("0017")->setAreaFocusColor("7007")->setScriptEvents(true);
146
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteNo"], [], true));
147
        $this->lblNo = $lbl;
148
        $line2->addChild($this->lblNo);
149
150
151
        $playersMax = $ladderMax = $this->gameDataStorage->getServerOptions()->currentMaxPlayers;
0 ignored issues
show
Unused Code introduced by
$ladderMax is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
152
        $spectatorMax = $ladderMax = $this->gameDataStorage->getServerOptions()->currentMaxSpectators;
0 ignored issues
show
Unused Code introduced by
$ladderMax is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
        $manialink->getFmlManialink()->getScript()->addScriptFunction("",
154
            <<<EOL
155
               Void updatePlayers() {
156
                   declare Integer serverPlayers = 0;
157
                   declare Integer serverSpectators = 0;
158
                   
159
                   foreach (Player in Players) {               
160
                       if (Player.RequestsSpectate) {
161
                            serverSpectators += 1;
162
                       } else {
163
                            serverPlayers += 1;
164
                       }               
165
                   }                     
166
                   
167
                   (Page.GetFirstChild("Players") as CMlLabel).Value = serverPlayers ^ " / {$playersMax}";
168
                   (Page.GetFirstChild("Spectators") as CMlLabel).Value = serverSpectators ^ " / {$spectatorMax}";
169
               }
170
EOL
171
        );
172
173
174
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
175
            <<<EOL
176
            
177
           if (AllPlayerCount != Players.count) {
178
               AllPlayerCount = Players.count;
179
               updatePlayers();
180
           }
181
           
182
           if (OldDateText != CurrentLocalDateText) {
183
                OldDateText = CurrentLocalDateText;
184
                declare Text Seconds = TextLib::SubString(CurrentLocalDateText,17,2);
185
                declare Text delim = ":";          
186
                Counter = (Counter+1)%2;
187
                if (Counter == 1) {
188
                    delim = ".";
189
                }
190
191
                declare Hours = TextLib::SubString(CurrentLocalDateText,10,3);
192
                declare Minutes = TextLib::SubString(CurrentLocalDateText,14,2);
193
                LocalTime.Value = "🕑 "^Hours^delim^Minutes;                                                
194
           } 
195
           
196
           
197
           
198
           
199
    
200
EOL
201
        );
202
203
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
204
            <<<EOL
205
            declare Integer AllPlayerCount = -1;
206
            declare Text OldDateText = "";
207
            declare Counter = 0;                                                                                           
208
            declare LocalTime = (Page.GetFirstChild("LocalTime") as CMlLabel);         
209
            updatePlayers();                                                                                     
210
EOL
211
        );
212
213
        $manialink->addChild($line);
214
215
    }
216
217
    /**
218
     * @param ManialinkInterface|Widget $manialink
219
     * @param string                    $login
220
     * @param array                     $entries
221
     * @param array                     $args
222
     */
223
    public function callbackVoteYes(ManialinkInterface $manialink, $login, $entries, $args)
224
    {
225
        $this->mapRatingsService->changeRating($login, 1);
226
    }
227
228
    /**
229
     * @param ManialinkInterface|Widget $manialink
230
     * @param string                    $login
231
     * @param array                     $entries
232
     * @param array                     $args
233
     */
234
    public function callbackVoteNo(ManialinkInterface $manialink, $login, $entries, $args)
235
    {
236
        $this->mapRatingsService->changeRating($login, -1);
237
    }
238
239
    public function callbackShowMapList(ManialinkInterface $manialink, $login, $entries, $args)
240
    {
241
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/maps", true);
242
    }
243
244
    public function callbackShowRecs(ManialinkInterface $manialink, $login, $entries, $args)
245
    {
246
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/recs", true);
247
    }
248
249
    public function callbackShowPlayers(ManialinkInterface $manialink, $login, $entries, $args)
250
    {
251
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/players", true);
252
    }
253
254
255
    /** @param Maprating[] $ratings */
256
    public function setMapRatings($ratings)
257
    {
258
        $yes = 0;
259
        $no = 0;
260 View Code Duplication
        foreach ($ratings as $login => $rating) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
261
            $score = $rating->getScore();
262
263
            if ($score === 1) {
264
                $yes++;
265
            }
266
            if ($score === -1) {
267
                $no++;
268
            }
269
        }
270
271
        $this->lblYes->setText($yes.' $cbb/ '.count($ratings));
272
        $this->lblNo->setText($no.' $cbb/ '.count($ratings));
273
    }
274
275
}
276