Module:DropRateCalc: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
(use for dynamic calcs on drop pages)
 
(add fix for vote toggle)
 
(9 intermediate revisions by the same user not shown)
Line 2: Line 2:


local buffs = {
local buffs = {
     {label = "Base", mult = 1.00},
     {label = "Base", reduction = 0},
     {label = "Donator", mult = 1.15},
     {label = "Donator", reduction = 0.15},
     {label = "Super Donator", mult = 1.25},
     {label = "Super donator", reduction = 0.25},
     {label = "Extreme Donator",mult = 1.30},
     {label = "Extreme donator", reduction = 0.30},
     {label = "Legendary Donator", mult = 1.35},
     {label = "Legendary donator", reduction = 0.35},
     {label = "Royal Donator", mult = 1.40},
     {label = "Royal donator", reduction = 0.40},
     {label = "Divine Donator", mult = 1.45},
     {label = "Divine donator", reduction = 0.45},
}
}
-- somewhat matches osrs wiki
local function getColor(rate)
    if rate == 1 then
        return "#0b5884"  -- always
    elseif rate <= 16 then
        return "#3c780a"  -- common
    elseif rate <= 64 then
        return "#a48900"  -- uncommon
    elseif rate <= 256 then
        return "#b55e0c"  -- rare
    elseif rate <= 999 then
        return "#9f261e"  -- very rare
    else
        return "#5c1a1a"  -- extremely rare
    end
end
-- pass rank like {{DropRateCalc|512||d}} or {{DropRateCalc|512||r}}.  The two || between rate and rank are necessary, otherwise the arg is matched as noboost.
-- fallback to title to save edits on all pre-existing div pages (hopefully)
local function npcRank(frame)
    local f = frame.args[3] and frame.args[3]:lower() or ""
    if f == "d" then return "divine" end
    if f == "r" then return "royal" end
    local title = mw.title.getCurrentTitle().text:lower()
    if title:find("divine", 1, true) then return "divine" end
    if title:find("royal", 1, true) then return "royal" end
end


function p.calc(frame)
function p.calc(frame)
     local base = tonumber(frame.args[1])
     local base = tonumber(frame.args[1])
     if not base then return "Invalid base rate" end
     if not base then return "Invalid base rate" end
    local noboost = frame.args[2] == "noboost"
    local rank = npcRank(frame)
     local cells = {}
     local cells = {}
     for _, tier in ipairs(buffs) do
     for i, b in ipairs(buffs) do
         table.insert(cells, "1/" .. math.floor(base / tier.mult + 0.5))
        local rate = math.floor(base * (1 - (noboost and 0 or b.reduction)))
        -- lesser ranks get N/A on div/royal NPCs
        local na = (rank == "divine" and i > 1 and i < 7)
                or (rank == "royal" and i > 1 and i < 6)
        local attrs = ''
        if not na then -- fix for MediaWiki:VoteBoostToggle.js. N/As can't have datas, else toggle will re-render them
            attrs = ' data-baserate="' .. base .. '" data-reduction="' .. b.reduction .. '"'
            if noboost then attrs = attrs .. ' data-noboost="1"' end
        end
         table.insert(cells,
            '<td' .. attrs
            .. ' style="background-color:' .. (na and '#4a4a55' or getColor(rate)) .. '; color:#ffffff; text-align:center;"'
            .. '>' .. (na and 'N/A' or '1/' .. rate) .. '</td>'
        )
     end
     end
     return table.concat(cells, " || ")
     return table.concat(cells, "")
end
end


return p
return p

Latest revision as of 15:52, 12 August 2026

Need to test if Larran's keys are buffed, specifically for Template:CallistoDrops and the other bosses like that.


local p = {}

local buffs = {
    {label = "Base", reduction = 0},
    {label = "Donator", reduction = 0.15},
    {label = "Super donator", reduction = 0.25},
    {label = "Extreme donator", reduction = 0.30},
    {label = "Legendary donator", reduction = 0.35},
    {label = "Royal donator", reduction = 0.40},
    {label = "Divine donator", reduction = 0.45},
}
-- somewhat matches osrs wiki
local function getColor(rate)
    if rate == 1 then
        return "#0b5884"  -- always
    elseif rate <= 16 then
        return "#3c780a"  -- common 
    elseif rate <= 64 then
        return "#a48900"  -- uncommon 
    elseif rate <= 256 then
        return "#b55e0c"  -- rare 
    elseif rate <= 999 then
        return "#9f261e"  -- very rare 
    else
        return "#5c1a1a"  -- extremely rare 
    end
end

-- pass rank like {{DropRateCalc|512||d}} or {{DropRateCalc|512||r}}.  The two || between rate and rank are necessary, otherwise the arg is matched as noboost.
-- fallback to title to save edits on all pre-existing div pages (hopefully)
local function npcRank(frame)
    local f = frame.args[3] and frame.args[3]:lower() or ""
    if f == "d" then return "divine" end
    if f == "r" then return "royal" end
    local title = mw.title.getCurrentTitle().text:lower()
    if title:find("divine", 1, true) then return "divine" end
    if title:find("royal", 1, true) then return "royal" end
end

function p.calc(frame)
    local base = tonumber(frame.args[1])
    if not base then return "Invalid base rate" end
    local noboost = frame.args[2] == "noboost"
    local rank = npcRank(frame)
    local cells = {}
    for i, b in ipairs(buffs) do
        local rate = math.floor(base * (1 - (noboost and 0 or b.reduction)))
        -- lesser ranks get N/A on div/royal NPCs
        local na = (rank == "divine" and i > 1 and i < 7)
                or (rank == "royal" and i > 1 and i < 6)
        local attrs = ''
        if not na then -- fix for MediaWiki:VoteBoostToggle.js. N/As can't have datas, else toggle will re-render them
            attrs = ' data-baserate="' .. base .. '" data-reduction="' .. b.reduction .. '"'
            if noboost then attrs = attrs .. ' data-noboost="1"' end
        end
        table.insert(cells,
            '<td' .. attrs
            .. ' style="background-color:' .. (na and '#4a4a55' or getColor(rate)) .. '; color:#ffffff; text-align:center;"'
            .. '>' .. (na and 'N/A' or '1/' .. rate) .. '</td>'
        )
    end
    return table.concat(cells, "")
end


return p