Module:Sandbox/Aurelain: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| Line 13: | Line 13: | ||
end | end | ||
-- Define the stats you want to display and how their headers should look. | |||
-- 'property' is the string passed to your Unit.get function. | |||
local statColumns = { | |||
{ property = "Initiative", header = "[[File:Icon_Stats_Initiative.png|30px|Initiative]]" }, | |||
{ property = "Speed", header = "[[File:Icon_Stats_Speed.png|30px|Speed]]" }, | |||
{ property = "HP", header = "[[File:Icon_Stats_HP.png|30px|HP]]" } | |||
-- Add more stats here as needed | |||
} | |||
function p. | -- Define the list of units to include in the table. | ||
-- (This could also be fetched dynamically from SMW/Cargo or frame arguments if needed). | |||
local unitList = { | |||
"Angel", | |||
"Archangel", | |||
"Apotheosis" | |||
} | |||
function p.renderStatsOverview(frame) | |||
-- Allow passing language code from the template, defaulting to 'en' | |||
local args = frame.args | |||
local lang = mw.text.trim(args[1] or args.lang or "en") | |||
-- We use an array to collect wikitext pieces for better performance | |||
local output = {} | |||
-- 1. Start the sortable wikitext table | |||
table.insert(output, '{| class="wikitable sortable"') | |||
-- 2. Construct the Header Row | |||
local headerRow = "! Unit" | |||
for _, col in ipairs(statColumns) do | |||
headerRow = headerRow .. " !! " .. col.header | |||
end | |||
table.insert(output, headerRow) | |||
-- 3. Loop through each unit to construct the data rows | |||
for _, unitName in ipairs(unitList) do | |||
table.insert(output, '|-') | |||
-- Add the unit name column (formatted as a wiki link) | |||
table.insert(output, '| [[' .. unitName .. ']]') | |||
-- Loop through each configured stat column and fetch the value | |||
for _, col in ipairs(statColumns) do | |||
local statValue = get(unitName, col.property, lang) | |||
-- Fallback to a dash or '0' if the stat isn't found to prevent table breakage | |||
if not statValue or statValue == "" then | |||
statValue = "-" | |||
end | |||
table.insert(output, '| ' .. statValue) | |||
end | |||
end | |||
-- 4. Close the table | |||
table.insert(output, '|}') | |||
-- Combine all pieces with newline characters and return the final wikitext | |||
return table.concat(output, '\n') | |||
end | end | ||
return p | return p | ||
Revision as of 15:31, 14 May 2026
Documentation for this module may be created at Module:Sandbox/Aurelain/doc
local p = {}
local Unit = require('Module:Unit')
-- Wrapper function that allows us to easily invoke the Module:Unit.
local function get(unit, property, lang)
return Unit.get({
args = {
["unit"] = unit,
["property"] = property,
["lang"] = lang
}
})
end
-- Define the stats you want to display and how their headers should look.
-- 'property' is the string passed to your Unit.get function.
local statColumns = {
{ property = "Initiative", header = "[[File:Icon_Stats_Initiative.png|30px|Initiative]]" },
{ property = "Speed", header = "[[File:Icon_Stats_Speed.png|30px|Speed]]" },
{ property = "HP", header = "[[File:Icon_Stats_HP.png|30px|HP]]" }
-- Add more stats here as needed
}
-- Define the list of units to include in the table.
-- (This could also be fetched dynamically from SMW/Cargo or frame arguments if needed).
local unitList = {
"Angel",
"Archangel",
"Apotheosis"
}
function p.renderStatsOverview(frame)
-- Allow passing language code from the template, defaulting to 'en'
local args = frame.args
local lang = mw.text.trim(args[1] or args.lang or "en")
-- We use an array to collect wikitext pieces for better performance
local output = {}
-- 1. Start the sortable wikitext table
table.insert(output, '{| class="wikitable sortable"')
-- 2. Construct the Header Row
local headerRow = "! Unit"
for _, col in ipairs(statColumns) do
headerRow = headerRow .. " !! " .. col.header
end
table.insert(output, headerRow)
-- 3. Loop through each unit to construct the data rows
for _, unitName in ipairs(unitList) do
table.insert(output, '|-')
-- Add the unit name column (formatted as a wiki link)
table.insert(output, '| [[' .. unitName .. ']]')
-- Loop through each configured stat column and fetch the value
for _, col in ipairs(statColumns) do
local statValue = get(unitName, col.property, lang)
-- Fallback to a dash or '0' if the stat isn't found to prevent table breakage
if not statValue or statValue == "" then
statValue = "-"
end
table.insert(output, '| ' .. statValue)
end
end
-- 4. Close the table
table.insert(output, '|}')
-- Combine all pieces with newline characters and return the final wikitext
return table.concat(output, '\n')
end
return p


