Passed
Pull Request — master (#293)
by
unknown
02:48
created

core/js/raids.content.js   A

Complexity

Total Complexity 17
Complexity/F 2.13

Size

Lines of Code 86
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
cc 0
c 6
b 1
f 2
nc 1
dl 0
loc 86
rs 10
wmc 17
mnd 3
bc 15
fnc 8
bpm 1.875
cpm 2.125
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B raids.content.js ➔ loadRaids 0 26 1
C raids.content.js ➔ printRaid 0 45 8
1
$(function () {
2
	$.getJSON( "core/json/variables.json", function(variables) {
3
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
4
		$('.raidsLoader').hide();
5
		var page = 0;
6
		loadRaids(page, pokeimg_suffix);
7
		page++;
8
		$('#loadMoreButton').click(function () {
9
			loadRaids(page, pokeimg_suffix);
10
			page++;
11
		});
12
	});
13
});
14
15
function loadRaids(page, pokeimg_suffix) {
16
	$('.raidsLoader').show();
17
	$.ajax({
18
		'type': 'GET',
19
		'dataType': 'json',
20
		'url': "core/process/aru.php",
21
		'data': {
22
			'type' : 'raids',
23
			'page' : page
24
		}
25
	}).done(function (data) {
26
		var internalIndex = 0;
27
		$.each(data.raids, function (gym_id, raid) {
28
			internalIndex++;
29
			printRaid(raid, pokeimg_suffix);
30
		});
31
		if(internalIndex < 10){
32
			$('#loadMoreButton').hide();
33
		}
34
		else{
35
			$('#loadMoreButton').removeClass('hidden');
36
			$('#loadMoreButton').show();
37
		}
38
		$('.raidsLoader').hide();
39
	});
40
};
41
42
function printRaid(raid, pokeimg_suffix) {
43
	var now = new Date();
44
	var raidStart = new Date(raid.start);
45
	var raidEnd = new Date(raid.end);
46
47
	var raidInfos = $('<tr>',{id: 'raidInfos_'+raid.gym_id}).css('border-bottom','2px solid '+(raid.level>2?'#fad94c':'#e872b7'));
48
	raidInfos.append($('<td>',{id: 'raidLevel_'+raid.gym_id, text: '★'.repeat(raid.level)}));
49
	raidInfos.append($('<td>',{id: 'raidTime_'+raid.gym_id, text: raid.starttime + ' - ' + raid.endtime}));
50
	raidInfos.append($('<td>',{id: 'raidRemaining_'+raid.gym_id, class: 'pokemon-remaining'}).append($('<span>',{class: (raidStart < now ? 'current' : 'upcoming')})));
51
	raidInfos.append($('<td>',{id: 'raidGym_'+raid.gym_id}).append($('<a>',{href: '/map/?lat=' + raid.latitude + '&lng=' + raid.longitude, text: raid.name})));
52
53
	var details = '';
54
	var raidPokemon = $('<div>',{class: 'pokemon-single'});
55
	if (raid.pokemon_id > 0) {
56
		raidPokemon.append(
57
			$('<a>', {href : 'pokemon/'+raid.pokemon_id}).append($('<img />',
58
				{src: 'core/pokemons/'+raid.pokemon_id+pokeimg_suffix})
59
			)
60
		);
61
		details = raid.cp + ' CP<br>' + raid.quick_move + ' / ' + raid.charge_move;
62
	} else {
63
		raidPokemon.append(
64
			$('<img />',
65
				{src: 'core/img/egg_' + (raid.level > 4 ? 'legendary' : raid.level > 2 ? 'rare' : 'normal') + '.png'})
66
		);
67
		if (raidStart < now) {
68
			raidPokemon.append($('<span>', {text:'?'}));
69
		}
70
	}
71
	raidInfos.append($('<td>',{id: 'raidBoss_'+raid.gym_id}).append(raidPokemon));
72
	raidInfos.append($('<td>',{id: 'raidBossdetails_'+raid.gym_id, class: 'pokemon-details'}).append(details));
73
74
	$('#raidsContainer').append(raidInfos);
75
76
	$('span', '#raidRemaining_'+raid.gym_id).countdown(
77
		(raidStart > now ? raidStart : raidEnd),
78
		{ elapse: true, precision: 30000 }
79
	).on('update.countdown', function(event) {
80
		if (event.elapsed) {
81
			$(this).html('00:00').css('text-decoration', 'line-through');
82
		} else {
83
			$(this).html(event.strftime('%-H:%M'));
84
		}
85
	}).countdown('start');
86
}