MediaWiki:Gadget-HiddenPowerCalculator.js

From Squidgy
Revision as of 14:08, 14 May 2025 by Squidgy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['mediawiki.util', 'jquery'], function () {
  $(function () {
    // Wait until the div exists in the DOM
    const tryInject = () => {
      const container = document.getElementById('hp-calc-container');
      if (!container) return;

      container.innerHTML = `
        <form id="dvForm">
          <label>Attack DV: <input type="number" id="atk" min="0" max="15" required></label><br>
          <label>Defense DV: <input type="number" id="def" min="0" max="15" required></label><br>
          <label>Speed DV: <input type="number" id="spe" min="0" max="15" required></label><br>
          <label>Special DV: <input type="number" id="spa" min="0" max="15" required></label><br>
          <button type="button" id="calculateBtn">Calculate</button>
        </form>
        <p id="results"></p>
      `;

      document.getElementById("calculateBtn").addEventListener("click", function () {
        const atk = parseInt(document.getElementById("atk").value);
        const def = parseInt(document.getElementById("def").value);
        const spe = parseInt(document.getElementById("spe").value);
        const spa = parseInt(document.getElementById("spa").value);

        if ([atk, def, spe, spa].some(n => isNaN(n) || n < 0 || n > 15)) {
          document.getElementById("results").innerText = "All values must be between 0 and 15.";
          return;
        }

        const hp = ((atk & 1) << 3) | ((def & 1) << 2) | ((spe & 1) << 1) | (spa & 1);
        const typeId = 4 * (atk % 4) + (def % 4);
        const hpTypes = [
          "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel",
          "Fire", "Water", "Grass", "Electric", "Psychic", "Ice", "Dragon", "Dark"
        ];
        const hpType = hpTypes[typeId];
        const v = spa >= 8 ? 1 : 0;
        const w = spe >= 8 ? 1 : 0;
        const x = def >= 8 ? 1 : 0;
        const y = atk >= 8 ? 1 : 0;
        const z = spa % 4;
        const power = Math.floor((5 * (v + 2 * w + 4 * x + 8 * y) + z) * 40 / 63);
        const shiny = [2, 3, 6, 7, 10, 11, 14, 15].includes(atk) && def === 10 && spe === 10 && spa === 10;

        document.getElementById("results").innerHTML = `
          <strong>HP DV:</strong> ${hp}<br>
          <strong>Hidden Power:</strong> ${hpType} (${power} Power)<br>
          <strong>Shiny:</strong> ${shiny ? "Yes ✨" : "No"}
        `;
      });
    };

    // Retry every 100ms until the div appears (max 2 seconds)
    let tries = 0;
    const interval = setInterval(() => {
      if (document.getElementById('hp-calc-container')) {
        clearInterval(interval);
        tryInject();
      }
      if (++tries > 20) clearInterval(interval); // Stop after 2 seconds
    }, 100);
  });
});