From 570f7a0f9a16420ec760840cfcca5eb6a23c4429 Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Mon, 29 Jun 2015 19:02:11 +0100 Subject: [PATCH] plugin: cache recently loaded portal details and always render on the map --- code/hooks.js | 3 +- code/map_data_request.js | 3 ++ plugins/cache-details-on-map.user.js | 62 ++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 plugins/cache-details-on-map.user.js diff --git a/code/hooks.js b/code/hooks.js index 2fd82477..9cdd428d 100644 --- a/code/hooks.js +++ b/code/hooks.js @@ -18,6 +18,7 @@ // portalSelected: called when portal on map is selected/unselected. // Provide guid of selected and unselected portal. // mapDataRefreshStart: called when we start refreshing map data +// mapDataEntityInject: called just as we start to render data. has callback to inject cached entities into the map render // mapDataRefreshEnd: called when we complete the map data load // portalAdded: called when a portal has been received and is about to // be added to its layer group. Note that this does NOT @@ -55,7 +56,7 @@ window._hooks = {} window.VALID_HOOKS = [ 'portalSelected', 'portalDetailsUpdated', - 'mapDataRefreshStart', 'mapDataRefreshEnd', + 'mapDataRefreshStart', 'mapDataEntityInject', 'mapDataRefreshEnd', 'portalAdded', 'linkAdded', 'fieldAdded', 'publicChatDataAvailable', 'factionChatDataAvailable', 'requestFinished', 'nicknameClicked', diff --git a/code/map_data_request.js b/code/map_data_request.js index 29b44532..514eabd8 100644 --- a/code/map_data_request.js +++ b/code/map_data_request.js @@ -239,6 +239,9 @@ window.MapDataRequest.prototype.refresh = function() { this.render.startRenderPass(tileParams.level, dataBounds); + var _render = this.render; + window.runHooks ('mapDataEntityInject', {callback: function(ents) { _render.processGameEntities(ents);}}); + this.render.processGameEntities(artifact.getArtifactEntities()); diff --git a/plugins/cache-details-on-map.user.js b/plugins/cache-details-on-map.user.js new file mode 100644 index 00000000..63a83111 --- /dev/null +++ b/plugins/cache-details-on-map.user.js @@ -0,0 +1,62 @@ +// ==UserScript== +// @id iitc-plugin-cache-details-on-map@jonatkins +// @name IITC plugin: Cache viewed portal details and always show them on the map +// @category Cache +// @version 0.1.0.@@DATETIMEVERSION@@ +// @namespace https://github.com/jonatkins/ingress-intel-total-conversion +// @updateURL @@UPDATEURL@@ +// @downloadURL @@DOWNLOADURL@@ +// @description [@@BUILDNAME@@-@@BUILDDATE@@] Cache the details of recently viewed portals and use this to populate the map when possible +// @include https://www.ingress.com/intel* +// @include http://www.ingress.com/intel* +// @match https://www.ingress.com/intel* +// @match http://www.ingress.com/intel* +// @include https://www.ingress.com/mission/* +// @include http://www.ingress.com/mission/* +// @match https://www.ingress.com/mission/* +// @match http://www.ingress.com/mission/* +// @grant none +// ==/UserScript== + +@@PLUGINSTART@@ + +// PLUGIN START //////////////////////////////////////////////////////// + + +// use own namespace for plugin +window.plugin.cachePortalDetailsOnMap = function() {}; + +window.plugin.cachePortalDetailsOnMap.MAX_AGE = 12*60*60; //12 hours max age for cached data + +window.plugin.cachePortalDetailsOnMap.portalDetailLoaded = function(data) { + window.plugin.cachePortalDetailsOnMap.cache[data.guid] = { loadtime: Date.now(), ent: data.ent }; +}; + +window.plugin.cachePortalDetailsOnMap.entityInject = function(data) { + var maxAge = Date.now() - window.plugin.cachePortalDetailsOnMap.MAX_AGE*1000; + + var ents = []; + for (var guid in window.plugin.cachePortalDetailsOnMap.cache) { + if (window.plugin.cachePortalDetailsOnMap.cache[guid].loadtime < maxAge) { + window.plugin.cachePortalDetailsOnMap.cache.delete(guid); + } else { + ents.push(window.plugin.cachePortalDetailsOnMap.cache[guid].ent); + } + } + data.callback(ents); +}; + + +window.plugin.cachePortalDetailsOnMap.setup = function() { + + window.plugin.cachePortalDetailsOnMap.cache = {}; + + addHook('portalDetailLoaded', window.plugin.cachePortalDetailsOnMap.portalDetailLoaded); + addHook('mapDataEntityInject', window.plugin.cachePortalDetailsOnMap.entityInject); +}; + +var setup = window.plugin.cachePortalDetailsOnMap.setup; + +// PLUGIN END ////////////////////////////////////////////////////////// + +@@PLUGINEND@@