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}}


{{#invoke:HeroList|display|faction=Grove|language=ja}}

local p = {}

-- Fonction de récupération SIMPLIFIÉE et ROBUSTE
-- Effectue une requête explicite pour un ID et une langue donnée
local function getTransDirect(target_id, lang)
    -- Sécurité de base
    if not target_id then return nil end
    
    local query = mw.ext.cargo.query("Translation", "name", {
        where = string.format("target_id = '%s' AND language = '%s'", target_id, lang),
        limit = 1
    })
    
    if query and #query > 0 then
        -- Si on trouve, on renvoie le nom exact
        return query[1].name
    else
        -- Si on ne trouve pas, on effectue un repli explicite sur l'anglais
        -- sauf si on cherchait déjà l'anglais
        if lang ~= 'en' then
            local fallback = mw.ext.cargo.query("Translation", "name", {
                where = string.format("target_id = '%s' AND language = 'en'", target_id),
                limit = 1
            })
            if fallback and #fallback > 0 then
                return fallback[1].name
            end
        end
    end
    
    -- Si vraiment rien n'est trouvé, on renvoie nil pour indiquer l'absence de donnée
    return nil
end

function p.display(frame)
    local faction = frame.args.faction
    local lang = frame.args.lang or mw.language.getContentLanguage():getCode()
    
    -- 1. Récupération des héros
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id, start_skills, start_magics, icon", {
        where = string.format("faction = '%s'", faction), orderBy = "id"
    })
    
    -- 2. Construction du tableau
    local html = {'{| class="wikitable sortable"', '! Portrait !! Nom !! Classe !! Icône Spéc. !! Spécialisation !! Compétences !! Sort(s)'}
    
    for _, h in ipairs(heroes) do
        -- RÉCUPÉRATION SÉCURISÉE DES DONNÉES
        
        -- Nom pour le fichier : On cherche l'anglais explicitement
        local h_name_en = getTransDirect(h.name_sid, 'en')
        -- Si l'anglais manque (cas critique !), on utilise l'ID, mais on affiche un avertissement
        if not h_name_en then h_name_en = "Missing English Translation For " .. h.name_sid end
        
        -- Nom pour l'affichage localisé (ex: fr, ou fallback en)
        local h_name_loc = getTransDirect(h.name_sid, lang) or h_name_en
        
        -- Nom de classe localisé
        local class_name_loc = getTransDirect(h.class_id, lang) or h.class_id
        
        -- Nom de spécialisation localisé
        local spec_name_loc = getTransDirect(h.specialization_id, lang) or "Spécialisation"
        
        -- ... [suite du code, inchangée car la logique des skills utilise déjà 's_name_en'] ...
    end
    
    -- [suite du code, inchangée] ...
end

return p