Difference between revisions of "Module:Drop Sources"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 72: Line 72:
  
 
local function genTable(monsters)
 
local function genTable(monsters)
     local s = "{|class=\"wikitable\"\n"
+
     local s = "{|class=\"wikitable sortable\"\n"
 
     s = s .. "!Monster\n"
 
     s = s .. "!Monster\n"
 
     s = s .. "!Quantity\n"
 
     s = s .. "!Quantity\n"

Revision as of 12:31, 28 June 2024


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 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 .. "!Monster\n"
    s = s .. "!Quantity\n"
    s = s .. "!Rarity\n"
    s = s .. "|-\n"
    for id, monsterDrops in pairsByKeys(monsters) do
        for _, drop in ipairs(monsterDrops) 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
    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