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