Failed Conditions
Pull Request — master (#283)
by
unknown
02:46
created

gymhistory.content.js ➔ ... ➔ historyTable.click   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
			}
88
			else{
89
				window.history.back();
90
			}
91
		};
92
93
	});
94
});
95
96
function loadGyms(page, name, teamSelector, rankingFilter, pokeimg_suffix, stayOnPage) {
97
	$('.gymLoader').show();
98
99
	if (stayOnPage) {
100
		// build a state for this name
101
		var state = {name: name, page: 'Gym History'};
102
		window.history.pushState(state, 'Gym History', 'gymhistory?name=' + name);
103
	}
104
	$.ajax({
105
		'async': true,
106
		'type': "GET",
107
		'global': false,
108
		'dataType': 'json',
109
		'url': "core/process/aru.php",
110
		'data': {
111
			'request': '',
112
			'target': 'arrange_url',
113
			'method': 'method_target',
114
			'type' : 'gyms',
115
			'page' : page,
116
			'name' : name,
117
			'team' : teamSelector,
118
			'ranking' :rankingFilter
119
		}
120
	}).done(function (data) {
121
		var internalIndex = 0;
122
		$.each(data.gyms, function (idx, gym) {
123
			internalIndex++
124
			printGym(gym, pokeimg_suffix, data.locale);
125
		});
126
		if (internalIndex < 10) {
127
			$('#loadMoreButton').hide();
128
		} else {
129
			$('#loadMoreButton').removeClass('hidden');
130
			$('#loadMoreButton').show();
131
		}
132
		$('.gymLoader').hide();
133
	});
134
}
135
136
function loadGymHistory(page, gym_id, pokeimg_suffix) {
137
	$('.gymLoader').show();
138
139
	$.ajax({
140
		'async': true,
141
		'type': "GET",
142
		'global': false,
143
		'dataType': 'json',
144
		'url': "core/process/aru.php",
145
		'data': {
146
			'request': '',
147
			'target': 'arrange_url',
148
			'method': 'method_target',
149
			'type' : 'gymhistory',
150
			'page' : page,
151
			'gym_id' : gym_id
152
		}
153
	}).done(function (data) {
154
		var internalIndex = 0;
155
		$.each(data.entries, function(idx, entry) {
156
			internalIndex++
157
			printGymHistory(gym_id, entry, pokeimg_suffix, data.locale);
158
		});
159
		$('#gymHistory_'+gym_id).addClass('active').show();
160
		if (internalIndex < 10) {
161
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').hide();
162
		} else {
163
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').removeClass('hidden');
164
			$('#gymHistory_'+gym_id).find('.loadMoreButtonHistory').data('page', page+1).show();
165
		}
166
		$('.gymLoader').hide();
167
	});
168
}
169
170
function printPokemonList(pokemons, pokeimg_suffix, hide_unchanged) {
171
	var gymPokemon = $('<ul>',{class: 'list-inline'});
172
	$.each(pokemons, function(idx, pokemon) {
173
		if (!hide_unchanged || pokemon.class) {
174
			var list = $('<li>', {class: pokemon.class});
175
			list.append($('<a>', { class: 'no-link', href : 'pokemon/'+pokemon.pokemon_id }).append($('<img />', { src: 'core/pokemons/'+pokemon.pokemon_id+pokeimg_suffix }).css('height', '2em')));
176
			list.append($('<br><span class="small">'+pokemon.cp+' CP</span>'));
177
			list.append($('<br><span style="font-size:70%"><a href="trainer?name='+pokemon.trainer_name+'" class="no-link">'+pokemon.trainer_name+'</a></span>'));
178
			gymPokemon.append(list);
179
		}
180
	});
181
	return gymPokemon;
182
}
183
184
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...
185
	var gymLevel = gymRanks.find(r => r.prestigeMin <= entry.gym_points && r.prestigeMax > entry.gym_points) || { level : 10 };
186
	var gymHistory = $('<tr>').css('border-bottom', '2px solid '+(entry.team_id=='3'?'#ffbe08':entry.team_id=='2'?'#ff7676':entry.team_id=='1'?'#00aaff':'#ddd'));
187
	gymHistory.append($('<td>',{text: entry.last_modified}));
188
	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'})));
189
	gymHistory.append($('<td>',{text: parseInt(entry.gym_points).toLocaleString('de-DE'), class: entry.class}).append(
190
		entry.gym_points_diff !== 0 ? $('<span class="small"> ('+(entry.gym_points_diff > 0 ? '+' : '')+entry.gym_points_diff+')</span>') : null
191
	));
192
	var gymPokemon = printPokemonList(entry.pokemon, pokeimg_suffix, false);
193
	gymHistory.append($('<td>').append(gymPokemon));
194
	$('#gymHistory_'+gym_id).find('tbody').append(gymHistory);
195
}
196
197
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...
198
	var gymLevel = gymRanks.find(r => r.prestigeMin <= gym.gym_points && r.prestigeMax > gym.gym_points) || { level : 10 };
199
	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() {
200
		if (!$('#gymHistory_'+gym.gym_id).hasClass('active')) {
201
			$('#gymsContainer').find('.gymhistory').removeClass('active').hide().find('tbody tr').remove();
202
			loadGymHistory(0, gym.gym_id, pokeimg_suffix);
203
		} else {
204
			$('#gymHistory_'+gym.gym_id).removeClass('active').hide().find('tbody tr').remove();
205
		}
206
	});
207
	gymsInfos.append($('<td>',{text: gym.last_modified}));
208
	if (gym.name.length > 50) gym.name = gym.name.substr(0, 50) + '…';
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...
209
	gymsInfos.append($('<td>',{text: gym.name}));
210
	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'})));
211
	gymsInfos.append($('<td>',{text: parseInt(gym.gym_points).toLocaleString('de-DE')}));
212
	var gymPokemon = printPokemonList(gym.pokemon, pokeimg_suffix, false);
213
	gymsInfos.append($('<td>').append(gymPokemon));
214
	$('#gymsContainer').append(gymsInfos);
215
	var historyTable = $('<table>',{class: 'table'});
216
	historyTable.append('<thead><tr><th style="min-width:7em">Time</th><th>Level</th><th>Prestige</th><th>Pokémon</th></tr></thead>');
217
	historyTable.append('<tbody></tbody>');
218
	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>');
219
	historyTable.find('.loadMoreButtonHistory').data('page', 0).click(function() {
220
		loadGymHistory($(this).data('page'), gym.gym_id, pokeimg_suffix);
221
	});
222
	var row = $('<td>',{colspan: 6});
223
	row.append(historyTable);
224
	$('#gymsContainer').append($('<tr>', {id: 'gymHistory_'+gym.gym_id, class: 'gymhistory'}).hide().append(row));
225
}
226