MediaWiki:Common.js: Difference between revisions

From Rhythm Game Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


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


checkAndExecute(".rgwiki-gadvia-score-calculator", (element) => {
findAndExecute(".rgwiki-gadvia-score-calculator", (root) => {
     element.textContent = "JavaScript works!"
     root.textContent = "";
 
    const inputGoldPerfect = document.createElement("input");
    inputGoldPerfect.type = "number";
    root.appendChild(inputGoldPerfect);
 
    const inputAnyPerfect = document.createElement("input");
    inputAnyPerfect.type = "number";
    root.appendChild(inputAnyPerfect);
 
    const inputGreat = document.createElement("input");
    inputGreat.type = "number";
    root.appendChild(inputGreat);
 
    const inputGood = document.createElement("input");
    inputGood.type = "number";
    root.appendChild(inputGood);
 
    const inputMiss = document.createElement("input");
    inputMiss.type = "number";
    root.appendChild(inputMiss);
 
    const calculateBtn = document.createElement("button");
    calculateBtn.textContent = "Calculate";
    root.appendChild(calculateBtn);
 
    const output = document.createElement("span");
    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}`;
    });
});
});

Revision as of 19:30, 17 August 2024

/* 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";
    root.appendChild(inputGoldPerfect);

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

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

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

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

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

    const output = document.createElement("span");
    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}`;
    });
});