Failed Conditions
Pull Request — master (#353)
by Florian
05:07
created

trainer.content.js ➔ printPokemon   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 7
nc 16
nop 4
dl 0
loc 91
rs 6.5033
c 4
b 1
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** global: trainerName */
2
$(function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3
	function htmlspecialchars_decode(string) {
4
		var escapeMap = {
5
			"&": "&",
6
			"&lt;": "<",
7
			"&gt;": ">",
8
			"&quot;": "\"",
9
			"&#39;": "'",
10
			"&#039;": "'"
11
		};
12
		return String(string).replace(/&(amp|lt|gt|quot|#0?39);/gi, function (s) {
13
			return escapeMap[s] || s;
14
		});
15
	}
16
17
	$.getJSON('core/json/variables.json', function(variables) {
18
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
19
		var iv_numbers = variables['system']['iv_numbers'];
20
21
		$('.trainerLoader').hide();
22
		var page = 0;
23
		var teamSelector = 0; //0=all;1=Blue;2=Red;3=Yellow
24
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
25
26
		$('input#name').filter(':visible').val(htmlspecialchars_decode(trainerName));
27
		loadTrainers(page, $('input#name').filter(':visible').val(), null, null, pokeimg_suffix, true, iv_numbers);
28
		if ($('input#name')) {
29
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
30
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
31
		}
32
33
		page++;
34
		$('#loadMoreButton').click(function() {
35
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
36
			page++;
37
		});
38
		$('#searchTrainer').submit(function(event) {
39
			page = 0;
40
			$('input#name').filter(':visible').val() != '' ? $('#trainersGraph').hide() : $('#trainersGraph').show();
41
			$('#trainersContainer tr:not(.trainersTemplate)').remove();
42
			loadTrainers(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true, iv_numbers);
43
			page++;
44
			event.preventDefault();
45
		});
46
		$('.teamSelectorItems').click(function(event) {
47
			switch ($(this).attr('id')) {
48
				case 'AllTeamsFilter':
49
					teamSelector = 0;
50
					break;
51
				case 'BlueTeamFilter':
52
					teamSelector = 1;
53
					break;
54
				case 'RedTeamFilter':
55
					teamSelector = 2;
56
					break;
57
				case 'YellowFilter':
58
					teamSelector = 3;
59
					break;
60
				default:
61
					teamSelector = 0;
62
			}
63
			$('#teamSelectorText').html($(this).html());
64
			event.preventDefault();
65
			$('#searchTrainer').submit();
66
67
		});
68
		$('.rankingOrderItems').click(function(event) {
69
			switch ($(this).attr('id')) {
70
				case 'levelsFirst':
71
					rankingFilter = 0;
72
					break;
73
				case 'gymsFirst':
74
					rankingFilter = 1;
75
					break;
76
				case 'maxCpFirst':
77
					rankingFilter = 2;
78
					break;
79
				default:
80
					rankingFilter = 0;
81
			}
82
			$('#rankingOrderText').html($(this).html());
83
			event.preventDefault();
84
			$('#searchTrainer').submit();
85
86
		});
87
		window.onpopstate = function() {
88
			if (window.history.state && 'Trainer' === window.history.state.page) {
89
				$('#trainersContainer').empty();
90
				$('input#name').filter(':visible').val(window.history.state.name);
91
				loadTrainers(0, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, false, iv_numbers);
92
			} else {
93
				window.history.back();
94
			}
95
		};
96
97
	});
98
});
99
100
function loadTrainers(page, name, teamSelector, rankingFilter, pokeimg_suffix, stayOnPage, iv_numbers) {
101
	$('.trainerLoader').show();
102
103
	if (stayOnPage) {
104
		// build a state for this name
105
		var state = { name: name, page: 'Trainer' };
106
		window.history.pushState(state, 'Trainer', 'trainer?name=' + name);
107
	}
108
	var trainerIndex = 0 + (page * 10);
109
	$.ajax({
110
		'type': 'GET',
111
		'global': false,
112
		'dataType': 'json',
113
		'url': 'core/process/aru.php',
114
		'data': {
115
			'type': 'trainer',
116
			'page': page,
117
			'name': name,
118
			'team': teamSelector,
119
			'ranking': rankingFilter
120
		}
121
	}).done(function(data) {
122
		var internalIndex = 0;
123
		$.each(data.trainers, function(trainerName, trainer) {
124
			trainerIndex++;
125
			internalIndex++
126
			printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, data.locale);
127
		});
128
		if (internalIndex < 10) {
129
			$('#loadMoreButton').hide();
130
		} else {
131
			$('#loadMoreButton').removeClass('hidden');
132
			$('#loadMoreButton').show();
133
		}
134
		$('.trainerLoader').hide();
135
	});
