MediaWiki:Common.js

From Rhythm Game Wiki
Revision as of 19:34, 17 August 2024 by TadeLn (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.
/* Any JavaScript here will be loaded for all users on every page load. */

function findAndExecute(selector, callback) {
    const elements = document.querySelectorAll(selector);
    elements.forEach((element) => {
        callback(element);
    });
}

findAndExecute(".rgwiki-gadvia-score-calculator", (root) => {
    root.textContent = "";

    const inputGoldPerfect = document.createElement("input");
    inputGoldPerfect.type = "number";
    inputGoldPerfect.size = 4;
    root.appendChild(inputGoldPerfect);

    const inputAnyPerfect = document.createElement("input");
    inputAnyPerfect.type = "number";
    inputAnyPerfect.size = 4;
    root.appendChild(inputAnyPerfect);

    const inputGreat = document.createElement("input");
    inputGreat.type = "number";
    inputGreat.size = 4;
    root.appendChild(inputGreat);

    const inputGood = document.createElement("input");
    inputGood.type = "number";
    inputGood.size = 4;
    root.appendChild(inputGood);

    const inputMiss = document.createElement("input");
    inputMiss.type = "number";
    inputMiss.size = 4;
    root.appendChild(inputMiss);

    const calculateBtn = document.createElement("button");
    calculateBtn.textContent = "Calculate";
    root.appendChild(calculateBtn);

    const output = document.createElement("p");
    root.appendChild(output);

    calculateBtn.addEventListener("click", () => {
        const goldPerfect = Number(inputGoldPerfect.value);
        const anyPerfect = Number(inputAnyPerfect.value);
        const great = Number(inputGreat.value);
        const good = Number(inputGood.value);
        const miss = Number(inputMiss.value);
        const hitNotes = anyPerfect + great + good;
        const totalNotes = anyPerfect + great + good + miss;
        const nonGoldPerfect = anyPerfect - goldPerfect;
        const score = Math.floor((anyPerfect + (great * 0.8) + (good * 0.5)) / totalNotes * 1000000) + (goldPerfect * 0.0001);
        output.textContent = `Score: ${score} - Hit notes: ${hitNotes}/${totalNotes} - Non-gold perfects: ${nonGoldPerfect}`;
    });
});