move portal entity array decoding into one place, and share the code for the common array elements

TODO: move link and field entities into here too
This commit is contained in:
Jon Atkins 2015-03-20 20:54:44 +00:00
parent 5298c98fdd
commit 5e8ddb2344
3 changed files with 82 additions and 50 deletions

80
code/entity_decode.js Normal file
View File

@ -0,0 +1,80 @@
// decode the on-network array entity format into an object format closer to that used before
// makes much more sense as an object, means that existing code didn't need to change, and it's what the
// stock intel site does internally too (the array format is only on the network)
// anonymous wrapper function
(function(){
window.decodeArray = function(){};
function parseMod(arr) {
if(arr == null) { return null; }
return {
owner: arr[0],
name: arr[1],
rarity: arr[2],
stats: arr[3],
};
}
function parseResonator(arr) {
if(arr == null) { return null; }
return {
owner: arr[0],
level: arr[1],
energy: arr[2],
};
}
var summaryArrayLength = undefined;
function basePortalData(a) {
return {
// a[0] == type (always 'p')
team: a[1],
latE6: a[2],
lngE6: a[3],
level: a[4],
health: a[5],
resCount: a[6],
image: a[7],
title: a[8],
ornaments: a[9],
mission: a[10],
mission50plus: a[11]
};
};
window.decodeArray.portalSummary = function(a) {
if (!a) return undefined;
if (a[0] != 'p') throw 'Error: decodeArray.portalSUmmary - not a portal';
if (summaryArrayLength===undefined) summaryArrayLength = a.length;
if (summaryArrayLength!=a.length) console.warn('decodeArray.portalSUmmary: inconsistant map data portal array lengths');
return basePortalData(a);
}
window.decodeArray.portalDetail = function(a) {
if (!a) return undefined;
if (a[0] != 'p') throw 'Error: decodeArray.portalDetail - not a portal';
if (summaryArrayLength===undefined) throw 'Error: decodeArray.portalDetail - not yet seen any portal summary data - cannot decode!';
// the portal details array is just an extension of the portal summary array
// to allow for niantic adding new items into the array before the extended details start,
// use the length of the summary array
return $.extend(basePortalData(a),{
mods: a[summaryArrayLength+0].map(parseMod),
resonators:a[summaryArrayLength+1].map(parseResonator),
owner: a[summaryArrayLength+2]
});
}
})();

View File

@ -295,20 +295,7 @@ window.Render.prototype.createPortalEntity = function(ent) {
var latlng = L.latLng(ent[2][2]/1E6, ent[2][3]/1E6);
var data = {
// ent[2][0] is type - not needed as we know we're a portal
team: ent[2][1],
latE6: ent[2][2],
lngE6: ent[2][3],
level: ent[2][4],
health: ent[2][5],
resCount: ent[2][6],
image: ent[2][7],
title: ent[2][8],
ornaments: ent[2][9],
mission: ent[2][10],
mission50plus: ent[2][11],
};
var data = decodeArray.portalSummary(ent[2]);
var dataOptions = {
level: portalLevel,

View File

@ -32,47 +32,12 @@ window.portalDetail.isFresh = function(guid) {
var handleResponse = function(guid, data, success) {
delete requestQueue[guid];
function parseMod(arr) {
if(arr == null) { return null; }
return {
owner: arr[0],
name: arr[1],
rarity: arr[2],
stats: arr[3],
};
}
function parseResonator(arr) {
if(arr == null) { return null; }
return {
owner: arr[0],
level: arr[1],
energy: arr[2],
};
}
if (!data || data.error || !data.result) {
success = false;
}
if (success) {
var dict = {
// raw: data.result,
// result[0] is type - not needed (always a portal!)
team: data.result[1],
latE6: data.result[2],
lngE6: data.result[3],
level: data.result[4],
health: data.result[5],
resCount: data.result[6],
image: data.result[7],
title: data.result[8],
ornaments: data.result[9],
mission: data.result[10],
mission50plus: data.result[11],
mods: data.result[12].map(parseMod),
resonators:data.result[13].map(parseResonator),
owner: data.result[14],
};
var dict = decodeArray.portalDetail(data.result);
cache.store(guid,dict);