Difference between revisions of "Module:Drop Sources"

From Idlescape Wiki
Jump to navigation Jump to search
(c)
 
m (HASH moved page Module:Drop sources to Module:Drop Sources without leaving a redirect)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
local findId = require("Module:FindId")
 
local findId = require("Module:FindId")
 +
local img = require("Module:img")
 
local monstersDropsData = mw.loadData("Module:Monsters drops/data")
 
local monstersDropsData = mw.loadData("Module:Monsters drops/data")
 
local monstersData = mw.loadData("Module:Monsters/data")
 
local monstersData = mw.loadData("Module:Monsters/data")
Line 45: Line 46:
  
 
local function getMonsters(itemId)
 
local function getMonsters(itemId)
 +
    local a = {}
 
     local monsters = {}
 
     local monsters = {}
     for monsterId, monsterDrops in pairs(monstersDropsData) do
+
     for monsterId, monsterDrops in pairsByKeys(monstersDropsData) do
         for i, drop in ipairs(monsterDrops) do
+
         for _, drop in ipairs(monsterDrops) do
 
             if drop["id"] == itemId then
 
             if drop["id"] == itemId then
                 monsters[monsterId] = drop
+
                 if not monsters[monsterId] then
 +
                    monsters[monsterId] = {}
 +
                    table.insert(monsters[monsterId],drop)
 +
                else
 +
                    table.insert(monsters[monsterId],drop)
 +
                end
 
             end
 
             end
 
         end
 
         end
Line 57: Line 64:
  
 
local function getMonsterName(monsterId)
 
local function getMonsterName(monsterId)
     local monsterName = monstersData[tostring(monsterId)]["name"]
+
     local monster = monstersData[tostring(monsterId)]
     return monsterName
+
    if monster then
 +
        return monster["name"]
 +
     else
 +
        return "Module:Monsters/data out of date"
 +
    end
 
end
 
end
  
 
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 .. "!colspan=2|Monster\n"
 
     s = s .. "!Quantity\n"
 
     s = s .. "!Quantity\n"
 
     s = s .. "!Rarity\n"
 
     s = s .. "!Rarity\n"
 
     s = s .. "|-\n"
 
     s = s .. "|-\n"
     for id, drop in pairsByKeys(monsters) do
+
     for id, monsterDrops in pairsByKeys(monsters) do
         s = s .. "|[[" .. getMonsterName(id) .. "]]\n"
+
         if #monsterDrops == 1 then
         if drop["minAmount"] == drop["maxAmount"] then
+
            s = s .. "|".. img._img({getMonsterName(id),"40"}) .. "\n"
            s = s .. "|style=\"text-align:center;\"|" .. tostring(drop["minAmount"]) .. "\n"
+
            s = s .. "|[[" .. getMonsterName(id) .. "]]\n"
        else
+
            if monsterDrops[1]["minAmount"] == monsterDrops[1]["maxAmount"] then
            s = s ..
+
                s = s .. "|style=\"text-align:center;\"|" .. tostring(monsterDrops[1]["minAmount"]) .. "\n"
            "|style=\"text-align:center;\"|" .. tostring(drop["minAmount"]) .. "-" .. tostring(drop["maxAmount"]) .. "\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 .. "|style=\"text-align:center;\"|" .. round(drop["chance"] * 100) .. "%\n"
 
        s = s .. "|-\n"
 
 
     end
 
     end
 
     s = s .. "|}"
 
     s = s .. "|}"
Line 82: Line 109:
 
end
 
end
  
function p.dropScources(frame)
+
function p.dropSources(frame)
     return p._monsters_stats_table(frame:getParent().args)
+
     return p._dropSources(frame:getParent().args)
 
end
 
end
  

Latest revision as of 03:12, 2 July 2024


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