Module:HeroList: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
-- ... (dans la fonction p.display)


-- Fonction utilitaire pour récupérer une traduction via Cargo
     -- 3. Construction du tableau (j'ai ajouté la colonne ID)
local function getTrans(target_id, lang, type)
     local html = {'{| class="wikitable sortable"', '! Portrait !! ID Technique !! Nom !! Classe !! Spécialisation'}
    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
    })
   
     -- Fallback sur l'anglais si rien trouvé
    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"
   
    -- 1. Récupération dynamique de l'ID de faction
    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
   
    -- 2. Récupération des héros
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id", {
        where = string.format("faction = '%s'", faction_id),
        orderBy = "id"
    })
   
    -- 3. Construction du tableau
     local html = {'{| class="wikitable sortable"', '! Portrait !! Nom !! Classe !! Spécialisation'}
      
      
     for _, h in ipairs(heroes) do
     for _, h in ipairs(heroes) do
Line 45: Line 8:
         local file_name = getTrans(h.name_sid, 'en', 'hero') or h.id
         local file_name = getTrans(h.name_sid, 'en', 'hero') or h.id
          
          
        -- Récupération de la classe (type 'class')
         local class_name = getTrans(h.class_id, lang, 'class') or h.class_id
         local class_name = getTrans(h.class_id, lang, 'class') or h.class_id
       
        -- Récupération de la spécialisation (type 'specialization')
         local spec_name = getTrans(h.specialization_id, lang, 'specialization') or h.specialization_id
         local spec_name = getTrans(h.specialization_id, lang, 'specialization') or h.specialization_id
          
          
         table.insert(html, "|-")
         table.insert(html, "|-")
         table.insert(html, string.format('| [[File:%s.png|70px]]', file_name))
         table.insert(html, string.format('| [[File:%s.png|70px]]', file_name))
        table.insert(html, "| " .. h.id) -- Colonne ID Technique
         table.insert(html, string.format('| [[%s|%s]]', file_name, name))
         table.insert(html, string.format('| [[%s|%s]]', file_name, name))
         table.insert(html, string.format('| %s', class_name))
         table.insert(html, string.format('| %s', class_name))
Line 60: Line 21:
     table.insert(html, "|}")
     table.insert(html, "|}")
     return table.concat(html, "\n")
     return table.concat(html, "\n")
end
-- ...
 
return p

Revision as of 16:37, 30 May 2026

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

Lua error at line 6: bad argument #1 to 'ipairs' (table expected, got nil).

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

Lua error at line 6: bad argument #1 to 'ipairs' (table expected, got nil).


-- ... (dans la fonction p.display)

    -- 3. Construction du tableau (j'ai ajouté la colonne ID)
    local html = {'{| class="wikitable sortable"', '! Portrait !! ID Technique !! 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
        
        local class_name = getTrans(h.class_id, lang, 'class') or h.class_id
        local spec_name = getTrans(h.specialization_id, lang, 'specialization') or h.specialization_id
        
        table.insert(html, "|-")
        table.insert(html, string.format('| [[File:%s.png|70px]]', file_name))
        table.insert(html, "| " .. h.id) -- Colonne ID Technique
        table.insert(html, string.format('| [[%s|%s]]', file_name, name))
        table.insert(html, string.format('| %s', class_name))
        table.insert(html, string.format('| %s', spec_name))
    end
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
-- ...