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)
    -- On récupère la classe soit via l'argument, soit via le nom de la page (sans le chemin)
    local pagename = mw.title.getCurrentTitle().text
    -- Si la page est "Classes/Knight", on ne récupère que "Knight"
    local pagename_clean = pagename:match("([^/]+)$") 
    
    local class_filter = frame.args.class_id or pagename_clean
    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 faction_map = {
            ["human"] = "Temple", ["necro"] = "Necropolis", ["nature"] = "Grove",
            ["unfrozen"] = "Schism", ["demon"] = "Hive", ["dungeon"] = "Dungeon"
        }
        
        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 "Pas de description"
        local clean_desc = spec_desc:gsub("\n", " "):gsub("\r", " "):gsub('"', '&quot;'):gsub("'", "&#39;"):gsub("<", "&lt;"):gsub(">", "&gt;")
        
        local spec_icon_data = mw.ext.cargo.query("HeroSpecialization", "icon", {where = string.format("id = '%s'", h.specialization_id), limit = 1})
        local icon_val = (spec_icon_data and #spec_icon_data > 0) and spec_icon_data[1].icon or ""
        
        local prefix = icon_val:match("^(%a+)_") or "dungeon"
        local replacement = faction_map[prefix] or toTitleCase(prefix)
        local icon_base = icon_val:gsub("^%a+_", replacement .. "_")
        local final_icon_name = toTitleCase(icon_base:gsub("_", " "))
        
        local spec_icon = string.format("[[File:%s.png|70px|link=]]", final_icon_name)
        local spec_formatted = string.format("'''%s'''<br><span title=\"%s\" style=\"cursor: help;\"><u>''-(+++++)-''</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
        
        -- CONSTRUCTION DE LA LIGNE (Optimisée pour le centrage et la hauteur)
        table.insert(html, '|- style="vertical-align: middle;"')
        
        -- Colonne 1 & 2 : Portrait + Nom (centrés verticalement)
        table.insert(html, string.format('| colspan=2 style="padding: 2px;" | <div style="display:flex; align-items:center; gap:8px;"> [[File:%s.png|70px|link=]] [[%s/%s|%s]] </div>', 
            file_name, file_name, lang, name))
            
        -- Colonne 4 & 5 : Icône + Spécialisation
        table.insert(html, string.format('| colspan=2 style="padding: 2px;" | <div style="display:flex; padding-left: 5px; align-items:center; gap:8px;">%s <div style="line-height: 1.2;">%s</div></div>', 
            spec_icon:gsub("|70px", "|60px"), spec_formatted)) -- Réduit la taille de l'icône spec pour compacter
        
        -- Colonne 6 : Compétences
        table.insert(html, '| style="padding: 2px;" | ' .. table.concat(skills_html, "<div style='margin-top:2px; padding-left: 5px;'></div>"))
        
        -- Colonne 7 : Sorts
        table.insert(html, '| style="padding: 2px;" | ' .. table.concat(spells_html, "<div style='margin-top:2px; padding-left: 5px;'></div>"))
    end
    
    table.insert(html, "|}")
    return table.concat(html, "\n")
end

return p