Failed Conditions
Pull Request — master (#250)
by
unknown
06:51
created

gym.maps.js ➔ ... ➔ $.done   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 16
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
B gym.maps.js ➔ ... ➔ navigator.geolocation.getCurrentPosition 0 12 5
1
/** global: google */
2
function initMap()
3
{
4
	$('.gym_details').hide();
5
	//ensure that gmaps is loaded before loading infobox (nasty but usefull trick)
6
	$.getScript("//cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js").done(function () {
7
		$.ajax({
8
			'async': true,
9
			'type': "GET",
10
			'global': false,
11
			'dataType': 'text',
12
			'url': "core/process/aru.php",
13
			'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target', 'type' : 'gym_map' }}).done(function (data) {
14
			
15
			
16
			// Get website variables
17
			
18
				$.getJSON("core/json/variables.json", function (variables) {
19
					var latitude = Number(variables['system']['map_center_lat']);
20
					var longitude = Number(variables['system']['map_center_long']);
21
					var zoom_level = Number(variables['system']['zoom_level']);
22
				
23
					// Convert return to JSON Array
24
				
25
					var locations = JSON.parse(data);
26
					var arr = [];
27
				
28
					for (i = 0; i < locations.length; i++) {
29
						arr.push(JSON.parse(locations[i]));
30
					}
31
					
32
					var map = new google.maps.Map(document.getElementById('map'), {
33
						center: {
34
							lat: latitude,
35
							lng: longitude
36
						},
37
						zoom: zoom_level,
38
						zoomControl: true,
39
						scaleControl: false,
40
						scrollwheel: true,
41
						disableDoubleClickZoom: false,
42
						streetViewControl: false,
43
						mapTypeControlOptions: {
44
							mapTypeIds: [
45
								google.maps.MapTypeId.ROADMAP,
46
								'pogo_style',
47
								'dark_style',
48
							]
49
						}
50
					});
51
52
					$.getJSON( 'core/json/pogostyle.json', function( data ) {
53
						var styledMap_pogo = new google.maps.StyledMapType(data, {name: 'PoGo'});
54
						map.mapTypes.set('pogo_style', styledMap_pogo);
55
					});
56
					$.getJSON( 'core/json/darkstyle.json', function( data ) {
57
						var styledMap_dark = new google.maps.StyledMapType(data, {name: 'Dark'});
58
						map.mapTypes.set('dark_style', styledMap_dark);
59
					});
60
					$.getJSON( 'core/json/defaultstyle.json', function( data ) {
61
						map.set('styles', data);
62
					});
63
64
		$.ajax({
65
			'async': true,
66
			'type': "GET",
67
			'global': false,
68
			'dataType': 'json',
69
			'url': "core/process/aru.php",
70
			'data': {
71
				'request': "",
72
				'target': 'arrange_url',
73
				'method': 'method_target',
74
				'type': 'maps_localization_coordinates'
75
			}
76
		}).done(function(coordinates) {
77
			if (navigator.geolocation) {
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
				navigator.geolocation.getCurrentPosition(function(position) {
79
					var pos = {
80
						lat: position.coords.latitude,
81
						lng: position.coords.longitude
82
					};
83
84
					if (position.coords.latitude <= coordinates.max_latitude && position.coords.latitude >= coordinates.min_latitude) {
85
						if (position.coords.longitude <= coordinates.max_longitude && position.coords.longitude >= coordinates.min_longitude) {
86
							map.setCenter(pos);
87
						}
88
					}
89
				});
90
			}
91
		});
92
93
					var infowindow = new InfoBox({
94
						content: document.getElementById("gym_details_template"),
95
						disableAutoPan: false,
96
						pixelOffset: new google.maps.Size(-35, 30),
97
						zIndex: null,
98
						closeBoxURL: "",
99
						infoBoxClearance: new google.maps.Size(1, 1)
100
					});
101
				
102
					google.maps.event.addListener(map, "click", function () {
103
						infowindow.close();
104
					});
105
				
106
					var marker, i;
107
			
108
					for (i = 0; i < arr.length; i++) {
109
						marker = new google.maps.Marker({
110
							position: new google.maps.LatLng(arr[i][2], arr[i][3]),
111
							map: map,
112
							icon: 'core/img/'+arr[i][1],
113
						});
114
					
115
					
116
						google.maps.event.addListener(marker, 'click', (function (marker, i) {
117
							return function () {
118
								infowindow.setContent(arr[i][0]);
119
								infowindow.open(map, marker);
120
								$.ajax({
121
									'async': true,
122
									'type': "GET",
123
									'global': false,
124
									'dataType': 'json',
125
									'url': "core/process/aru.php",
126
									'data': {
127
										'request': "",
128
										'target': 'arrange_url',
129
										'method': 'method_target',
130
										'type' : 'gym_defenders',
131
										'gym_id' : arr[i][5]
132
									},
133
									'success': function (data) {
134
										setGymDetails(data);
135
										infowindow.setContent($('#gym_details_template').html());
136
									}
137
								});
138
							}
139
						})(marker, i));
140
					}
141
				});
142
			});
143
	});
144
}
145
146
function setGymDetails(gym)
147
{
148
	// replace http with https to fix mixed content
149
	var imgurl = gym.gymDetails.gymInfos.url;
150
	imgurl = imgurl.replace(/^http:\/\//i, 'https://');
151
	$('#gym_details_template #circleImage').css("background", "url("+imgurl+") no-repeat center");
152
	$('#gym_details_template #gymName').html(gym.gymDetails.gymInfos.name);
153
	$('#gym_details_template #gymDescription').html(gym.gymDetails.gymInfos.description);
154
	$('#gym_details_template #gymDefenders').html(gym.infoWindow);
155
	$('#gym_details_template #gymPrestigeDisplay').html(gym.gymDetails.gymInfos.points);
156
	
157
	$('#gym_details_template #gymLastScannedDisplay').html(gym.gymDetails.gymInfos.last_scanned);
158
	var currentTeamColor = 'white';
159
	if(gym.gymDetails.gymInfos.team=="1") {
160
		currentTeamColor = 'rgb(0, 170, 255)';
161
	} else if(gym.gymDetails.gymInfos.team=="2") {
162
		currentTeamColor = 'rgb(255, 118, 118)';
163
	} else if(gym.gymDetails.gymInfos.team=="3") {
164
		currentTeamColor = 'rgb(255, 190, 8)';
165
	}
166
	var currentGymPrestige = gym.gymDetails.gymInfos.points;
167
	formatGyms(currentGymPrestige, currentTeamColor);
168
	$('#gym_details_template').show();
169
}
170
171
function formatGyms(gymPrestigeValue,teamColor){
172
	var gymPrestige = gymPrestigeValue;
173
	var gymRanks = [
174
	{
175
		level : 1,
176
		prestigeMax : 2000,
177
		prestigeMin : 0
178
	},
179
	{
180
		level : 2,
181
		prestigeMax : 4000,
182
		prestigeMin : 2000
183
	},
184
	{
185
		level : 3,
186
		prestigeMax : 8000,
187
		prestigeMin : 4000
188
	},
189
	{
190
		level : 4,
191
		prestigeMax : 12000,
192
		prestigeMin : 8000
193
	},
194
	{
195
		level : 5,
196
		prestigeMax : 16000,
197
		prestigeMin : 12000
198
	},
199
	{
200
		level : 6,
201
		prestigeMax : 20000,
202
		prestigeMin : 16000
203
	},
204
	{
205
		level : 7,
206
		prestigeMax : 30000,
207
		prestigeMin : 20000
208
	},
209
	{
210
		level : 8,
211
		prestigeMax : 40000,
212
		prestigeMin : 30000
213
	},
214
	{
215
		level : 9,
216
		prestigeMax : 50000,
217
		prestigeMin : 40000
218
	},
219
	{
220
		level : 10,
221
		prestigeMax : 52000,
222
		prestigeMin : 50000
223
	}
224
	];
225
	
226
	$('#gym_details_template #gymInfos').css("border-color", teamColor);
227
	//Set rank positions (50000 = 90% for rank 10 to be visible)
228
	var gymPercent = 50000/90;
229
	if (gymPrestige>50000) {
230
		//compensate for last rank
231
		gymPrestige=(50000+((gymPrestige-50000)*2.775))
232
	}
233
	$('.bar-step').removeClass('active');
234
	for (var i in gymRanks) {
235
		if (!gymRanks.hasOwnProperty(i)) {
236
			continue; // Skip keys from the prototype.
237
		}
238
		var width = (((gymRanks[i].prestigeMax)-(gymRanks[i].prestigeMin))/gymPercent);
239
		if(gymRanks[i].level > 9) {
240
			width = 10;
241
		}
242
		var left = (gymRanks[i].prestigeMin/gymPercent);
243
		var active = (gymPrestige >= gymRanks[i].prestigeMax);
244
		if(active){
245
			$('.gymRank'+gymRanks[i].level).addClass('active');
246
		}
247
		$('gymRank'+gymRanks[i].level).css({width:width+'%',left:left+'%'});
248
		
249
	}
250
	$('#gym_details_template #gymPrestigeBar').css({'width':((gymPrestige/55550)*100)+'%', 'background-color':teamColor});
251
}
252