Passed
Pull Request — master (#145)
by PoUpA
02:24
created

gym.maps.js ➔ formatGyms   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 78
rs 8.4316

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
function initMap()
2
{
3
	$('.gym_details').hide();
4
	//ensure that gmaps is loaded before loading infobox (nasty but usefull trick)
5
	$.getScript("//rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js").done(function () {
6
		$.ajax({
7
			'async': true,
8
			'type': "GET",
9
			'global': false,
10
			'dataType': 'text',
11
			'url': "core/process/aru.php",
12
			'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target', 'type' : 'gym_map' }}).done(function (data) {
13
			
14
			
15
			// Get website variables
16
			
17
				$.getJSON("core/json/variables.json", function ( jsondata ) {
18
				
19
					var variables = jsondata;
20
												
21
					var lattitude = Number(variables['system']['map_center_lat']);
22
					var longitude = Number(variables['system']['map_center_long']);
23
					var zoom_level = Number(variables['system']['zoom_level']);
24
				
25
					// Convert return to JSON Array
26
				
27
					var locations = jQuery.parseJSON(data);
28
					var arr = [];
29
				
30
					for (i = 0; i < locations.length; i++) {
31
						arr.push(jQuery.parseJSON(locations[i]));
32
					}
33
				
34
				
35
					var map = new google.maps.Map(document.getElementById('map'), {
36
						center: {lat: lattitude, lng: longitude},
37
						zoom: zoom_level,
38
						zoomControl: true,
39
						scaleControl: false,
40
						scrollwheel: true,
41
						disableDoubleClickZoom: false,
42
					});
43
				
44
					var infowindow = new InfoBox({
45
						content: document.getElementById("gym_details_template"),
46
						disableAutoPan: false,
47
						pixelOffset: new google.maps.Size(-35, 30),
48
						zIndex: null,
49
						closeBoxURL: "",
50
						infoBoxClearance: new google.maps.Size(1, 1)
51
					});
52
				
53
					google.maps.event.addListener(map, "click", function () {
54
						infowindow.close();
55
					});
56
				
57
					var marker, i;
58
			
59
					for (i = 0; i < arr.length; i++) {
60
						marker = new google.maps.Marker({
61
							position: new google.maps.LatLng(arr[i][2], arr[i][3]),
62
							map: map,
63
							icon: 'core/img/'+arr[i][1],
64
						});
65
					
66
					
67
						google.maps.event.addListener(marker, 'click', (function (marker, i) {
68
							return function () {
69
								infowindow.setContent(arr[i][0]);
70
								infowindow.open(map, marker);
71
								$.ajax({
72
									'async': true,
73
									'type': "GET",
74
									'global': false,
75
									'dataType': 'json',
76
									'url': "core/process/aru.php",
77
									'data': {
78
										'request': "",
79
										'target': 'arrange_url',
80
										'method': 'method_target',
81
										'type' : 'gym_defenders',
82
										'gym_id' : arr[i][5]
83
									},
84
									'success': function (data) {
85
										setGymDetails(data);
86
										infowindow.setContent($('#gym_details_template').html());
87
									}
88
								});
89
							}
90
						})(marker, i));
91
					}
92
					map.set('styles',[{"featureType":"all","elementType":"labels.text.fill","stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]},{"featureType":"all","elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]},{"featureType":"all","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#fefefe"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":20}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#dedede"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#c2ffd7"}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#ffffff"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":16}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#f2f2f2"},{"lightness":19}]},{"featureType":"water","elementType":"geometry","stylers":[{"color":"#e9e9e9"},{"lightness":17}]},{"featureType":"water","elementType":"geometry.fill","stylers":[{"color":"#b3d8f9"}]}]);
93
				});
94
			});
95
	});
96
}
97
98
function setGymDetails(gym)
99
{
100
	// replace http with https to fix mixed content
101
	var imgurl = gym.gymDetails.gymInfos.url;
102
	imgurl = imgurl.replace(/^http:\/\//i, 'https://');
103
	$('#gym_details_template #circleImage').css("background", "url("+imgurl+") no-repeat center");
104
	$('#gym_details_template #gymName').html(gym.gymDetails.gymInfos.name);
105
	$('#gym_details_template #gymDescription').html(gym.gymDetails.gymInfos.description);
106
	$('#gym_details_template #gymDefenders').html(gym.infoWindow);
107
	$('#gym_details_template #gymPrestigeDisplay').html(gym.gymDetails.gymInfos.points);
108
	
109
	$('#gym_details_template #gymLastModifiedDisplay').html(gym.gymDetails.gymInfos.last_modified);
110
	var currentTeamColor = 'white';
0 ignored issues
show
Unused Code introduced by
The assignment to variable currentTeamColor seems to be never used. Consider removing it.
Loading history...
111
	switch (gym.gymDetails.gymInfos.team) {
112
		case "1":
113
			currentTeamColor = 'rgb(0, 170, 255)';
114
		break;
115
		case "2":
116
			currentTeamColor = 'rgb(255, 118, 118)';
117
		break;
118
		case "3":
119
			currentTeamColor = 'rgb(255, 190, 8)';
120
		break;
121
			
122
		default:
123
			currentTeamColor = 'white';
124
		break;
125
	}
126
	var currentGymPrestige = gym.gymDetails.gymInfos.points;
127
	formatGyms(currentGymPrestige, currentTeamColor);
128
	$('#gym_details_template').show();
129
}
130
131
function formatGyms(gymPrestigeValue,teamColor){
132
	var gymPrestige = gymPrestigeValue;
133
	var gymRanks = [
134
	{
135
		level : 1,
136
		prestigeMax : 2000,
137
		prestigeMin : 0
138
	},
139
	{
140
		level : 2,
141
		prestigeMax : 4000,
142
		prestigeMin : 2000
143
	},
144
	{
145
		level : 3,
146
		prestigeMax : 8000,
147
		prestigeMin : 4000
148
	},
149
	{
150
		level : 4,
151
		prestigeMax : 12000,
152
		prestigeMin : 8000
153
	},
154
	{
155
		level : 5,
156
		prestigeMax : 16000,
157
		prestigeMin : 12000
158
	},
159
	{
160
		level : 6,
161
		prestigeMax : 20000,
162
		prestigeMin : 16000
163
	},
164
	{
165
		level : 7,
166
		prestigeMax : 30000,
167
		prestigeMin : 20000
168
	},
169
	{
170
		level : 8,
171
		prestigeMax : 40000,
172
		prestigeMin : 30000
173
	},
174
	{
175
		level : 9,
176
		prestigeMax : 50000,
177
		prestigeMin : 40000
178
	},
179
	{
180
		level : 10,
181
		prestigeMax : 52000,
182
		prestigeMin : 50000
183
	}
184
	];
185
	
186
	$('#gym_details_template #gymInfos').css("border-color", teamColor);
187
	//Set rank positions (50000 = 90% for rank 10 to be visible)
188
	var gymPercent = 50000/90;
189
	if (gymPrestige>50000) {
190
		//compensate for last rank
191
		gymPrestige=(50000+((gymPrestige-50000)*2.775))
192
	}
193
	$('.bar-step').removeClass('active');
194
	for (var i in gymRanks) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
195
		var width = (((gymRanks[i].prestigeMax)-(gymRanks[i].prestigeMin))/gymPercent);
196
		if(gymRanks[i].level > 9) {
197
			width = 10;
198
		}
199
		var left = (gymRanks[i].prestigeMin/gymPercent);
200
		var active = (gymPrestige >= gymRanks[i].prestigeMax);
201
		if(active){
202
			$('.gymRank'+gymRanks[i].level).addClass('active');
203
		}
204
		$('gymRank'+gymRanks[i].level).css({width:width+'%',left:left+'%'});
205
		
206
	}
207
	$('#gym_details_template #gymPrestigeBar').css({'width':((gymPrestige/55550)*100)+'%', 'background-color':teamColor});
208
}