Module:ItemDrops

From August Wiki
Jump to navigation Jump to search

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

-- Module:ItemDrops
local p = {}

local rarityStyles = {
    Always = { 'table-bg-blue', 1 },
    Common = { 'table-bg-green', 16 },
    Uncommon = { 'table-bg-yellow', 64 },
    Rare = { 'table-bg-orange', 256 },
    ['Very rare'] = { 'table-bg-red', 1024 },
}

-- Main function to generate the item item table
function p.main(frame)
    local args = frame.args

    -- Start building the table HTML
    local html = [[
<table class="wikitable sortable filterable item-drops align-center-2 align-center-3 align-center-4 autosort=3,a jquery-tablesorter rsw-dropsline-hidealch">
  <tr>
    <th class="drop-disp-btn btn-first headerSort" tabindex="0" role="columnheader button" title="Sort ascending">Item</th>
    <th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">Quantity</th>
    <th class="drops-rarity-header headerSort headerSortUp" tabindex="0" role="columnheader button" title="Sort ascending">Rarity</th>
  </tr>
]]

    -- Generate rows dynamically
    local index = 1
    while args['item' .. index] do
        local item = args['item' .. index]
        local rarity = args['rarity' .. index] or ''

        -- Check for item-specific quantity first, then fall back to global quantity
        local quantity = args['item' .. index .. 'Quant'] or args['itemQuant'] or ''

        local rarityClass = ''
        if rarityStyles[rarity] then
            rarityClass = rarityStyles[rarity][1]
            raritySort = rarityStyles[rarity][2]
        end

        -- Build the row HTML
        html = html .. '  <tr>\n'
        html = html .. '    <td>[[' .. item .. ']]</td>\n'

        -- Quantity cell
        html = html .. '    <td data-sort-value="' .. quantity .. '">' .. quantity .. '</td>\n'

        -- Rarity cell
        html = html .. '    <td data-sort-value="' .. raritySort ..  '" class="' .. rarityClass.. '">' .. rarity ..  '</td>\n'
        html = html .. '  </tr>\n'

        -- Move to next index
        index = index + 1
    end

    -- Close the table
    html = html .. '</table>'

    return html
end

-- Function to render HTML from template-style parameters
function p.render(frame)
    -- Get all arguments from parent template
    local parentArgs = frame:getParent().args
    return p.main({args = parentArgs})
end

return p