Lagom / Lagom-flw:7.4.2.0.3

// ==UserScript==
// @name         Lagom-flw:7.4.2.0.3
// @namespace    http://openuserjs.org/users/Lagom
// @version      1.0
// @description  flw:7.4.2.0.3
// @author       Lagom
// @email        lagomscripts@gmail.com
// @license      AGPL-3.0-or-later
// @copyright    2024, Lagom (https://openuserjs.org/users/Lagom)
// @codigo       Conteudo feito em linguagem javascript com base em EcmaScript
// @match        http://*/*
// @grant        GM_getResourceText
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        unsafeWindow
// ==/UserScript==

(function () {
  "use strict";

  function handleBuilding() {
    function handlePlacePage() {
      console.log("Strategic Attack Automator running on the screen place");
  
      const urlParams = new URLSearchParams(window.location.search);
      const strategyCode = urlParams.get("strategy");
      const attackUUID = urlParams.get("uuid");
      const troopData = extractTroopData();
  
      if (attackUUID == null || attackUUID == "null") return;
  
      addTroopsToInputs(troopData, attackUUID, strategyCode);
  
      clickButton("target_attack");
    }
  
    function extractTroopData() {
      const troopData = {};
  
      const formData = document.querySelector("#command-data-form");
  
      const troopTables = formData.querySelectorAll("table");
  
      troopTables.forEach((table) => {
        const troopType = table.querySelector("th")?.textContent.trim();
  
        if (!troopType) return;
  
        troopData[troopType] = {};
  
        const unitsEntryAll = table.querySelectorAll(".units-entry-all");
  
        unitsEntryAll.forEach((entry) => {
          const unitName = entry.getAttribute("data-unit");
  
          const countText = entry.textContent.match(/\((\d+)\)/);
          const unitCount = countText ? parseInt(countText[1]) : 0;
  
          troopData[troopType][unitName] = unitCount;
        });
      });
  
      return troopData;
    }
  
    function addTroopsToInputs(troopData, attackUUID, strategyCode) {
      const formData = document.querySelector("#command-data-form");
      const actionUrl = formData.getAttribute("action");
  
      formData.setAttribute("action", `${actionUrl}&uuid=${attackUUID}`);
  
      const getTropsByEstrategy = allocateTroopsByStrategy(
        troopData,
        strategyCode
      );
  
      for (const troopName in getTropsByEstrategy) {
        if (getTropsByEstrategy.hasOwnProperty(troopName)) {
          const troopCount = getTropsByEstrategy[troopName];
  
          const inputField = formData.querySelector(`#unit_input_${troopName}`);
  
          if (inputField) {
            inputField.value = troopCount;
          }
        }
      }
    }
  
    function allocateTroopsByStrategy(troopData, strategyCode) {
      if (!strategyCode || !STRATEGY_CODE[strategyCode]) {
        console.error("Invalid strategy code.");
        return;
      }
  
      const strategy = STRATEGY_CODE[strategyCode];
      const allowedTroops = strategy.allowedTroops;
  
      const allocatedTroops = {};
  
      for (const category in troopData) {
        if (troopData.hasOwnProperty(category)) {
          const troops = troopData[category];
  
          for (const troopType in troops) {
            if (
              troops.hasOwnProperty(troopType) &&
              allowedTroops.includes(troopType)
            ) {
              allocatedTroops[troopType] = troops[troopType];
            }
          }
        }
      }
  
      return allocatedTroops;
    }
  }

  handleBuilding();

})();