Failed Conditions
Pull Request — master (#366)
by
unknown
04:28
created

trainer.content.js ➔ ... ➔ trainersInfos.click   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

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 3
rs 10
1
/** global: trainerName */
2
$(function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3
4
	$.getJSON('core/json/variables.json', function(variables) {
5
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
6
		var iv_numbers = variables['system']['iv_numbers'];
7
8
		$('.trainerLoader').hide();
9
		var page = 0;
10
		var teamSelector = 0; //0=all;1=Blue;2=Red;3=Yellow
11
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
12
13
		$('input#name').filter(':visible').val(trainerName);
14
		loadTrainers(page, $('input#name').filter(':visible').val(), null, null, pokeimg_suffix, true, iv_numbers);
15
		if ($('input#name')) {
16
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
17
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
18
		}
19
20
		page++;
21
		$('#loadMoreButton').click(function() {
22
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
23
			page++;
24
		});
25
		$('#searchTrainer').submit(function(event) {
26
			page = 0;
27
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
28
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
29
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
30
			page++;
31
			event.preventDefault();
32
		});
33
		$('.teamSelectorItems').click(function(event) {
34
			switch ($(this).attr('id')) {
35
				case 'AllTeamsFilter':
36
					teamSelector = 0;
37
					break;
38
				case 'BlueTeamFilter':
39
					teamSelector = 1;
40
					break;
41
				case 'RedTeamFilter':
42
					teamSelector = 2;
43
					break;
44
				case 'YellowFilter':
45
					teamSelector = 3;
46
					break;
47
				default:
48
					teamSelector = 0;
49
			}
50
			$('#teamSelectorText').html($(this).html());
51
			event.preventDefault();
52
			$('#searchTrainer').submit();
53
54
		});
55
		$('.rankingOrderItems').click(function(event) {
56
			switch ($(this).attr('id')) {
57
				case 'levelsFirst':
58
					rankingFilter = 0;
59
					break;
60
				case 'gymsFirst':
61
					rankingFilter = 1;
62
					break;
63
				case 'maxCpFirst':
64
					rankingFilter = 2;
65
					break;
66
				default:
67
					rankingFilter = 0;
68
			}
69
			$('#rankingOrderText').html($(this).html());
70
			event.preventDefault();
71
			$('#searchTrainer').submit();
72
73
		});
74
		window.onpopstate = function() {
75
			if (window.history.state && 'Trainer' === window.history.state.page) {
76
				$('#trainersContainer').empty();
77
				$('input#name').filter(':visible').val(window.history.state.name);
78
				loadTrainers(0, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, false, iv_numbers);
79
			} else {
80
				window.history.back();
81
			}
82
		};
83
84
	});
85
});
86
87
function loadTrainers(page, name, teamSelector, rankingFilter, pokeimg_suffix, stayOnPage, iv_numbers) {
88
	$('.trainerLoader').show();
89
90
	if (stayOnPage) {
91
		// build a state for this name
92
		var state = { name: name, page: 'Trainer' };
93
		window.history.pushState(state, 'Trainer', 'trainer?name=' + name);
94
	}
95
	var trainerIndex = 0 + (page * 10);
96
	$.ajax({
97
		'type': 'GET',
98
		'global': false,
99
		'dataType': 'json',
100
		'url': 'core/process/aru.php',
101
		'data': {
102
			'type': 'trainer',
103
			'page': page,
104
			'name': name,
105
			'team': teamSelector,
106
			'ranking': rankingFilter
107
		}
108
	}).done(function(data) {
109
		var internalIndex = 0;
110
		$.each(data.trainers, function(trainerName, trainer) {
111
			trainerIndex++;
112
			internalIndex++
113
			printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, data.locale);
114
		});
115
		if (internalIndex < 10) {
116
			$('#loadMoreButton').hide();
117
		} else {
118
			$('#loadMoreButton').removeClass('hidden');
119
			$('#loadMoreButton').show();
120
		}
121
		$('.trainerLoader').hide();
122
	});
123
};
124
125
126
127
function printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, locale) {
128
	var trainersInfos = $('<tr>', { id: 'trainerInfos_' + trainer.name }).css('border-bottom', '2px solid ' + (trainer.team == '3' ? '#ffbe08' : trainer.team == '2' ? '#ff7676' : '#00aaff'));
129
	trainersInfos.append($('<td>', { id: 'trainerIndex_' + trainer.name, text: trainerIndex }));
130
	trainersInfos.append($('<td>', { id: 'trainerRank_' + trainer.name, text: trainer.rank }));
131
	trainersInfos.append($('<td>', { id: 'trainerName_' + trainer.name }).append($('<a>', { href: 'trainer?name=' + trainer.name, text: trainer.name })).click(
132
		function(e) {
133
			e.preventDefault();
134
			$('input#name').filter(':visible').val(trainer.name);
135
			$('#searchTrainer').submit();
136
			$('#trainerName_' + trainer.name).off('click');
137
		}
138
	));
139
	trainersInfos.append($('<td>', { id: 'trainerLevel_' + trainer.name, text: trainer.level }));
140
	trainersInfos.append($('<td>', { id: 'trainerGyms_' + trainer.name, text: trainer.gyms }));
141
	trainersInfos.append($('<td>', { id: 'trainerLastSeen_' + trainer.name, text: trainer.last_seen }));
142
	trainersInfos.append($('<td>', { id: 'trainerShowAll_' + trainer.name }).append('<input type="checkbox" id="showAll">'));
143
	$('#trainersContainer').append(trainersInfos);
144
	var trainersPokemonsRow = $('<tr>', { id: 'trainerPokemons_' + trainer.name });
145
	var trainersPokemons = $('<td>', { colspan: 7 });
146
	var trainersPokemonsContainer = $('<div>', { class: '' });
147
	for (var pokeIndex = 0; pokeIndex < trainer.pokemons.length; pokeIndex++) {
148
		var pokemon = trainer.pokemons[pokeIndex];
149
		trainersPokemonsContainer.append(printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale));
150
	}
