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