Module:HeroList
From Heroes of Might and Magic: Olden Era Official Wiki
Description
This module generates an automated table of heroes belonging to a specific faction. It dynamically retrieves localization for hero names, classes, and specializations, and formats the starting skills and spells into a clean, sortable layout.
Usage
The module is invoked via the display function:
{{#invoke:HeroList|display|faction=...|language=...}}
Parameters
| Parameter | Description | Example |
|---|---|---|
faction |
The name of the faction (as stored in the Translation table). | faction=Necropolis
|
language |
Forces the Wiki language (if omitted, it detects the page suffix). | language=fr
|
Key Features
- Dynamic Localization: Translates headers (Name, Class, etc.) and hero-specific data (Names, Skills, Spells) based on the provided language.
- Automatic Formatting:
- Displays hero portraits and icons.
- Formats specializations with tooltips showing descriptions.
- Lists starting skills and spells as link-lists with icons.
- Sorting Support: The table uses the
sortableclass, allowing users to reorder heroes based on columns. - Data Cleanup: Automatically filters out campaign, tutorial, and internal testing heroes (e.g.,
cm_fun).
Implementation Example
To display all heroes of the "Necropolis" faction:
== Necropolis Heroes ==
{{#invoke:HeroList|display|faction=Necropolis}}
Technical Notes
- The module queries the
Herotable and joins data with theTranslationtable. - It uses
sortabletables; ensure the MediaWiki "Sortable" extension is active for full functionality. - Specialization icons are automatically inferred from the
HeroSpecializationtable and formatted to match faction naming conventions.
Examples
{{#invoke:HeroList|display|faction=Necropolis}}
Lua error: Error: Table WikiTranslation not found..
{{#invoke:HeroList|display|faction=Grove|language=ja}}
Lua error: Error: Table WikiTranslation not found..
local p = {}
-- Fonction pour mettre la première lettre de chaque mot en majuscule
local function toTitleCase(str)
return str:gsub("(%a)([%w_]*)", function(first, rest)
return first:upper() .. rest:lower()
end)
end
-- Fonction pour récupérer les traductions Wiki
local function getWikiTrans(target_id, lang)
if not target_id or target_id == "" then return nil end
local query = mw.ext.cargo.query("WikiTranslation", "name", {
where = string.format("target_id = '%s' AND language = '%s'", target_id, lang),
limit = 1
})
if not query or #query == 0 then
query = mw.ext.cargo.query("WikiTranslation", "name", {
where = string.format("target_id = '%s' AND language = 'en'", target_id),
limit = 1
})
end
return (query and #query > 0) and query[1].name or nil
end
-- Fonction utilitaire pour récupérer une traduction jeu
local function getTrans(target_id, lang, type, field)
field = field or "name"
if not target_id or target_id == "" then return nil end
local query = mw.ext.cargo.query("Translation", field, {
where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, type),
limit = 1
})
if not query or #query == 0 then
query = mw.ext.cargo.query("Translation", "name", {
where = string.format("target_id = '%s' AND language = 'en' AND type = '%s'", target_id, type),
limit = 1
})
end
return (query and #query > 0) and query[1][field] or nil
end
function p.display(frame)
local raw_faction = frame.args.faction
local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
local h_name = getWikiTrans("wiki_name", lang) or "Name"
local h_class = getWikiTrans("wiki_hero_class", lang) or "Class"
local h_spec = getWikiTrans("wiki_hero_spec", lang) or "Specialization"
local h_skills = getTrans("starting_skills", lang, "ui") or "Starting Skills"
local h_spells = getTrans("starting_spells", lang, "ui") or "Starting Spells"
local faction_query = mw.ext.cargo.query("Translation", "target_id", {
where = string.format("name = '%s' AND type = 'faction'", raw_faction),
limit = 1
})
local faction_id = (faction_query and #faction_query > 0) and faction_query[1].target_id or raw_faction
local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id, start_skills, start_magics", {
where = string.format("faction = '%s' AND id NOT LIKE '%%campaign%%' AND id NOT LIKE '%%tutorial%%' AND id NOT LIKE '%%cm_fun%%'", faction_id)
})
table.sort(heroes, function(a, b)
local numA = tonumber(a.id:match("(%d+)$")) or 0
local numB = tonumber(b.id:match("(%d+)$")) or 0
return numA < numB
end)
local html = {'{| class="wikitable sortable" style="width:100%"'}
table.insert(html, string.format('! %s !! %s !! %s !! %s !! %s', h_name, h_class, h_spec, h_skills, h_spells))
for _, h in ipairs(heroes) do
local name = getTrans(h.name_sid, lang, 'hero') or h.name_sid
local file_name = getTrans(h.name_sid, 'en', 'hero') or h.id
local class_name = getTrans(h.class_id, lang, 'hero_class') or h.class_id
local class_name_en = getTrans(h.class_id, 'en', 'hero_class') or h.class_id
local faction_map = { ["human"] = "Temple", ["necro"] = "Necropolis", ["nature"] = "Grove", ["unfrozen"] = "Schism", ["demon"] = "Hive", ["dungeon"] = "Dungeon" }
local spec_name = getTrans(h.specialization_id, lang, 'hero_specialization') or h.specialization_id
local spec_desc = getTrans(h.specialization_id, lang, 'hero_specialization', 'description') or "Pas de description"
local clean_desc = spec_desc:gsub("\n", " "):gsub('"', '"'):gsub("'", "'")
local spec_icon_data = mw.ext.cargo.query("HeroSpecialization", "icon", {where = string.format("id = '%s'", h.specialization_id), limit = 1})
local icon_val = (spec_icon_data and #spec_icon_data > 0) and spec_icon_data[1].icon or ""
local prefix = icon_val:match("^(%a+)_") or "dungeon"
local replacement = faction_map[prefix] or toTitleCase(prefix)
local final_icon_name = toTitleCase(icon_val:gsub("^%a+_", replacement .. "_"):gsub("_", " "))
local spec_icon = string.format("[[File:%s.png|70px|link=]]", final_icon_name)
local spec_formatted = string.format("'''%s'''<br><span title=\"%s\" style=\"cursor: help;\"><u>''-(+++++)-''</u></span>", spec_name, clean_desc)
local skills_html = {}
if h.start_skills then
for skill_id in string.gmatch(h.start_skills, "([^,]+)") do
local s_name = getTrans(skill_id, lang, 'skill') or skill_id
local s_name_en = getTrans(skill_id, 'en', 'skill') or skill_id
local target_page = (s_name_en == "Summon Avatar") and "Summon Avatar (Skill)" or s_name_en
table.insert(skills_html, string.format("[[File:Skill_%s.png|25px|link=]] [[%s/%s|%s]]", s_name_en:gsub("%s+", "_"), target_page:gsub("%s+", "_"), lang, s_name))
end
end
local spells_html = {}
if h.start_magics then
for spell_id in string.gmatch(h.start_magics, "([^,]+)") do
if spell_id ~= "change_use_necromancy" then
local s_name = getTrans(spell_id, lang, 'spell') or spell_id
local s_name_en = getTrans(spell_id, 'en', 'spell') or spell_id
local clean_name_en = s_name_en:gsub("^Masterful%s+", ""):gsub("^Basic%s+", "")
local target_page = (clean_name_en == "Summon Avatar") and "Summon Avatar (Spell)" or clean_name_en
table.insert(spells_html, string.format("[[File:%s.png|25px|link=]] [[%s/%s|%s]]", clean_name_en:gsub("%s+", "_"), target_page:gsub("%s+", "_"), lang, s_name))
end
end
end
table.insert(html, "|-")
table.insert(html, string.format('| [[File:%s.png|70px|left]] [[%s/%s|%s]]', file_name, file_name, lang, name))
table.insert(html, string.format('| [[%s/%s|%s]]', class_name_en, lang, class_name))
table.insert(html, "| " .. spec_icon .. " " .. spec_formatted)
table.insert(html, "| " .. table.concat(skills_html, "<br>"))
table.insert(html, "| " .. table.concat(spells_html, "<br>"))
end
table.insert(html, "|}")
return table.concat(html, "\n")
end
return p


