Failed Conditions
Pull Request — master (#331)
by
unknown
03:30
created

core/js/gymhistory.content.js   B

Complexity

Total Complexity 52
Complexity/F 2.6

Size

Lines of Code 203
Function Count 20

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 4 Features 4
Metric Value
cc 0
c 9
b 4
f 4
nc 2
dl 0
loc 203
rs 7.9487
wmc 52
mnd 3
bc 30
fnc 20
bpm 1.5
cpm 2.6
noi 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A gymhistory.content.js ➔ hideGymHistoryTables 0 6 1
B gymhistory.content.js ➔ loadGyms 0 34 2
B gymhistory.content.js ➔ printGymHistory 0 11 9
B gymhistory.content.js ➔ loadGymHistory 0 29 1
C gymhistory.content.js ➔ printGym 0 28 8
A gymhistory.content.js ➔ printPokemonList 0 11 1

How to fix   Complexity   

Complexity

Complex classes like core/js/gymhistory.content.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/** global: gymName */
2
3
$(function () {
4
	$.getJSON("core/json/variables.json", function(variables) {
5
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
6
		var hide_cp_changes = variables['system']['gymhistory_hide_cp_changes'];
7
8
		$('.gymLoader').hide();
9
10
		var page = 0;
11
		var teamSelector = ''; //''=all; 0=neutral; 1=Blue; 2=Red; 3=Yellow
12
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
13
14
		$('input#name').filter(':visible').val(gymName);
15
16
		$('#loadMoreButton').click(function () {
17
			loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, hide_cp_changes, true);
18
			page++;
19
		}).trigger('click');
20
21
		$('#searchGyms').submit(function ( event ) {
22
			page = 0;
23
			$('#gymsContainer').empty();
24
			$('#loadMoreButton').trigger('click');
25
			event.preventDefault();
26
		});
27
28
		$('.teamSelectorItems').click(function ( event ) {
29
			switch ($(this).attr('id')) {
30
				case 'NeutralTeamsFilter':
31
					teamSelector=0;
32
					break;
33
				case 'BlueTeamFilter':
34
					teamSelector=1;
35
					break;
36
				case 'RedTeamFilter':
37
					teamSelector=2;
38
					break;
39
				case 'YellowFilter':
40
					teamSelector=3;
41
					break;
42
				default:
43
					teamSelector='';
44
			}
45
			$('#teamSelectorText').html($(this).html());
46
			event.preventDefault();
47
			$('#searchGyms').submit();
48
49
		});
50
		$('.rankingOrderItems').click(function ( event ) {
51
			switch ($(this).attr('id')) {
52
				case 'changedFirst':
53
					rankingFilter=0;
54
					break;
55
				case 'nameFirst':
56
					rankingFilter=1;
57
					break;
58
				case 'totalcpFirst':
59
					rankingFilter=2;
60
					break;
61
				default:
62
					rankingFilter=0;
63
			}
64
			$('#rankingOrderText').html($(this).html());
65
			event.preventDefault();
66
			$('#searchGyms').submit();
67
		});
68
		window.onpopstate = function() {
69
			if (window.history.state && 'gymhistory' === window.history.state.page) {
70
				$('input#name').filter(':visible').val(window.history.state.name);
71
				page = 0;
72
				$('#gymsContainer').empty();
73
				loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, hide_cp_changes, pokeimg_suffix, false);
74
				page++;
75
			} else {
76
				window.history.back();
77
			}
78
		};
79
	});
80
});
81
82
function loadGyms(page, name, teamSelector, rankingFilter, pokeimg_suffix, hide_cp_changes, stayOnPage) {
83
	$('.gymLoader').show();
84
	if (stayOnPage) {
85
		// build a state for this name
86
		var state = {name: name, page: 'gymhistory'};
87
		window.history.pushState(state, 'gymhistory', 'gymhistory?name=' + name);
88
	}
89
	$.ajax({
90
		'async': true,
91
		'type': 'GET',
92
		'global': false,
93
		'dataType': 'json',
94
		'url': 'core/process/aru.php',
95
		'data': {
96
			'type' : 'gyms',
97
			'page' : page,
98
			'name' : name,
99
			'team' : teamSelector,
100
			'ranking' :rankingFilter
101
		}
102
	}).done(function (data) {
103
		var internalIndex = 0;
104
		$.each(data.gyms, function (idx, gym) {
105
			internalIndex++
106
			printGym(gym, pokeimg_suffix, hide_cp_changes);
107
		});
108
		if (internalIndex < 10) {
109
			$('#loadMoreButton').hide();
110
		} else {
111
			$('#loadMoreButton').removeClass('hidden').show();
112
		}
113
		$('.gymLoader').hide();
114
	});
115
}
116
117
function loadGymHistory(page, gym_id, pokeimg_suffix, hide_cp_changes) {
118
	$('#gymHistory_'+gym_id).addClass('active').show();
119
	$('#gymHistory_'+gym_id).find('.gymHistoryLoader').show();
120
	$.ajax({
121
		'async': true,
122
		'type': 'GET',
123
		'global': false,
124
		'dataType': 'json',
125
		'url': 'core/process/aru.php',
126
		'data': {
127
			'type' : 'gymhistory',
128
			'page' : page,
129
			'gym_id' : gym_id
130
		}
131
	}).done(function (data) {
132
		var internalIndex = 0;
133
		$.each(data.entries, function(idx, entry) {
134
			internalIndex++
135
			if (entry.only_cp_changed && hide_cp_changes) return;
1 ignored issue
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...
136
			printGymHistory(gym_id, entry, pokeimg_suffix);
137
		});
138
		if (internalIndex < 10) {
139
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').hide();
140
		} else {
141
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').removeClass('hidden').data('page', page+1).show();
142
		}
143
		$('#gymHistory_'+gym_id).find('.gymHistoryLoader').hide();
144
	});
145
}
146
147
function printPokemonList(pokemons, pokeimg_suffix) {
148
	var gymPokemon = $('<ul>',{class: 'list-inline'});
149
	$.each(pokemons, function(idx, pokemon) {
150
		var list = $('<li>', {class: pokemon.class});
151
		list.append($('<a>', { class: 'no-link', href : 'pokemon/'+pokemon.pokemon_id }).append($('<img />', { src: 'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix }).css('height', '2em')));
152
		list.append($('<br><span class="small">'+pokemon.cp+' CP</span>'));
153
		list.append($('<br><span style="font-size:70%"><a href="trainer?name='+pokemon.trainer_name+'" class="no-link">'+pokemon.trainer_name+'</a></span>'));
154
		gymPokemon.append(list);
155
	});
156
	return gymPokemon;
157
}
158
159
function printGymHistory(gym_id, entry, pokeimg_suffix) {
160
	var gymHistory = $('<tr>').css('border-bottom', '2px solid '+(entry.team_id=='3'?'#ffbe08':entry.team_id=='2'?'#ff7676':entry.team_id=='1'?'#00aaff':'#ddd'));
161
	gymHistory.append($('<td>',{text: entry.last_modified}));
162
	gymHistory.append($('<td>',{text: entry.pokemon_count, class: 'level'}).prepend($('<img />', {src:'core/img/map_'+(entry.team_id=='1'?'blue':entry.team_id=='2'?'red':entry.team_id=='3'?'yellow':'white')+'.png'})));
163
	gymHistory.append($('<td>',{text: parseInt(entry.total_cp).toLocaleString('de-DE'), class: entry.class}).append(
164
		entry.total_cp_diff !== 0 ? $('<span class="small"> ('+(entry.total_cp_diff > 0 ? '+' : '')+entry.total_cp_diff+')</span>') : null
165
	));
166
	var gymPokemon = printPokemonList(entry.pokemon, pokeimg_suffix);
167
	gymHistory.append($('<td>').append(gymPokemon));
168
	$('#gymHistory_'+gym_id).find('tbody').append(gymHistory);
169
}
170
171
function hideGymHistoryTables(gymHistoryTables) {
172
	gymHistoryTables.removeClass('active').hide();
173
	gymHistoryTables.find('tbody tr').remove();
174
	gymHistoryTables.find('.loadMoreButtonHistory').hide();
175
	gymHistoryTables.find('.gymHistoryLoader').hide();
176
}
177
178
function printGym(gym, pokeimg_suffix, hide_cp_changes) {
179
	var gymsInfos = $('<tr>',{id: 'gymInfos_'+gym.gym_id}).css('cursor', 'pointer').css('border-bottom', '2px solid '+(gym.team_id=='3'?'#ffbe08':gym.team_id=='2'?'#ff7676':gym.team_id=='1'?'#00aaff':'#ddd')).click(function() {
180
		if (!$('#gymHistory_'+gym.gym_id).hasClass('active')) {
181
			hideGymHistoryTables($('#gymsContainer').find('.gymhistory'));
182
			loadGymHistory(0, gym.gym_id, pokeimg_suffix, hide_cp_changes);
183
		} else {
184
			hideGymHistoryTables($('#gymHistory_'+gym.gym_id));
185
		}
186
	});
187
	gymsInfos.append($('<td>',{text: gym.last_modified}));
188
	if (gym.name.length > 50) { gym.name = gym.name.substr(0, 50) + '…'; }
189
	gymsInfos.append($('<td>',{text: gym.name}));
190
	gymsInfos.append($('<td>',{text: gym.pokemon_count, class: 'level'}).prepend($('<img />', {src:'core/img/map_'+(gym.team_id=='1'?'blue':gym.team_id=='2'?'red':gym.team_id=='3'?'yellow':'white')+'.png'})));
191
	gymsInfos.append($('<td>',{text: parseInt(gym.total_cp).toLocaleString('de-DE')}));
192
	var gymPokemon = printPokemonList(gym.pokemon, pokeimg_suffix);
193
	gymsInfos.append($('<td>').append(gymPokemon));
194
	$('#gymsContainer').append(gymsInfos);
195
	var historyTable = $('<table>',{class: 'table'});
196
	historyTable.append('<thead><tr><th style="min-width:7em">Time</th><th>Level</th><th>Total CP</th><th>Pokémon</th></tr></thead>');
197
	historyTable.append('<tbody></tbody>');
198
	historyTable.append('<tfoot><tr class="loadMore text-center"><td colspan="4"><button class="loadMoreButtonHistory btn btn-default btn-sm hidden">Load more</button></td></tr><tr class="gymHistoryLoader"><td colspan="4"><div class="loader"></div></td></tr></tfoot>');
199
	historyTable.find('.loadMoreButtonHistory').data('page', 0).click(function() {
200
		loadGymHistory($(this).data('page'), gym.gym_id, pokeimg_suffix, hide_cp_changes);
201
	});
202
	var row = $('<td>',{colspan: 6});
203
	row.append(historyTable);
204
	$('#gymsContainer').append($('<tr>', {id: 'gymHistory_'+gym.gym_id, class: 'gymhistory'}).hide().append(row));
205
}
206