MediaWiki:Common.js: Difference between revisions

From Rhythm Game Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:
     root.textContent = "";
     root.textContent = "";


     const inputGoldPerfect = document.createElement("input");
     function createFormField(root, labelText) {
    inputGoldPerfect.type = "number";
        const fieldContainer = document.createElement("div");
    inputGoldPerfect.size = 4;
        fieldContainer.style.display = "flex";
    root.appendChild(inputGoldPerfect);
        fieldContainer.style.flexDirection = "column";


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


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


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


     const inputMiss = document.createElement("input");
     const inputGoldPerfect = createFormField(root, "Gold Perfect");
     inputMiss.type = "number";
     const inputAnyPerfect = createFormField(root, "Any Perfect");
     inputMiss.size = 4;
     const inputGreat = createFormField(root, "Great");
     root.appendChild(inputMiss);
     const inputGood = createFormField(root, "Good");
    const inputMiss = createFormField(root, "Miss");


     const calculateBtn = document.createElement("button");
     const calculateBtn = document.createElement("button");

Revision as of 19:44, 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 = "";

    function createFormField(root, labelText) {
        const fieldContainer = document.createElement("div");
        fieldContainer.style.display = "flex";
        fieldContainer.style.flexDirection = "column";

        const label = document.createElement("label");
        label.textContent = labelText;
        fieldContainer.appendChild(label);

        const inputElement = document.createElement("input");
        inputElement.type = "number";
        inputElement.size = 6;
        fieldContainer.appendChild(inputElement);

        root.appendChild(fieldContainer);
    }

    const inputGoldPerfect = createFormField(root, "Gold Perfect");
    const inputAnyPerfect = createFormField(root, "Any Perfect");
    const inputGreat = createFormField(root, "Great");
    const inputGood = createFormField(root, "Good");
    const inputMiss = createFormField(root, "Miss");

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