Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Mob: Difference between revisions

From Mine in Abyss
No edit summary
No edit summary
Line 40: Line 40:
             local amountStr = drop.lootingWorks and ("<b>" .. drop.amount .. "</b>") or drop.amount
             local amountStr = drop.lootingWorks and ("<b>" .. drop.amount .. "</b>") or drop.amount
              
              
             -- Uses the {{Slot}} template dynamically
             -- Expand the 'Slot' template dynamically before appending it to the string
            local slotItemHtml = frame:expandTemplate{ title = 'Slot', args = { drop.item } }
           
             dropsHtml = dropsHtml .. string.format(
             dropsHtml = dropsHtml .. string.format(
                 '<tr><td>{{Slot|%s}}</td><td>%s</td><td title="%s">%s</td></tr>\n',  
                 '<tr><td>%s</td><td>%s</td><td title="%s">%s</td></tr>\n',  
                 drop.item, drop.chance, title, amountStr
                 slotItemHtml, drop.chance, title, amountStr
             )
             )
         end
         end

Revision as of 22:03, 11 May 2026

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

local p = {}

-- Load the data from the database module using mw.loadData for caching efficiency
local db = mw.loadData('Module:Mob/Aliases')

function p.main(frame)
    -- Get arguments from either the module call or the parent template
    local args = frame:getParent().args
    local mobName = args[1] or frame.args[1]

    if not mobName or mobName == "" then
        return "<strong class='error'>Error: Please specify a mob name.</strong>"
    end

    local mob = db[mobName]
    if not mob then
        return "<strong class='error'>Error: Mob '" .. mobName .. "' not found in database.</strong>"
    end

    -- 1. Format the layers (avoiding the # operator which fails on mw.loadData)
    local layersHtml = {}
    if mob.layers then
        for _, layer in ipairs(mob.layers) do
            table.insert(layersHtml, "[[File:" .. layer .. ".png|16px|link=]]")
        end
    end

    -- 2. Format the drops table (avoiding the # operator)
    local dropsHtml = ""
    if mob.drops then
        local hasDrops = false
        for _, drop in ipairs(mob.drops) do
            -- Only initialize the table header if there is at least one valid drop
            if not hasDrops then
                dropsHtml = '<table class="wikitable" style="width:100%; margin-top:5px;">\n<tr><th>Drop</th><th>Chance</th><th>Amount</th></tr>\n'
                hasDrops = true
            end
            
            local title = drop.lootingWorks and "Looting works" or "No looting"
            local amountStr = drop.lootingWorks and ("<b>" .. drop.amount .. "</b>") or drop.amount
            
            -- Expand the 'Slot' template dynamically before appending it to the string
            local slotItemHtml = frame:expandTemplate{ title = 'Slot', args = { drop.item } }
            
            dropsHtml = dropsHtml .. string.format(
                '<tr><td>%s</td><td>%s</td><td title="%s">%s</td></tr>\n', 
                slotItemHtml, drop.chance, title, amountStr
            )
        end
        if hasDrops then
            dropsHtml = dropsHtml .. "</table>"
        end
    end

    -- 3. Construct the arguments to pass to the visual Template
    local targs = {
        name = mob.name,
        namecolor = mob.nameColor or "inherit",
        health = tostring(mob.health),
        category = mob.category,
        layers = table.concat(layersHtml, " "),
        drops = dropsHtml,
        image = args.image or args[2] or "" 
    }

    -- 4. Pass the calculated data to the layout template
    return frame:expandTemplate{ title = 'Template:Mob/Layout', args = targs }
end

return p