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

De Wiki Gla
Ir para navegação Ir para pesquisar
m (subskill)
Etiqueta: Revertido
m
 
(33 revisões intermediárias por 2 usuários não estão sendo mostradas)
Linha 1: Linha 1:
-- Módulo:Info — resolve metadados e skills (robusto/corrigido + gambiarra expandTier/expandTags)
-- Módulo:Info — resolve metadados e skills (com Subskill e cores inline)
-- Este módulo re-exporta funções dos submódulos para manter compatibilidade
local p = {}
local p = {}


-- ===== util =====
-- Importar submódulos
local function trim(s)
local utils = require("Módulo:Info.Utils")
    return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
local skills = require("Módulo:Info.Skills")
end
local expand = require("Módulo:Info.Expand")
local function safeArgs(node)
    return (node and node.args) or {}
end


local function requireCharacterModule(charName)
-- Re-exportar funções de utils (para compatibilidade caso alguém use diretamente)
    charName = trim(charName)
p.trim = utils.trim
    if charName == "" then
p.safeArgs = utils.safeArgs
        return nil
p.collectJsonObjects = utils.collectJsonObjects
    end
p.requireCharacterModule = utils.requireCharacterModule
    local ok, data
p.resolveCharFromFrames = utils.resolveCharFromFrames
p.colorize = utils.colorize
p.I18N = utils.I18N
p.COLOR = utils.COLOR


    ok, data = pcall(function()
-- Re-exportar funções de skills
        return require("Módulo:" .. charName)
p.skill = skills.skill
    end)
p.subskill = skills.subskill
    if ok and type(data) == "table" then
p.emote = skills.emote
        return data
p.weapon = skills.weapon
    end


    ok, data = pcall(function()
-- Re-exportar funções de expand
        return require("Modulo:" .. charName)
p.getTier = expand.getTier
    end)
p.getTags = expand.getTags
    if ok and type(data) == "table" then
p.expandTier = expand.expandTier
        return data
p.expandTags = expand.expandTags
    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
 
-- ============================================================================
-- NOVO: gerador de SubSkill para a predefinição {{SubSkill}}
-- Emite um OBJETO JSON (sem colchetes); o {{Skill|subs= ...}} junta em array
-- ============================================================================
function p.sub(frame)
    local a = frame:getParent() and frame:getParent().args or frame.args
    local function nz(s)
        s = trim(s);
        return (s ~= "" and s or nil)
    end
 
    local desc_i = {
        pt = colorize(trim(a.desc_pt)),
        en = colorize(trim(a.desc_en)),
        es = colorize(trim(a.desc_es)),
        pl = colorize(trim(a.desc_pl))
    }
 
    local t = {
        name = trim(a.name ~= "" and a.name or a.nome),
        icon = trim(a.icon or ""),
        video = trim(a.video or ""),
        level = trim(a.level or ""),
 
        -- formato "pve,pvp,energy,cd" (o front já entende)
        attrs = table.concat({trim(a.powerpve or ""), trim(a.powerpvp or ""), trim(a.energy or ""),
                              trim(a.cooldown or "")}, ", "),
 
        desc_i18n = desc_i
    }
 
    return mw.text.jsonEncode(t)
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
 
    -- =========================================================================
    -- NOVO: suporte a |subs= com uma lista de {{SubSkill}} (expande e agrega)
    -- Aceita tanto uma string JSON (array) quanto wikitext com vários {{SubSkill}}
    -- =========================================================================
    do
        local raw = a.subs or a.sub or ""
        raw = tostring(raw or "")
        raw = raw:gsub("^%s+", ""):gsub("%s+$", "")
        if raw ~= "" then
            -- expande wikitext (transforma {{SubSkill|...}} em objetos JSON)
            local expanded = frame:preprocess(raw)
 
            -- Se não começar com "[", embrulha e injeta vírgulas entre }{
            local text = expanded
            if not text:match("^%s*%[") then
                text = text:gsub("%}%s*%{", "},{")
                text = "[" .. text .. "]"
            end
 
            local ok, arr = pcall(mw.text.jsonDecode, text)
            if ok and type(arr) == "table" and #arr > 0 then
                obj.subs = arr
            end
        end
    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
return p

Edição atual tal como às 00h18min de 27 de novembro de 2025

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

-- Módulo:Info — resolve metadados e skills (com Subskill e cores inline)
-- Este módulo re-exporta funções dos submódulos para manter compatibilidade
local p = {}

-- Importar submódulos
local utils = require("Módulo:Info.Utils")
local skills = require("Módulo:Info.Skills")
local expand = require("Módulo:Info.Expand")

-- Re-exportar funções de utils (para compatibilidade caso alguém use diretamente)
p.trim = utils.trim
p.safeArgs = utils.safeArgs
p.collectJsonObjects = utils.collectJsonObjects
p.requireCharacterModule = utils.requireCharacterModule
p.resolveCharFromFrames = utils.resolveCharFromFrames
p.colorize = utils.colorize
p.I18N = utils.I18N
p.COLOR = utils.COLOR

-- Re-exportar funções de skills
p.skill = skills.skill
p.subskill = skills.subskill
p.emote = skills.emote
p.weapon = skills.weapon

-- Re-exportar funções de expand
p.getTier = expand.getTier
p.getTags = expand.getTags
p.expandTier = expand.expandTier
p.expandTags = expand.expandTags

return p