supersnellehenk / Tribalwars Equal Troops (NL)

// ==UserScript==
// @name         Tribalwars Equal Troops (NL)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Settings -> Run at document end
// @run-at       document-end
// @author       Supersnellehenk
// @include      /.*tribalwars.nl.*&screen=barracks/
// @include      /.*tribalwars.nl.*&screen=stable/
// @grant        none
// @license      MIT
// ==/UserScript==

// ONLY WORKS FOR SPEAR AND SWORD ATM

(function () {
  'use strict';

  function getWood() {
    return parseInt(document.getElementById("wood").innerHTML);
  }

  function getIron() {
    return parseInt(document.getElementById("iron").innerHTML);
  }

  function getStone() {
    return parseInt(document.getElementById("stone").innerHTML);
  }

  const troops = {
    spear: {
      name: "spear",
      wood: 50,
      stone: 30,
      iron: 10
    },
    sword: {
      name: "sword",
      wood: 30,
      stone: 30,
      iron: 70
    },
    axe: {
      name: "axe",
      wood: 60,
      stone: 30,
      iron: 40
    },
    archer: {
      name: "archer",
      wood: 100,
      stone: 30,
      iron: 60
    },
    spy: {
      name: "spy",
      wood: 50,
      stone: 50,
      iron: 20
    },
    light: {
      name: "light",
      wood: 125,
      stone: 100,
      iron: 250
    },
    marcher: {
      name: "marcher",
      wood: 250,
      stone: 100,
      iron: 150
    },
    heavy: {
      name: "heavy",
      wood: 200,
      stone: 150,
      iron: 600
    }
  };

  function createCheckboxes() {
    const button = document.createElement("button");
    button.className = "btn";
    button.textContent = "Bereken";
    button.setAttribute("type", "button");
    button.setAttribute("style", "float: right;");
    button.addEventListener("click", calcHandler);
    document
      .querySelector("#train_form")
      .insertBefore(button, document.querySelector("#train_form > table"));
    let inputs = document.querySelectorAll(
      "#train_form > table > tbody > tr.row_a"
    );

    let inputCheckboxes = Object.values(inputs);
    inputCheckboxes.forEach(el => {
      let i1 = el.childNodes[1];
      let i2 = el.childNodes[1].childNodes[1];
      let input = document.createElement("input");
      input.setAttribute("type", "checkbox");
      input.setAttribute("name", "training_calc");
      input.value = i2.dataset.unit;

      i1.insertBefore(input, i2);
    });
  }

  function calcHandler() {
    const inputs = document.querySelectorAll(
      "#train_form > table > tbody > tr.row_a > td.nowrap > input[type=checkbox]:checked"
    );

    let troops = [];

    Object.values(inputs).forEach(troop => {
      troops.push(troop.value);
    });

    equalTroops(troops);
  }

  function calcMaxTroops(troopName) {
    if (troopName === null || troopName === undefined) {
      return;
    }
    const wood = getWood();
    const stone = getStone();
    const iron = getIron();

    let used = {
      wood: 0,
      stone: 0,
      iron: 0
    };
    let reachedCap = false;
    let amount = 0;

    while (reachedCap === false) {
      if (wood > used.wood && stone > used.stone && iron > used.iron) {
        const troop = troops[troopName];
        if (
          troop.wood < wood - used.wood &&
          troop.stone < stone - used.stone &&
          troop.iron < iron - used.iron
        ) {
          amount += 1;
          used.wood += troop.wood;
          used.stone += troop.stone;
          used.iron += troop.iron;
        }
        else {
          reachedCap = true;
        }
      }
      else {
        reachedCap = true;
      }
    }
    return amount;
  }

  function equalTroops(troopNames) {
    const allInputs = document.getElementsByClassName("recruit_unit");
    Object.values(allInputs).forEach(input => {
      input.value = 0;
    });
    if (
      troopNames === null ||
      troopNames === undefined ||
      troopNames.length === 0
    ) {
      return;
    }
    const wood = getWood();
    const stone = getStone();
    const iron = getIron();

    let used = {
      wood: 0,
      stone: 0,
      iron: 0
    };
    let reachedCap = false;
    let amount = {};
    const diffTroops = troopNames.length;
    let currentTroop = 0;

    while (reachedCap === false) {
      if (wood > used.wood && stone > used.stone && iron > used.iron) {
        if (currentTroop >= diffTroops) {
          currentTroop = 0;
        }
        const troop = troops[troopNames[currentTroop]];
        if (
          troop.wood < wood - used.wood &&
          troop.stone < stone - used.stone &&
          troop.iron < iron - used.iron
        ) {
          if (amount[troop.name] === undefined) {
            amount[troop.name] = 0;
          }
          amount[troop.name] += 1;
          used.wood += troop.wood;
          used.stone += troop.stone;
          used.iron += troop.iron;
        }
        else {
          reachedCap = true;
        }
        currentTroop += 1;
      }
      else {
        reachedCap = true;
      }
    }
    const keys = Object.keys(amount);
    keys.forEach(el => {
      document.getElementsByName(el)[0].value = amount[el];
    });
  }

  createCheckboxes();

})();