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

gym.maps.js ➔ formatGyms   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
nc 12
nop 2
dl 0
loc 81
rs 8.3904

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
	switch (gym.gymDetails.gymInfos.team) {
111
		case "1":
112
			var currentTeamColor = 'rgb(0, 170, 255)';
113
		break;
114
		case "2":
115
			var currentTeamColor = 'rgb(255, 118, 118)';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable currentTeamColor already seems to be declared on line 112. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
116
		break;
117
		case "3":
118
			var currentTeamColor = 'rgb(255, 190, 8)';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable currentTeamColor already seems to be declared on line 112. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
119
		break;
120
			
121
		default:
122
			var currentTeamColor = 'white';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable currentTeamColor already seems to be declared on line 112. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
123
		break;
124
	}
125
	var currentGymPrestige = gym.gymDetails.gymInfos.points;
126
	formatGyms(currentGymPrestige, currentTeamColor);
127
	$('#gym_details_template').show();
128
}
129
130
function formatGyms(gymPrestigeValue,teamColor){
131
	var gymPrestige = gymPrestigeValue;
132
	var gymRanks = [
133
	{
134
		level : 1,
135
		prestigeMax : 2000,
136
		prestigeMin : 0
137
	},
138
	{
139
		level : 2,
140
		prestigeMax : 4000,
141
		prestigeMin : 2000
142
	},
143
	{
144
		level : 3,
145
		prestigeMax : 8000,
146
		prestigeMin : 4000
147
	},
148
	{
149
		level : 4,
150
		prestigeMax : 12000,
151
		prestigeMin : 8000
152
	},
153
	{
154
		level : 5,
155
		prestigeMax : 16000,
156
		prestigeMin : 12000
157
	},
158
	{
159
		level : 6,
160
		prestigeMax : 20000,
161
		prestigeMin : 16000
162
	},
163
	{
164
		level : 7,
165
		prestigeMax : 30000,
166
		prestigeMin : 20000
167
	},
168
	{
169
		level : 8,
170
		prestigeMax : 40000,
171
		prestigeMin : 30000
172
	},
173
	{
174
		level : 9,
175
		prestigeMax : 50000,
176
		prestigeMin : 40000
177
	},
178
	{
179
		level : 10,
180
		prestigeMax : 52000,
181
		prestigeMin : 50000
182
	}
183
	];
184
	
185
	$('#gym_details_template #gymInfos').css("border-color", teamColor);
186
	//Set rank positions (50000 = 90% for rank 10 to be visible)
187
	var gymPercent = 50000/90;
188
	if (gymPrestige>50000) {
189
		//compensate for last rank
190
		gymPrestige=(50000+((gymPrestige-50000)*2.775))
191
	}
192
	$('.bar-step').removeClass('active');
193
	for (var i in gymRanks) {
194
		if (!gymRanks.hasOwnProperty(i)) {
195
			continue; // Skip keys from the prototype.
196
		}
197
		var width = (((gymRanks[i].prestigeMax)-(gymRanks[i].prestigeMin))/gymPercent);
198
		if(gymRanks[i].level > 9) {
199
			width = 10;
200
		}
201
		var left = (gymRanks[i].prestigeMin/gymPercent);
202
		var active = (gymPrestige >= gymRanks[i].prestigeMax);
203
		if(active){
204
			$('.gymRank'+gymRanks[i].level).addClass('active');
205
		}
206
		$('gymRank'+gymRanks[i].level).css({width:width+'%',left:left+'%'});
207
		
208
	}
209
	$('#gym_details_template #gymPrestigeBar').css({'width':((gymPrestige/55550)*100)+'%', 'background-color':teamColor});
210
}