Completed
Pull Request — master (#254)
by
unknown
03:22
created

CurrentMapWidgetFactory::callbackShowRecs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
crap 2
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\uiLabel;
14
use FML\Script\ScriptLabel;
15
16
class CurrentMapWidgetFactory extends WidgetFactory
17
{
18
19
    /** @var UiLabel */
20
    public $lblNo;
21
22
    /** @var UiLabel */
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
        /* first row */
80
        $lbl = $this->uiFactory->createLabel("unknown map", uiLabel::TYPE_NORMAL, "MapName");
81
        $lbl->setAlign("center", "center");
82
        $lbl->setTextSize(1)->setSize(60, 4);
83
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
84
        $manialink->addChild($lbl);
85
86
        /* second row */
87
        $line = $this->uiFactory->createLayoutLine(-(19 / 2) - 1, -5, [], 0.75);
88
        $manialink->addChild($line);
89
90
        $lbl = $this->uiFactory->createLabel("0 / 0", uiLabel::TYPE_NORMAL, "Players");
91
        $lbl->setTextPrefix("👥  ");
92
        $lbl->setAlign("center", "center2");
93
        $lbl->setTextSize(1)->setSize(19.5, 4);
94
        $lbl->setAreaColor("0017")->setAreaFocusColor("0013")->setScriptEvents(true);
95
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackShowPlayers"], [], true));
96
        $tooltip->addTooltip($lbl, "Players on server");
97
        $line->addChild($lbl);
98
99
        $lbl = $this->uiFactory->createLabel("0 / 0", uiLabel::TYPE_NORMAL, "Spectators");
100
        $lbl->setTextPrefix("🎥  ");
101
        $lbl->setAlign("center", "center2");
102
        $lbl->setTextSize(1)->setSize(19.5, 4);
103
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
104
        $tooltip->addTooltip($lbl, "Spectators on server");
105
        $line->addChild($lbl);
106
107
        $ladderMin = $this->gameDataStorage->getServerOptions()->ladderServerLimitMin / 1000;
108
        $ladderMax = $this->gameDataStorage->getServerOptions()->ladderServerLimitMax / 1000;
109
110
        $lbl = $this->uiFactory->createLabel($ladderMin." - ".$ladderMax."k", uiLabel::TYPE_NORMAL);
111
        $lbl->setAlign("center", "center2");
112
        $lbl->setTextSize(1)->setSize(19.5, 4);
113
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
114
        $tooltip->addTooltip($lbl, "Ladder limits");
115
        $line->addChild($lbl);
116
117
        /* third row */
118
        $line2 = $this->uiFactory->createLayoutLine(-16, -10, [], 1.33);
119
        $manialink->addChild($line2);
120
121
        $lbl = $this->uiFactory->createLabel("Recs", uiLabel::TYPE_NORMAL);
122
        $lbl->setAlign("center", "center2");
123
        $lbl->setTextSize(1)->setSize(14, 4);
124
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
125
        $lbl->setAction($this->actionFactory->createManialinkAction(
126
            $manialink, [$this, "callbackShowRecs"], [], true)
127
        );
128
        $tooltip->addTooltip($lbl, "Show Local Records");
129
        $line2->addChild($lbl);
130
131
        $lbl = $this->uiFactory->createLabel("Maps", uiLabel::TYPE_NORMAL);
132
        $lbl->setAlign("center", "center2");
133
        $lbl->setTextSize(1)->setSize(14, 4);
134
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
135
        $lbl->setAction($this->actionFactory->createManialinkAction(
136
            $manialink, [$this, "callbackShowMapList"], [], true)
137
        );
138
        $tooltip->addTooltip($lbl, "Show Map List");
139
        $line2->addChild($lbl);
140
141
        $lbl = $this->uiFactory->createLabel("", uiLabel::TYPE_NORMAL);
142
        $lbl->setTextPrefix("  ");
143
        $lbl->setAlign("center", "center2");
144
        $lbl->setTextSize(1)->setSize(14, 4);
145
        $lbl->setAreaColor("0017")->setAreaFocusColor("0707")->setScriptEvents(true);
146
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteYes"], [], true));
147
        $tooltip->addTooltip($lbl, "Vote up the map");
148
        $this->lblYes = $lbl;
0 ignored issues
show
Documentation Bug introduced by
It seems like $lbl of type object<eXpansion\Framewo...Gui\Components\uiLabel> is incompatible with the declared type object<eXpansion\Bundle\...ap\Plugins\Gui\UiLabel> of property $lblYes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
149
        $line2->addChild($this->lblYes);
150
151
        $lbl = $this->uiFactory->createLabel("", uiLabel::TYPE_NORMAL);
152
        $lbl->setTextPrefix("  ");
153
        $lbl->setAlign("center", "center2");
154
        $lbl->setTextSize(1)->setSize(14, 4);
155
        $lbl->setAreaColor("0017")->setAreaFocusColor("7007")->setScriptEvents(true);
156
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteNo"], [], true));
157
        $tooltip->addTooltip($lbl, "Vote down the map");
158
        $this->lblNo = $lbl;
0 ignored issues
show
Documentation Bug introduced by
It seems like $lbl of type object<eXpansion\Framewo...Gui\Components\uiLabel> is incompatible with the declared type object<eXpansion\Bundle\...ap\Plugins\Gui\UiLabel> of property $lblNo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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