From 00523fc03d743004aad25d0e5d2d6bb7fe30b693 Mon Sep 17 00:00:00 2001 From: Morgan Jones Date: Tue, 23 Apr 2013 20:21:48 -0500 Subject: [PATCH] Completely overhauled passcode redemption. * Show table by default, offer plaintext option for reward copy-pasting * Use recommendations from https://plus.google.com/115907431535033114848/posts/PVV9j4fTR2m for item plaintext listing * Break passcode redemption responses and errors into window.REDEEM_* variables * If an item isn't known, don't simply let it slip into the Aurbis. Log it separately. * Don't show AP or XM if the passcode didn't give any. --- code/redeeming.js | 159 +++++++++++++++++++++++---------------------- code/utils_misc.js | 5 +- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/code/redeeming.js b/code/redeeming.js index 08fee9ce..e4017d73 100644 --- a/code/redeeming.js +++ b/code/redeeming.js @@ -1,79 +1,90 @@ // REDEEMING ///////////////////////////////////////////////////////// +window.REDEEM_RES_LONG = {'RES_SHIELD' : 'Portal Shield', + 'EMITTER_A' : 'Resonator', + 'EMP_BURSTER' : 'XMP Burster', + 'POWER_CUBE' : 'Power Cube'}; + +window.REDEEM_RES_SHORT = {'RES_SHIELD' : 'S', + 'EMITTER_A' : 'R', + 'EMP_BURSTER' : 'X', + 'POWER_CUBE' : 'C'}; + +window.REDEEM_ERRORS = {'ALREADY_REDEEMED' : 'The passcode has already been redeemed.', + 'ALREADY_REDEEMED_BY_PLAYER' : 'You have already redeemed this passcode.', + 'INVALID_PASSCODE' : 'This passcode is invalid.'}; + +window.REDEEM_STATUSES = {429 : 'You have been rate-limited by the server. Wait a bit and try again.'}; + window.handleRedeemResponse = function(data, textStatus, jqXHR) { - if(data.error) { - var error = ''; - if(data.error === 'ALREADY_REDEEMED') { - error = 'The passcode has already been redeemed.'; - } else if(data.error === 'ALREADY_REDEEMED_BY_PLAYER') { - error = 'You have already redeemed this passcode.'; - } else if(data.error === 'INVALID_PASSCODE') { - error = 'This passcode is invalid.'; - } else { - error = 'There was a problem redeeming the passcode. Try again?'; - } + if (data.error) { + // Errors are now in window.REDEEM_ERRORS. + var error = window.REDEEM_ERRORS[data.error] || 'There was a problem redeeming the passcode. Try again?'; + + // Show an alert and add a console log alert('' + data.error + '\n' + error); + console.log(this.passcode + ' => [ERROR] ' + data.error); } else if(data.result) { - var tblResult = $(''); - tblResult.append($('')); - - if(data.result.apAward) - tblResult.append($('')); - if(data.result.xmAward) - tblResult.append($('')); - - var resonators = {}; - var bursts = {}; - var shields = {}; - var cubes = {}; - - for(var i in data.result.inventoryAward) { - var acquired = data.result.inventoryAward[i][2]; - if(acquired.modResource) { - if(acquired.modResource.resourceType === 'RES_SHIELD') { - var rarity = acquired.modResource.rarity.split('_').map(function (i) {return i[0]}).join(''); - if(!shields[rarity]) shields[rarity] = 0; - shields[rarity] += 1; - } - } else if(acquired.resourceWithLevels) { - if(acquired.resourceWithLevels.resourceType === 'EMITTER_A') { - var level = acquired.resourceWithLevels.level - if(!resonators[level]) resonators[level] = 0; - resonators[level] += 1; - } else if(acquired.resourceWithLevels.resourceType === 'EMP_BURSTER') { - var level = acquired.resourceWithLevels.level - if(!bursts[level]) bursts[level] = 0; - bursts[level] += 1; - } else if(acquired.resourceWithLevels.resourceType === 'POWER_CUBE') { - var level = acquired.resourceWithLevels.level - if(!cubes[level]) cubes[level] = 0; - cubes[level] += 1; - } + // Successful redemption + var payload = {}; + var table_result = [''], plain_result = []; + var table = '', plain = ''; + + // Get AP, XM, and other static quantities + var scores = [[parseInt(data.result.apAward), 'AP'], [parseInt(data.result.xmAward), 'XM']]; + for (var i in scores) { + if (scores[i][0] > 0) { + table_result.push(''); + plain_result.push(scores[i][0] + ' ' + scores[i][1]); } } - - $.each(resonators, function(lvl, count) { - var text = 'Resonator'; - if(count >= 2) text += ' ('+count+')'; - tblResult.append($('')); - }); - $.each(bursts, function(lvl, count) { - var text = 'Xmp Burster'; - if(count >= 2) text += ' ('+count+')'; - tblResult.append($('')); - }); - $.each(cubes, function(lvl, count) { - var text = 'Power Cube'; - if(count >= 2) text += ' ('+count+')'; - tblResult.append($('')); - }); - $.each(shields, function(lvl, count) { - var text = 'Portal Shield'; - if(count >= 2) text += ' ('+count+')'; - tblResult.append($('')); - }); - alert(tblResult, true); + + // Track frequencies and levels of items + for (var i in data.result.inventoryAward) { + var acquired = data.result.inventoryAward[i][2], primary, secondary, type; + if (acquired.modResource) { + primary = acquired.modResource.resourceType; + secondary = acquired.modResource.rarity; + type = 'mod'; + } else if (acquired.resourceWithLevels) { + primary = acquired.resourceWithLevels.resourceType; + secondary = parseInt(acquired.resourceWithLevels.level); + type = 'leveled'; + } + + payload[primary] = payload[primary] || {}; + payload[primary][secondary] = payload[primary][secondary] || {}; + payload[primary][secondary].type = payload[primary][secondary].type || type; + payload[primary][secondary].count = payload[primary][secondary].count || 0; + payload[primary][secondary].count += 1; + } + + // Build the table and plaintext arrays + var keys = Object.keys(payload).sort(); + for (var k in keys) { + var primary = payload[keys[k]], long_name = window.REDEEM_RES_LONG[keys[k]] || keys[k], short_name = window.REDEEM_RES_SHORT[keys[k]] || '?'; + var table_array = [], plain_array = []; + for (var secondary in primary) { + var acquired = primary[secondary]; + var span_prefix = acquired.type === 'leveled' ? '' : ''; + var span_infix = acquired.type === 'leveled' ? secondary : secondary.split('_').map(function (i) {return i[0];}).join(''); + var span_suffix = '' + table_array.push(''); + plain_array.push(primary[secondary].count + '@' + (acquired.type === 'leveled' ? short_name : '') + span_prefix + span_infix + span_suffix); + } + table_result.push(table_array.join('')); + plain_result.push(plain_array.join('/')); + } + + // Add more HTML tags + plain = '' + plain_result.join('/') + ''; + table_result.push('
Passcode accepted!
+' + data.result.apAward + 'AP
+' + data.result.xmAward + 'XM
Passcode accepted!+' + scores[i][0] + ' ' + scores[i][1] + '
L' +lvl+ '' + text + '
L' +lvl+ '' + text + '
L' +lvl+ '' + text + '
'+lvl+''+text+'
' + span_prefix + (acquired.type === 'leveled' ? 'L' : '') + span_infix + span_suffix + '' + long_name + ' [' + primary[secondary].count + ']>>[plaintext]'); + table = '' + table_result.map(function(a) {return '' + a + '';}).join("\n") + '
'; + + // Display formatted versions in a table, plaintext, and the console log + alert(table, true); + console.log(this.passcode + ' => ' + $(plain).text()); } } @@ -81,16 +92,12 @@ window.setupRedeem = function() { $("#redeem").keypress(function(e) { if((e.keyCode ? e.keyCode : e.which) != 13) return; var data = {passcode: $(this).val()}; + window.postAjax('redeemReward', data, window.handleRedeemResponse, function(response) { - var extra = ''; - if(response && response.status) { - if(response.status === 429) { - extra = 'You have been rate-limited by the server. Wait a bit and try again.'; - } else { - extra = 'The server indicated an error.'; - } - extra += '\nResponse: HTTP ' + response.status + '.'; + var extra = '' + if (response.status) { + extra = (window.REDEEM_STATUSES[response.status] || 'The server indicated an error.') + ' (HTTP ' + response.status + ')'; } else { extra = 'No status code was returned.'; } diff --git a/code/utils_misc.js b/code/utils_misc.js index 84562c92..6058a864 100644 --- a/code/utils_misc.js +++ b/code/utils_misc.js @@ -78,13 +78,14 @@ window.digits = function(d) { // able arguments: http://api.jquery.com/jQuery.ajax/ // error: see above. Additionally it is logged if the request failed. window.postAjax = function(action, data, success, error) { - data = JSON.stringify($.extend({method: 'dashboard.'+action}, data)); + var post_data = JSON.stringify($.extend({method: 'dashboard.'+action}, data)); var remove = function(data, textStatus, jqXHR) { window.requests.remove(jqXHR); }; var errCnt = function(jqXHR) { window.failedRequestCount++; window.requests.remove(jqXHR); }; var result = $.ajax({ url: '/rpc/dashboard.'+action, type: 'POST', - data: data, + data: post_data, + context: data, dataType: 'json', success: [remove, success], error: error ? [errCnt, error] : errCnt,