151
	trainersInfos.find('#showAll').click(function() {
152
		trainersPokemonsContainer.find('div.pokemon-single.unseen').fadeToggle();
153
	});
154
	trainersPokemons.append(trainersPokemonsContainer);
155
	trainersPokemonsRow.append(trainersPokemons);
156
	$('#trainersContainer').append(trainersPokemonsRow);
157
}
158
159
function printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale) {
160
	var gymClass = pokemon.gym_id === null ? ' unseen' : '';
161
	var trainerPokemon = $('<div>', { id: 'trainerPokemon_' + pokemon.pokemon_uid, class: 'col-md-1 col-xs-4 pokemon-single' + gymClass, style: 'text-align: center' });
162
	if (gymClass) trainerPokemon.hide();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
163
	trainerPokemon.append(
164
		$('<a>', { href: 'pokemon/' + pokemon.pokemon_id }).append($('<img />', {
165
			src: 'core/pokemons/' + pokemon.pokemon_id + pokeimg_suffix,
166
			'class': 'img-responsive' + gymClass
167
		}))
168
	);
169
	trainerPokemon.append($('<p>', { class: 'pkmn-name' }).append(pokemon.cp));
170
	var progressBar
171
	if (iv_numbers) {
172
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '15px', 'margin-bottom': '0' });
173
		progressBar.append(
174
			$('<div>', {
175
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
176
				class: 'progress-bar progress-bar-danger',
177
				role: 'progressbar',
178
				text: pokemon.iv_attack,
179
				'aria-valuenow': pokemon.iv_attack,
180
				'aria-valuemin': 0,
181
				'aria-valuemax': 45
182
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
183
		progressBar.append(
184
			$('<div>', {
185
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
186
				class: 'progress-bar progress-bar-info',
187
				role: 'progressbar',
188
				text: pokemon.iv_defense,
189
				'aria-valuenow': pokemon.iv_defense,
190
				'aria-valuemin': 0,
191
				'aria-valuemax': 45
192
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
193
		progressBar.append(
194
			$('<div>', {
195
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
196
				class: 'progress-bar progress-bar-success',
197
				role: 'progressbar',
198
				text: pokemon.iv_stamina,
199
				'aria-valuenow': pokemon.iv_stamina,
200
				'aria-valuemin': 0,
201
				'aria-valuemax': 45
202
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
203
	} else {
204
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '6px', 'margin-bottom': '0' });
205
		progressBar.append(
206
			$('<div>', {
207
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
208
				class: 'progress-bar progress-bar-danger',
209
				role: 'progressbar',
210
				'aria-valuenow': pokemon.iv_attack,
211
				'aria-valuemin': 0,
212
				'aria-valuemax': 45
213
			}).css('width', ((100 / 45) * pokemon.iv_attack) + '%'))
214
		progressBar.append(
215
			$('<div>', {
216
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
217
				class: 'progress-bar progress-bar-info',
218
				role: 'progressbar',
219
				'aria-valuenow': pokemon.iv_defense,
220
				'aria-valuemin': 0,
221
				'aria-valuemax': 45
222
			}).css('width', ((100 / 45) * pokemon.iv_defense) + '%'))
223
		progressBar.append(
224
			$('<div>', {
225
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
226
				class: 'progress-bar progress-bar-success',
227
				role: 'progressbar',
228
				'aria-valuenow': pokemon.iv_stamina,
229
				'aria-valuemin': 0,
230
				'aria-valuemax': 45
231
			}).css('width', ((100 / 45) * pokemon.iv_stamina) + '%'))
232
	}
233
	trainerPokemon.append(progressBar);
234
	if (pokemon.deployment_time) {
235
		var diff = (new Date() - new Date(pokemon.deployment_time.replace(/-/g, '/'))) / 1000;
236
		trainerPokemon.append($('<small>', { text: parseInt(diff / 3600) + 'h ' + parseInt((diff / 60) % 60) + 'm' }));
237
	} else if (pokemon.last_scanned === '1') {
238
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.day }));
239
	} else {
240
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.days }));
241
	}
242
	return trainerPokemon;
243
}