Failed Conditions
Pull Request — master (#173)
by PoUpA
02:20
created

pokemon.maps.js ➔ initLive   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 23
rs 9.0856

1 Function

Rating   Name   Duplication   Size   Complexity  
A pokemon.maps.js ➔ ... ➔ $(ꞌ#liveFilterSelectorꞌ).bind(ꞌvaluesChangedꞌ) 0 7 1
1
/** global: google */
2
/** global: pokemon_id */
3
var map, heatmap;
4
var pokemonMarkers = [];
5
var updateLiveTimeout;
6
7
var ivMin = 80;
8
var ivMax = 100;
9
10
function initMap() {
11
	
12
	$.getJSON( "core/json/variables.json", function( jsondata ) {
13
14
		var variables = jsondata;
15
16
		var lattitude = Number(variables['system']['map_center_lat']);
17
		var longitude = Number(variables['system']['map_center_long']);
18
		var zoom_level = Number(variables['system']['zoom_level']);
19
		var pokeimg_suffix=jsondata['system']['pokeimg_suffix'];
20
21
		map = new google.maps.Map(document.getElementById('map'), {
22
			center: {lat: lattitude, lng: longitude},
23
			zoom: zoom_level,
24
			zoomControl: true,
25
			scaleControl: false,
26
			scrollwheel: true,
27
			disableDoubleClickZoom: false,
28
		});
29
		
30
		map.set('styles',[
31
			{
32
				"featureType":"all",
33
				"elementType":"labels.text.fill",
34
				"stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]
35
			},
36
			{
37
				"featureType":"all",
38
				"elementType":"labels.text.stroke",
39
				"stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]
40
			},
41
			{
42
				"featureType":"all",
43
				"elementType":"labels.icon",
44
				"stylers":[{"visibility":"off"}]
45
			},
46
			{
47
				"featureType":"administrative",
48
				"elementType":"geometry.fill",
49
				"stylers":[{"color":"#fefefe"},{"lightness":20}]
50
			},
51
			{
52
				"featureType":"administrative",
53
				"elementType":"geometry.stroke",
54
				"stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]
55
			},
56
			{
57
				"featureType":"landscape",
58
				"elementType":"geometry",
59
				"stylers":[{"color":"#f5f5f5"},{"lightness":20}]
60
			},
61
			{
62
				"featureType":"poi",
63
				"elementType":"geometry",
64
				"stylers":[{"color":"#f5f5f5"},{"lightness":21}]
65
			},
66
			{
67
				"featureType":"poi.park",
68
				"elementType":"geometry",
69
				"stylers":[{"color":"#dedede"},{"lightness":21}]
70
			},
71
			{
72
				"featureType":"poi.park",
73
				"elementType":"geometry.fill",
74
				"stylers":[{"color":"#c2ffd7"}]
75
			},
76
			{
77
				"featureType":"road.highway",
78
				"elementType":"geometry.fill",
79
				"stylers":[{"color":"#ffffff"},{"lightness":17}]},
80
			{
81
				"featureType":"road.highway",
82
				"elementType":"geometry.stroke",
83
				"stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},
84
			{
85
				"featureType":"road.arterial",
86
				"elementType":"geometry",
87
				"stylers":[{"color":"#ffffff"},{"lightness":18}]},
88
			{
89
				"featureType":"road.local",
90
				"elementType":"geometry",
91
				"stylers":[{"color":"#ffffff"},{"lightness":16}]},
92
			{
93
				"featureType":"transit",
94
				"elementType":"geometry",
95
				"stylers":[{"color":"#f2f2f2"},{"lightness":19}]},
96
			{
97
				"featureType":"water",
98
				"elementType":"geometry",
99
				"stylers":[{"color":"#e9e9e9"},{"lightness":17}]},
100
			{
101
				"featureType":"water",
102
				"elementType":"geometry.fill",
103
				"stylers":[{"color":"#b3d8f9"}]
104
			}
105
		]);
106
		initHeatmap();
107
		initSelector(pokeimg_suffix);
108
		});
109
110
}
111
112
function initSelector(pokeimg_suffix){
113
	$('#heatmapSelector').click(function(){
114
		hideLive();
115
		showHeatmap();
116
		$('#heatmapSelector').addClass('active');
117
		$('#liveSelector').removeClass('active');
118
	});
119
	$('#liveSelector').click(function(){
120
		hideHeatmap();
121
		initLive(pokeimg_suffix);
122
		
123
		
124
		$('#liveSelector').addClass('active');
125
		$('#heatmapSelector').removeClass('active');
126
	});
127
}
128
129
function initLive(pokeimg_suffix){
130
	showLive(pokeimg_suffix);
0 ignored issues
show
Bug introduced by
The call to showLive seems to have too many arguments starting with pokeimg_suffix.
Loading history...
131
	$("#liveFilterSelector").rangeSlider({
132
		bounds:{
133
			min: 0,
134
			max: 100
135
		},
136
		defaultValues:{
137
			min: ivMin,
138
			max: ivMax
139
		}
140
	});
141
	
142
	$("#liveFilterSelector").bind("valuesChanged",function(e, data){
143
		clearTimeout(updateLiveTimeout);
144
		removePokemonMarkerByIv(data.values.min,data.values.max);
145
		ivMin = data.values.min;
146
		ivMax = data.values.max;
147
		updateLive(pokeimg_suffix);
148
	});
149
	updateLive(pokeimg_suffix);
150
	
151
}
152
153
function initHeatmap(){
154
	$.ajax({
155
		'async': true,
156
		'type': "GET",
157
		'global': false,
158
		'dataType': 'json',
159
		'url': "core/process/aru.php",
160
		'data': {
161
			'request': "",
162
			'target': 'arrange_url',
163
			'method': 'method_target',
164
			'type' : 'pokemon_slider_init'
165
		}
166
	}).done(function(bounds){
167
		var boundMin = new Date(bounds.min.replace(/-/g, "/"));
168
		var boundMax = new Date(bounds.max.replace(/-/g, "/"));
169
		var selectorMax = boundMax;
170
		var selectorMin = boundMin;
171
		
172
		var maxMinus2Weeks = new Date(selectorMax.getTime() - 2 * 7 * 24 * 60 * 60 * 1000);
173
		if(selectorMin < maxMinus2Weeks){
174
			selectorMin = maxMinus2Weeks;
175
		}
176
		$("#timeSelector").dateRangeSlider({
177
			bounds:{
178
				min: boundMin,
179
				max: boundMax
180
			},
181
			defaultValues:{
182
				min: selectorMin,
183
				max: selectorMax
184
			}
185
		});
186
		createHeatmap();
187
		
188
	});
189
	
190
}
191
function createHeatmap() {
192
	
193
	heatmap = new google.maps.visualization.HeatmapLayer({
194
		data: [],
195
		map: map
196
	});
197
198
	var gradient = [
199
		'rgba(0, 255, 255, 0)',
200
		'rgba(0, 255, 255, 1)',
201
		'rgba(0, 191, 255, 1)',
202
		'rgba(0, 127, 255, 1)',
203
		'rgba(0, 63, 255, 1)',
204
		'rgba(0, 0, 255, 1)',
205
		'rgba(0, 0, 223, 1)',
206
		'rgba(0, 0, 191, 1)',
207
		'rgba(0, 0, 159, 1)',
208
		'rgba(0, 0, 127, 1)',
209
		'rgba(63, 0, 91, 1)',
210
		'rgba(127, 0, 63, 1)',
211
		'rgba(191, 0, 31, 1)',
212
		'rgba(255, 0, 0, 1)'
213
	];
214
	heatmap.set('gradient', gradient);
215
	heatmap.setMap(map);
216
	$("#timeSelector").bind("valuesChanged",function(){updateHeatmap()});
217
	$("#timeSelector").dateRangeSlider("min"); // will trigger valuesChanged
218
}
219
220
function updateHeatmap() {
221
	var dateMin = $("#timeSelector").dateRangeSlider("min");
222
	var dateMax = $("#timeSelector").dateRangeSlider("max");
223
	$("#loaderContainer").show();
224
	$.ajax({
225
		'async': true,
226
		'type': "GET",
227
		'global': false,
228
		'dataType': 'json',
229
		'url': "core/process/aru.php",
230
		'data': {
231
			'request': "",
232
			'target': 'arrange_url',
233
			'method': 'method_target',
234
			'type' : 'pokemon_heatmap_points',
235
			'pokemon_id' : pokemon_id,
236
			'start' : Math.floor(dateMin.getTime()/1000),
237
			'end' : Math.floor(dateMax.getTime()/1000)
238
		}
239
	}).done(function(points){
240
		var googlePoints = [];
241
		for (var i = 0; i < points.length; i++) {
242
			googlePoints.push(new google.maps.LatLng(points[i].latitude,points[i].longitude))
243
		}
244
		var newPoints = new google.maps.MVCArray(googlePoints);
245
		heatmap.set('data', newPoints);
246
		$("#loaderContainer").hide();
247
	});
248
}
249
250
function hideHeatmap() {
251
	$("#timeFilterContainer").hide();
252
	heatmap.set('map', null);
253
}
254
255
function showHeatmap() {
256
	$("#timeFilterContainer").show();
257
	heatmap.set('map', map);
258
	hideLive();
259
}
260
261
function hideLive() {
262
	$("#liveFilterContainer").hide();
263
	clearTimeout(updateLiveTimeout);
264
	clearPokemonMarkers();
265
}
266
267
function showLive() {
268
	hideHeatmap();
269
	clearTimeout(updateLiveTimeout);
270
	$("#liveFilterContainer").show();
271
	
272
}
273
274
function updateLive(pokeimg_suffix){
275
	$.ajax({
276
		'async': true,
277
		'type': "POST",
278
		'global': false,
279
		'dataType': 'json',
280
		'url': "core/process/aru.php",
281
		'data': {
282
			'request': "",
283
			'target': 'arrange_url',
284
			'method': 'method_target',
285
			'type' : 'pokemon_live',
286
			'pokemon_id' : pokemon_id,
287
			'inmap_pokemons' : extractEncountersId(),
288
			'ivMin' : ivMin,
289
			'ivMax' : ivMax
290
		}
291
	}).done(function(pokemons){
292
		for (var i = 0; i < pokemons.length; i++) {
293
			addPokemonMarker(pokemons[i],pokeimg_suffix)
294
		}
295
		updateLiveTimeout=setTimeout(function(){ updateLive(pokeimg_suffix) },5000);
296
	});
297
}
298
299
function addPokemonMarker(pokemon,pokeimg_suffix) {
300
	var image = {
301
		url:'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix,
302
		scaledSize: new google.maps.Size(32, 32),
303
		origin: new google.maps.Point(0,0),
304
		anchor: new google.maps.Point(16, 16),
305
		labelOrigin : new google.maps.Point(16, 36)
306
	};
307
	var ivPercent = ((100/45)*(parseInt(pokemon.individual_attack)+parseInt(pokemon.individual_defense)+parseInt(pokemon.individual_stamina))).toFixed(2);
308
	var marker = new google.maps.Marker({
309
		position: {lat: parseFloat(pokemon.latitude), lng:parseFloat(pokemon.longitude)},
310
		map: map,
311
		icon: image,
312
		label:{
313
			color:getIvColor(ivPercent),
314
			text:ivPercent+"%"
315
		},
316
		encounterId: pokemon.encounter_id,
317
		ivPercent: ivPercent
318
	});
319
	var ivFormatted = ((100/45)*(parseInt(pokemon.individual_attack)+parseInt(pokemon.individual_defense)+parseInt(pokemon.individual_stamina))).toFixed(2);
320
	var contentString = '<div>'+
321
			'<h4> '+pokemon.name+' #'+pokemon.pokemon_id+' IV: '+ivFormatted+'% </h4>'+
322
			'<div id="bodyContent">'+
323
				'<p class="disappear_time_display text-center">'+pokemon.disappear_time_real+'<span class="disappear_time_display_timeleft"></span></p>'+
324
				
325
				'<p></p>'+
326
				'<div class="progress" style="height: 6px; width: 120px; margin-bottom: 10px; margin-top: 2px; margin-left: auto; margin-right: auto">'+
327
					'<div title="Attack IV: '+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)+'%">'+
328
						'<span class="sr-only">Attack IV: '+pokemon.individual_attack+'</span>'+
329
					'</div>'+
330
					'<div title="Defense IV: '+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)+'%">'+
331
						'<span class="sr-only">Defense IV: '+pokemon.individual_defense+'</span>'+
332
					'</div>'+
333
					'<div title="Stamina IV: '+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)+'%">'+
334
						'<span class="sr-only">Stamina IV: '+pokemon.individual_stamina+'</span>'+
335
					'</div>'+
336
				'</div>'+
337
				'<p class="text-center">('+pokemon.individual_attack+"/"+pokemon.individual_defense+"/"+pokemon.individual_stamina+')</p>'+
338
			'</div>'+
339
		'</div>';
340
341
	var infoWindow = new google.maps.InfoWindow({
342
		content: contentString
343
	});
344
	infoWindow.isClickOpen = false;
345
	marker.addListener('click', function() {
346
		infoWindow.isClickOpen = true;
347
		infoWindow.open(map, this);
348
	});
349
	google.maps.event.addListener(infoWindow,'closeclick',function(){
350
		this.isClickOpen = false;
351
	});
352
	marker.addListener('mouseover', function() {
353
		infoWindow.open(map, this);
354
	});
355
356
	// assuming you also want to hide the infowindow when user mouses-out
357
	marker.addListener('mouseout', function() {
358
		if(infoWindow.isClickOpen === false){
359
			infoWindow.close();
360
		}
361
	});
362
	pokemonMarkers.push(marker);
363
	var now = new Date().getTime();
364
	var endTime = new Date(pokemon.disappear_time_real.replace(/-/g, "/")).getTime();
365
	
366
	setTimeout(function(){ removePokemonMarker(pokemon.encounter_id) },endTime-now);
367
}
368
369
370
function getIvColor(ivPercent){
371
	var ivColor="rgba(0, 0, 0, 0)";
372
	if(ivPercent>80){
373
		ivColor="rgba(0, 0, 255, 0.70)";
374
	}
375
	if(ivPercent>90){
376
		ivColor="rgba(246, 178, 107,  0.90)";
377
	}
378
	if(ivPercent>99){
379
		ivColor="rgba(255, 0, 0, 1)";
380
	}
381
	return ivColor;
382
}
383
384
function clearPokemonMarkers() {
385
	for (var i = 0; i < pokemonMarkers.length; i++) {
386
		pokemonMarkers[i].setMap(null);
387
	}
388
	pokemonMarkers = [];
389
}
390
function removePokemonMarker(encounter_id) {
391
	for (var i = 0; i < pokemonMarkers.length; i++) {
392
		if(pokemonMarkers[i].encounterId == encounter_id){
393
			pokemonMarkers[i].setMap(null);
394
			pokemonMarkers.splice(i,1);
395
			break;
396
		}
397
		
398
	}
399
	
400
}
401
402
function removePokemonMarkerByIv(ivMin,ivMax) {
403
	var cleanMarkers=[];
404
	for (var i = 0; i < pokemonMarkers.length; i++) {
405
		if(pokemonMarkers[i].ivPercent < ivMin || pokemonMarkers[i].ivPercent > ivMax){
406
			pokemonMarkers[i].setMap(null);
407
		}
408
		else{
409
			cleanMarkers.push(pokemonMarkers[i]);
410
		}
411
		
412
	}
413
	pokemonMarkers = cleanMarkers;
414
}
415
416
function extractEncountersId(){
417
	var inmapEncounter = [];
418
	for (var i = 0; i < pokemonMarkers.length; i++) {
419
		inmapEncounter[i]=pokemonMarkers[i].encounterId;
420
	}
421
	
422
	return inmapEncounter;
423
}