Completed
Pull Request — master (#302)
by
unknown
57:22
created

raids.content.js ➔ ... ➔ $.fail   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
$(function () {
2
	$.getJSON( "core/json/variables.json", function(variables) {
3
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
4
		var location_url = variables['system']['location_url'] || 'https://maps.google.com/?q={latitude},{longitude}&ll={latitude},{longitude}&z=16';
5
		$('.raidsLoader').hide();
6
		var page = 0;
7
		loadRaids(page, pokeimg_suffix, location_url);
8
		page++;
9
		$('#loadMoreButton').click(function () {
10
			loadRaids(page, pokeimg_suffix, location_url);
11
			page++;
12
		});
13
	});
14
});
15
16
function loadRaids(page, pokeimg_suffix, location_url) {
17
	$('.raidsLoader').show();
18
	$.ajax({
19
		'type': 'GET',
20
		'dataType': 'json',
21
		'url': "core/process/aru.php",
22
		'data': {
23
			'type' : 'raids',
24
			'page' : page
25
		}
26
	}).done(function (data) {
27
		var internalIndex = 0;
28
		if (data.raids.length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing data.raids.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
29
			var raidInfos = $('<tr>');
30
			raidInfos.append($('<td>',{colspan: 6, text: data.locale.noraids})).css('text-align', 'center');
31
			$('#raidsContainer').append(raidInfos);
32
		}
33
		$.each(data.raids, function (gym_id, raid) {
34
			internalIndex++;
35
			printRaid(raid, pokeimg_suffix, location_url);
36
		});
37
		if (internalIndex < 10) {
38
			$('#loadMoreButton').hide();
39
		} else {
40
			$('#loadMoreButton').removeClass('hidden');
41
			$('#loadMoreButton').show();
42
		}
43
	}).fail(function() {
44
		var raidInfos = $('<tr>');
45
		raidInfos.append($('<td>',{colspan: 6, text: 'ERROR'})).css('text-align', 'center');
46
		$('#raidsContainer').append(raidInfos);
47
	}).always(function() {
48
		$('.raidsLoader').hide();
49
	});
50
};
51
52
function printRaid(raid, pokeimg_suffix, location_url) {
53
	var now = new Date();
54
	var raidStart = new Date(raid.start.replace(/-/g, '/'));
55
	var raidEnd = new Date(raid.end.replace(/-/g, '/'));
56
57
	var raidInfos = $('<tr>',{id: 'raidInfos_'+raid.gym_id}).css('border-bottom','2px solid '+(raid.level>2?'#fad94c':'#e872b7'));
58
	raidInfos.append($('<td>',{id: 'raidLevel_'+raid.gym_id, class: 'level', text: '★'.repeat(raid.level)}));
59
	raidInfos.append($('<td>',{id: 'raidTime_'+raid.gym_id, text: raid.starttime + ' - ' + raid.endtime}));
60
	raidInfos.append($('<td>',{id: 'raidRemaining_'+raid.gym_id, class: 'pokemon-remaining'}).append($('<span>',{class: (raidStart < now ? 'current' : 'upcoming')})));
61
62
	var locationLink = location_url.replace(/\{latitude\}/g, raid.latitude).replace(/\{longitude\}/g, raid.longitude)
63
	raidInfos.append($('<td>',{id: 'raidGym_'+raid.gym_id}).append($('<a>',{href: locationLink, text: raid.name})));
64
65
	var details = '';
66
	var raidPokemon = $('<div>',{class: 'pokemon-single'});
67
	if (raid.pokemon_id > 0) {
68
		raidPokemon.append(
69
			$('<a>', {href : 'pokemon/'+raid.pokemon_id}).append($('<img />',
70
				{src: 'core/pokemons/'+raid.pokemon_id+pokeimg_suffix})
71
			)
72
		);
73
		details = raid.cp + ' CP<br>' + raid.quick_move + ' / ' + raid.charge_move;
74
	} else {
75
		raidPokemon.append(
76
			$('<img />',
77
				{src: 'core/img/egg_' + (raid.level > 4 ? 'legendary' : raid.level > 2 ? 'rare' : 'normal') + '.png'})
78
		);
79
		if (raidStart < now) {
80
			raidPokemon.append($('<span>', {text:'?'}));
81
		}
82
	}
83
	raidInfos.append($('<td>',{id: 'raidBoss_'+raid.gym_id}).append(raidPokemon));
84
	raidInfos.append($('<td>',{id: 'raidBossdetails_'+raid.gym_id, class: 'pokemon-details'}).append(details));
85
86
	$('#raidsContainer').append(raidInfos);
87
88
	$('span', '#raidRemaining_'+raid.gym_id).countdown(
89
		(raidStart > now ? raidStart : raidEnd),
90
		{ elapse: true, precision: 30000 }
91
	).on('update.countdown', function(event) {
92
		if (event.elapsed) {
93
			$(this).html('00:00').css('text-decoration', 'line-through');
94
		} else {
95
			$(this).html(event.strftime('%-H:%M'));
96
		}
97
	}).countdown('start');
98
}
99