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 Spécialisation
Bulwark Death Knight File:Icon necro hero 1 specialization.png necro_hero_1_specialization
King-of-Kings Death Knight File:Icon necro hero 2 specialization.png necro_hero_2_specialization
Onkos Death Knight File:Icon necro hero 3 specialization.png necro_hero_3_specialization
Kel'Ghul Death Knight File:Icon necro hero 4 specialization.png necro_hero_4_specialization
Natalida Death Knight File:Icon necro hero 5 specialization.png necro_hero_5_specialization
Artorius Veritas Death Knight File:Icon necro hero 6 specialization.png necro_hero_6_specialization
Marl Death Knight File:Icon necro hero 7 specialization.png necro_hero_7_specialization
Tarius Death Knight File:Icon necro hero 8 specialization.png necro_hero_8_specialization
Zam Death Knight File:Icon necro hero 9 specialization.png necro_hero_9_specialization
Mag Necromancer File:Icon necro hero 10 specialization.png necro_hero_10_specialization
Adahn Necromancer File:Icon necro hero 11 specialization.png necro_hero_11_specialization
Ethric Necromancer File:Icon necro hero 12 specialization.png necro_hero_12_specialization
Guildmaster Klastor Necromancer File:Icon necro hero 13 specialization.png necro_hero_13_specialization
Shadespinner Oona Necromancer File:Icon necro hero 14 specialization.png necro_hero_14_specialization
Laura Necromancer File:Icon necro hero 15 specialization.png necro_hero_15_specialization
Lord Rufus Necromancer File:Icon necro hero 16 specialization.png necro_hero_16_specialization
Funerella Necromancer File:Icon necro hero 17 specialization.png necro_hero_17_specialization
Milossa the Golden Necromancer File:Icon necro hero 18 specialization.png necro_hero_18_specialization


{{#invoke:HeroList|display|faction=Grove|language=ja}}
Portrait Nom Classe Spécialisation
イース ウォーデン File:Icon nature hero 1 specialization.png nature_hero_1_specialization
槍頭のゴレル ウォーデン File:Icon nature hero 2 specialization.png nature_hero_2_specialization
ジンジャーテール ウォーデン File:Icon nature hero 3 specialization.png nature_hero_3_specialization
巡礼の老人 ウォーデン File:Icon nature hero 4 specialization.png nature_hero_4_specialization
オクタヴィア ウォーデン File:Icon nature hero 5 specialization.png nature_hero_5_specialization
ニャオワ ウォーデン File:Icon nature hero 6 specialization.png nature_hero_6_specialization
ファレオール ウォーデン File:Icon nature hero 7 specialization.png nature_hero_7_specialization
魅惑のシャー ウォーデン File:Icon nature hero 8 specialization.png nature_hero_8_specialization
ダリアおばさん ウォーデン File:Icon nature hero 9 specialization.png nature_hero_9_specialization
ヴァトウナ ドルイド File:Icon nature hero 10 specialization.png nature_hero_10_specialization
老ツ=キッシュ ドルイド File:Icon nature hero 11 specialization.png nature_hero_11_specialization
エアリニエル ドルイド File:Icon nature hero 12 specialization.png nature_hero_12_specialization
グレイシア ドルイド File:Icon nature hero 13 specialization.png nature_hero_13_specialization
ヴィム ドルイド File:Icon nature hero 14 specialization.png nature_hero_14_specialization
ハロン ドルイド File:Icon nature hero 15 specialization.png nature_hero_15_specialization
エコリリー ドルイド File:Icon nature hero 16 specialization.png nature_hero_16_specialization
サリー ドルイド File:Icon nature hero 17 specialization.png nature_hero_17_specialization
吟遊詩人 ドルイド File:Icon nature hero 18 specialization.png nature_hero_18_specialization

local p = {}

-- Fonction utilitaire pour récupérer une traduction
local function getTrans(target_id, lang, type)
    if not target_id or target_id == "" then return nil end
    local query = mw.ext.cargo.query("Translation", "name", {
        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].name 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
    
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id", {
        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 !! Spécialisation'}
    
    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
        
        -- 1. Récupération des noms traduits
        local class_name = getTrans(h.class_id, lang, 'hero_class') or h.class_id
        local spec_name = getTrans(h.specialization_id, lang, 'specialization') or h.specialization_id
        
        -- 2. Récupération du nom anglais pour le nom du fichier image
        local class_en = getTrans(h.class_id, 'en', 'hero_class') or h.class_id
        
        -- 3. Construction du lien dynamique : [[NomAnglais/lang|NomTraduit]]
        local class_link = string.format("[[%s/%s|%s]]", class_en, lang, class_name)
        
        -- 4. Construction des icônes (utilise le nom anglais)
        local class_icon = string.format("[[File:%s.png|25px|link=]] ", class_en)
        local spec_icon = string.format("[[File:Icon_%s.png|25px|link=]] ", h.specialization_id)
        
        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_icon .. class_link)
        table.insert(html, "| " .. spec_icon .. spec_name)
    end
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
end

return p