136
};
137
138
139
140
function printTrainer(trainer, trainerIndex, pokeimg_suffix, iv_numbers, locale) {
141
	var trainersInfos = $('<tr>', { id: 'trainerInfos_' + trainer.name }).css('border-bottom', '2px solid ' + (trainer.team == '3' ? '#ffbe08' : trainer.team == '2' ? '#ff7676' : '#00aaff'));
142
	trainersInfos.append($('<td>', { id: 'trainerIndex_' + trainer.name, text: trainerIndex }));
143
	trainersInfos.append($('<td>', { id: 'trainerRank_' + trainer.name, text: trainer.rank }));
144
	trainersInfos.append($('<td>', { id: 'trainerName_' + trainer.name }).append($('<a>', { href: 'trainer?name=' + trainer.name, text: trainer.name })).click(
145
		function(e) {
146
			e.preventDefault();
147
			$('input#name').filter(':visible').val(trainer.name);
148
			$('#searchTrainer').submit();
149
			$('#trainerName_' + trainer.name).off('click');
150
		}
151
	));
152
	trainersInfos.append($('<td>', { id: 'trainerLevel_' + trainer.name, text: trainer.level }));
153
	trainersInfos.append($('<td>', { id: 'trainerGyms_' + trainer.name, text: trainer.gyms }));
154
	trainersInfos.append($('<td>', { id: 'trainerLastSeen_' + trainer.name, text: trainer.last_seen }));
155
	trainersInfos.append($('<td>', { id: 'trainerShowAll_' + trainer.name }).append('<input type="checkbox" id="showAll">'));
156
	$('#trainersContainer').append(trainersInfos);
157
	var trainersPokemonsRow = $('<tr>', { id: 'trainerPokemons_' + trainer.name });
158
	var trainersPokemons = $('<td>', { colspan: 7 });
159
	var trainersPokemonsContainer = $('<div>', { class: '' });
160
	for (var pokeIndex = 0; pokeIndex < trainer.pokemons.length; pokeIndex++) {
161
		var pokemon = trainer.pokemons[pokeIndex];
162
		trainersPokemonsContainer.append(printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale));
163
	}
164
	trainersInfos.find('#showAll').click(function() {
165
		trainersPokemonsContainer.find('div.pokemon-single.unseen').fadeToggle();
166
	});
167
	trainersPokemons.append(trainersPokemonsContainer);
168
	trainersPokemonsRow.append(trainersPokemons);
169
	$('#trainersContainer').append(trainersPokemonsRow);
