This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
/
mobiletrib.js
468 lines (421 loc) · 14.4 KB
/
mobiletrib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* Copyright (c) 2012 kayson (kaysond) & Noah Manneschmidt (psoplayer)
* https://github.com/noahm/MobileTribunal
*
* Licensed under the MIT License.
* https://raw.github.com/noahm/MobileTribunal/master/mit-license.txt
*/
// Array Remove - By John Resig (MIT Licensed) http://ejohn.org/blog/javascript-array-remove/
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30304199-1']);
_gaq.push(['_trackPageview']);
$(function() {
// in case there is an updated version
if (window.applicationCache) {
window.applicationCache.addEventListener('updateready', onUpdateReady);
if (window.applicationCache.status === window.applicationCache.UPDATEREADY) {
onUpdateReady();
}
}
// init the login form
$('#login form').submit(submitLogin);
$('#realm').val($.store.get('realm'));
$('#username').val($.store.get('username'));
$.store.clear('password'); // just in case they used the original version
//handle recaptcha display
$('#realm').change(function() { displayRecaptcha();});
// logout handler
$('#logout').click(doLogout);
// handle opening and closing the menu
$('#game-selected').tappable(function() {
$('#games').toggle();
});
// handle switching games
$(document).on('click', '#games li', function(event) {
$('#games').hide();
loadGame(this.value);
});
// handle showing the verdict options
$('#verdict').tappable(function() {showOnly('submit');});
$('#return').tappable(function() {showOnly('game');});
// handle showing inventory details
$(document).on('click', '#inventory img', function() {
var data = $(this).data('info');
// TODO replace this alert with a popover div that looks nice
alert(data.name + "\n" + data.description);
});
// handle chat log filters
$('#chat-filter').change(function() {
$('#chat').removeClass('only-all hide-enemy hide-allied only-reported').addClass(this.value);
});
// handle refreshing captcha
//$('#refresh-captcha').click(reloadCaptcha);
// handle submitting a verdict
$('#pardon,#punish').click(function() {
if (timeLeft > 0) return alert('Please spend more time reviewing the case');
if ($(this).is('[disabled]')) return;
showOnly('loading');
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: { cmd: 'sendVerdict', verdict: this.id, "captcha-result": "" }, //$('#captcha-result').val() },
success: processCaseSubmissionResult
});
});
// handle the skip button
$('#skip').click(function() {
showOnly('loading');
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: { cmd: 'sendSkip' },
success: processCaseSubmissionResult
});
});
//Initial ajax request
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: { cmd: 'getCase' },
success: function (data) {
if (data.status === 'failed' || data.status === 'nosess') {
displayRecaptcha();
return showOnly('login');
}
if ( data.status === 'finished' ) {
return showOnly('finished');
}
if ( data.status === 'ok' ) {
loadCase(data);
}
}
});
});
function onUpdateReady() {
if (confirm('An update is available. Press OK to reload this page and apply the update.')) window.location.reload();
}
function urlPrefix() {
if (!window.imgPrefix) {
window.imgPrefix = 'http://' + $('#realm').val() + '.leagueoflegends.com';
}
return window.imgPrefix;
}
function formatImageUrl(url) {
return url.replace('/tribunal/bundles/riothelper/game_data/3.03.0.1/', urlPrefix()+'/tribunal/bundles/riothelper/game_data/3.03.0.1/');
}
// shows only one component of the app and hides all the others
// nothing is hidden if called for an element that isn't a major part of the game
function showOnly(elemId) {
var mainPageItems = ['login', 'game', 'submit', 'finished', 'recess', 'underlevel', 'unknown'];
var alwaysHide = ['loading', 'games'];
$('#'+elemId).show();
$('.req-by-'+elemId).show(); // show anything required by this item
$('.exc-by-'+elemId).hide(); // hide anything excluded by this item
if ((i = mainPageItems.indexOf(elemId)) >= 0) { // was one of the main page items
// hide other main page items
mainPageItems.splice(i,1);
$('#'+mainPageItems.join(',#')).hide();
// regular page maintence
$('#'+alwaysHide.join(',#')).hide();
window.scroll(0,0);
} else if (elemId === 'loading') window.scroll(0,0);
}
function doLogout() {
_gaq.push(['_trackEvent', 'User', 'Logout', 'Manual']);
showOnly('loading');
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: {cmd: 'logout'},
success: function() {
showOnly('login');
}
});
}
function submitLogin(event) {
event.preventDefault();
showOnly('loading');
// perform the saving of inputs
$.store.set('realm', $('#realm').val());
$.store.set('username', $('#username').val());
// submit the login
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: {
cmd: 'login',
username: $('#username').val(),
password: $('#password').val(),
realm: $('#realm').val(),
recaptcha_response: $('#recaptcha_response').val(),
recaptcha_challenge: $('#recaptcha_challenge').val()
},
success: processLoginResult
});
$('#password').val('');
}
function processLoginResult(data) {
if (data.status === 'ok') {
_gaq.push(['_trackEvent', 'User', 'Login '+$('#realm').val(), 'Success']);
showOnly('game');
loadCase(data);
} else if (data.status === 'error') {
// put each elemnt of response.feedback as a paragraph in #feedback
$('#feedback').html(data.feedback.join('<br>'));
if( $('#realm').val() == 'na' )
loadRecaptcha();
showOnly('login');
} else {
showOnly(data.status);
}
}
function processCaseSubmissionResult(data) {
showOnly('submit');
if (data.status === 'failed') {
_gaq.push(['_trackEvent', 'Case', 'Submit', 'Comm Error']);
alert('Error communicating with Riot servers');
}
else if (data.status === 'captchafail') {
<<<<<<< HEAD
_gaq.push(['_trackEvent', 'Case', 'Submit', 'Captcha Failed']);
alert('Incorrect captcha');
}
else if (data.status === 'finished') {
_gaq.push(['_trackEvent', 'Case', 'Submit', 'Accepted']);
_gaq.push(['_trackEvent', 'User', 'Cases Exhausted']);
showOnly('finished'); // TODO have a button to retry that checks if you are still expired
}
else if (data.status === 'nosess') {
_gaq.push(['_trackEvent', 'User', 'Logout', 'Timed out']);
showOnly('login'); // TODO show login form instead of reloading
}
else if (data.status === 'ok') {
_gaq.push(['_trackEvent', 'Case', 'Submit', 'Accepted']);
=======
alert('Incorrect captcha');
}
else if (data.status === 'finished') {
showOnly('finished'); // TODO have a button to retry that checks if you are still expired
}
else if (data.status === 'nosess') {
showOnly('login'); // TODO show login form instead of reloading
}
else if (data.status === 'ok') {
>>>>>>> master
loadCase(data);
}
}
function reloadCaptcha() {
if (!window.captchaIsLoading) {
window.captchaIsLoading = true;
$.ajax({
type: 'POST',
dataType: 'text',
url: 'ajax.php',
data: { cmd: 'getCaptcha' },
success: function(data) {
$('#captcha').attr('src',data);
$('#captcha-result').val('');
window.captchaIsLoading = false;
window.captchaLoaded = true;
}
});
}
}
function loadRecaptcha() {
$('#recaptcha_img').attr('src','');
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: { cmd: 'getRecaptcha' },
success: function(data) {
$('#recaptcha_img').attr('src',data.image);
$('#recaptcha_challenge').val(data.challenge);
$('#recaptcha_response').val('');
}
});
}
function displayRecaptcha() {
if( $('#realm').val() == 'na' ) {
loadRecaptcha();
$('#recaptcha').show();
}
else
$('#recaptcha').hide();
}
function loadCase(data) {
showOnly('loading');
//window.captchaLoaded = false;
//window.captchaIsLoading = false;
window.cachedGames = {};
// handle verdict timer
$('#timer-message').show();
window.timeLeft = 20;
if (!window.timerInterval)
window.timerInterval = window.setInterval(timerTick, 1000);
$('#captcha-result').val('');
var num = Number(data.numGames);
if (num < 1) {
showOnly('login');
return alert('Could not get case data from Riot');
}
// create the list of games
$('#games').empty();
for (var i=1; i<=num; i++) {
$('<li onclick="void(0)"></li>').attr('value',i).html('Game '+i).appendTo('#games');
}
$('#caseid').html(data['case']);
loadGame('1');
// if we are loading for the first time, grab a new captcha in the background
//reloadCaptcha();
}
function timerTick() {
if ($('#game').is(':visible')) window.timeLeft -= 1;
if (window.timeLeft >= 1) {
$('#timer-message time').text(window.timeLeft);
$('#verdict time').text('in '+window.timeLeft+'s');
$('#pardon,#punish').attr('disabled', true);
} else {
window.clearInterval(window.timerInterval);
delete window.timerInterval;
$('#timer-message').hide();
$('#verdict time').text('now');
$('#pardon,#punish').attr('disabled', false);
}
}
function loadGame(gameNumber) {
showOnly('loading');
$('#game-selected').html('Game '+gameNumber);
if (!window.cachedGames[gameNumber]) {
_gaq.push(['_trackEvent', 'Case', 'Load Game', 'Uncached', gameNumber]);
$.ajax({
type: 'POST',
dataType: 'json',
url: 'ajax.php',
data: { cmd: 'getGame', game: gameNumber },
success: function(gameData) {
if ( gameData.status === 'nosess' ) return showOnly('login');
applyData(initData(gameData, gameNumber));
}
});
} else {
_gaq.push(['_trackEvent', 'Case', 'Load Game', 'Cached', gameNumber]);
applyData(window.cachedGames[gameNumber]);
}
}
// performs some parsing and caches the result of a fetched game
function initData(gameData, gameNumber) {
//quick fix: gameData.offender is now an element of gameData.players, so we'll find the offender and recreate gameData.offender
for( var i = 0; i < gameData.players.length; i++ ) {
if( gameData.players[i].association_to_offender == 'offender' )
gameData.offender = gameData.players[i];
}
// calculate a regular timestamp
var minutes = gameData.offender.time_played % 60;
if (minutes < 10) minutes = '0' + minutes;
gameData.time_played = Math.floor(gameData.offender.time_played / 60) + ':' + minutes;
// cache the fixed data
window.cachedGames[gameNumber] = gameData;
// apply champion portrait in games list - removed because riot doesn't send individual images anymore
//$('#games img')[gameNumber-1].src = formatImageUrl(gameData.offender.champion_url);
return gameData;
}
function applyData(gameData) {
var i, item;
// expand the data into the #game div
$('#portrait-img').attr('style', gameData.offender.champion_icon);
$('#champname span').text(gameData.offender.champion_name);
$('#summoner1').attr('style', gameData.offender.summoner_spell_1_icon);
$('#summoner2').attr('style', gameData.offender.summoner_spell_2_icon);
$('#level').text(gameData.offender.level);
$('#time').text(gameData.time_played);
// we should probably display different stats if dominion
$('#kills').text(gameData.offender.scores.kills);
$('#deaths').text(gameData.offender.scores.deaths);
$('#assists').text(gameData.offender.scores.assists);
$('#outcome').text(gameData.offender.outcome);
$('#minion-kills').text(gameData.offender.minions_killed);
$('#gold-earned').text(gameData.offender.gold_earned);
$('#date-played').text(gameData.game_creation_time);
$('#dps-out').text(gameData.offender.total_damage_dealt);
$('#dps-in').text(gameData.offender.total_damage_received);
// setup inventory-container
$('#inventory-container').empty();
for (i=0; i<gameData.offender.item_icons.length; i++) {
item = gameData.offender.item_icons[i];
$('<div>')
.attr('style', item)
.appendTo('#inventory-container');
}
//display teammates
$('#teammates tbody').empty();
for (var playerid in gameData.players) {
var player = gameData.players[playerid];
if (player.association_to_offender === 'ally') {
var teammate = $('#teammates thead tr.template').clone();
teammate.removeClass('template').addClass('teammate');
teammate.find('.port div').attr('style', player.champion_icon);
teammate.find('.level').html(player.level);
teammate.find('.champname').html(player.champion_name);
teammate.find('.score').html(player.scores.kills +'/'+ player.scores.deaths +'/'+ player.scores.assists);
teammate.find('.summ1').attr('style',player.summoner_spell_1_icon);
teammate.find('.summ2').attr('style',player.summoner_spell_2_icon);
teammate.find('.gold').html(player.gold_earned > 1000 ? (Math.round(player.gold_earned / 1000) + 'k') : player.gold_earned);
teammate.find('.cs').html(player.minions_killed);
for (i = player.item_icons.length - 1; i >= 0; i--) {
item = teammate.find('.item'+i);
item.attr('style', player.item_icons[i]);
}
teammate.appendTo('#teammates tbody');
}
}
// add reports
$('#reports').empty();
$('#allied-report-count').html(gameData.allied_report_count);
$('#enemy-report-count').html(gameData.enemy_report_count);
for (i = gameData.reports.length - 1; i >= 0; i--) {
var report = gameData.reports[i];
item = $('<li></li>');
item.addClass(report.association_to_offender);
item.html(report.comment).append($('<h2></h2>').html(gameData.translated_report_reasons[report.offense]));
if (report.association_to_offender === 'ally') {
item.prependTo('#reports');
} else {
item.appendTo('#reports');
}
}
// build chat log
var $chat = $('#chat').empty();
for (i = gameData.chat_log.length - 1; i >= 0; i--) {
var chat_line = gameData.chat_log[i];
var classes = chat_line.association_to_offender;
if (classes === 'offender') classes += ' ally';
if (chat_line.sent_to === 'All') classes += ' all';
$('<li></li>').addClass(classes)
.append(
$('<span class="author"></span>')
.append($('<span class="character"></span>').text(chat_line.champion_name))
)
.append($('<time></time>').text(chat_line.time))
.append(': '+chat_line.message)
.prependTo($chat);
}
// reset chat filter controls
$('#chat-filter')[0].selectedIndex = 0;
$('#chat-filter').change();
// show our handywork
showOnly('game');
}
function disclaim() {
alert("Mobile Tribunal is provided as is with no guarantees as to its functionality. While every effort is made to protect your information (all connections are encrypted with SSL), the Mobile Tribunal is not responsible for theft of your data, including, but not limited to, usernames and passwords.");
}