Passed
Pull Request — master (#140)
by PoUpA
02:30
created

home.script.js ➔ ... ➔ $(data).each   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 14
rs 9.4285
1
function updateCounter(new_value, classname)
2
{
3
		
4
	var CurrentValue = $(classname).text();
5
	
6
	$({someValue: CurrentValue}).animate({someValue: new_value}, {
7
		duration: 3000,
8
		easing:'swing',
9
		step: function () {
10
			$(classname).text(Math.round(this.someValue));
11
		}
12
	});
13
	
14
}
15
16
17
(function cron()
18
{
19
	
20
	$.ajax({
21
		url: 'core/process/aru.php?type=home_update',
22
		success: function (data) {
23
					
24
			var pokemon	= data[0];
25
			var lure	= data[1];
26
27
			var gym		= data[2];
28
			var red		= data[3];
29
			var blue	= data[4];
30
			var yellow	= data[5];
31
			var neutral	= data[6];
32
			
33
			updateCounter(pokemon,'.total-pkm-js');
34
			updateCounter(lure,'.total-lure-js');
35
			updateCounter(gym,'.total-gym-js');
36
			updateCounter(red,'.total-valor-js');
37
			updateCounter(blue,'.total-mystic-js');
38
			updateCounter(yellow,'.total-instinct-js');
39
			updateCounter(neutral,'.total-rocket-js');
40
			
41
		},
42
		complete: function () {
43
			// Schedule the next request when the current one's complete
44
			setTimeout(cron, 5000);
45
		}
46
	});
47
})();
48
49
50
51
(function spawn()
52
{
53
	
54
	var last_uid = $('.last-mon-js div:first-child').attr('data-pokeuid');
55
	 
56
	$.ajax({
57
		url: 'core/process/aru.php?type=spawnlist_update&last_uid='+last_uid,
58
		success: function (data) {
59
			
60
			if (!$.isEmptyObject(data)) {
61
                           
62
                                $(data).each(function(index,element) {
63
                                    $('.last-mon-js').prepend(element.html);
64
				
65
                                    // stop timer of last child
66
                                    
67
                                    stopTimer();
68
                                    // replace child
69
                                    $('.last-mon-js > div:last-child').fadeOut();
70
                                    $('.last-mon-js > div:first-child').fadeIn();
71
                                    $('.last-mon-js > div:last-child').remove();
72
73
                                    // start timer for new child
74
                                    startTimer(element.countdown,element.pokemon_uid);
75
                                });
76
				
77
			}
78
			
79
			
80
		},
81
		complete: function () {
82
			// Schedule the next request when the current one's complete
83
			setTimeout(spawn, 5000);
84
		}
85
	});
86
})();
87
88
// Array with timer IDs
89
var timers = [];
90
91
function startTimer(duration, element)
92
{       timers.push(setInterval(function () {
93
                $("[data-pokeuid='"+element+"']").find('.pokemon-timer').text(formatDuration(duration));
94
		if (--duration >= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter duration. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
95
            		$("[data-pokeuid='"+element+"']").find('.pokemon-timer').css({ 'color': 'rgb(62, 150, 62)'});
96
		} else {
97
			$("[data-pokeuid='"+element+"']").find('.pokemon-timer').css({ 'color': 'rgb(210, 118, 118)'});
98
		}
99
	}, 1000));
100
}
101
102
function stopTimer()
103
{
104
	var lastTimer = timers.shift();
105
	clearInterval(lastTimer);
106
}
107
108
function formatDuration(duration) {
109
    var countdown = duration, hours, minutes, seconds;
110
    hours = Math.abs(parseInt(countdown / 3600, 10));
111
    minutes = Math.abs(parseInt((countdown / 60) % 60, 10));
112
    seconds = Math.abs(parseInt(countdown % 60, 10));
113
114
    hours = hours < 10 ? "0" + hours : hours;
115
    minutes = minutes < 10 ? "0" + minutes : minutes;
116
    seconds = seconds < 10 ? "0" + seconds: seconds;
117
118
    var output = (countdown<0?"- ":"")+hours + ":" + minutes + ":" + seconds;
119
    return output;
120
}