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

CurrentMapWidgetFactory::callbackVoteNo()   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", "center2")->setPosition(30, 0);
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(0, -4.45, [], 0.5);
88
        $line->setAlign("left", "top");
89
        $manialink->addChild($line);
90
        $div = ((60 - 1)/ 3);
91
92
        $lbl = $this->uiFactory->createLabel("0 / 0", uiLabel::TYPE_NORMAL, "Players");
93
        $lbl->setTextPrefix("👥  ");
94
        $lbl->setAlign("center", "center2");
95
        $lbl->setTextSize(1)->setSize($div, 4);
96
        $lbl->setAreaColor("0017")->setAreaFocusColor("0013")->setScriptEvents(true);
97
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackShowPlayers"], [],
98
            true));
99
        $tooltip->addTooltip($lbl, "Players on server");
100
        $line->addChild($lbl);
101
102
        $lbl = $this->uiFactory->createLabel("0 / 0", uiLabel::TYPE_NORMAL, "Spectators");
103
        $lbl->setTextPrefix("🎥  ");
104
        $lbl->setAlign("center", "center2");
105
        $lbl->setTextSize(1)->setSize($div, 4);
106
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
107
        $tooltip->addTooltip($lbl, "Spectators on server");
108
        $line->addChild($lbl);
109
110
        $ladderMin = $this->gameDataStorage->getServerOptions()->ladderServerLimitMin / 1000;
111
        $ladderMax = $this->gameDataStorage->getServerOptions()->ladderServerLimitMax / 1000;
112
113
        $lbl = $this->uiFactory->createLabel($ladderMin." - ".$ladderMax."k", uiLabel::TYPE_NORMAL);
114
        $lbl->setAlign("center", "center2");
115
        $lbl->setTextSize(1)->setSize($div, 4);
116
        $lbl->setAreaColor("0017")->setAreaFocusColor("0017")->setScriptEvents(true);
117
        $tooltip->addTooltip($lbl, "Ladder limits");
118
        $line->addChild($lbl);
119
120
        /* third row */
121
        $line2 = $this->uiFactory->createLayoutLine(0, -9.0, [], 0.5);
122
        $manialink->addChild($line2);
123
        $div = ((60 - 1.5) / 4);
124
125
        $lbl = $this->uiFactory->createLabel("Recs", uiLabel::TYPE_NORMAL);
126
        $lbl->setAlign("center", "center2");
127
        $lbl->setTextSize(1)->setSize($div, 4);
128
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
129
        $lbl->setAction($this->actionFactory->createManialinkAction(
130
            $manialink, [$this, "callbackShowRecs"], [], true)
131
        );
132
        $tooltip->addTooltip($lbl, "Show Local Records");
133
        $line2->addChild($lbl);
134
135
        $lbl = $this->uiFactory->createLabel("Maps", uiLabel::TYPE_NORMAL);
136
        $lbl->setAlign("center", "center2");
137
        $lbl->setTextSize(1)->setSize($div, 4);
138
        $lbl->setAreaColor("0017")->setAreaFocusColor("0014")->setScriptEvents(true);
139
        $lbl->setAction($this->actionFactory->createManialinkAction(
140
            $manialink, [$this, "callbackShowMapList"], [], true)
141
        );
142
        $tooltip->addTooltip($lbl, "Show Map List");
143
        $line2->addChild($lbl);
144
145
        $lbl = $this->uiFactory->createLabel("", uiLabel::TYPE_NORMAL);
146
        $lbl->setTextPrefix("  ");
147
        $lbl->setAlign("center", "center2");
148
        $lbl->setTextSize(1)->setSize($div, 4);
149
        $lbl->setAreaColor("0017")->setAreaFocusColor("0707")->setScriptEvents(true);
150
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteYes"], [], true));
151
        $tooltip->addTooltip($lbl, "Vote up the map");
152
        $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...
153
        $line2->addChild($this->lblYes);
