diff --git a/code/entity_decode.js b/code/entity_decode.js new file mode 100644 index 00000000..76fdd26f --- /dev/null +++ b/code/entity_decode.js @@ -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] + }); + + } + + +})(); diff --git a/code/map_data_render.js b/code/map_data_render.js index 0f14793d..a113620b 100644 --- a/code/map_data_render.js +++ b/code/map_data_render.js @@ -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, diff --git a/code/portal_detail.js b/code/portal_detail.js index 811fcdcb..8e8f80a7 100644 --- a/code/portal_detail.js +++ b/code/portal_detail.js @@ -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);