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