154
155
        $lbl = $this->uiFactory->createLabel("", uiLabel::TYPE_NORMAL);
156
        $lbl->setTextPrefix("  ");
157
        $lbl->setAlign("center", "center2");
158
        $lbl->setTextSize(1)->setSize($div, 4);
159
        $lbl->setAreaColor("0017")->setAreaFocusColor("7007")->setScriptEvents(true);
160
        $lbl->setAction($this->actionFactory->createManialinkAction($manialink, [$this, "callbackVoteNo"], [], true));
161
        $tooltip->addTooltip($lbl, "Vote down the map");
162
        $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...
163
        $line2->addChild($this->lblNo);
164
165
166
        $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...
167
        $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...
168
        $manialink->getFmlManialink()->getScript()->addScriptFunction("",
169
            <<<EOL
170
               Void updatePlayers() {
171
                   declare Integer serverPlayers = 0;
172
                   declare Integer serverSpectators = 0;
173
                   
174
                   foreach (Player in Players) {               
175
                       if (Player.RequestsSpectate) {
176
                            serverSpectators += 1;
177
                       } else {
178
                            serverPlayers += 1;
179
                       }               
180
                   }                     
181
                   
182
                   (Page.GetFirstChild("Players") as CMlLabel).Value = serverPlayers ^ " / {$playersMax}";
183
                   (Page.GetFirstChild("Spectators") as CMlLabel).Value = serverSpectators ^ " / {$spectatorMax}";
184
               }
185
EOL
186
        );
187
188
189
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
190
            <<<EOL
191
            
192
           if (AllPlayerCount != Players.count) {
193
               AllPlayerCount = Players.count;
194
               updatePlayers();
195
           }   
196
    
197
EOL
198
        );
199
200
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
201
            <<<EOL
202
            declare Integer AllPlayerCount = -1;                                               
203
            (Page.GetFirstChild("MapName") as CMlLabel).Value = Map.AuthorNickName ^ "\$z\$s - " ^ Map.MapName;                                         
204
            updatePlayers();                                                                                     
205
EOL
206
        );
207
208
        $manialink->addChild($line);
209
210
    }
211
212
    /**
213
     * @param ManialinkInterface|Widget $manialink
214
     * @param string                    $login
215
     * @param array                     $entries
216
     * @param array                     $args
217
     */
218
    public function callbackVoteYes(ManialinkInterface $manialink, $login, $entries, $args)
219
    {
220
        $this->mapRatingsService->changeRating($login, 1);
221
    }
222
223
    /**
224
     * @param ManialinkInterface|Widget $manialink
225
     * @param string                    $login
226
     * @param array                     $entries
227
     * @param array                     $args
228
     */
229
    public function callbackVoteNo(ManialinkInterface $manialink, $login, $entries, $args)
230
    {
231
        $this->mapRatingsService->changeRating($login, -1);
232
    }
233
234
    public function callbackShowMapList(ManialinkInterface $manialink, $login, $entries, $args)
235
    {
236
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/maps", true);
237
    }
238
239
    public function callbackShowRecs(ManialinkInterface $manialink, $login, $entries, $args)
240
    {
241
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/recs", true);
242
    }
243
244
    public function callbackShowPlayers(ManialinkInterface $manialink, $login, $entries, $args)
245
    {
246
        $this->chatCommandDataProvider->onPlayerChat($login, $login, "/players", true);
247
    }
248
249
250
    /** @param Maprating[] $ratings */
251
    public function setMapRatings($ratings)
252
    {
253
        $yes = 0;
254
        $no = 0;
255 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...
256
            $score = $rating->getScore();
257
258
            if ($score === 1) {
259
                $yes++;
260
            }
261
            if ($score === -1) {
262
                $no++;
263
            }
264
        }
265
266
        $this->lblYes->setText($yes.' $cbb/ '.count($ratings));
267
        $this->lblNo->setText($no.' $cbb/ '.count($ratings));
268
    }
269
270
}
271