Jump to content

Module:MonsterDrops: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Make table user-sortable with proper rarity sort values (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Sort by rarity ascending, then quantity ascending (via update-page on MediaWiki MCP Server)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


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


Line 38: Line 38:
     if not rarity then return 99 end
     if not rarity then return 99 end
     return rarityOrder[string.lower(rarity)] or 99
     return rarityOrder[string.lower(rarity)] or 99
end
-- Get quantity sort value (max if present, otherwise min)
local function getQuantitySortValue(min, max)
    if max and max ~= '' then
        return tonumber(max) or 0
    end
    return tonumber(min) or 0
end
end


Line 55: Line 63:
     end
     end
      
      
     -- Sort by rarity (ascending)
     -- Sort by rarity ascending, then quantity ascending
     table.sort(drops, function(a, b)
     table.sort(drops, function(a, b)
         return getRarityValue(a.rarity) < getRarityValue(b.rarity)
         local rarityA = getRarityValue(a.rarity)
        local rarityB = getRarityValue(b.rarity)
        if rarityA ~= rarityB then
            return rarityA < rarityB
        end
        return getQuantitySortValue(a.min, a.max) < getQuantitySortValue(b.min, b.max)
     end)
     end)
      
      
     -- Build table
     -- Build table using mw.html
     local html = {}
     local tbl = mw.html.create('table')
    table.insert(html, '{| class="wikitable sortable"')
        :addClass('wikitable')
     table.insert(html, '|-')
        :addClass('sortable')
     table.insert(html, '! class="unsortable" | !! Item !! Quantity !! Rarity')
      
    -- Header row
    local headerRow = tbl:tag('tr')
     headerRow:tag('th'):addClass('unsortable'):wikitext('')
    headerRow:tag('th'):wikitext('Item')
    headerRow:tag('th'):wikitext('Quantity')
    headerRow:tag('th'):wikitext('Rarity')
      
      
     for _, drop in ipairs(drops) do
     for _, drop in ipairs(drops) do
Line 70: Line 89:
         local quantity = formatQuantity(drop.min, drop.max)
         local quantity = formatQuantity(drop.min, drop.max)
         local rarityValue = getRarityValue(drop.rarity)
         local rarityValue = getRarityValue(drop.rarity)
        local quantityValue = getQuantitySortValue(drop.min, drop.max)
          
          
         table.insert(html, '|-')
         local row = tbl:tag('tr')
         table.insert(html, string.format('| style="text-align:center;" | {{Sprite|%s|x2}}', sprite))
          
         table.insert(html, string.format('| [[%s]]', drop.item))
        -- Sprite cell
         table.insert(html, string.format('| %s', quantity))
        local spriteWikitext = frame:preprocess(string.format('{{Sprite|%s|x2}}', sprite))
         table.insert(html, string.format('| data-sort-value="%d" | %s', rarityValue, drop.rarity or ''))
         row:tag('td')
            :css('text-align', 'center')
            :wikitext(spriteWikitext)
       
        -- Item cell
        row:tag('td')
            :wikitext(string.format('[[%s]]', drop.item))
          
        -- Quantity cell with sort value
        row:tag('td')
            :attr('data-sort-value', quantityValue)
            :wikitext(quantity)
          
        -- Rarity cell with sort value
        row:tag('td')
            :attr('data-sort-value', rarityValue)
            :wikitext(drop.rarity or '')
     end
     end
      
      
    table.insert(html, '|}')
     return tostring(tbl)
   
     return frame:preprocess(table.concat(html, '\n'))
end
end


return p
return p

Latest revision as of 01:05, 28 January 2026

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

local p = {}

-- Drop rarity sort order (ascending)
local rarityOrder = {
    common = 1,
    semirare = 2,
    rare = 3,
    veryrare = 4,
    ultrarare = 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

-- Get quantity sort value (max if present, otherwise min)
local function getQuantitySortValue(min, max)
    if max and max ~= '' then
        return tonumber(max) or 0
    end
    return tonumber(min) or 0
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, then quantity ascending
    table.sort(drops, function(a, b)
        local rarityA = getRarityValue(a.rarity)
        local rarityB = getRarityValue(b.rarity)
        if rarityA ~= rarityB then
            return rarityA < rarityB
        end
        return getQuantitySortValue(a.min, a.max) < getQuantitySortValue(b.min, b.max)
    end)
    
    -- Build table using mw.html
    local tbl = mw.html.create('table')
        :addClass('wikitable')
        :addClass('sortable')
    
    -- Header row
    local headerRow = tbl:tag('tr')
    headerRow:tag('th'):addClass('unsortable'):wikitext('')
    headerRow:tag('th'):wikitext('Item')
    headerRow:tag('th'):wikitext('Quantity')
    headerRow:tag('th'):wikitext('Rarity')
    
    for _, drop in ipairs(drops) do
        local sprite = getItemSprite(drop.item)
        local quantity = formatQuantity(drop.min, drop.max)
        local rarityValue = getRarityValue(drop.rarity)
        local quantityValue = getQuantitySortValue(drop.min, drop.max)
        
        local row = tbl:tag('tr')
        
        -- Sprite cell
        local spriteWikitext = frame:preprocess(string.format('{{Sprite|%s|x2}}', sprite))
        row:tag('td')
            :css('text-align', 'center')
            :wikitext(spriteWikitext)
        
        -- Item cell
        row:tag('td')
            :wikitext(string.format('[[%s]]', drop.item))
        
        -- Quantity cell with sort value
        row:tag('td')
            :attr('data-sort-value', quantityValue)
            :wikitext(quantity)
        
        -- Rarity cell with sort value
        row:tag('td')
            :attr('data-sort-value', rarityValue)
            :wikitext(drop.rarity or '')
    end
    
    return tostring(tbl)
end

return p