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

gym.maps.js ➔ formatGyms   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

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