n00b / Oneshot expo

// ==UserScript==
// @name         Oneshot expo
// @namespace    http://tampermonkey.net/
// @version      2024-05-23
// @description  Add expedition button to the fleet page
// @author       n00b
// @copyright    2024, n00b (https://openuserjs.org/users/n00b)
// @license      MIT
// @updateURL    https://openuserjs.org/meta/n00b/Oneshot_expo.meta.js
// @downloadURL  https://openuserjs.org/install/n00b/Oneshot_expo.user.js
// @match        https://s161-nl.ogame.gameforge.com/game/index.php?page=ingame&component=fleetdispatch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gameforge.com
// ==/UserScript==

const oseSendShips = (order, galaxy, system, planet, planettype, ships, additionalParams) => {
  let params = {
    mission: order,
    galaxy: galaxy,
    system: system,
    position: planet,
    type: planettype,
    shipCount: 0,
    token: token
  }

  Object.keys(ships).forEach(function (shipId) {
    additionalParams['am' + shipId] = ships[shipId];
    fleetDispatcher.shipsOnPlanet.find(s => s.id == shipId).number -= ships[shipId];
  });

  if (additionalParams && typeof additionalParams === 'object') {
    Object.keys(additionalParams).map(key => {
      if (!params[key]) {
        params[key] = additionalParams[key]
      }
    })
  }

  $.ajax(miniFleetLink, {
    data: params,
    dataType: "json",
    type: "POST",
    success: (data) => {
      token = data.newAjaxToken;
      updateOverlayToken('phalanxSystemDialog', data.newAjaxToken);
      updateOverlayToken('phalanxDialog', data.newAjaxToken);
      const status = (data.response.success) ? "success" : "error"
      showNotification(data.response.message, status)
    }
  })
}

const sendExpedition = () => {
  if (expeditionCount == maxExpeditionCount) {
    showNotification("Already at maximum expeditions", "error")
    return
  }

  let hasErrors = false;
  let expeditionFleetTemplate, message;
  let idx = 1;
  while (true) {
    expeditionFleetTemplate = expeditionFleetTemplates.find(template => template.name === `oneshot-${idx}`)
    if (typeof expeditionFleetTemplate === undefined) {
      hasErrors = true
      message = "No oneshot expedition template found"
      break
    }

    let invalid = false
    Object.keys(expeditionFleetTemplate.ships).forEach((id) => {
      if (expeditionFleetTemplate.ships[id] > (fleetDispatcher.shipsOnPlanet.find(s => s.id == id) || {
          number: 0
        }).number) {
        invalid = true
      }
    })

    if (invalid) {
      idx++
      continue
    }

    break
  }

  if (hasErrors) {
    showNotification(message, "error")
    return
  }

  let additionalParams = {
    'speed': expeditionFleetTemplate.fleetSpeed / 10,
    'holdingtime': expeditionFleetTemplate.expeditionTime
  };

  const cp = fleetDispatcher.currentPlanet
  oseSendShips(15, cp.galaxy, cp.system, 16, 1, expeditionFleetTemplate.ships, additionalParams)
}

(function () {
  'use strict';

  const a = document.createElement("a");
  a.classList.add('expedition')
  a.classList.add('on')
  a.onclick = sendExpedition
  a.textContent = 'Send Expedition'
  a.style.fontSize = '16px'

  const fleet1 = document.querySelector("div#fleet1").append(a)

  document.onkeydown = (e) => {
    if (e.ctrlKey || e.altKey || e.metaKey) return;
    var char = e.which;
    if (!char) return;
    if (e.which == 69) {
      sendExpedition()
    }
  };
})();