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

-- Fonction utilitaire pour Title Case
local function toTitleCase(str)
    return str:gsub("(%a)([%w_]*)", function(first, rest) return first:upper() .. rest:lower() end)
end

function p.display(frame)
    local class_filter = frame.args.class_id
    local lang = frame.args.language or mw.title.getCurrentTitle().text:match("/(%a%a)$") or "en"
    
    local class_map = {
        ["herald"] = "magic_demon", ["warlock"] = "magic_dungeon", ["cleric"] = "magic_human",
        ["druid"] = "magic_nature", ["necromancer"] = "magic_undead", ["riftspeaker"] = "magic_unfrozen",
        ["enforcer"] = "might_demon", ["overlord"] = "might_dungeon", ["warden"] = "might_nature",
        ["death knight"] = "might_undead", ["oathkeeper"] = "might_unfrozen", ["knight"] = "might_human"
    }
    
    local html = {'{| class="wikitable sortable" style="width:100%"'}
    
    local where_clause = "id NOT LIKE '%campaign%' AND id NOT LIKE '%tutorial%' AND id NOT LIKE '%cm_fun%'"
    local mapped_id = nil
    if class_filter and class_filter ~= "" then
        mapped_id = class_map[class_filter:lower()] or class_filter
        where_clause = where_clause .. string.format(" AND class_id = '%s'", mapped_id)
    end
    
    local heroes = mw.ext.cargo.query("Hero", "id, name_sid, class_id, specialization_id, start_skills, start_magics", {where = where_clause})
    
    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"
    
    table.insert(html, string.format('! colspan=2 | %s !! colspan=2 | %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 "No description"
        
        -- NETTOYAGE STRICT
        -- 1. Supprimer les retours à la ligne réels
        local clean_desc = spec_desc:gsub("\n", " ") 
        -- 2. Échapper les guillemets pour l'attribut title
        clean_desc = clean_desc:gsub('"', '"')
        
        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"
        
        -- Nettoyage du nom de fichier
        local clean_icon = icon_val:gsub("_", " "):gsub("(%a)([%w_]*)", function(first, rest) return first:upper() .. rest:lower() end)
        local spec_icon = string.format("[[File:%s.png|60px|link=]]", clean_icon)
        
        -- Utilisation d'une structure plus simple pour éviter les erreurs d'interprétation
        local spec_formatted = string.format("'''%s'''<br><span title=\"%s\" style=\"cursor: help; border-bottom: 1px dotted black;\"><u>''-(Spé)-''</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
                local s_icon = string.format("[[File:Skill_%s.png|25px|link=]]", s_name_en:gsub("%s+", "_"))
                table.insert(skills_html, s_icon .. " " .. s_name)
            end
        end
        
        -- SORTS
        local spells_html = {}
        if h.start_magics then
            for spell_id in string.gmatch(h.start_magics, "([^,]+)") do
                local s_name = getTrans(spell_id, lang, 'spell') or spell_id
                local s_name_en = getTrans(spell_id, 'en', 'spell') or spell_id
                local icon_name = s_name_en:gsub("^Masterful%s+", ""):gsub("^Basic%s+", ""):gsub("%s+", "_")
                table.insert(spells_html, string.format("[[File:%s.png|25px|link=]] %s", icon_name, s_name))
            end
        end
        
        -- LIGNE FINALE (Correction de la syntaxe Lua)
        table.insert(html, '|- style="vertical-align: middle;"')
        
        -- Colonne 1 : Portrait + Nom
        table.insert(html, string.format('| style="padding: 2px;" | [[File:%s.png|70px|link=]] [[%s/%s|%s]]', file_name, file_name, lang, name))
        
        -- Colonne 2 : Spécialisation
        table.insert(html, string.format('| colspan=2 style="padding: 2px;" | <div style="display:flex; align-items:center; gap:8px;">%s %s</div>', spec_icon, spec_formatted))
        
        -- Colonne 3 : Compétences
        table.insert(html, '| style="padding: 2px;" | ' .. table.concat(skills_html, "<div style='margin-top:2px;'></div>"))
        
        -- Colonne 4 : Sorts
        table.insert(html, '| style="padding: 2px;" | ' .. table.concat(spells_html, "<div style='margin-top:2px;'></div>"))
    end
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
end

return p