170
}
171
172
function printPokemon(pokemon, pokeimg_suffix, iv_numbers, locale) {
173
	var gymClass = pokemon.gym_id === null ? ' unseen' : '';
174
	var trainerPokemon = $('<div>', { id: 'trainerPokemon_' + pokemon.pokemon_uid, class: 'col-md-1 col-xs-4 pokemon-single' + gymClass, style: 'text-align: center' });
175
	if (gymClass) {
176
		trainerPokemon.hide();
177
	}
178
	trainerPokemon.append(
179
		$('<a>', { href: 'pokemon/' + pokemon.pokemon_id }).append($('<img />', {
180
			src: 'core/pokemons/' + pokemon.pokemon_id + pokeimg_suffix,
181
			'class': 'img-responsive' + gymClass
182
		}))
183
	);
184
	trainerPokemon.append($('<p>', { class: 'pkmn-name' }).append(pokemon.cp));
185
	var progressBar
186
	if (iv_numbers) {
187
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '15px', 'margin-bottom': '0' });
188
		progressBar.append(
189
			$('<div>', {
190
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
191
				class: 'progress-bar progress-bar-danger',
192
				role: 'progressbar',
193
				text: pokemon.iv_attack,
194
				'aria-valuenow': pokemon.iv_attack,
195
				'aria-valuemin': 0,
196
				'aria-valuemax': 45
197
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
198
		progressBar.append(
199
			$('<div>', {
200
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
201
				class: 'progress-bar progress-bar-info',
202
				role: 'progressbar',
203
				text: pokemon.iv_defense,
204
				'aria-valuenow': pokemon.iv_defense,
205
				'aria-valuemin': 0,
206
				'aria-valuemax': 45
207
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
208
		progressBar.append(
209
			$('<div>', {
210
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
211
				class: 'progress-bar progress-bar-success',
212
				role: 'progressbar',
213
				text: pokemon.iv_stamina,
214
				'aria-valuenow': pokemon.iv_stamina,
215
				'aria-valuemin': 0,
216
				'aria-valuemax': 45
217
			}).css({ 'width': (100 / 3) + '%', 'line-height': '16px' }))
218
	} else {
219
		progressBar = $('<div>', { class: 'progress' }).css({ 'height': '6px', 'margin-bottom': '0' });
220
		progressBar.append(
221
			$('<div>', {
222
				title: locale.ivAttack + ' :' + pokemon.iv_attack,
223
				class: 'progress-bar progress-bar-danger',
224
				role: 'progressbar',
225
				'aria-valuenow': pokemon.iv_attack,
226
				'aria-valuemin': 0,
227
				'aria-valuemax': 45
228
			}).css('width', ((100 / 45) * pokemon.iv_attack) + '%'))
229
		progressBar.append(
230
			$('<div>', {
231
				title: locale.ivDefense + ' :' + pokemon.iv_defense,
232
				class: 'progress-bar progress-bar-info',
233
				role: 'progressbar',
234
				'aria-valuenow': pokemon.iv_defense,
235
				'aria-valuemin': 0,
236
				'aria-valuemax': 45
237
			}).css('width', ((100 / 45) * pokemon.iv_defense) + '%'))
238
		progressBar.append(
239
			$('<div>', {
240
				title: locale.ivStamina + ' :' + pokemon.iv_stamina,
241
				class: 'progress-bar progress-bar-success',
242
				role: 'progressbar',
243
				'aria-valuenow': pokemon.iv_stamina,
244
				'aria-valuemin': 0,
245
				'aria-valuemax': 45
246
			}).css('width', ((100 / 45) * pokemon.iv_stamina) + '%'))
247
	}
248
	trainerPokemon.append(progressBar);
249
	if (pokemon.deployment_time) {
250
		var diff = (new Date() - new Date(pokemon.deployment_time.replace(/-/g, '/'))) / 1000;
251
		if (diff >= 86400) {
252
			trainerPokemon.append($('<small>', { text: parseInt(diff / 86400) + 'd ' + parseInt((diff / 3600) % 24) + 'h'}));
253
		} else {
254
			trainerPokemon.append($('<small>', { text: parseInt(diff / 3600) + 'h ' + parseInt((diff / 60) % 60) + 'm' }));
255
		}
256
	} else if (pokemon.last_scanned === '1') {
257
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.day }));
258
	} else {
259
		trainerPokemon.append($('<small>', { text: pokemon.last_scanned + ' ' + locale.days }));
260
	}
261
	return trainerPokemon;
262
}
263