Completed
Pull Request — master (#194)
by De Cramer
24:15 queued 02:45
created

MarkersWidget::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 96
rs 8.3859
c 1
b 0
f 0
ccs 0
cts 8
cp 0
cc 1
eloc 14
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\CustomUi\Plugins\Gui;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Model\Gui\Widget;
7
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
8
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
9
use FML\Script\ScriptLabel;
10
11
class MarkersWidget extends WidgetFactory
12
{
13
14
    /**
15
     * ChatHelperWidget constructor.
16
     * @param                      $name
17
     * @param                      $sizeX
18
     * @param                      $sizeY
19
     * @param                      $posX
20
     * @param                      $posY
21
     * @param WidgetFactoryContext $context
22
     */
23
    public function __construct(
24
        $name,
25
        $sizeX,
26
        $sizeY,
27
        $posX,
28
        $posY,
29
        WidgetFactoryContext $context
30
    ) {
31
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
32
    }
33
34
35
    /**
36
     * @param ManialinkInterface|Widget $manialink
37
     */
38
    protected function createContent(ManialinkInterface $manialink)
39
    {
40
        parent::createContent($manialink);
41
        $manialink->getFmlManialink()->getScript()->addScriptFunction("",
42
            /** @lang text */
43
            <<<EOL
44
               Text TimeToText(Integer intime) {
45
                declare time = MathLib::Abs(intime);
46
                declare Integer cent = time % 1000;	
47
                declare Integer sec = (time / 1000) % 60;
48
                declare Integer min = time / 60000;
49
                declare Integer hour = time / 3600000;
50
                declare Text sign = "";
51
                if (intime < 0)  {
52
                    sign = "-";
53
                }
54
                
55
                if (hour > 0) {
56
                    return sign ^ hour ^ ":" ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);
57
                } 
58
                return sign ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);                                
59
            }
60
            
61
            Void UpdateData() {                    
62
                declare Integer[Vec3] MapBestCheckpoints for Page = Integer[Vec3];                                    
63
                declare Text[Vec3] MapBestNicknames for Page = Text[Vec3];
64
                declare Vec3[] Positions for Page = Vec3[];          
65
                   
66
                declare xml = "";
67
              
68
                foreach(pos in Positions) {          
69
                if (MapBestCheckpoints[pos] != 99999999) {                                                                                                           
70
                    xml ^= "<marker label='"^MapBestNicknames[pos] ^ " " ^ TimeToText(MapBestCheckpoints[pos]) ^ 
71
                    "' pos='"^pos.X^" "^(pos.Y + 3.)^" "^pos.Z^"' distmax='300' />";
72
                    }                                                                               
73
                }
74
                ClientUI.MarkersXML = xml;                    
75
            }
76
                                
77
EOL
78
        );
79
80
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
81
            /** @lang ManiaScript */
82
            <<<EOL
83
            declare Integer[Vec3] MapBestCheckpoints for Page = Integer[Vec3];                                    
84
            declare Text[Vec3] MapBestNicknames for Page = Text[Vec3];
85
            declare Vec3[] Positions for Page = Vec3[];                                                                 
86
                
87
                    
88
                                                                       
89
            // clear
90
            foreach (position in MapCheckpointPos) {
91
                MapBestCheckpoints[position] = 99999999;
92
                MapBestNicknames[position] = "";       
93
                Positions.add(position);
94
            }            
95
                        
96
            UpdateData();                                                                            
97
EOL
98
        );
99
100
        /**
101
         * Loop
102
         */
103
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
104
105
            /** @lang text */
106
            <<<EOL
107
                                
108
            foreach (RaceEvent in RaceEvents) {                                
109
                if (RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {                                                                                              
110
                    declare Vec3 pos = RaceEvent.Player.Position;
111
                    declare Vec3 key = Vec3;
112
                    declare Boolean found =  False;
113
                    foreach(cpPos in Positions) {                                              
114
                        if (MathLib::Distance (pos, cpPos) < 12.5) {                        
115
                            key = cpPos;      
116
                            found = True;                             
117
                            break;                     
118
                        }
119
                    }
120
                    
121
                    if (found) {                        
122
                        if (RaceEvent.LapTime < MapBestCheckpoints[key]) {
123
                            MapBestCheckpoints[key] = RaceEvent.LapTime;
124
                            MapBestNicknames[key] = RaceEvent.Player.User.Name;
125
                            UpdateData();
126
                        }
127
                    }
128
                }        
129
            }
130
EOL
131
        );
132
133
    }
134
135
136
}
137