Module:CollectionLog

From Roat Pkz
Revision as of 07:24, 19 May 2025 by Hefner (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:CollectionLog/doc

local p = {}

-- Helper function to format numbers with K notation if needed
local function formatNumber(num)
    if num >= 100000 then
        return string.format("%.1fk", num / 1000):gsub("%.0", "")
    else
        return tostring(num)
    end
end

function p.render(frame)
    local args = frame:getParent().args
    local html = mw.html.create('div'):addClass('collection-log-wrapper')

    -- Killcount display with BossName and ItemsTotal
    if args.BossName and args.Killcount and args.ItemsTotal then
        local killcountNum = tonumber(args.Killcount) or 0
        local itemsTotalNum = tonumber(args.ItemsTotal) or 0
        local bossName = args.BossName

        html:node(
            mw.html.create('div')
                :addClass('collection-log-killcount')
                :wikitext(string.format(
                    '%s: %d/%d obtained',
                    bossName,
                    killcountNum,
                    itemsTotalNum
                ))
        )
    elseif args.Killcount then
        local killcountNum = tonumber(args.Killcount) or 0
        html:node(
            mw.html.create('div')
                :addClass('collection-log-killcount')
                :wikitext('Killcount: ' .. killcountNum)
        )
    end

    local grid = mw.html.create('div'):addClass('collection-log-grid')

    local i = 1
    while true do
        local itemName = args['item' .. i]
        if not itemName then break end

        local qty = tonumber(args['qty' .. i]) or 0
        local qtyDisplay = qty
        local qtySuffix = ""

        -- Format quantity for badge and tooltip display
        if qty >= 100000 then
            qtySuffix = " ×" .. formatNumber(qty)
        elseif qty > 1 then
            qtySuffix = " ×" .. qty
        end

        -- Display name (underscores replaced with spaces)
        local displayName = itemName:gsub("_", " ")

        -- Tooltip hover HTML (shows exact qty, no formatting)
        local hoverHtml = string.format(
            '<span class="hoveritem" style="position: absolute; top: 2.5em; left: 1.8em; z-index: 1;">' ..
            '<span style="display: block; border: solid 1px rgb(0, 0, 0); border-radius: 3px; background-color: #313e59; box-shadow: 0 0 4px rgb(0, 0, 0); padding: 5px; margin: 0 0.5em 0.5em 0.5em; white-space: nowrap;" class="hover-info-box">%s</span>' ..
            '</span>',
            displayName .. (qty > 1 and (" ×" .. qty) or "")
        )

        -- Item HTML with link to the item page (not image)
        local itemHtml = string.format(
            '<span class="hoverbox hover-info" style="position: relative;">' ..
            '[[File:%s.png|32x32px|alt=%s]]' ..
            '%s' ..
            '</span>',
            itemName, itemName, hoverHtml
        )

        local item = mw.html.create('div')
            :addClass('collection-log-item')
            :addClass(qty > 0 and 'obtained' or 'missing')
            :wikitext(itemHtml)

        -- Add quantity badge if quantity > 1
        if qty > 1 then
            local badge = mw.html.create('div')
                :addClass('collection-log-qty-badge')
                :wikitext(qtySuffix == "" and tostring(qty) or qtySuffix:gsub(" ×", ""))
            item:node(badge)
        end

        grid:node(item)
        i = i + 1
    end

    html:node(grid)
    return tostring(html)
end

return p