ramonwebergmbhgmail.com / IITC plugin: Attack-Range7

// ==UserScript==

// @name           IITC plugin: Attack-Range7
// @category       Layer
// @version        0.1
// @namespace      Desktop
// @description    [moeh] Shows the maximum Attack-Range of XMP7.
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// @grant          none
// ==/UserScript==


function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};

//PLUGIN AUTHORS: writing a plugin outside of the IITC build environment? if so, delete these lines!!
//(leaving them in place might break the 'About IITC' page or break update checks)
plugin_info.buildName = 'moeh';
plugin_info.dateTimeVersion = '20141103';
plugin_info.pluginId = 'attackrange7';
//END PLUGIN AUTHORS NOTE



// PLUGIN START ///////////////////////////////////////////////////////

  // use own namespace for plugin
  window.plugin.attackrange7 = function() {};
  window.plugin.attackrange7.attackLayers = {};
  window.plugin.attackrange7.MIN_MAP_ZOOM = 14;

  window.plugin.attackrange7.portalAdded = function(data) {
    data.portal.on('add', function() {
      window.plugin.attackrange7.draw(this.options.guid, this.options.data.team);
    });

    data.portal.on('remove', function() {
      window.plugin.attackrange7.remove(this.options.guid, this.options.data.team);
    });
  }

  window.plugin.attackrange7.remove = function(guid, faction) {
    var previousLayer = window.plugin.attackrange7.attackLayers[guid];
    if(previousLayer) {
      if(faction === 'ENLIGHTENED') {
        window.plugin.attackrange7.zapCircleEnlHolderGroup.removeLayer(previousLayer);
      } else {
        window.plugin.attackrange7.zapCircleResHolderGroup.removeLayer(previousLayer);
      }
      delete window.plugin.attackrange7.attackLayers[guid];
    }
  }

  window.plugin.attackrange7.draw = function(guid, faction) {
    var d = window.portals[guid];

    if(faction !== "NEUTRAL") {
      var coo = d._latlng;
      var latlng = new L.LatLng(coo.lat,coo.lng);
      var portalLevel = d.options.level;
      var optCircle = {color:'DarkMagenta',opacity:0.7,fillColor:'DarkMagenta',fillOpacity:0.2,weight:1,clickable:false, dashArray: [10,6]};
      var range = 178;
      var circle = new L.Circle(latlng, range, optCircle);

      if(faction === 'ENLIGHTENED') {
        circle.addTo(window.plugin.attackrange7.zapCircleEnlHolderGroup);
      } else {
        circle.addTo(window.plugin.attackrange7.zapCircleResHolderGroup);
      }
      window.plugin.attackrange7.attackLayers[guid] = circle;
    }
  }

  window.plugin.attackrange7.showOrHide = function() {
    if(map.getZoom() >= window.plugin.attackrange7.MIN_MAP_ZOOM) {
      // show the layer
      if(!window.plugin.attackrange7.zapLayerEnlHolderGroup.hasLayer(window.plugin.attackrange7.zapCircleEnlHolderGroup)) {
        window.plugin.attackrange7.zapLayerEnlHolderGroup.addLayer(window.plugin.attackrange7.zapCircleEnlHolderGroup);
        $('.leaflet-control-layers-list span:contains("Attrange7 Enlightened")').parent('label').removeClass('disabled').attr('title', '');
      }
      if(!window.plugin.attackrange7.zapLayerResHolderGroup.hasLayer(window.plugin.attackrange7.zapCircleResHolderGroup)) {
        window.plugin.attackrange7.zapLayerResHolderGroup.addLayer(window.plugin.attackrange7.zapCircleResHolderGroup);
        $('.leaflet-control-layers-list span:contains("Attrange7 Resistance")').parent('label').removeClass('disabled').attr('title', '');
      }
    } else {
      // hide the layer
      if(window.plugin.attackrange7.zapLayerEnlHolderGroup.hasLayer(window.plugin.attackrange7.zapCircleEnlHolderGroup)) {
        window.plugin.attackrange7.zapLayerEnlHolderGroup.removeLayer(window.plugin.attackrange7.zapCircleEnlHolderGroup);
        $('.leaflet-control-layers-list span:contains("Attrange7 Enlightened")').parent('label').addClass('disabled').attr('title', 'Zoom in to show those.');
      }
      if(window.plugin.attackrange7.zapLayerResHolderGroup.hasLayer(window.plugin.attackrange7.zapCircleResHolderGroup)) {
        window.plugin.attackrange7.zapLayerResHolderGroup.removeLayer(window.plugin.attackrange7.zapCircleResHolderGroup);
        $('.leaflet-control-layers-list span:contains("Attrange7 Resistance")').parent('label').addClass('disabled').attr('title', 'Zoom in to show those.');
      }
    }
  }

  var setup =  function() {
    // this layer is added to the layer chooser, to be toggled on/off
    window.plugin.attackrange7.zapLayerEnlHolderGroup = new L.LayerGroup();
    window.plugin.attackrange7.zapLayerResHolderGroup = new L.LayerGroup();

    // this layer is added into the above layer, and removed from it when we zoom out too far
    window.plugin.attackrange7.zapCircleEnlHolderGroup = new L.LayerGroup();
    window.plugin.attackrange7.zapCircleResHolderGroup = new L.LayerGroup();

    window.plugin.attackrange7.zapLayerEnlHolderGroup.addLayer(window.plugin.attackrange7.zapCircleEnlHolderGroup);
    window.plugin.attackrange7.zapLayerResHolderGroup.addLayer(window.plugin.attackrange7.zapCircleResHolderGroup);

    // to avoid any favouritism, we'll put the player's own faction layer first
    if (PLAYER.team == 'RESISTANCE') {
      window.addLayerGroup('Attrange7 Resistance', window.plugin.attackrange7.zapLayerResHolderGroup, true);
      window.addLayerGroup('Attrange7 Enlightened', window.plugin.attackrange7.zapLayerEnlHolderGroup, true);
    } else {
      window.addLayerGroup('Attrange7 Enlightened', window.plugin.attackrange7.zapLayerEnlHolderGroup, true);
      window.addLayerGroup('Attrange7 Resistance', window.plugin.attackrange7.zapLayerResHolderGroup, true);
    }

    window.addHook('portalAdded', window.plugin.attackrange7.portalAdded);

    map.on('zoomend', window.plugin.attackrange7.showOrHide);

    window.plugin.attackrange7.showOrHide();
  }

// PLUGIN END //////////////////////////////////////////////////////////


setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();
} // wrapper end
// inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(document.body || document.head || document.documentElement).appendChild(script);