MediaWiki:Gadget-HiddenPowerCalculator.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
$(function () { | 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 | const interval = setInterval(() => { | ||
if (document.getElementById('hp-calc-container')) { | |||
clearInterval(interval); | |||
tryInject(); | |||
} | |||
if (++tries > 20) clearInterval(interval); // Stop after 2 seconds | |||
}, 100); | |||
}); | }); | ||
}); | }); |
Latest revision as of 14:08, 14 May 2025
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); }); });