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 sortable class, 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 Hero table and joins data with the Translation table.
  • It uses sortable tables; ensure the MediaWiki "Sortable" extension is active for full functionality.
  • Specialization icons are automatically inferred from the HeroSpecialization table and formatted to match faction naming conventions.

Examples

{{#invoke:HeroList|display|faction=Necropolis}}
Portrait Nom Classe Icône Spécialisation Compétences Sorts
Bulwark Death Knight File:Necro Hero 1 Specialization Icon.png Built Different
-(+++++)-
Necromancy
Defense
File:Spell change use necromancy.png change_use_necromancy
King-of-Kings Death Knight File:Necro Hero 2 Specialization Icon.png Charismatic
-(+++++)-
Necromancy
Diplomacy
File:Spell change use necromancy.png change_use_necromancy
Onkos Death Knight File:Necro Hero 3 Specialization Icon.png Perfect Puppets
-(+++++)-
Necromancy
Offense
File:Spell change use necromancy.png change_use_necromancy
Kel'Ghul Death Knight File:Necro Hero 4 Specialization Icon.png Dreadful War
-(+++++)-
Necromancy
Tactics
File:Spell change use necromancy.png change_use_necromancy
Natalida Death Knight File:Necro Hero 5 Specialization Icon.png Part of a Pack
-(+++++)-
Necromancy
Scouting
File:Spell change use necromancy.png change_use_necromancy
Artorius Veritas Death Knight File:Necro Hero 6 Specialization Icon.png Drive 'em Crazy
-(+++++)-
Necromancy
Resistance
File:Spell night 13 magic berserker special.png night_13_magic_berserker_special
File:Spell change use necromancy.png change_use_necromancy
Marl Death Knight File:Necro Hero 7 Specialization Icon.png Languid Soil
-(+++++)-
Necromancy
Logistics
File:Spell night 2 magic web special.png night_2_magic_web_special
File:Spell change use necromancy.png change_use_necromancy
Tarius Death Knight File:Necro Hero 8 Specialization Icon.png Undead Legion
-(+++++)-
Necromancy File:Spell change use necromancy.png change_use_necromancy
Zam Death Knight File:Necro Hero 9 Specialization Icon.png Alchemist
-(+++++)-
Necromancy
Recruitment
File:Spell primal 18 magic primordial chaos.png primal_18_magic_primordial_chaos
File:Spell change use necromancy.png change_use_necromancy
Mag Necromancer File:Necro Hero 10 Specialization Icon.png Arcane Dominion
-(+++++)-
Necromancy
Arcane Magic
File:Spell space 9 magic impending fate.png space_9_magic_impending_fate
File:Spell change use necromancy.png change_use_necromancy
Adahn Necromancer File:Necro Hero 11 Specialization Icon.png Amnesiac
-(+++++)-
Necromancy
Battlecraft
File:Spell night 5 magic shade cloak.png night_5_magic_shade_cloak
File:Spell change use necromancy.png change_use_necromancy
Ethric Necromancer File:Necro Hero 12 Specialization Icon.png Tomb-bound Will
-(+++++)-
Necromancy
Wisdom
File:Spell day 10 magic second song.png day_10_magic_second_song
File:Spell change use necromancy.png change_use_necromancy
Guildmaster Klastor Necromancer File:Necro Hero 13 Specialization Icon.png Bones Guild
-(+++++)-
Necromancy
Luck
File:Spell night 18 magic nairas veil.png night_18_magic_nairas_veil
File:Spell change use necromancy.png change_use_necromancy
Shadespinner Oona Necromancer File:Necro Hero 14 Specialization Icon.png Shadowmastery
-(+++++)-
Necromancy
Nightshade Magic
File:Spell night 6 magic deaths grip.png night_6_magic_deaths_grip
File:Spell change use necromancy.png change_use_necromancy
Laura Necromancer File:Necro Hero 15 Specialization Icon.png Moldering
-(+++++)-
Necromancy
Sorcery
File:Spell night 4 magic despair special.png night_4_magic_despair_special
File:Spell change use necromancy.png change_use_necromancy
Lord Rufus Necromancer File:Necro Hero 16 Specialization Icon.png Wistful Nostalgia
-(+++++)-
Necromancy
Thaumaturgy
File:Spell space 12 magic rewind special.png space_12_magic_rewind_special
File:Spell change use necromancy.png change_use_necromancy
Funerella Necromancer File:Necro Hero 17 Specialization Icon.png Undead Legion
-(+++++)-
Necromancy
Summon Avatar
File:Spell bonus magic astral summon 1.png bonus_magic_astral_summon_1
File:Spell night 17 magic read minds.png night_17_magic_read_minds
File:Spell change use necromancy.png change_use_necromancy
Milossa the Golden Necromancer File:Necro Hero 18 Specialization Icon.png Wealthy
-(+++++)-
Necromancy
Battle Magic
File:Spell night 7 magic fatal decay.png night_7_magic_fatal_decay
File:Spell change use necromancy.png change_use_necromancy


{{#invoke:HeroList|display|faction=Grove|language=ja}}
Portrait Nom Classe Icône Spécialisation Compétences Sorts
イース ウォーデン File:Nature Hero 1 Specialization Icon.png パスファインダー
-(+++++)-
ざわめき
偵察
槍頭のゴレル ウォーデン File:Nature Hero 2 Specialization Icon.png 射手
-(+++++)-
ざわめき
攻勢
ジンジャーテール ウォーデン File:Nature Hero 3 Specialization Icon.png フォーンソング
-(+++++)-
ざわめき
統率
巡礼の老人 ウォーデン File:Nature Hero 4 Specialization Icon.png 自然の選択
-(+++++)-
ざわめき
戦闘魔法
File:Spell space 11 magic decimate special.png space_11_magic_decimate_special
オクタヴィア ウォーデン File:Nature Hero 5 Specialization Icon.png 四葉の馬蹄
-(+++++)-
ざわめき
武運
ニャオワ ウォーデン File:Nature Hero 6 Specialization Icon.png 魅惑の輝き
-(+++++)-
ざわめき
戦技
File:Spell night 13 magic berserker.png night_13_magic_berserker
ファレオール ウォーデン File:Nature Hero 7 Specialization Icon.png フクスミラの歩み
-(+++++)-
ざわめき File:Spell primal 4 magic fire globe special.png primal_4_magic_fire_globe_special
魅惑のシャー ウォーデン File:Nature Hero 8 Specialization Icon.png カリスマ
-(+++++)-
ざわめき
外交
ダリアおばさん ウォーデン File:Nature Hero 9 Specialization Icon.png 学びの願い
-(+++++)-
ざわめき
洞察
ヴァトウナ ドルイド File:Nature Hero 10 Specialization Icon.png 魂の活力
-(+++++)-
ざわめき
叡智
File:Spell primal 18 magic primordial chaos.png primal_18_magic_primordial_chaos
老ツ=キッシュ ドルイド File:Nature Hero 11 Specialization Icon.png 最古の樹
-(+++++)-
ざわめき
奇術
File:Spell space 3 magic energyze.png space_3_magic_energyze
エアリニエル ドルイド File:Nature Hero 12 Specialization Icon.png 穏やかな燃えさし
-(+++++)-
ざわめき
原初魔法
File:Spell primal 7 magic wall of flame special.png primal_7_magic_wall_of_flame_special
グレイシア ドルイド File:Nature Hero 13 Specialization Icon.png 氷撃
-(+++++)-
ざわめき
原初魔法
File:Spell primal 6 magic ice bolt special.png primal_6_magic_ice_bolt_special
ヴィム ドルイド File:Nature Hero 14 Specialization Icon.png ぶちかませ!
-(+++++)-
ざわめき
原初魔法
File:Spell primal 8 magic cave in special.png primal_8_magic_cave_in_special
ハロン ドルイド File:Nature Hero 15 Specialization Icon.png 轟雷
-(+++++)-
ざわめき
原初魔法
File:Spell primal 12 magic chain lightning special.png primal_12_magic_chain_lightning_special
エコリリー ドルイド File:Nature Hero 16 Specialization Icon.png ざわめきの模倣
-(+++++)-
ざわめき
秘術魔法
File:Spell space 10 magic mirror copy special.png space_10_magic_mirror_copy_special
サリー ドルイド File:Nature Hero 17 Specialization Icon.png ざわめきの森の原住民
-(+++++)-
ざわめき
化身召喚
File:Spell bonus magic astral summon nature 1.png bonus_magic_astral_summon_nature_1
File:Spell day 6 magic cleansing ray.png day_6_magic_cleansing_ray
吟遊詩人 ドルイド File:Nature Hero 18 Specialization Icon.png 放浪の音楽家
-(+++++)-
ざわめき
魔術知識
File:Spell space 2 magic energy explosion.png space_2_magic_energy_explosion

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 utilitaire pour récupérer une traduction
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 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
    
    -- Ajout de start_magics à la requête
    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"', '! Portrait !! Nom !! Classe !! Icône !! Spécialisation !! Compétences !! Sorts'}
    
    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
        
        -- SPÉCIALISATION
        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("\r", " "):gsub('"', '&quot;'):gsub("'", "&#39;"):gsub("<", "&lt;"):gsub(">", "&gt;")
        
        local icon_data = mw.ext.cargo.query("HeroSpecialization", "icon", {where = string.format("id = '%s'", h.specialization_id), limit = 1})
        local icon_val = (icon_data and #icon_data > 0) and icon_data[1].icon or ""
        local formatted_icon = toTitleCase(icon_val:gsub("_", " "))
        local spec_icon = string.format("[[File:%s.png|70px|link=]]", formatted_icon)
        local spec_formatted = string.format("'''%s'''<br><span title=\"%s\" style=\"cursor: help;\"><u>''-(+++++)-''</u></span>", spec_name, clean_desc)
        
        -- COMPÉTENCES
        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 icon_name = s_name_en:gsub("%s+", "_")
                local s_icon = string.format("[[File:Skill_%s.png|25px|link=]]", icon_name)
                local s_link = string.format("[[%s/%s|%s]]", s_name_en:gsub("%s+", "_"), lang, s_name)
                table.insert(skills_html, s_icon .. " " .. s_link)
            end
        end
        
        -- SORTS DE DÉPART
        local spells_html = {}
        if h.start_magics then
            for spell_id in string.gmatch(h.start_magics, "([^,]+)") do
                local s_name = getTrans(spell_id, lang, 'magic') or spell_id
                local s_name_en = getTrans(spell_id, 'en', 'magic') or spell_id
                local icon_name = s_name_en:gsub("%s+", "_")
                local s_icon = string.format("[[File:Spell_%s.png|25px|link=]]", icon_name)
                local s_link = string.format("[[%s/%s|%s]]", s_name_en:gsub("%s+", "_"), lang, s_name)
                table.insert(spells_html, s_icon .. " " .. s_link)
            end
        end
        
        table.insert(html, "|-")
        table.insert(html, string.format('| [[File:%s.png|70px]]', file_name))
        table.insert(html, string.format('| [[%s|%s]]', file_name, name))
        table.insert(html, "| " .. class_name)
        table.insert(html, "| " .. spec_icon)
        table.insert(html, "| " .. 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