Module:Monster Drops

From Idlescape Wiki
Revision as of 14:52, 4 July 2024 by HASH (talk | contribs) (Monster Drops)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

local p = {}
local findId = require("Module:FindId")
local img = require("Module:img")
local itemsData = mw.loadData("Module:Items/data")
local monstersDropsData = mw.loadData("Module:Monsters drops/data")
local monstersData = mw.loadData("Module:Monsters/data")
local pageName = mw.title.getCurrentTitle().fullText

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

local function addSeparator(num)
    return tostring(tonumber(num)):reverse():gsub("(%d%d%d)", "%1,"):gsub(",(%-?)$", "%1"):reverse()
end

---decides the collase state based on the given string
---@param collapse string
---@return string
local function decideCollapseState(collapse)
    if collapse == "collapsible" then
        collapse = " mw-made-collapsible mw-collapsible"
    elseif collapse == "collapsed" then
        collapse = " mw-made-collapsible mw-collapsible mw-collapsed"
    else
        collapse = ""
    end
    return collapse
end

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

---fetches an item object from items/data module
---@param itemId integer|string or string
---@return table|string
local function getItem(itemId)
    local item = itemsData[tostring(itemId)]
    if item then
        return item
    else
        return "Module:Items/data out of date"
    end
end

local function createImgCell(item)
    local imgCell = mw.html.create('td')
        :wikitext(img._img({ item.name, '30' }))
        :done()
    return imgCell
end

local function createNameCell(item)
    local nameCell = mw.html.create('td')
        :wikitext("[[" .. item.name .. "]]")
        :done()
    return nameCell
end

local function createQuantityCell(drop)
    local s = ""
    if drop.minAmount == drop.maxAmount then
        s = drop.minAmount
    else
        s = drop.minAmount .. "-" .. drop.maxAmount
    end
    local QuantityCell = mw.html.create('td'):css('text-align', 'center')
        :wikitext(s)
        :done()
    return QuantityCell
end

local function createRarityCell(drop)
    local RarityCell = mw.html.create('td'):css('text-align', 'center')
        :wikitext(round(drop.chance * 100) .. "%")
        :done()
    return RarityCell
end

local function createPriceCell(item)
    local PriceCell = mw.html.create('td'):css('text-align', 'center')
        :wikitext(addSeparator(item.value or 1) .. " ")
            :tag('img')
                :attr('src', "https://www.play.idlescape.com/images/gold_coin.png")
                :attr('width', '15')
                :attr('height', '15')
                :attr('alt', "Gold Coin")
            :done()
        :done()
    return PriceCell
end

local function genTable(name, collapse)

    local mId = findId._findId({name, 'monster'})
    local t = mw.html.create('table')
        :addClass("wikitable sortable jquery-tablesorter" .. collapse)
        :tag('tr')
        :tag('th')
        :attr("colspan", "2")
        :wikitext("Item")
        :done()
        :tag('th')
        :wikitext("Quantity")
        :done()
        :tag('th')
        :wikitext("Rarity")
        :done()
        :tag('th')
        :wikitext("Vendor Price")
        :done()
        :done()
    for _, drop in ipairs(monstersDropsData[tostring(mId)]) do
        local item = getItem(drop.id)
        t:tag('tr')
            :node(createImgCell(item))
            :node(createNameCell(item))
            :node(createQuantityCell(drop))
            :node(createRarityCell(drop))
            :node(createPriceCell(item))
            :done()
    end
    return t
end

function p.monsterDrops(frame)
    return p._monsterDrops(frame:getParent().args)
end

function p._monsterDrops(_args)
    local name = ""
    if _args[1] then
        name = _args[1]
    else
        name = pageName
    end
    local t = genTable(name, decideCollapseState(_args["collapse"]))
    return t
end

return p