MediaWiki:DropCalc.js

From Roat Pkz
Revision as of 15:36, 5 March 2026 by Hefner (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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// --- DROPRATE CALCULATOR WITH TABLE ---
mw.hook('wikipage.content').add(function () {
    var container = document.getElementById("dropCalcContainer");
    if (!container) return;

    // Build HTML: Table
    container.innerHTML =
        '<table id="dropCalcTable" style="border-collapse: collapse; width: 100%; max-width: 500px;">' +
        '<thead></thead><tbody></tbody></table>' +
        '<br><button id="calcDropBtn">Calculate Drop Rate</button>' +
        '<div style="margin-top:10px;">Final Drop Rate: <span id="finalDrop">0</span></div>';

    var table = document.getElementById("dropCalcTable");
    var header = table.createTHead();
    var headerRow = header.insertRow();

    headerRow.innerHTML =
        '<th style="padding-left: 10px;">Bonus</th>' +
        '<th>Percentage</th>' +
        '<th>Drop Rate</th>';

    headerRow.style.background = '#222e45';
    headerRow.style.color = '#fff';
    headerRow.style.fontWeight = 'bold';
    headerRow.style.textAlign = 'left';

    var tbody = table.tBodies[0];

    var bonuses = [
        { name: "Donator Rank", id: "donatorBonus", options: [
            {name: "None", value:0}, {name: "Donator 15%", value:15},
            {name: "Super Donator 25%", value:25}, {name: "Extreme Donator 30%", value:30},
            {name: "Legendary Donator 35%", value:35}, {name: "Royal Donator 40%", value:40},
            {name: "Divine Donator 45%", value:45}
        ]},
        { name: "Monster Slayer Perk", id: "slayerBonus", options:[
            {name:"None", value:0},{name:"10%", value:10}
        ]},
        { name: "Collector's Ring", id: "ringBonus", options:[
            {name:"None", value:0},{name:"3%", value:3},{name:"6%", value:6}
        ]},
        { name: "Skull Bonus", id: "skullBonus", options:[
            {name:"None", value:0},{name:"20%", value:20}
        ]},
        { name: "Voting Bonus", id: "voteBonus", options:[
            {name:"None", value:0},{name:"20%", value:20}
        ]}
    ];

    bonuses.forEach(function(bonus, index) {
        var row = tbody.insertRow();
        row.style.background = index % 2 === 0 ? '#313e59' : '#222e45';

        var cell1 = row.insertCell();
        cell1.textContent = bonus.name;
        cell1.style.padding = '5px';

        var cell2 = row.insertCell();
        var select = document.createElement('select');
        select.id = bonus.id;
        bonus.options.forEach(function(opt){
            var option = document.createElement('option');
            option.value = opt.value;
            option.textContent = opt.name;
            select.appendChild(option);
        });
        cell2.appendChild(select);
        cell2.style.padding = '5px';

        var cell3 = row.insertCell();
        cell3.id = bonus.id + "Preview";
        cell3.textContent = "-";
        cell3.style.padding = '5px';

        // Recalculate when bonus changes
        select.addEventListener('change', function() {
            document.getElementById("calcDropBtn").click();
        });
    });

    // Searchable item selector with X inside input
    var baseDiv = document.createElement('div');
    baseDiv.style.margin = '10px 0';

    var searchWrapper = document.createElement('div');
    searchWrapper.style.position = 'relative';
    searchWrapper.style.display = 'inline-block';
    searchWrapper.style.width = '220px';

    var itemInput = document.createElement('input');
    itemInput.id = 'itemSearch';
    itemInput.setAttribute('list','dropItems');
    itemInput.placeholder = 'Search item...';
    itemInput.style.width = '100%';
    itemInput.style.boxSizing = 'border-box';
    searchWrapper.appendChild(itemInput);

    var clearBtn = document.createElement('span');
    clearBtn.textContent = '×';
    clearBtn.style.position = 'absolute';
    clearBtn.style.right = '8px';
    clearBtn.style.top = '50%';
    clearBtn.style.transform = 'translateY(-50%)';
    clearBtn.style.cursor = 'pointer';
    clearBtn.style.fontWeight = 'bold';
    clearBtn.style.fontSize = '16px';
    clearBtn.style.color = '#888';
    searchWrapper.appendChild(clearBtn);

    clearBtn.onclick = function() {
        itemInput.value = '';
        document.getElementById("baseRate").value = 1000;
        document.getElementById("calcDropBtn").click();
    };

    baseDiv.innerHTML = 'Item: ';
    baseDiv.appendChild(searchWrapper);
    baseDiv.innerHTML += ' Base Rate: <input type="number" id="baseRate" value="1000" style="width:80px;">';

    baseDiv.innerHTML += '<datalist id="dropItems">' +
        '<option data-rate="256" value="Zaryte Crossbow (1/256)">' +
        '<option data-rate="350" value="Torva Full Helm (1/350)">' +
        '<option data-rate="2560" value="Voidwaker gem (1/2560)">' +
        '<option data-rate="2560" value="Voidwaker hilt (1/2560)">' +
        '<option data-rate="2560" value="Voidwaker blade (1/2560)">' +
        '<option data-rate="128" value="Ancient godsword (1/128)">' +
        '<option data-rate="64" value="Zaryte vambraces (1/64)">' +
        '<option data-rate="10240" value="Elysian sigil (1/10240)">' +
        '<option data-rate="1024" value="Smoudering heart (1/1024)">' +
        '<option data-rate="75" value="TokHaar-kal (1/75)">' +
        '<option data-rate="2560" value="Enchanted Collector\'s Ring (6%) (1/2560)">' +
    '</datalist>';

    container.insertBefore(baseDiv, table);

    // Items that disable Skull Bonus
    var skullDisabledItems = ["TokHaar-kal"];

    // Auto-fill base rate and recalc
    itemInput.addEventListener('input', function() {
        var options = document.getElementById("dropItems").options;
        var val = itemInput.value;

        for (var i = 0; i < options.length; i++) {
            if (options[i].value === val) {
                var rate = options[i].getAttribute("data-rate");
                document.getElementById("baseRate").value = rate;

                var skullSelect = document.getElementById("skullBonus");
                var rawName = val.replace(/\s*\(1\/\d+\)$/, '');
                if (skullDisabledItems.includes(rawName)) {
                    skullSelect.disabled = true;
                    skullSelect.style.opacity = 0.5;
                } else {
                    skullSelect.disabled = false;
                    skullSelect.style.opacity = 1;
                }

                document.getElementById("calcDropBtn").click();
                break;
            }
        }
    });

    // Calculation
    document.getElementById("calcDropBtn").onclick = function() {
        var base = parseFloat(document.getElementById("baseRate").value) || 0;
        var finalRate = base;

        bonuses.forEach(function(bonus) {
            var val = parseFloat(document.getElementById(bonus.id).value) || 0;
            finalRate *= (1 - val / 100);
            document.getElementById(bonus.id + "Preview").textContent = Math.floor(finalRate);
        });

        document.getElementById("finalDrop").textContent = Math.floor(finalRate);
    };
});