Module:HeroListByClass

From Heroes of Might and Magic: Olden Era Official Wiki

Documentation for this module may be created at Module:HeroListByClass/doc

local p = {}

-- Fonction utilitaire pour récupérer une traduction (adaptée pour accepter plusieurs types)
local function getTrans(target_id, lang, type_force, field)
    field = field or "name"
    if not target_id or target_id == "" then return nil end
    local types = type_force and {type_force} or {"wiki", "ui", "hero", "hero_class", "skill", "spell", "hero_specialization"}
    
    for _, t in ipairs(types) do
        local query = mw.ext.cargo.query("Translation", field, {
            where = string.format("target_id = '%s' AND language = '%s' AND type = '%s'", target_id, lang, t),
            limit = 1
        })
        if query and #query > 0 then return query[1][field] end
    end
    return nil
end

function p.display(frame)
    local class_filter = frame.args.class_id -- On récupère le filtre de classe
    local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
    
    -- En-têtes
    local h_name = getTrans("wiki_name", lang, "wiki") or "Name"
    local h_spec = getTrans("wiki_hero_spec", lang, "wiki") or "Specialization"
    local h_skills = getTrans("starting_skills", lang, "ui") or "Starting Skills"
    local h_spells = getTrans("starting_spells", lang, "ui") or "Starting Spells"
    
    -- Construction de la requête Cargo avec filtre optionnel
    local where_clause = "id NOT LIKE '%campaign%' AND id NOT LIKE '%tutorial%' AND id NOT LIKE '%cm_fun%'"
    if class_filter and class_filter ~= "" then
        where_clause = where_clause .. string.format(" AND class_id = '%s'", class_filter)
    end
    
    -- TEST DE DÉBOGAGE (Affiche le nombre de résultats en haut du tableau)
local count_test = mw.ext.cargo.query("Hero", "COUNT(*)", {where = where_clause})
local total = (count_test and #count_test > 0) and count_test[1]["COUNT(*)"] or 0
table.insert(html, "<tr><td colspan='4'>DEBUG: Nombre de héros trouvés pour '" .. (class_filter or "tous") .. "' = " .. total .. "</td></tr>")
    
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id, start_skills, start_magics", {
        where = where_clause
    })
    
    -- Construction du tableau
    local html = {'{| class="wikitable sortable" style="width:100%"'}
    table.insert(html, string.format('! %s !! %s !! %s !! %s', h_name, h_spec, h_skills, h_spells))
    
    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
        
        -- 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 ""
        local clean_desc = spec_desc:gsub('"', '&quot;'):gsub("'", "&#39;")
        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 "Default"
        local spec_icon = string.format("[[File:%s.png|50px|link=]]", icon_val:gsub("_", " "):gsub("(%a+)", tostring):gsub("^%l", string.upper))
        
        -- ... (Conserver ici ta logique de construction des listes skills_html et spells_html comme dans ton modèle) ...
        -- (Pour la brièveté, j'ai omis la boucle, mais tu peux copier celle de ton précédent script)

        table.insert(html, '|- style="vertical-align: middle;"')
        table.insert(html, string.format('| [[File:%s.png|70px|link=]] [[%s/%s|%s]]', file_name, file_name, lang, name))
        local spec_formatted = string.format("'''%s'''<br><span title=%q style=\"cursor: help;\"><u>''-(Spé)-''</u></span>", spec_name, clean_desc)
        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