Mudanças entre as edições de "Módulo:Info"

De Wiki Gla
Ir para navegação Ir para pesquisar
m
Etiqueta: Revertido
m
Etiqueta: Reversão manual
Linha 15: Linha 15:
         return nil
         return nil
     end
     end
     local title = mw.title.new("Módulo:" .. charName .. " — dados do personagem")
     local ok, data
     if title and title.exists then
 
        local ok, mod = pcall(require, title.prefixedText)
    ok, data = pcall(function()
         if ok then return mod end
        return require("Módulo:" .. charName)
    end)
     if ok and type(data) == "table" then
         return data
     end
     end
    -- fallback: tentar Módulo:Nome sem “— dados do personagem”
 
     local title2 = mw.title.new("Módulo:" .. charName)
     ok, data = pcall(function()
     if title2 and title2.exists then
        return require("Modulo:" .. charName)
        local ok, mod = pcall(require, title2.prefixedText)
    end)
         if ok then return mod end
     if ok and type(data) == "table" then
         return data
     end
     end
    return {}
end


local function colorize(txt)
     ok, data = pcall(function()
     if not txt or txt == "" then return "" end
         return require("Module:" .. charName)
    local s = tostring(txt)
    -- marcações {{debuff:60}} {{atk:}} {{def:}} {{ms:}} {{hp:}} {{sec:3 segundos}}
    s = s:gsub("{{(%w+)%:([^\n}]-)}}", function(kind, val)
        kind = kind:lower()
        local map = {
            debuff = "#ff5252",
            atk    = "#ffcc00",
            def    = "#64b5f6",
            ms    = "#43a047",
            hp    = "#42a5f5",
            sec    = "#aaaaaa",
        }
        local col = map[kind] or "#cccccc"
         return string.format("<span style='color:%s;'>%s</span>", col, trim(val))
     end)
     end)
     return s
     if ok and type(data) == "table" then
        return data
    end
 
    return nil
end
end


local function resolveCharFromFrames(frame, a)
local function resolveCharFromFrames(frame, a)
     -- tenta descobrir o nome do personagem a partir do parent frame ou título da página
     if a.char and trim(a.char) ~= "" then
    local parent = frame:getParent()
         return trim(a.char)
    if parent and parent.args and trim(parent.args.nome or "") ~= "" then
         return trim(parent.args.nome)
     end
     end
     -- Se vier via |char= também aceita
     if a.nome and trim(a.nome) ~= "" then
     if a and trim(a.char or "") ~= "" then
        return trim(a.nome)
         return trim(a.char)
    end
 
    local p1 = frame:getParent()
     if p1 then
        local ap1 = safeArgs(p1)
        if ap1.char and trim(ap1.char) ~= "" then
            return trim(ap1.char)
        end
        if ap1.nome and trim(ap1.nome) ~= "" then
            return trim(ap1.nome)
        end
 
        local p2 = p1.getParent and p1:getParent()
         if p2 then
            local ap2 = safeArgs(p2)
            if ap2.char and trim(ap2.char) ~= "" then
                return trim(ap2.char)
            end
            if ap2.nome and trim(ap2.nome) ~= "" then
                return trim(ap2.nome)
            end
        end
     end
     end
    -- fallback: título atual
 
     local title = mw.title.getCurrentTitle()
     local title = mw.title.getCurrentTitle()
     return title and trim(title.text) or ""
     return title and trim(title.text) or ""
Linha 71: Linha 82:
     ms = "#43a047",
     ms = "#43a047",
     hp = "#42a5f5",
     hp = "#42a5f5",
     sec = "#aaaaaa",
     sec = "#ffcc00"
}
}


-- ====== Tier/Tags expanders (mantidos) ======
local function colorize(txt)
    if not txt or txt == "" then
        return ""
    end
    return (txt:gsub("{{(%a+):([^}]+)}}", function(tag, inner)
        local hex = COLOR[tag]
        if not hex then
            return "{{" .. tag .. ":" .. inner .. "}}"
        end
        return string.format('<span style="color:%s;">%s</span>', hex, inner)
    end))
end
 
-- ===== API =====
 
