Passed
Pull Request — master (#250)
by
unknown
14:11 queued 05:31
created

tor.geolocation.getCurrentPosition   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 8.8571
1
/** global: google */
2
/** global: navigator */
3
4
function initMap()
5
{
6
	$('.gym_details').hide();
7
	//ensure that gmaps is loaded before loading infobox (nasty but usefull trick)
8
	$.getScript("//cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js").done(function () {
9
		$.ajax({
10
			'async': true,
11
			'type': "GET",
12
			'global': false,
13
			'dataType': 'text',
14
			'url': "core/process/aru.php",
15
			'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target', 'type' : 'gym_map' }}).done(function (data) {
16
			
17
			
18
			// Get website variables
19
			
20
				$.getJSON("core/json/variables.json", function (variables) {
21
					var latitude = 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 = JSON.parse(data);
28
					var arr = [];
29
				
30
					for (i = 0; i < locations.length; i++) {
31
						arr.push(JSON.parse(locations[i]));
32
					}
33
					
34
					var map = new google.maps.Map(document.getElementById('map'), {
35
						center: {
36
							lat: latitude,
37
							lng: longitude
38
						},
39
						zoom: zoom_level,
40
						zoomControl: true,
41
						scaleControl: false,
42
						scrollwheel: true,
43
						disableDoubleClickZoom: false,
44
						streetViewControl: false,
45
						mapTypeControlOptions: {
46
							mapTypeIds: [
47
								google.maps.MapTypeId.ROADMAP,
48
								'pogo_style',
49
								'dark_style',
50
							]
51
						}
52
					});
53
54
					$.getJSON( 'core/json/pogostyle.json', function( data ) {
55
						var styledMap_pogo = new google.maps.StyledMapType(data, {name: 'PoGo'});
56
						map.mapTypes.set('pogo_style', styledMap_pogo);
57
					});
58
					$.getJSON( 'core/json/darkstyle.json', function( data ) {
59
						var styledMap_dark = new google.maps.StyledMapType(data, {name: 'Dark'});
60
						map.mapTypes.set('dark_style', styledMap_dark);
61
					});
62
					$.getJSON( 'core/json/defaultstyle.json', function( data ) {
63
						map.set('styles', data);
64
					});
65
66
		$.ajax({
67
			'async': true,
68
			'type': "GET",
69
			'global': false,
70
			'dataType': 'json',
71
			'url': "core/process/aru.php",
72
			'data': {
73
				'request': "",
74
				'target': 'arrange_url',
75
				'method': 'method_target',
76
				'type': 'maps_localization_coordinates'
77
			}
78
		}).done(function(coordinates) {
79
			if (navigator.geolocation) {
80
				navigator.geolocation.getCurrentPosition(function(position) {
81
					var pos = {
82
						lat: position.coords.latitude,
83
						lng: position.coords.longitude
84
					};
85
86
					if (position.coords.latitude <= coordinates.max_latitude && position.coords.latitude >= coordinates.min_latitude) {
87
						if (position.coords.longitude <= coordinates.max_longitude && position.coords.longitude >= coordinates.min_longitude) {
88
							map.setCenter(pos);
89
						}
90
					}
91
				});
92
			}
93
		});
94
95
					var infowindow = new InfoBox({
96
						content: document.getElementById("gym_details_template"),
97
						disableAutoPan: false,
98
						pixelOffset: new google.maps.Size(-35, 30),
99
						zIndex: null,
100
						closeBoxURL: "",
101
						infoBoxClearance: new google.maps.Size(1, 1)
102
					});
103
				
104
					google.maps.event.addListener(map, "click", function () {
105
						infowindow.close();
106
					});
107
				
108
					var marker, i;
109
			
110
					for (i = 0; i < arr.length; i++) {
111
						marker = new google.maps.Marker({
112
							position: new google.maps.LatLng(arr[i][2], arr[i][3]),
113
							map: map,
114
							icon: 'core/img/'+arr[i][1],
115
						});
116
					
117
					
118
						google.maps.event.addListener(marker, 'click', (function (marker, i) {
119
							return function () {
120
								infowindow.setContent(arr[i][0]);
121
								infowindow.open(map, marker);
122
								$.ajax({
123
									'async': true,
124
									'type': "GET",
125
									'global': false,
126
									'dataType': 'json',
127
									'url': "core/process/aru.php",
128
									'data': {
129
										'request': "",
130
										'target': 'arrange_url',
131
										'method': 'method_target',
132
										'type' : 'gym_defenders',
133
										'gym_id' : arr[i][5]
134
									},
135
									'success': function (data) {
136
										setGymDetails(data);
137
										infowindow.setContent($('#gym_details_template').html());
138
									}
139
								});
140
							}
141
						})(marker, i));
142
					}
143
				});
144
			});
145
	});
146
}
147
148
function setGymDetails(gym)
149
{
150
	// replace http with https to fix mixed content
151
	var imgurl = gym.gymDetails.gymInfos.url;
152
	imgurl = imgurl.replace(/^http:\/\//i, 'https://');
153
	$('#gym_details_template #circleImage').css("background", "url("+imgurl+") no-repeat center");
154
	$('#gym_details_template #gymName').html(gym.gymDetails.gymInfos.name);
155
	$('#gym_details_template #gymDescription').html(gym.gymDetails.gymInfos.description);
156
	$('#gym_details_template #gymDefenders').html(gym.infoWindow);
157
	$('#gym_details_template #gymPrestigeDisplay').html(gym.gymDetails.gymInfos.points);
158
	
159
	$('#gym_details_template #gymLastScannedDisplay').html(gym.gymDetails.gymInfos.last_scanned);
160
	var currentTeamColor = 'white';
161
	if(gym.gymDetails.gymInfos.team=="1") {
162
		currentTeamColor = 'rgb(0, 170, 255)';
163
	} else if(gym.gymDetails.gymInfos.team=="2") {
164
		currentTeamColor = 'rgb(255, 118, 118)';
165
	} else if(gym.gymDetails.gymInfos.team=="3") {
166
		currentTeamColor = 'rgb(255, 190, 8)';
167
	}
168
	var currentGymPrestige = gym.gymDetails.gymInfos.points;
169
	formatGyms(currentGymPrestige, currentTeamColor);
170
	$('#gym_details_template').show();
171
}
172
173
function formatGyms(gymPrestigeValue,teamColor){
174
	var gymPrestige = gymPrestigeValue;
175
	var gymRanks = [
176
	{
177
		level : 1,
178
		prestigeMax : 2000,
179
		prestigeMin : 0
180
	},
181
	{
182
		level : 2,
183
		prestigeMax : 4000,
184
		prestigeMin : 2000
185
	},
186
	{
187
		level : 3,
188
		prestigeMax : 8000,
189
		prestigeMin : 4000
190
	},
191
	{
192
		level : 4,
193
		prestigeMax : 12000,
194
		prestigeMin : 8000
195
	},
196
	{
197
		level : 5,
198
		prestigeMax : 16000,
199
		prestigeMin : 12000
200
	},
201
	{
202
		level : 6,
203
		prestigeMax : 20000,
204
		prestigeMin : 16000
205
	},
206
	{
207
		level : 7,
208
		prestigeMax : 30000,
209
		prestigeMin : 20000
210
	},
211
	{
212
		level : 8,
213
		prestigeMax : 40000,
214
		prestigeMin : 30000
215
	},
216
	{
217
		level : 9,
218
		prestigeMax : 50000,
219
		prestigeMin : 40000
220
	},
221
	{
222
		level : 10,
223
		prestigeMax : 52000,
224
		prestigeMin : 50000
225
	}
226
	];
227
	
228
	$('#gym_details_template #gymInfos').css("border-color", teamColor);
229
	//Set rank positions (50000 = 90% for rank 10 to be visible)
230
	var gymPercent = 50000/90;
231
	if (gymPrestige>50000) {
232
		//compensate for last rank
233
		gymPrestige=(50000+((gymPrestige-50000)*2.775))
234
	}
235
	$('.bar-step').removeClass('active');
236
	for (var i in gymRanks) {
237
		if (!gymRanks.hasOwnProperty(i)) {
238
			continue; // Skip keys from the prototype.
239
		}
240
		var width = (((gymRanks[i].prestigeMax)-(gymRanks[i].prestigeMin))/gymPercent);
241
		if(gymRanks[i].level > 9) {
242
			width = 10;
243
		}
244
		var left = (gymRanks[i].prestigeMin/gymPercent);
245
		var active = (gymPrestige >= gymRanks[i].prestigeMax);
246
		if(active){
247
			$('.gymRank'+gymRanks[i].level).addClass('active');
248
		}
249
		$('gymRank'+gymRanks[i].level).css({width:width+'%',left:left+'%'});
250
		
251
	}
252
	$('#gym_details_template #gymPrestigeBar').css({'width':((gymPrestige/55550)*100)+'%', 'background-color':teamColor});
253
}
254