Passed
Pull Request — master (#283)
by
unknown
05:33
created

➔ $.getJSON(ꞌcore/json/variables.jsonꞌ)   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 75
rs 9

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: gymName */
2
3
var gymRanks = [
4
	{ level : 1,  prestigeMax : 2000,  prestigeMin : 0 },
5
	{ level : 2,  prestigeMax : 4000,  prestigeMin : 2000 },
6
	{ level : 3,  prestigeMax : 8000,  prestigeMin : 4000 },
7
	{ level : 4,  prestigeMax : 12000, prestigeMin : 8000 },
8
	{ level : 5,  prestigeMax : 16000, prestigeMin : 12000 },
9
	{ level : 6,  prestigeMax : 20000, prestigeMin : 16000 },
10
	{ level : 7,  prestigeMax : 30000, prestigeMin : 20000 },
11
	{ level : 8,  prestigeMax : 40000, prestigeMin : 30000 },
12
	{ level : 9,  prestigeMax : 50000, prestigeMin : 40000 },
13
	{ level : 10, prestigeMax : 52000, prestigeMin : 50000 }
14
];
15
16
$(function () {
17
18
	$.getJSON("core/json/variables.json", function(variables) {
19
		var pokeimg_suffix = variables['system']['pokeimg_suffix'];
20
21
		$('.gymLoader').hide();
22
		var page = 0;
23
		var teamSelector = ''; //''=all;0=neutral;1=Blue;2=Red;3=Yellow
24
		var rankingFilter = 0; //0=Level & Gyms; 1=Level; 2=Gyms
25
26
		$('input#name').filter(':visible').val(gymName);
27
		loadGyms(page, $('input#name').filter(':visible').val(), null, null, pokeimg_suffix, true);
28
29
		page++;
30
		$('#loadMoreButton').click(function () {
31
			loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true);
32
			page++;
33
		});
34
		$("#searchGyms").submit(function ( event ) {
35
			page = 0;
36
			$('#gymsContainer tr:not(.gymsTemplate)').remove();
37
			loadGyms(page, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, true);
38
			page++;
39
			event.preventDefault();
40
		});
41
		$(".teamSelectorItems").click(function ( event ) {
42
			switch ($(this).attr("id")) {
43
				case "NeutralTeamsFilter":
44
					teamSelector=0;
45
					break;
46
				case "BlueTeamFilter":
47
					teamSelector=1;
48
					break;
49
				case "RedTeamFilter":
50
					teamSelector=2;
51
					break;
52
				case "YellowFilter":
53
					teamSelector=3;
54
					break;
55
				default:
56
					teamSelector='';
57
			}
58
			$("#teamSelectorText").html($(this).html());
59
			event.preventDefault();
60
			$("#searchGyms").submit();
61
62
		});
63
		$(".rankingOrderItems").click(function ( event ) {
64
			switch ($(this).attr("id")) {
65
				case "changedFirst":
66
					rankingFilter=0;
67
					break;
68
				case "nameFirst":
69
					rankingFilter=1;
70
					break;
71
				case "prestigeFirst":
72
					rankingFilter=2;
73
					break;
74
				default:
75
					rankingFilter=0;
76
			}
77
			$("#rankingOrderText").html($(this).html());
78
			event.preventDefault();
79
			$("#searchGyms").submit();
80
81
		});
82
		window.onpopstate = function() {
83
			if (window.history.state && "gym" === window.history.state.page) {
84
				$('#gymsContainer').empty();
85
				$('input#name').filter(':visible').val(window.history.state.name);
86
				loadGyms(0, $('input#name').filter(':visible').val(), teamSelector, rankingFilter, pokeimg_suffix, false);
87
			} else {
88
				window.history.back();
89
			}
90
		};
91
92
	});
93
});
94
95
function loadGyms(page, name, teamSelector, rankingFilter, pokeimg_suffix, stayOnPage) {
96
	$('.gymLoader').show();
97
98
	if (stayOnPage) {
99
		// build a state for this name
100
		var state = {name: name, page: 'Gym History'};
101
		window.history.pushState(state, 'Gym History', 'gymhistory?name=' + name);
102
	}
103
	$.ajax({
104
		'async': true,
105
		'type': "GET",
106
		'global': false,
107
		'dataType': 'json',
108
		'url': "core/process/aru.php",
109
		'data': {
110
			'request': '',
111
			'target': 'arrange_url',
112
			'method': 'method_target',
113
			'type' : 'gyms',
114
			'page' : page,
115
			'name' : name,
116
			'team' : teamSelector,
117
			'ranking' :rankingFilter
118
		}
119
	}).done(function (data) {
120
		var internalIndex = 0;
121
		$.each(data.gyms, function (idx, gym) {
122
			internalIndex++
123
			printGym(gym, pokeimg_suffix, data.locale);
124
		});
125
		if (internalIndex < 10) {
126
			$('#loadMoreButton').hide();
127
		} else {
128
			$('#loadMoreButton').removeClass('hidden');
129
			$('#loadMoreButton').show();
130
		}
131
		$('.gymLoader').hide();
132
	});
133
}
134
135
function loadGymHistory(page, gym_id, pokeimg_suffix) {
136
	$('.gymLoader').show();
137
138
	$.ajax({
139
		'async': true,
140
		'type': "GET",
141
		'global': false,
142
		'dataType': 'json',
143
		'url': "core/process/aru.php",
144
		'data': {
145
			'request': '',
146
			'target': 'arrange_url',
147
			'method': 'method_target',
148
			'type' : 'gymhistory',
149
			'page' : page,
150
			'gym_id' : gym_id
151
		}
152
	}).done(function (data) {
153
		var internalIndex = 0;
154
		$.each(data.entries, function(idx, entry) {
155
			internalIndex++
156
			printGymHistory(gym_id, entry, pokeimg_suffix, data.locale);
157
		});
158
		$('#gymHistory_'+gym_id).addClass('active').show();
159
		if (internalIndex < 10) {
160
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').hide();
161
		} else {
162
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').removeClass('hidden');
163
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').data('page', page+1).show();
164
		}
165
		$('.gymLoader').hide();
166
	});
167
}
168
169
function printPokemonList(pokemons, pokeimg_suffix, hide_unchanged) {
170
	var gymPokemon = $('<ul>',{class: 'list-inline'});
171
	$.each(pokemons, function(idx, pokemon) {
172
		if (!hide_unchanged || pokemon.class) {
173
			var list = $('<li>', {class: pokemon.class});
174
			list.append($('<a>', { class: 'no-link', href : 'pokemon/'+pokemon.pokemon_id }).append($('<img />', { src: 'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix }).css('height', '2em')));
175
			list.append($('<br><span class="small">'+pokemon.cp+' CP</span>'));
176
			list.append($('<br><span style="font-size:70%"><a href="trainer?name='+pokemon.trainer_name+'" class="no-link">'+pokemon.trainer_name+'</a></span>'));
177
			gymPokemon.append(list);
178
		}
179
	});
180
	return gymPokemon;
181
}
182
183
function printGymHistory(gym_id, entry, pokeimg_suffix, locale) {
0 ignored issues
show
Unused Code introduced by
The parameter locale is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
184
	var gymLevel = gymRanks.find(r => r.prestigeMin <= entry.gym_points && r.prestigeMax > entry.gym_points) || { level : 10 };
185
	var gymHistory = $('<tr>').css('border-bottom', '2px solid '+(entry.team_id=='3'?'#ffbe08':entry.team_id=='2'?'#ff7676':entry.team_id=='1'?'#00aaff':'#ddd'));
186
	gymHistory.append($('<td>',{text: entry.last_modified}));
187
	gymHistory.append($('<td>',{text: gymLevel.level, 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'})));
188
	gymHistory.append($('<td>',{text: parseInt(entry.gym_points).toLocaleString('de-DE'), class: entry.class}).append(
189
		entry.gym_points_diff !== 0 ? $('<span class="small"> ('+(entry.gym_points_diff > 0 ? '+' : '')+entry.gym_points_diff+')</span>') : null
190
	));
191
	var gymPokemon = printPokemonList(entry.pokemon, pokeimg_suffix, false);
192
	gymHistory.append($('<td>').append(gymPokemon));
193
	$('#gymHistory_'+gym_id).find('tbody').append(gymHistory);
194
}
195
196
function hideGymHistoryTables(gymHistoryTables) {
197
	gymHistoryTables.removeClass('active').hide();
198
	gymHistoryTables.find('tbody tr').remove();
199
	gymHistoryTables.find('.loadMoreButtonHistory').hide();
200
	gymHistoryTables.find('.gymHistoryLoader').hide();
201
}
202
203
function printGym(gym, pokeimg_suffix, locale) {
0 ignored issues
show
Unused Code introduced by
The parameter locale is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
204
	var gymLevel = gymRanks.find(r => r.prestigeMin <= gym.gym_points && r.prestigeMax > gym.gym_points) || { level : 10 };
205
	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() {
206
		if (!$('#gymHistory_'+gym.gym_id).hasClass('active')) {
207
			hideGymHistoryTables($('#gymsContainer').find('.gymhistory'));
208
			loadGymHistory(0, gym.gym_id, pokeimg_suffix);
209
		} else {
210
			hideGymHistoryTables($('#gymHistory_'+gym.gym_id));
211
		}
212
	});
213
	gymsInfos.append($('<td>',{text: gym.last_modified}));
214
	if (gym.name.length > 50) { gym.name = gym.name.substr(0, 50) + '…'; }
215
	gymsInfos.append($('<td>',{text: gym.name}));
216
	gymsInfos.append($('<td>',{text: gymLevel.level, 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'})));
217
	gymsInfos.append($('<td>',{text: parseInt(gym.gym_points).toLocaleString('de-DE')}));
218
	var gymPokemon = printPokemonList(gym.pokemon, pokeimg_suffix, false);
219
	gymsInfos.append($('<td>').append(gymPokemon));
220
	$('#gymsContainer').append(gymsInfos);
221
	var historyTable = $('<table>',{class: 'table'});
222
	historyTable.append('<thead><tr><th style="min-width:7em">Time</th><th>Level</th><th>Prestige</th><th>Pokémon</th></tr></thead>');
223
	historyTable.append('<tbody></tbody>');
224
	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></tfoot>');
225
	historyTable.find('.loadMoreButtonHistory').data('page', 0).click(function() {
226
		loadGymHistory($(this).data('page'), gym.gym_id, pokeimg_suffix);
227
	});
228
	var row = $('<td>',{colspan: 6});
229
	row.append(historyTable);
230
	$('#gymsContainer').append($('<tr>', {id: 'gymHistory_'+gym.gym_id, class: 'gymhistory'}).hide().append(row));
231
}
232