Jump to content

Module:MonsterDrops: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Sort drops by rarity ascending (common to legendary) (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Make table user-sortable with proper rarity sort values (via update-page on MediaWiki MCP Server)
Line 62: Line 62:
     -- Build table
     -- Build table
     local html = {}
     local html = {}
     table.insert(html, '{| class="wikitable"')
     table.insert(html, '{| class="wikitable sortable"')
     table.insert(html, '|-')
     table.insert(html, '|-')
     table.insert(html, '! !! Item !! Quantity !! Rarity')
     table.insert(html, '! class="unsortable" | !! Item !! Quantity !! Rarity')
      
      
     for _, drop in ipairs(drops) do
     for _, drop in ipairs(drops) do
         local sprite = getItemSprite(drop.item)
         local sprite = getItemSprite(drop.item)
         local quantity = formatQuantity(drop.min, drop.max)
         local quantity = formatQuantity(drop.min, drop.max)
        local rarityValue = getRarityValue(drop.rarity)
          
          
         table.insert(html, '|-')
         table.insert(html, '|-')
Line 74: Line 75:
         table.insert(html, string.format('| [[%s]]', drop.item))
         table.insert(html, string.format('| [[%s]]', drop.item))
         table.insert(html, string.format('| %s', quantity))
         table.insert(html, string.format('| %s', quantity))
         table.insert(html, string.format('| %s', drop.rarity or ''))
         table.insert(html, string.format('| data-sort-value="%d" | %s', rarityValue, drop.rarity or ''))
     end
     end
      
      

Revision as of 00:56, 28 January 2026

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

local p = {}

-- Rarity sort order (ascending)
local rarityOrder = {
    common = 1,
    uncommon = 2,
    rare = 3,
    epic = 4,
    legendary = 5
}

-- Get sprite for an item from Cargo, fallback to item name
local function getItemSprite(itemName)
    local result = mw.ext.cargo.query('Items', 'sprite', {
        where = 'name="' .. itemName .. '"',
        limit = 1
    })
    
    if result and result[1] and result[1].sprite and result[1].sprite ~= '' then
        return result[1].sprite
    end
    return itemName
end

-- Format quantity display
local function formatQuantity(min, max)
    if not min or min == '' then
        return '?'
    end
    if not max or max == '' or min == max then
        return tostring(min)
    end
    return min .. '-' .. max
end

-- Get rarity sort value
local function getRarityValue(rarity)
    if not rarity then return 99 end
    return rarityOrder[string.lower(rarity)] or 99
end

-- Main function to display monster drops
function p.display(frame)
    local args = frame:getParent().args
    local source = args.source or mw.title.getCurrentTitle().text
    local sourceType = args.type or 'Monster'
    
    -- Query ItemSource for drops
    local drops = mw.ext.cargo.query('ItemSource', 'item,rarity,min,max', {
        where = 'source="' .. source .. '" AND source_type="' .. sourceType .. '"'
    })
    
    if not drops or #drops == 0 then
        return ''
    end
    
    -- Sort by rarity (ascending)
    table.sort(drops, function(a, b)
        return getRarityValue(a.rarity) < getRarityValue(b.rarity)
    end)
    
    -- Build table
    local html = {}
    table.insert(html, '{| class="wikitable sortable"')
    table.insert(html, '|-')
    table.insert(html, '! class="unsortable" | !! Item !! Quantity !! Rarity')
    
    for _, drop in ipairs(drops) do
        local sprite = getItemSprite(drop.item)
        local quantity = formatQuantity(drop.min, drop.max)
        local rarityValue = getRarityValue(drop.rarity)
        
        table.insert(html, '|-')
        table.insert(html, string.format('| style="text-align:center;" | {{Sprite|%s|x2}}', sprite))
        table.insert(html, string.format('| [[%s]]', drop.item))
        table.insert(html, string.format('| %s', quantity))
        table.insert(html, string.format('| data-sort-value="%d" | %s', rarityValue, drop.rarity or ''))
    end
    
    table.insert(html, '|}')
    
    return frame:preprocess(table.concat(html, '\n'))
end

return p