Jump to content

Module:ItemSources: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Rewrite to use Cargo with sortable table and proper data-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)
Line 49: Line 49:
     end
     end
      
      
     -- Sort by rarity (ascending)
     -- Sort by rarity ascending, then quantity ascending
     table.sort(sources, function(a, b)
     table.sort(sources, 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)
      
      

Revision as of 01:04, 28 January 2026

Module:ItemSources

This module displays item drop sources from NPCs in a table format.

Basic Usage

{{#invoke:ItemSources|list
|source1=NPC Name,Quantity,Rarity
|source2=NPC Name,Quantity,Rarity
}}

Source Format

Each source uses the format:

NPC Name,Quantity,Rarity
  • NPC Name - Required. The name of the NPC (automatically linked)
  • Quantity - Optional. Amount dropped (default: 1). Can be a range like "2-4"
  • Rarity - Optional. Drop rate like "1/50", "Rare", or "Always"

Parameters

Parameter Required Description
source1, source2, etc. Yes Source entries in "NPC,Quantity,Rarity" format
s1, s2, etc. No Shorthand for source1, source2, etc.

Examples

Basic Drop Table

{{#invoke:ItemSources|list
|source1=Goblin,1,1/50
|source2=Orc Warrior,2-4,1/100
|source3=Dragon,5,1/500
}}

Without Rarity

If no sources have rarity specified, the Rarity column is automatically hidden:

{{#invoke:ItemSources|list
|source1=Goblin,1
|source2=Orc Warrior,3
|source3=Chest,1
}}

Mixed Rarity

Some sources can have rarity while others don't:

{{#invoke:ItemSources|list
|source1=Cave Troll,1
|source2=Tomb Worker,1
|source3=Bear,1,Semi Rare
|source4=Bandit,1,Rare
}}

Using Shorthand

{{#invoke:ItemSources|list
|s1=Goblin,1,1/50
|s2=Orc,2,1/100
}}

Guaranteed Drops

{{#invoke:ItemSources|list
|source1=Quest Reward,1,Always
|source2=Boss,1,100%
|source3=Chest,1-3,Always
}}

Table Columns

Column Description
Source Linked name of the NPC
Quantity Amount dropped (can be a range)
Rarity Drop rate (only shown if any source has rarity)

Notes

  • The Rarity column only appears if at least one source has a rarity value
  • NPC names are automatically linked using [[NPC Name]]
  • Quantity can be a single number or a range (e.g., "1-5", "2-4")

local p = {}

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

-- 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 item sources
function p.display(frame)
    local args = frame:getParent().args
    local item = args.item or args[1] or mw.title.getCurrentTitle().text
    
    -- Query ItemSource for sources
    local sources = mw.ext.cargo.query('ItemSource', 'source,source_type,rarity,min,max', {
        where = 'item="' .. item .. '"'
    })
    
    if not sources or #sources == 0 then
        return ''
    end
    
    -- Sort by rarity ascending, then quantity ascending
    table.sort(sources, 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('Source')
    headerRow:tag('th'):wikitext('Quantity')
    headerRow:tag('th'):wikitext('Rarity')
    
    for _, src in ipairs(sources) do
        local quantity = formatQuantity(src.min, src.max)
        local rarityValue = getRarityValue(src.rarity)
        local quantityValue = getQuantitySortValue(src.min, src.max)
        
        local row = tbl:tag('tr')
        
        -- Preview cell (use MonsterPreview for monsters)
        local previewWikitext = frame:preprocess(string.format('{{MonsterPreview|%s|x2}}', src.source))
        row:tag('td')
            :css('text-align', 'center')
            :wikitext(previewWikitext)
        
        -- Source cell
        row:tag('td')
            :wikitext(string.format('[[%s]]', src.source))
        
        -- 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(src.rarity or '')
    end
    
    return tostring(tbl)
end

return p