Module:Drop Sources

From Idlescape Wiki
Revision as of 07:53, 28 June 2024 by HASH (talk | contribs) (c)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p = {}
local findId = require("Module:FindId")
local monstersDropsData = mw.loadData("Module:Monsters drops/data")
local monstersData = mw.loadData("Module:Monsters/data")

local function pairsByKeys(t, f)
    local a = {}
    local orgi_key_type
    local orgi_key_numbered
    for n in pairs(t) do
        if tonumber(n) == nil then
            table.insert(a, n)
            orgi_key_type = "word"
        elseif type(n) == "number" then
            table.insert(a, n)
            orgi_key_type = "int"
        elseif type(n) == "string" and type(tonumber(n) == "number") then
            orgi_key_type = "number"
            table.insert(a, tonumber(n))
        end
    end
    table.sort(a, f)
    local key
    local value
    local i = 0             -- iterator variable
    local iter = function() -- iterator function
        i = i + 1
        if a[i] == nil then
            return nil
        elseif orgi_key_type == "word" or orgi_key_type == "int" then
            key = a[i]
            value = t[a[i]]
        elseif orgi_key_type == "number" then
            key = tostring(a[i])
            value = t[tostring(a[i])]
        end
        return key, value
    end
    return iter
end

local function round(num)
    return tostring(math.floor(num * 1000 + 0.5) / 1000)
end

local function getMonsters(itemId)
    local monsters = {}
    for monsterId, monsterDrops in pairs(monstersDropsData) do
        for i, drop in ipairs(monsterDrops) do
            if drop["id"] == itemId then
                monsters[monsterId] = drop
            end
        end
    end
    return monsters
end

local function getMonsterName(monsterId)
    local monsterName = monstersData[tostring(monsterId)]["name"]
    return monsterName
end

local function genTable(monsters)
    local s = "{|class=\"wikitable\"\n"
    s = s .. "!Monster\n"
    s = s .. "!Quantity\n"
    s = s .. "!Rarity\n"
    s = s .. "|-\n"
    for id, drop in pairsByKeys(monsters) do
        s = s .. "|[[" .. getMonsterName(id) .. "]]\n"
        if drop["minAmount"] == drop["maxAmount"] then
            s = s .. "|style=\"text-align:center;\"|" .. tostring(drop["minAmount"]) .. "\n"
        else
            s = s ..
            "|style=\"text-align:center;\"|" .. tostring(drop["minAmount"]) .. "-" .. tostring(drop["maxAmount"]) .. "\n"
        end
        s = s .. "|style=\"text-align:center;\"|" .. round(drop["chance"] * 100) .. "%\n"
        s = s .. "|-\n"
    end
    s = s .. "|}"
    return s
end

function p.dropScources(frame)
    return p._monsters_stats_table(frame:getParent().args)
end

function p._dropSources(_args)
    local itemId = findId._findId({ _args[1], "item" })
    local monsters = getMonsters(itemId)
    local s = ""
    s = genTable(monsters)
    return s
end

return p