Failed Conditions
Pull Request — master (#250)
by
unknown
04:11 queued 01:42
created

ion   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

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 14
rs 8.8571
1
/** global: google */
2
/** global: pokemon_id */
3
/** global: navigator */
4
/** global: max_latitude */
5
/** global: min_latitude */
6
/** global: max_longitude */
7
/** global: min_longitude */
8
9
var map, heatmap;
10
var pokemonMarkers = [];
11
var updateLiveTimeout;
12
13
var ivMin = 80;
14
var ivMax = 100;
15
16
function initMap() {
17
	$.getJSON( "core/json/variables.json", function( variables ) {
18
		var latitude = Number(variables['system']['map_center_lat']);
19
		var longitude = Number(variables['system']['map_center_long']);
20
		var zoom_level = Number(variables['system']['zoom_level']);
21
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
22
23
		map = new google.maps.Map(document.getElementById('map'), {
24
			center: {
25
				lat: latitude,
26
				lng: longitude
27
			},
28
			zoom: zoom_level,
29
			zoomControl: true,
30
			scaleControl: false,
31
			scrollwheel: true,
32
			disableDoubleClickZoom: false,
33
			streetViewControl: false,
34
			mapTypeControlOptions: {
35
				mapTypeIds: [
36
					google.maps.MapTypeId.ROADMAP,
37
					'pogo_style',
38
					'dark_style',
39
				]
40
			}
41
		});
42
43
		$.getJSON( 'core/json/pogostyle.json', function( data ) {
44
			var styledMap_pogo = new google.maps.StyledMapType(data, {name: 'PoGo'});
45
			map.mapTypes.set('pogo_style', styledMap_pogo);
46
		});
47
		$.getJSON( 'core/json/darkstyle.json', function( data ) {
48
			var styledMap_dark = new google.maps.StyledMapType(data, {name: 'Dark'});
49
			map.mapTypes.set('dark_style', styledMap_dark);
50
		});
51
		$.getJSON( 'core/json/defaultstyle.json', function( data ) {
52
			map.set('styles', data);
53
		});
54
55
		$.ajax({
56
			'async': true,
57
			'type': "GET",
58
			'global': false,
59
			'dataType': 'json',
60
			'url': "core/process/aru.php",
61
			'data': {
62
				'request': "",
63
				'target': 'arrange_url',
64
				'method': 'method_target',
65
				'type': 'pokemon_coordinates_area'
66
		    }
67
		}).done(function(coordinates) {
68
			getArea(coordinates);
69
		});
70
		
71
		if (navigator.geolocation) {
72
			navigator.geolocation.getCurrentPosition(function(position) {
73
				var pos = {
74
					lat: position.coords.latitude,
75
					lng: position.coords.longitude
76
				};
77
78
				coordinate_area();
79
80
				if (position.coords.latitude <= max_latitude && position.coords.latitude >= min_latitude) {
81
					if (position.coords.longitude <= max_longitude && position.coords.longitude >= min_longitude) {
82
						map.setCenter(pos);
83
					}
84
				}
85
			});
86
		}
87
		
88
		initHeatmap();
89
		initSelector(pokeimg_suffix);
90
	});
91
}
92
93
function initSelector(pokeimg_suffix){
94
	$('#heatmapSelector').click(function(){
95
		hideLive();
96
		showHeatmap();
97
		$('#heatmapSelector').addClass('active');
98
		$('#liveSelector').removeClass('active');
99
	});
100
	$('#liveSelector').click(function(){
101
		hideHeatmap();
102
		map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
103
		initLive(pokeimg_suffix);
104
		
105
		
106
		$('#liveSelector').addClass('active');
107
		$('#heatmapSelector').removeClass('active');
108
	});
109
}
110
111
function initLive(pokeimg_suffix){
112
	showLive();
113
	$("#liveFilterSelector").rangeSlider({
114
		bounds:{
115
			min: 0,
116
			max: 100
117
		},
118
		defaultValues:{
119
			min: ivMin,
120
			max: ivMax
121
		},
122
		formatter:function(val) {
123
			return "IV: "+Math.round(val)+"%";
124
		}
125
	});
126
	
127
	$("#liveFilterSelector").bind("valuesChanged",function(e, data){
128
		clearTimeout(updateLiveTimeout);
129
		removePokemonMarkerByIv(data.values.min,data.values.max);
130
		ivMin = data.values.min;
131
		ivMax = data.values.max;
132
		updateLive(pokeimg_suffix);
133
	});
134
	updateLive(pokeimg_suffix);
135
	
136
}
137
138
function initHeatmap(){
139
	$.ajax({
140
		'async': true,
141
		'type': "GET",
142
		'global': false,
143
		'dataType': 'json',
144
		'url': "core/process/aru.php",
145
		'data': {
146
			'request': "",
147
			'target': 'arrange_url',
148
			'method': 'method_target',
149
			'type' : 'pokemon_slider_init'
150
		}
151
	}).done(function(bounds){
152
		initHeatmapData(bounds);
153
	});
154
	
155
}
156
157
function initHeatmapData(bounds){
158
	var boundMin = new Date(bounds.min.replace(/-/g, "/"));
159
	var boundMax = new Date(bounds.max.replace(/-/g, "/"));
160
	var selectorMax = boundMax;
161
	var selectorMin = boundMin;
162
163
	// two weeks in millisec
164
	var twoWeeks = 12096e5;
165
	var maxMinus2Weeks = new Date(selectorMax.getTime() - twoWeeks);
166
	if(selectorMin < maxMinus2Weeks){
167
		selectorMin = maxMinus2Weeks;
168
	}
169
170
	// dict with millisec => migration nr.
171
	var migrations = {};
172
	// start at 4 because 06. Oct 2016 was the 4th migration
173
	var migr_nr = 4;
174
	$("#timeSelector").dateRangeSlider({
175
		bounds:{
176
			min: boundMin,
177
			max: boundMax
178
		},
179
		defaultValues:{
180
			min: selectorMin,
181
			max: selectorMax
182
		},
183
		scales: [{
184
			first: function(value) {
185
				// 06. Oct 2016 (4th migration). 2 week schedule starts with this migration
186
				var migrationStart = new Date("2016-10-06");
187
				var now = new Date();
188
				var result = new Date();
189
				for (var migration = migrationStart; migration <= now; migration.setTime(migration.getTime() + twoWeeks)) {
190
					if (migration >= value) {
191
						result = migration;
192
						migrations[result.getTime()] = migr_nr;
193
						break;
194
					}
195
					migr_nr++;
196
				}
197
				return result;
198
			},
199
			next: function(value){
200
				var next = new Date(new Date(value).setTime(value.getTime() + twoWeeks));
201
				migr_nr++;
202
				migrations[next.getTime()] = migr_nr;
203
				return next;
204
			},
205
			label: function(value){
206
				if (isMobileDevice() && isTouchDevice()) {
207
					return "#" + migrations[value.getTime()];
208
				}
209
				return "Migration #" + migrations[value.getTime()];
210
			},
211
		}]
212
	});
213
	createHeatmap();
214
215
}
216
217
function createHeatmap() {
218
	
219
	heatmap = new google.maps.visualization.HeatmapLayer({
220
		data: [],
221
		map: map
222
	});
223
224
	var gradient = [
225
		'rgba(0, 255, 255, 0)',
226
		'rgba(0, 255, 255, 1)',
227
		'rgba(0, 191, 255, 1)',
228
		'rgba(0, 127, 255, 1)',
229
		'rgba(0, 63, 255, 1)',
230
		'rgba(0, 0, 255, 1)',
231
		'rgba(0, 0, 223, 1)',
232
		'rgba(0, 0, 191, 1)',
233
		'rgba(0, 0, 159, 1)',
234
		'rgba(0, 0, 127, 1)',
235
		'rgba(63, 0, 91, 1)',
236
		'rgba(127, 0, 63, 1)',
237
		'rgba(191, 0, 31, 1)',
238
		'rgba(255, 0, 0, 1)'
239
	];
240
	heatmap.set('gradient', gradient);
241
	heatmap.setMap(map);
242
	$("#timeSelector").bind("valuesChanged",function(){updateHeatmap()});
243
	$("#timeSelector").dateRangeSlider("min"); // will trigger valuesChanged
244
}
245
246
function updateHeatmap() {
247
	var dateMin = $("#timeSelector").dateRangeSlider("min");
248
	var dateMax = $("#timeSelector").dateRangeSlider("max");
249
	$("#loaderContainer").show();
250
	$.ajax({
251
		'async': true,
252
		'type': "GET",
253
		'global': false,
254
		'dataType': 'json',
255
		'url': "core/process/aru.php",
256
		'data': {
257
			'request': "",
258
			'target': 'arrange_url',
259
			'method': 'method_target',
260
			'type' : 'pokemon_heatmap_points',
261
			'pokemon_id' : pokemon_id,
262
			'start' : Math.floor(dateMin.getTime()/1000),
263
			'end' : Math.floor(dateMax.getTime()/1000)
264
		}
265
	}).done(function(points){
266
		var googlePoints = [];
267
		for (var i = 0; i < points.length; i++) {
268
			googlePoints.push(new google.maps.LatLng(points[i].latitude,points[i].longitude))
269
		}
270
		var newPoints = new google.maps.MVCArray(googlePoints);
271
		heatmap.set('data', newPoints);
272
		$("#loaderContainer").hide();
273
	});
274
}
275
276
function hideHeatmap() {
277
	$("#timeFilterContainer").hide();
278
	heatmap.set('map', null);
279
}
280
281
function showHeatmap() {
282
	$("#timeFilterContainer").show();
283
	heatmap.set('map', map);
284
	hideLive();
285
}
286
287
function hideLive() {
288
	$("#liveFilterContainer").hide();
289
	clearTimeout(updateLiveTimeout);
290
	clearPokemonMarkers();
291
}
292
293
function showLive() {
294
	hideHeatmap();
295
	clearTimeout(updateLiveTimeout);
296
	$("#liveFilterContainer").show();
297
	
298
}
299
300
function updateLive(pokeimg_suffix){
301
	$.ajax({
302
		'async': true,
303
		'type': "POST",
304
		'global': false,
305
		'dataType': 'json',
306
		'url': "core/process/aru.php",
307
		'data': {
308
			'request': "",
309
			'target': 'arrange_url',
310
			'method': 'method_target',
311
			'type' : 'pokemon_live',
312
			'pokemon_id' : pokemon_id,
313
			'inmap_pokemons' : extractEncountersId(),
314
			'ivMin' : ivMin,
315
			'ivMax' : ivMax
316
		}
317
	}).done(function(pokemons){
318
		for (var i = 0; i < pokemons.points.length; i++) {
319
			addPokemonMarker(pokemons.points[i],pokeimg_suffix, pokemons.locale)
320
		}
321
		updateLiveTimeout=setTimeout(function(){ updateLive(pokeimg_suffix) },5000);
322
	});
323
}
324
325
function addPokemonMarker(pokemon,pokeimg_suffix, locale) {
326
	var image = {
327
		url:'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix,
328
		scaledSize: new google.maps.Size(32, 32),
329
		origin: new google.maps.Point(0,0),
330
		anchor: new google.maps.Point(16, 16),
331
		labelOrigin : new google.maps.Point(16, 36)
332
	};
333
	var encounter = false;
334
	var ivPercent = 100;
335
	if (pokemon.individual_attack != null) {
0 ignored issues
show
Best Practice introduced by
Comparing pokemon.individual_attack to null using the != operator is not safe. Consider using !== instead.
Loading history...
336
		encounter = true;
337
		ivPercent = ((100/45)*(parseInt(pokemon.individual_attack)+parseInt(pokemon.individual_defense)+parseInt(pokemon.individual_stamina))).toFixed(2);
338
	}
339
	var marker = new google.maps.Marker({
340
		position: {lat: parseFloat(pokemon.latitude), lng:parseFloat(pokemon.longitude)},
341
		map: map,
342
		icon: image,
343
		encounterId: pokemon.encounter_id,
344
		ivPercent: ivPercent
345
	});
346
	if (encounter) {
347
		marker.setLabel({
348
			color:getIvColor(ivPercent),
349
			text:ivPercent+"%"
350
		});
351
	}
352
	var contentString = '<div>'+
353
			'<h4> '+pokemon.name+' #'+pokemon.pokemon_id+ (encounter?' IV: '+ivPercent+'% ':'')+'</h4>'+
354
			'<div id="bodyContent">'+
355
				'<p class="disappear_time_display text-center">'+pokemon.disappear_time_real+'<span class="disappear_time_display_timeleft"></span></p>';
356
	if (encounter) {
357
		contentString +=
358
				'<p></p>'+
359
				'<div class="progress" style="height: 6px; width: 120px; margin-bottom: 10px; margin-top: 2px; margin-left: auto; margin-right: auto">'+
360
					'<div title="'+locale.ivAttack+': '+pokemon.individual_attack+'" class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="'+pokemon.individual_attack+'" aria-valuemin="0" aria-valuemax="45" style="width: '+(((100/15)*pokemon.individual_attack)/3)+'%">'+
361
						'<span class="sr-only">'+locale.ivAttack+': '+pokemon.individual_attack+'</span>'+
362
					'</div>'+
363
					'<div title="'+locale.ivDefense+': '+pokemon.individual_defense+'" class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="'+pokemon.individual_defense+'" aria-valuemin="0" aria-valuemax="45" style="width: '+(((100/15)*pokemon.individual_defense)/3)+'%">'+
364
						'<span class="sr-only">'+locale.ivDefense+': '+pokemon.individual_defense+'</span>'+
365
					'</div>'+
366
					'<div title="'+locale.ivStamina+': '+pokemon.individual_stamina+'" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="'+pokemon.individual_stamina+'" aria-valuemin="0" aria-valuemax="45" style="width: '+(((100/15)*pokemon.individual_stamina)/3)+'%">'+
367
						'<span class="sr-only">'+locale.ivStamina+': '+pokemon.individual_stamina+'</span>'+
368
					'</div>'+
369
				'</div>'+
370
				'<p class="text-center">('+pokemon.individual_attack+"/"+pokemon.individual_defense+"/"+pokemon.individual_stamina+')</p>'+
371
				'<p class="text-center">'+pokemon.quick_move+"/"+pokemon.charge_move+'</p>';
372
		}
373
		contentString +='</div>';
374
375
	var infoWindow = new google.maps.InfoWindow({
376
		content: contentString
377
	});
378
	infoWindow.isClickOpen = false;
379
	marker.addListener('click', function() {
380
		infoWindow.isClickOpen = true;
381
		infoWindow.open(map, this);
382
	});
383
	google.maps.event.addListener(infoWindow,'closeclick',function(){
384
		this.isClickOpen = false;
385
	});
386
	marker.addListener('mouseover', function() {
387
		infoWindow.open(map, this);
388
	});
389
390
	// assuming you also want to hide the infowindow when user mouses-out
391
	marker.addListener('mouseout', function() {
392
		if(infoWindow.isClickOpen === false){
393
			infoWindow.close();
394
		}
395
	});
396
	pokemonMarkers.push(marker);
397
	var now = new Date().getTime();
398
	var endTime = new Date(pokemon.disappear_time_real.replace(/-/g, "/")).getTime();
399
	
400
	setTimeout(function(){ removePokemonMarker(pokemon.encounter_id) },endTime-now);
401
}
402
403
404
function getIvColor(ivPercent){
405
	var ivColor="rgba(0, 0, 0, 0)";
406
	if(ivPercent>80){
407
		ivColor="rgba(0, 0, 255, 0.70)";
408
	}
409
	if(ivPercent>90){
410
		ivColor="rgba(246, 178, 107, 0.90)";
411
	}
412
	if(ivPercent>99){
413
		ivColor="rgba(255, 0, 0, 1)";
414
	}
415
	return ivColor;
416
}
417
418
function clearPokemonMarkers() {
419
	for (var i = 0; i < pokemonMarkers.length; i++) {
420
		pokemonMarkers[i].setMap(null);
421
	}
422
	pokemonMarkers = [];
423
}
424
function removePokemonMarker(encounter_id) {
425
	for (var i = 0; i < pokemonMarkers.length; i++) {
426
		if(pokemonMarkers[i].encounterId == encounter_id){
427
			pokemonMarkers[i].setMap(null);
428
			pokemonMarkers.splice(i,1);
429
			break;
430
		}
431
		
432
	}
433
	
434
}
435
436
function removePokemonMarkerByIv(ivMin,ivMax) {
437
	var cleanMarkers=[];
438
	for (var i = 0; i < pokemonMarkers.length; i++) {
439
		if(pokemonMarkers[i].ivPercent < ivMin || pokemonMarkers[i].ivPercent > ivMax){
440
			pokemonMarkers[i].setMap(null);
441
		}
442
		else{
443
			cleanMarkers.push(pokemonMarkers[i]);
444
		}
445
		
446
	}
447
	pokemonMarkers = cleanMarkers;
448
}
449
450
function extractEncountersId(){
451
	var inmapEncounter = [];
452
	for (var i = 0; i < pokemonMarkers.length; i++) {
453
		inmapEncounter[i]=pokemonMarkers[i].encounterId;
454
	}
455
	
456
	return inmapEncounter;
457
}
458
459
function isTouchDevice() {
460
    // Should cover most browsers
461
    return 'ontouchstart' in window || navigator.maxTouchPoints
462
}
463
464
function isMobileDevice() {
465
    //  Basic mobile OS (not browser) detection
466
    return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))
467
}
468
469
function getArea(coordinates){
470
	max_latitude = coordinates.max_latitude;
471
	min_latitude = coordinates.min_latitude;
472
	max_longitude = coordinates.max_longitude;
473
	min_longitude = coordinates.min_longitude;
474
}
475