Mudanças entre as edições de "Módulo:C.Utils"

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
Linha 80: Linha 80:
     -- Tenta Módulo:Nome
     -- Tenta Módulo:Nome
     ok, data = pcall(function()
     ok, data = pcall(function()
         local mod = require(moduleName)
         return require(moduleName)
        -- Validação extra para Urouge: verifica se o módulo foi carregado corretamente
        if char == "Urouge" and type(mod) == "table" then
            -- Força validação das flags no módulo Urouge
            if mod.skills then
                for skillName, skill in pairs(mod.skills) do
                    if type(skill) == "table" and skill.flags then
                        -- Garante que flags seja uma tabela válida
                        if type(skill.flags) ~= "table" or #skill.flags == 0 then
                            skill.flags = nil
                        end
                    end
                end
            end
        end
        return mod
     end)
     end)
     if ok and type(data) == "table" then
     if ok and type(data) == "table" then

Edição das 00h31min de 4 de janeiro de 2026

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

-- Módulo:C.Utils — funções utilitárias compartilhadas
local M = {}

-- Cache de módulos carregados
M._moduleCache = {}

--------------------------------------------------------------------------------
-- Utils
--------------------------------------------------------------------------------

function M.trim(s)
    return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end

-- Gera URL pública de arquivo (tenta "Special:" e "Especial:" por compat)
function M.fileURL(name)
    name = M.trim(name or "")
    if name == "" then
        return ""
    end
    name = name:gsub("^Arquivo:", ""):gsub("^File:", "")
    local t1 = mw.title.new("Special:FilePath/" .. name)
    if t1 then
        local u = t1:fullUrl()
        if u and u ~= "" then
            return u
        end
    end
    local t2 = mw.title.new("Especial:FilePath/" .. name)
    if t2 then
        local u = t2:fullUrl()
        if u and u ~= "" then
            return u
        end
    end
    return ""
end

-- Normaliza dimensões (mantém %, px, rem; números viram %)
function M.normalizeDim(x)
    x = mw.text.trim(tostring(x or ""))
    if x == "" then
        return nil
    end
    if x:match("%%$") then
        return x
    end
    if x:match("^%d+%.?%d*$") then
        return x .. "%"
    end
    return x
end

-- Monta string dos atributos (PVE, PvP, Energia, CD), usando '-' quando vazio
function M.makeAttrString(pve, pvp, energy, cd)
    local function nzOrDash(s)
        s = M.trim(s or "")
        return (s ~= "" and s or "-")
    end
    return table.concat({ nzOrDash(pve), nzOrDash(pvp), nzOrDash(energy), nzOrDash(cd) }, ", ")
end

-- Carrega o módulo do personagem (Módulo:<Nome>/Modulo/Module) com cache
-- Carrega APENAS pelo nome exato passado, sem fallbacks
function M.requireCharModule(char)
    char = M.trim(char or "")
    if char == "" then
        return nil
    end

    -- Verifica cache primeiro (mas permite limpar cache para debugging)
    -- Se o nome do módulo for "Urouge", força recarregar (workaround para problemas de cache)
    if M._moduleCache[char] and char ~= "Urouge" then
        return M._moduleCache[char]
    end

    local ok, data
    local moduleName = "Módulo:" .. char

    -- Tenta Módulo:Nome
    ok, data = pcall(function()
        return require(moduleName)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    -- Tenta com espaços (underscores → espaço)
    local spaced = char:gsub("_", " ")
    if spaced ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. spaced)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Tenta com underscores (espaços → _)
    local underscored = char:gsub(" ", "_")
    if underscored ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. underscored)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Fallback: tenta última palavra do nome (ex: "Trafalgar Law" → "Law")
    local lastWord = char:match("(%S+)%s*$")
    if lastWord and lastWord ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. lastWord)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Tenta Modulo:Nome (sem acento)
    ok, data = pcall(function()
        return require("Modulo:" .. char)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    -- Tenta Module:Nome (inglês)
    ok, data = pcall(function()
        return require("Module:" .. char)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    return nil
end

-- Separa uma sequência de {} (JSONs colados) em uma tabela de chunks
function M.collectJsonObjects(s)
    s = tostring(s or "")
    local out = {}
    for chunk in s:gmatch("%b{}") do
        table.insert(out, chunk)
    end
    return out
end

return M