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
Bulwark Death Knight File:Necro Hero 1 Specialization Icon.png Built Different
(+++++)
Necromancy
Defense
King-of-Kings Death Knight File:Necro Hero 2 Specialization Icon.png Charismatic
(+++++)
Necromancy
Diplomacy
Onkos Death Knight File:Necro Hero 3 Specialization Icon.png Perfect Puppets
(+++++)
Necromancy
Offense
Kel'Ghul Death Knight File:Necro Hero 4 Specialization Icon.png Dreadful War
(+++++)
Necromancy
Tactics
Natalida Death Knight File:Necro Hero 5 Specialization Icon.png Part of a Pack
(+++++)
Necromancy
Scouting
Artorius Veritas Death Knight File:Necro Hero 6 Specialization Icon.png Drive 'em Crazy
(+++++)
Necromancy
Resistance
Marl Death Knight File:Necro Hero 7 Specialization Icon.png Languid Soil
(+++++)
Necromancy
Logistics
Tarius Death Knight File:Necro Hero 8 Specialization Icon.png Undead Legion
(+++++)
Necromancy
Zam Death Knight File:Necro Hero 9 Specialization Icon.png Alchemist
(+++++)
Necromancy
Recruitment
Mag Necromancer File:Necro Hero 10 Specialization Icon.png Arcane Dominion
(+++++)
Necromancy
Arcane Magic
Adahn Necromancer File:Necro Hero 11 Specialization Icon.png Amnesiac
(+++++)
Necromancy
Battlecraft
Ethric Necromancer File:Necro Hero 12 Specialization Icon.png Tomb-bound Will
(+++++)
Necromancy
Wisdom
Guildmaster Klastor Necromancer File:Necro Hero 13 Specialization Icon.png Bones Guild
(+++++)
Necromancy
Luck
Shadespinner Oona Necromancer File:Necro Hero 14 Specialization Icon.png Shadowmastery
(+++++)
Necromancy
Nightshade Magic
Laura Necromancer File:Necro Hero 15 Specialization Icon.png Moldering
(+++++)
Necromancy
Sorcery
Lord Rufus Necromancer File:Necro Hero 16 Specialization Icon.png Wistful Nostalgia
(+++++)
Necromancy
Thaumaturgy
Funerella Necromancer File:Necro Hero 17 Specialization Icon.png Undead Legion
(+++++)
Necromancy
Summon Avatar
Milossa the Golden Necromancer File:Necro Hero 18 Specialization Icon.png Wealthy
(+++++)
Necromancy
Battle Magic


{{#invoke:HeroList|display|faction=Grove|language=ja}}
Portrait Nom Classe Icône Spécialisation Compétences
イース ウォーデン 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:Nature Hero 5 Specialization Icon.png 四葉の馬蹄
(+++++)
ざわめき
武運
ニャオワ ウォーデン File:Nature Hero 6 Specialization Icon.png 魅惑の輝き
(+++++)
ざわめき
戦技
ファレオール ウォーデン File:Nature Hero 7 Specialization Icon.png フクスミラの歩み
(+++++)
ざわめき
魅惑のシャー ウォーデン File:Nature Hero 8 Specialization Icon.png カリスマ
(+++++)
ざわめき
外交
ダリアおばさん ウォーデン File:Nature Hero 9 Specialization Icon.png 学びの願い
(+++++)
ざわめき
洞察
ヴァトウナ ドルイド File:Nature Hero 10 Specialization Icon.png 魂の活力
(+++++)
ざわめき
叡智
老ツ=キッシュ ドルイド File:Nature Hero 11 Specialization Icon.png 最古の樹
(+++++)
ざわめき
奇術
エアリニエル ドルイド File:Nature Hero 12 Specialization Icon.png 穏やかな燃えさし
(+++++)
ざわめき
原初魔法
グレイシア ドルイド File:Nature Hero 13 Specialization Icon.png 氷撃
(+++++)
ざわめき
原初魔法
ヴィム ドルイド File:Nature Hero 14 Specialization Icon.png ぶちかませ!
(+++++)
ざわめき
原初魔法
ハロン ドルイド File:Nature Hero 15 Specialization Icon.png 轟雷
(+++++)
ざわめき
原初魔法
エコリリー ドルイド File:Nature Hero 16 Specialization Icon.png ざわめきの模倣
(+++++)
ざわめき
秘術魔法
サリー ドルイド File:Nature Hero 17 Specialization Icon.png ざわめきの森の原住民
(+++++)
ざわめき
化身召喚
吟遊詩人 ドルイド File:Nature Hero 18 Specialization Icon.png 放浪の音楽家
(+++++)
ざわめき
魔術知識

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_skills à la requête
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id, start_skills", {
        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'}
    
    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
                
                -- Formatage icône : "Skill_Nom_Anglais.png"
                -- On remplace les espaces par des underscores
                local icon_name = s_name_en:gsub("%s+", "_")
                local s_icon = string.format("[[File:Skill_%s.png|25px|link=]]", icon_name)
                
                table.insert(skills_html, s_icon .. " " .. s_name)
            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>"))
    end
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
end

return p