tUrG0n / Fleet Management

// ==UserScript==
// @name         Fleet Management
// @namespace    http://tampermonkey.net/
// @version      0.4.4
// @description  Automate management of your ogame fleets
// @author       Piotr Yordanov
// @grant        none
// @require      https://raw.githubusercontent.com/caolan/async/master/dist/async.min.js
// @require      https://raw.githubusercontent.com/piotryordanov/ogame/master/sendFleet.js

// @include     *.ogame*gameforge.com/game/index.php*
// @exclude      http://127.0.0.1:3000/


// ==/UserScript==

(function() {
  'use strict';
  // Use this to make it automatic to attack spied Resources
  function waitForElementToDisplay(selector, time) {
    if(document.querySelector(selector)!=null) {
      // Create space for the last Icon
      $($(".spyTableIcon").parent()[1]).width(110);
      var spiedTargets = $(".spyTableIcon").parent()
      var addButton = function(target, cb) {
        var href = $($(target).children()[4]).attr('href');
        $(target).append('<a href=' + href + ' class="spyTableIcon autoAttackIcon">A</a>')
        cb()
      }
      async.eachSeries(spiedTargets, addButton, function(err, result){
        $( ".autoAttackIcon" ).click(function(event) {
          event.preventDefault();
          $("#fleetManagementBox").show()
          var href = $(this).attr('href');
          var originalCoords = href.split("fleet1")[1];
          var sendingFleet = {
            "sendFleetData": [{
              "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet1",
              "data": ""
            }, {
              "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet2",
              "data": "&am203=929&am204=&am205=&am206=&am207=&am208=&am209=&am210=&am211=&am213=&mission=1&speed=10&type=1" + originalCoords
            }, {
              "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet3",
              "data": "acsValues=-&mission=1&speed=10&type=1&union=0" + originalCoords
            }, {
              "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=movement",
              "data": "acsValues=-&expeditiontime=1&holdingOrExpTime=0&holdingtime=1&mission=1&prioCrystal=2&prioDeuterium=3&prioMetal=1&speed=10&token=10b3c1cf0bb6b890e6aa57c3043ef788&type=1&union2=0" + originalCoords
            }
          ]};
          sendingFleet.step = 1;
          var element = $(this).parent()
          nextDispatchCb = function() {
            $("#fleetManagementBox").hide()
            $($(element).children()[4]).addClass('attacking');
          };
          $("#fleetManagementBox").text("Dispatching the Attack...")

          var XMLcallback = function(response) {
            SendFleet(response, sendingFleet)
          }
          PostXMLHttpRequest(sendingFleet.sendFleetData[0].url,sendingFleet.sendFleetData[0].data,XMLcallback);
        });
      });
      return;
    }
    else {
      setTimeout(function() {
        waitForElementToDisplay(selector, time);
      }, time);
    }
  }
  waitForElementToDisplay("#agoSpyReportOverview")


  // Bouton options
  var aff_option = '<span class="menu_icon"></span><a id="turgon" class="menubutton "';
  aff_option += 'href="#" accesskey="" target="_self">';
  aff_option += '<span  class="textlabel">Send Resources</span></a>';

  var tableau = document.createElement("li");
  tableau.innerHTML = aff_option;
  tableau.id = 'optionTopRaider';
  document.getElementById('menuTableTools').appendChild(tableau);//,
  /* Dialog Box Showing the progress of each dispatch */
  var aff2 = '<div id="fleetManagementBox" style="padding:10px;z-index: 10000;width:410px;position: fixed; bottom: 40%; left: 40%; border-radius: 5px; background-color: rgba(0,0,0,0.01); background-repeat: repeat-x; background-position: 0 -225px; border-right: 1px solid #000; border-left: 1px solid #000; height: 7px;">Sending Resources<center></center></div>';
  var newElement3 = document.createElement("div"); // On crée un nouvelle élément div
  newElement3.innerHTML = aff2;
  if (document.getElementById('boxBG')) {
    document.getElementById('boxBG').appendChild(newElement3);
  } else {
    document.getElementById('empire').appendChild(newElement3);
  }
  $("#fleetManagementBox").css('background-image', 'url(http://gf3.geo.gfsrv.net/cdne1/d03835718066a5a592a6426736e019.png)');
  $("#fleetManagementBox").hide()

  document.getElementById('turgon').addEventListener("click", function (event) {
    $("#fleetManagementBox").show()
    var koord = $($('.planet-koords')[0]).text();
    var system = koord.split(":")[1];
    var position = koord.split(":")[2].split("]")[0];
    var galaxy = koord.split(":")[0].split("[")[1];

    var sendingFleet = {};
    var planets = $(".smallplanet");
    planets.splice(0,1);
    curr = 1
    var total = planets.length
    var dispatchResources = function(planet, callback) {
      $("#fleetManagementBox").text("Sending Resources: " + curr + "/" + total + ". Do NOT refresh the page!")
      var id = $(planet).attr('id').split("-")[1];
      var url = "https://s127-fr.ogame.gameforge.com/game/index.php?page=overview&cp=" + id
      PostXMLHttpRequest(url,[],function(response){
        var crystal = parseFloat($(response).find('#resources_crystal').text().replace(/\./g, ''))
        var metal = parseFloat($(response).find('#resources_metal').text().replace(/\./g, ''))
        var deut = parseFloat($(response).find('#resources_deuterium').text().replace(/\./g, ''))
        var reqTranspo = Math.ceil((crystal + metal + deut) / 25000)
        sendingFleet = {
          "sendFleetData": [{
            "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet1&cp=" + id,
            "data": ""
          }, {
            "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet2",
            "data": "am202=&am203=929&am204=&am205=&am206=&am207=&am208=&am209=&am210=&am211=&am213=&galaxy=" + galaxy + "&mission=3&position=" + position + "&speed=10&system=" + system + "&type=1"
          }, {
            "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=fleet3",
            "data": "acsValues=-&am203=929&galaxy=" + galaxy + "&mission=3&position=" + position + "&speed=10&system=" + system + "&type=1&union=0"
          }, {
            "url": "https://s127-fr.ogame.gameforge.com/game/index.php?page=movement",
            "data": "acsValues=-&am203=" + reqTranspo + "&crystal=" + crystal + "&deuterium=" + deut + "&expeditiontime=1&galaxy=" + galaxy + "&holdingOrExpTime=0&holdingtime=1&metal=" + metal + "&mission=3&position=" + position + "&prioCrystal=2&prioDeuterium=3&prioMetal=1&speed=10&system=" + system + "&token=10b3c1cf0bb6b890e6aa57c3043ef788&type=1&union2=0"
          }
        ]};
        sendingFleet.step = 1;
        nextDispatchCb = callback;

        var XMLcallback = function(response) {
          SendFleet(response, sendingFleet)
        }
        PostXMLHttpRequest(sendingFleet.sendFleetData[0].url,sendingFleet.sendFleetData[0].data,XMLcallback);
      });
    };

    async.eachSeries(planets, dispatchResources, function(err, result){
      location.reload()
    });


  }, true);
})();