Completed
Push — master ( 43ea8a...39fe47 )
by
unknown
01:32
created

MarkersWidget::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 98
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 98
ccs 0
cts 8
cp 0
rs 8.3352
c 1
b 0
f 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='150' />";
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
            // clear
88
            foreach (position in MapCheckpointPos) {
89
                MapBestCheckpoints[position] = 99999999;
90
                MapBestNicknames[position] = "";       
91
                Positions.add(position);
92
            }            
93
                        
94
            UpdateData();                                                                            
95
EOL
96
        );
97
98
        /**
99
         * Loop
100
         */
101
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
102
103
            /** @lang textmate */
104
            <<<EOL
105
                                
106
            foreach (RaceEvent in RaceEvents) {                                
107
                if (RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {                                                                                              
108
                    declare Vec3 pos = RaceEvent.Player.Position;
109
                    declare Vec3 key = Vec3;
110
                    declare Boolean found =  False;
111
                    foreach(cpPos in Positions) {
112
                        
113
                        log("position:" ^ pos.X ^","^  pos.Y ^","^  pos.Z ^" to " ^ cpPos.X ^","^  cpPos.Y ^","^  cpPos.Z);
114
                        log("Distance:" ^MathLib::Distance (pos, cpPos));
115
                        if (MathLib::Distance (pos, cpPos) < 12.5) {                        
116
                            key = cpPos;      
117
                            found = True; 
118
                            log("found!");
119
                            break;                     
120
                        }
121
                    }
122
                    
123
                    if (found) {                        
124
                        if (RaceEvent.LapTime < MapBestCheckpoints[key]) {
125
                            MapBestCheckpoints[key] = RaceEvent.LapTime;
126
                            MapBestNicknames[key] = RaceEvent.Player.User.Name;
127
                            UpdateData();
128
                        }
129
                    }
130
                }        
131
            }
132
EOL
133
        );
134
135
    }
136
137
138
}
139