Module:Drop Sources

From Idlescape Wiki
Jump to navigation Jump to search

local p = {}
local findId = require("Module:FindId")
local img = require("Module:img")
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 a = {}
    local monsters = {}
    for monsterId, monsterDrops in pairsByKeys(monstersDropsData) do
        for _, drop in ipairs(monsterDrops) do
            if drop["id"] == itemId then
                if not monsters[monsterId] then
                    monsters[monsterId] = {}
                    table.insert(monsters[monsterId],drop)
                else
                    table.insert(monsters[monsterId],drop)
                end
            end
        end
    end
    return monsters
end

local function getMonsterName(monsterId)
    local monster = monstersData[tostring(monsterId)]
    if monster then
        return monster["name"]
    else
        return "Module:Monsters/data out of date"
    end
end

local function genTable(monsters)
    local s = "{|class=\"wikitable sortable\"\n"
    s = s .. "!colspan=2|Monster\n"
    s = s .. "!Quantity\n"
    s = s .. "!Rarity\n"
    s = s .. "|-\n"
    for id, monsterDrops in pairsByKeys(monsters) do
        if #monsterDrops == 1 then
            s = s .. "|".. img._img({getMonsterName(id),"40"}) .. "\n"
            s = s .. "|[[" .. getMonsterName(id) .. "]]\n"
            if monsterDrops[1]["minAmount"] == monsterDrops[1]["maxAmount"] then
                s = s .. "|style=\"text-align:center;\"|" .. tostring(monsterDrops[1]["minAmount"]) .. "\n"
            else
                s = s ..
                "|style=\"text-align:center;\"|" .. tostring(monsterDrops[1]["minAmount"]) .. "-" .. tostring(monsterDrops[1]["maxAmount"]) .. "\n"
            end
            s = s .. "|style=\"text-align:center;\"|" .. round(monsterDrops[1]["chance"] * 100) .. "%\n"
            s = s .. "|-\n"
        elseif #monsterDrops > 1 then
            s = s .. "|rowspan=" .. tostring(#monsterDrops) .."\"|" .. img._img({getMonsterName(id),"40"}) .. "\n"
            s = s .. "|rowspan=" .. tostring(#monsterDrops) .. "\"|[[" .. getMonsterName(id) .. "]]\n"
            for _, drop in ipairs(monsterDrops) do
                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
        end
    end
    s = s .. "|}"
    return s
end

function p.dropSources(frame)
    return p._dropSources(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