Module:Mob: Difference between revisions
From Mine in Abyss
More actions
Created page with "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[..." |
No edit summary |
||
| Line 18: | Line 18: | ||
end | end | ||
-- 1. Format the layers | -- 1. Format the layers (avoiding the # operator which fails on mw.loadData) | ||
local layersHtml = {} | local layersHtml = {} | ||
if mob.layers | if mob.layers then | ||
for _, layer in ipairs(mob.layers) do | for _, layer in ipairs(mob.layers) do | ||
table.insert(layersHtml, "[[File:" .. layer .. ".png|16px|link=]]") | table.insert(layersHtml, "[[File:" .. layer .. ".png|16px|link=]]") | ||
| Line 26: | Line 26: | ||
end | end | ||
-- 2. Format the drops table | -- 2. Format the drops table (avoiding the # operator) | ||
local dropsHtml = "" | local dropsHtml = "" | ||
if mob.drops | if mob.drops then | ||
local hasDrops = false | |||
for _, drop in ipairs(mob.drops) do | 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 title = drop.lootingWorks and "Looting works" or "No looting" | ||
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 | -- Uses the {{Slot}} template dynamically | ||
dropsHtml = dropsHtml .. string.format( | dropsHtml = dropsHtml .. string.format( | ||
| Line 39: | Line 46: | ||
) | ) | ||
end | end | ||
dropsHtml = dropsHtml .. "</table>" | if hasDrops then | ||
dropsHtml = dropsHtml .. "</table>" | |||
end | |||
end | end | ||
| Line 50: | Line 59: | ||
layers = table.concat(layersHtml, " "), | layers = table.concat(layersHtml, " "), | ||
drops = dropsHtml, | drops = dropsHtml, | ||
image = args.image or args[2] or "" | image = args.image or args[2] or "" | ||
} | } | ||
Revision as of 22:00, 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
-- Uses the {{Slot}} template dynamically
dropsHtml = dropsHtml .. string.format(
'<tr><td>{{Slot|%s}}</td><td>%s</td><td title="%s">%s</td></tr>\n',
drop.item, 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