MediaWiki:Gadget-HiddenPowerCalculator.js

From Squidgy
Revision as of 13:55, 14 May 2025 by Squidgy (talk | contribs)
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.
$(function () {
  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;
    }

    // HP DV
    const hp = ((atk & 1) << 3) | ((def & 1) << 2) | ((spe & 1) << 1) | (spa & 1);

    // Hidden Power Type
    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];

    // Hidden Power Power
    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);

    // Shininess
    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"}
    `;
  });
});