function p.getTier(frame)
function p.getTier(frame)
     local a = safeArgs(frame)
     local a = safeArgs(frame)
     local char = trim(a.char or (frame:getParent() and frame:getParent().args.nome) or "")
     local char = trim(frame.args[1] or a.nome)
     if char == "" then char = resolveCharFromFrames(frame, a) end
     if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
     local data = requireCharacterModule(char) or {}
     local data = requireCharacterModule(char) or {}
     local lang = trim(a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower()
     return trim(data.tier or "")
    local tier = (data.tier_i18n and data.tier_i18n[lang]) or data.tier or ""
    return trim(tier)
end
end


function p.getTags(frame)
function p.getTags(frame)
     local a = safeArgs(frame)
     local a = safeArgs(frame)
     local char = trim(a.char or (frame:getParent() and frame:getParent().args.nome) or "")
     local char = trim(frame.args[1] or a.nome)
     if char == "" then char = resolveCharFromFrames(frame, a) end
     if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
     local data = requireCharacterModule(char) or {}
     local data = requireCharacterModule(char) or {}
    local lang = trim(a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower()
     local tags = data.tags or {}
     local tags = (data.tags_i18n and data.tags_i18n[lang]) or data.tags or {}
     if type(tags) ~= "table" then
     if type(tags) == "table" then
         return ""
         return table.concat(tags, " / ")
    end
    return trim(tags or "")
end
 
-- ===== Skill / Subskill =====
local function parseMSmart(aM, aS, aParentM)
    -- retorna (isSub, parentM, sIndex, mInt)
    local M = trim(aM or "")
    if M ~= "" and M:find("%.") then
        local p, s = M:match("^(%d+)%.(%d+)$")
        if p and s then
            return true, tonumber(p) or 0, tonumber(s) or 1, tonumber(p) or 0
        end
    end
    local s = tonumber(aS or "") or nil
    local pm = tonumber(aParentM or "") or nil
    if s and pm then
        return true, pm, s, pm
     end
     end
     return false, nil, nil, tonumber(M) or 0
     return trim(table.concat(tags, " / "))
end
end


-- Skill (normal: com M)
function p.skill(frame)
function p.skill(frame)
     local a = safeArgs(frame)
     local a = safeArgs(frame)
Linha 124: Linha 133:
     end
     end
     local data = requireCharacterModule(char) or {}
     local data = requireCharacterModule(char) or {}
    local isSub, parentM, sIndex, mInt = parseMSmart(a.M, a.S, a.parentM)


     local name, desc_i18n = nil, {}
     local name, desc_i18n = nil, {}


    -- PRINCIPAL (M inteiro)
     if a.M and trim(a.M) ~= "" then
     if not isSub and a.M and trim(a.M) ~= "" then
         local m = tonumber(a.M) or 0
         local m = tonumber(a.M) or 0
         local order = data.order or {}
         local order = data.order or {}
Linha 148: Linha 154:
         elseif sk.desc and trim(sk.desc) ~= "" then
         elseif sk.desc and trim(sk.desc) ~= "" then
             desc_i18n["pt"] = colorize(sk.desc) -- fallback: único texto vira pt
             desc_i18n["pt"] = colorize(sk.desc) -- fallback: único texto vira pt
        end
        -- NOVO: se a principal tiver suborder, exporta pro front
        if type(sk.suborder) == "table" and #sk.suborder > 0 then
            _SUBS = sk.suborder
            _M_INDEX = m
            _PARENT_KEY = key
         end
         end
    end
    -- SUBSKILL (por M decimal ou S+parentM)
    if isSub then
        local order = data.order or {}
        local parentKey = order[parentM or 0] or ""
        local parentSk = (data.skills or {})[parentKey] or {}
        -- lista de filhas: suborder ou fallback por parent=name
        local subs = {}
        if type(parentSk.suborder) == "table" then
            subs = parentSk.suborder
        else
            for k, v in pairs(data.skills or {}) do
                if type(v) == "table" and v.parent == parentKey then
                    table.insert(subs, k)
                end
            end
            table.sort(subs)
        end
        local childKey = subs[sIndex or 1] or ""
        local sk = (data.skills or {})[childKey] or {}
        name = trim(sk.name or childKey or "")
        if type(sk.desc) == "table" then
            for _, code in ipairs({"pt","en","es","pl"}) do
                local d = sk.desc[code]
                if d and trim(d) ~= "" then
                    desc_i18n[code] = colorize(d)
                end
            end
        elseif sk.desc and trim(sk.desc) ~= "" then
            desc_i18n["pt"] = colorize(sk.desc)
        end
        _IS_SUB = true
        _PARENT_M = parentM
        _PARENT_NAME = parentKey
     end
     end


Linha 217: Linha 176:
         obj.name = name
         obj.name = name
     end
     end
     if chosen and chosen ~= "" then
     if chosen ~= "" then
         obj.desc = chosen
         obj.desc = chosen
     end
     end
Linha 228: Linha 187:
         obj.descEs = desc_i18n.es
         obj.descEs = desc_i18n.es
         obj.descPl = desc_i18n.pl
         obj.descPl = desc_i18n.pl
    end
    -- NOVO: metadados hierárquicos
    if _IS_SUB then
        obj.type = "sub"
        obj.parentM = _PARENT_M
        obj.parentName = _PARENT_NAME
    else
        if _M_INDEX then obj.m = _M_INDEX end
        if _SUBS and type(_SUBS) == "table" and #_SUBS > 0 then
            obj.subs_names = _SUBS
        end
     end
     end


Linha 248: Linha 195:
     local a = safeArgs(frame)
     local a = safeArgs(frame)
     local obj = {
     local obj = {
        name = "Emote",
        desc = "",
         icon = (trim(a.icon or "") ~= "" and a.icon or "Nada.png"),
         icon = (trim(a.icon or "") ~= "" and a.icon or "Nada.png"),
         video = a.video or ""
        level = 25,
         video = a.video or "",
        desc_i18n = {
            pt = "",
            en = "",
            es = "",
            pl = ""
        },
        descPt = "",
        descEn = "",
        descEs = "",
        descPl = ""
     }
     }
     return mw.text.jsonEncode(obj)
     return mw.text.jsonEncode(obj)
end
end


-- ======= Gambis helpers para Tier/Tags no cabeçalho =======
-- expandTier/expandTags mantidos
function p.expandTier(frame)
function p.expandTier(frame)
     local a = safeArgs(frame)
     local token = trim(frame.args[1] or "")
     local nome = trim(a.nome or (frame:getParent() and frame:getParent().args.nome) or "")
     local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
     if nome == "" then
     if token == "" then
         nome = resolveCharFromFrames(frame, a)
        return ""
    end
    if token:lower():sub(1, 4) ~= "tier" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^tier(.+)$") or ""
    if char == "" then
         local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
     end
     end
     local data = requireCharacterModule(nome) or {}
     local data = requireCharacterModule(char) or {}
     local lang = trim(a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower()
     local i18n = (data.tier_i18n and data.tier_i18n.pt) or data.tier
    local tier = (data.tier_i18n and data.tier_i18n[lang]) or data.tier or ""
     return trim(i18n or "")
     return trim(tier)
end
end


function p.expandTags(frame)
function p.expandTags(frame)
     local a = safeArgs(frame)
     local token = trim(frame.args[1] or "")
     local nome = trim(a.nome or (frame:getParent() and frame:getParent().args.nome) or "")
     local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
     if nome == "" then
     if token == "" then
         nome = resolveCharFromFrames(frame, a)
        return ""
    end
    if token:lower():sub(1, 5) ~= "class" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^class(.+)$") or ""
    if char == "" then
         local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
     end
     end
     local data = requireCharacterModule(nome) or {}
     local data = requireCharacterModule(char) or {}
    local lang = trim(a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower()
     local arr = (data.tags_i18n and data.tags_i18n.pt) or data.tags
     local arr = (data.tags_i18n and data.tags_i18n[lang]) or data.tags
     if type(arr) == "table" then
     if type(arr) == "table" then
         return table.concat(arr, " / ")
         return table.concat(arr, " / ")

Edição das 02h04min de 14 de setembro de 2025

A documentação para este módulo pode ser criada em Módulo:Info/doc

-- Módulo:Info — resolve metadados e skills (robusto/corrigido + gambiarra expandTier/expandTags)
local p = {}

-- ===== util =====
local function trim(s)
    return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function safeArgs(node)
    return (node and node.args) or {}
end

local function requireCharacterModule(charName)
    charName = trim(charName)
    if charName == "" then
        return nil
    end
    local ok, data

    ok, data = pcall(function()
        return require("Módulo:" .. charName)
    end)
    if ok and type(data) == "table" then
        return data
    end

    ok, data = pcall(function()
        return require("Modulo:" .. charName)
    end)
    if ok and type(data) == "table" then
        return data
    end

    ok, data = pcall(function()
        return require("Module:" .. charName)
    end)
    if ok and type(data) == "table" then
        return data
    end

    return nil
end

local function resolveCharFromFrames(frame, a)
    if a.char and trim(a.char) ~= "" then
        return trim(a.char)
    end
    if a.nome and trim(a.nome) ~= "" then
        return trim(a.nome)
    end

    local p1 = frame:getParent()
    if p1 then
        local ap1 = safeArgs(p1)
        if ap1.char and trim(ap1.char) ~= "" then
            return trim(ap1.char)
        end
        if ap1.nome and trim(ap1.nome) ~= "" then
            return trim(ap1.nome)
        end

        local p2 = p1.getParent and p1:getParent()
        if p2 then
            local ap2 = safeArgs(p2)
            if ap2.char and trim(ap2.char) ~= "" then
                return trim(ap2.char)
            end
            if ap2.nome and trim(ap2.nome) ~= "" then
                return trim(ap2.nome)
            end
        end
    end

    local title = mw.title.getCurrentTitle()
    return title and trim(title.text) or ""
end

-- paleta para marcadores
local COLOR = {
    debuff = "#ff5252",
    atk = "#ffcc00",
    def = "#64b5f6",
    ms = "#43a047",
    hp = "#42a5f5",
    sec = "#ffcc00"
}

local function colorize(txt)
    if not txt or txt == "" then
        return ""
    end
    return (txt:gsub("{{(%a+):([^}]+)}}", function(tag, inner)
        local hex = COLOR[tag]
        if not hex then
            return "{{" .. tag .. ":" .. inner .. "}}"
        end
        return string.format('<span style="color:%s;">%s</span>', hex, inner)
    end))
end

-- ===== API =====

function p.getTier(frame)
    local a = safeArgs(frame)
    local char = trim(frame.args[1] or a.nome)
    if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
    local data = requireCharacterModule(char) or {}
    return trim(data.tier or "")
end

function p.getTags(frame)
    local a = safeArgs(frame)
    local char = trim(frame.args[1] or a.nome)
    if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
    local data = requireCharacterModule(char) or {}
    local tags = data.tags or {}
    if type(tags) ~= "table" then
        return ""
    end
    return trim(table.concat(tags, " / "))
end

-- Skill (normal: com M)
function p.skill(frame)
    local a = safeArgs(frame)
    local lang = trim((a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower())
    local char = trim(a.char or (frame:getParent() and frame:getParent().args.nome) or "")
    if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
    local data = requireCharacterModule(char) or {}

    local name, desc_i18n = nil, {}

    if a.M and trim(a.M) ~= "" then
        local m = tonumber(a.M) or 0
        local order = data.order or {}
        local key = order[m] or ""
        local sk = (data.skills or {})[key] or {}
        name = trim(sk.name or key or "")

        -- monta tabela de descrições por idioma (já colorizadas)
        if type(sk.desc) == "table" then
            local langs = {"pt", "en", "es", "pl"}
            for _, code in ipairs(langs) do
                local d = sk.desc[code]
                if d and trim(d) ~= "" then
                    desc_i18n[code] = colorize(d)
                end
            end
        elseif sk.desc and trim(sk.desc) ~= "" then
            desc_i18n["pt"] = colorize(sk.desc) -- fallback: único texto vira pt
        end
    end

    local function nz(v)
        v = v or ""
        return (trim(v) ~= "" and v or nil)
    end

    local chosen = desc_i18n[lang] or desc_i18n["pt"] or ""

    local obj = {
        icon = (trim(a.icon or "") ~= "" and a.icon or "Nada.png"),
        level = (trim(a.level or "") ~= "" and a.level or "NIVEL"),
        energy = nz(a.energy),
        powerpve = nz(a.powerpve),
        powerpvp = nz(a.powerpvp),
        cooldown = nz(a.cooldown),
        video = a.video or ""
    }
    if name and name ~= "" then
        obj.name = name
    end
    if chosen ~= "" then
        obj.desc = chosen
    end

    -- inclui as variantes para o Character expor como data-desc-*
    if next(desc_i18n) ~= nil then
        obj.desc_i18n = desc_i18n
        obj.descPt = desc_i18n.pt
        obj.descEn = desc_i18n.en
        obj.descEs = desc_i18n.es
        obj.descPl = desc_i18n.pl
    end

    return mw.text.jsonEncode(obj)
end
-- Skill (emote: sem M)
function p.emote(frame)
    local a = safeArgs(frame)
    local obj = {
        name = "Emote",
        desc = "",
        icon = (trim(a.icon or "") ~= "" and a.icon or "Nada.png"),
        level = 25,
        video = a.video or "",
        desc_i18n = {
            pt = "",
            en = "",
            es = "",
            pl = ""
        },
        descPt = "",
        descEn = "",
        descEs = "",
        descPl = ""
    }
    return mw.text.jsonEncode(obj)
end

-- expandTier/expandTags mantidos
function p.expandTier(frame)
    local token = trim(frame.args[1] or "")
    local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
    if token == "" then
        return ""
    end
    if token:lower():sub(1, 4) ~= "tier" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^tier(.+)$") or ""
    if char == "" then
        local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
    end
    local data = requireCharacterModule(char) or {}
    local i18n = (data.tier_i18n and data.tier_i18n.pt) or data.tier
    return trim(i18n or "")
end

function p.expandTags(frame)
    local token = trim(frame.args[1] or "")
    local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
    if token == "" then
        return ""
    end
    if token:lower():sub(1, 5) ~= "class" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^class(.+)$") or ""
    if char == "" then
        local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
    end
    local data = requireCharacterModule(char) or {}
    local arr = (data.tags_i18n and data.tags_i18n.pt) or data.tags
    if type(arr) == "table" then
        return table.concat(arr, " / ")
    end
    return trim(arr or "")
end

return p