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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
Linha 3: Linha 3:


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


-- tenta require nos 3 namespaces: "Módulo:", "Modulo:", "Module:"
local function requireCharacterModule(charName)
local function requireCharacterModule(charName)
  charName = trim(charName)
    charName = trim(charName)
  if charName == "" then return nil end
    if charName == "" then return nil end
  local ok, data
    local ok, data


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


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


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


  return nil
    return nil
end
end


-- sobe na árvore de frames e tenta achar |nome=; cai no título da página se precisar
local function resolveCharFromFrames(frame, a)
local function resolveCharFromFrames(frame, a)
  if a.char and trim(a.char) ~= "" then return trim(a.char) end
    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
    if a.nome and trim(a.nome) ~= "" then return trim(a.nome) end


  local p1 = frame:getParent()
    local p1 = frame:getParent()
  if p1 then
    if p1 then
    local ap1 = safeArgs(p1)
        local ap1 = safeArgs(p1)
    if ap1.char and trim(ap1.char) ~= "" then return trim(ap1.char) end
        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
        if ap1.nome and trim(ap1.nome) ~= "" then return trim(ap1.nome) end


    local p2 = p1.getParent and p1:getParent()
        local p2 = p1.getParent and p1:getParent()
    if p2 then
        if p2 then
      local ap2 = safeArgs(p2)
            local ap2 = safeArgs(p2)
      if ap2.char and trim(ap2.char) ~= "" then return trim(ap2.char) end
            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
            if ap2.nome and trim(ap2.nome) ~= "" then return trim(ap2.nome) end
        end
     end
     end
  end


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


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


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


-- ===== API principal =====
-- ===== API =====


-- aceita {{#invoke:Info|getTier|Kalifa}} ou resolve pelo frame
function p.getTier(frame)
function p.getTier(frame)
  local a    = safeArgs(frame)
    local a    = safeArgs(frame)
  local char = trim(frame.args[1] or a.nome)
    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 {}
  return trim(data.tier or "")
    return trim(data.tier or "")
end
end


function p.getTags(frame)
function p.getTags(frame)
  local a    = safeArgs(frame)
    local a    = safeArgs(frame)
  local char = trim(frame.args[1] or a.nome)
    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 tags = data.tags or {}
    local tags = data.tags or {}
  if type(tags) ~= "table" then return "" end
    if type(tags) ~= "table" then return "" end
  return trim(table.concat(tags, " / "))
    return trim(table.concat(tags, " / "))
end
end


-- Gera o JSON de UMA skill (usado por Predefinição:Skill)
-- Skill M/SEM M
function p.skill(frame)
function p.skill(frame)
  local a     = safeArgs(frame)
    local a   = safeArgs(frame)
  local m    = tonumber(a.M) or 0
    local lang = trim((a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower())
  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 {}


  -- resolve char (parent |nome= ou fallback)
    local name, desc = nil, nil
   local char  = trim(a.char or (frame:getParent() and frame:getParent().args.nome) or "")
    if a.M and trim(a.M) ~= "" then
  if char == "" then char = resolveCharFromFrames(frame, a) end
        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 "")
        if type(sk.desc) == "table" then
            desc = sk.desc[lang] or sk.desc["pt"] or ""
        else
            desc = sk.desc or ""
        end
        desc = colorize(desc)
    end


  local data  = requireCharacterModule(char) or {}
    local function nz(v)
  local order = data.order or {}
        v = v or ""
  local key  = order[m] or ""               -- ex.: "Kick"
        return (trim(v) ~= "" and v or nil)
  local sk    = (data.skills or {})[key] or {}
    end


  -- name/desc multilíngue
    local obj = {
  local name  = trim(sk.name or key or "")
        -- só inclui name/desc se realmente existir
  local desc  = ""
        icon    = (trim(a.icon or "") ~= "" and a.icon or "Nada.png"),
   if type(sk.desc) == "table" then
        level    = (trim(a.level or "") ~= "" and a.level or "NIVEL"),
    desc = sk.desc[lang] or sk.desc["pt"] or ""
        energy   = nz(a.energy),
  else
        powerpve = nz(a.powerpve),
     desc = sk.desc or ""
        powerpvp = nz(a.powerpvp),
  end
        cooldown = nz(a.cooldown),
  desc = colorize(desc)
        video    = a.video or "",
    }
     if name and name ~= "" then obj.name = name end
    if desc and desc ~= "" then obj.desc = desc end


  local function nz(v) v = v or "" return (trim(v) ~= "" and v or nil) end
     return mw.text.jsonEncode(obj)
 
  local obj = {
     name    = name,
    icon    = (trim(a.icon or "")  ~= "" and a.icon  or "Nada.png"),
    level    = (trim(a.level or "") ~= "" and a.level or "NIVEL"),
    desc    = desc,
    energy  = nz(a.energy),
    powerpve = nz(a.powerpve),
    powerpvp = nz(a.powerpvp),
    cooldown = nz(a.cooldown),
    video    = a.video or "",
  }
 
  return mw.text.jsonEncode(obj)
end
end


-- ===== “gambiarra” para tier/tags vindos como tokens =====
-- expandTier/expandTags mantidos
-- uso na Predefinição:Character:
-- |tier  = {{#invoke:Info|expandTier|{{{tier|}}}|{{{nome|}}}}}
-- |classe = {{#invoke:Info|expandTags|{{{classe|}}}|{{{nome|}}}}}
 
-- {{#invoke:Info|expandTier|tierKalifa|Kalifa}}
function p.expandTier(frame)
function p.expandTier(frame)
  local token = trim(frame.args[1] or "")
    local token = trim(frame.args[1] or "")
  local nome  = trim(frame.args[2] 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 token == "" then return "" end
    if token == "" then return "" end
 
    if token:lower():sub(1, 4) ~= "tier" then return token end
  -- aceita "tierKalifa", "tierAuto", "tier<Nome>", ou texto livre
    local char = nome ~= "" and nome or token:match("^tier(.+)$") or ""
  if token:lower():sub(1,4) ~= "tier" then
    if char == "" then
    return token -- não é token? devolve como veio
        local title = mw.title.getCurrentTitle()
  end
        char = title and trim(title.text) or ""
 
    end
  local char = nome ~= "" and nome or token:match("^tier(.+)$") or ""
    local data = requireCharacterModule(char) or {}
  if char == "" then
    local i18n = (data.tier_i18n and data.tier_i18n.pt) or data.tier
    local title = mw.title.getCurrentTitle()
    return trim(i18n or "")
    char = title and trim(title.text) or ""
  end
 
  local data = requireCharacterModule(char) or {}
  -- i18n preferido (PT por enquanto); fallback para data.tier
  local i18n = (data.tier_i18n and data.tier_i18n.pt) or data.tier
  return trim(i18n or "")
end
end


-- {{#invoke:Info|expandTags|classKalifa|Kalifa}}
function p.expandTags(frame)
function p.expandTags(frame)
  local token = trim(frame.args[1] or "")
    local token = trim(frame.args[1] or "")
  local nome  = trim(frame.args[2] 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 token == "" then return "" end
    if token == "" then return "" end
 
    if token:lower():sub(1, 5) ~= "class" then return token end
  -- aceita "classKalifa", "classAuto", "class<Nome>", ou texto livre
    local char = nome ~= "" and nome or token:match("^class(.+)$") or ""
  if token:lower():sub(1,5) ~= "class" then
    if char == "" then
    return token -- não é token? devolve como veio
        local title = mw.title.getCurrentTitle()
  end
        char = title and trim(title.text) or ""
 
    end
  local char = nome ~= "" and nome or token:match("^class(.+)$") or ""
    local data = requireCharacterModule(char) or {}
  if char == "" then
    local arr  = (data.tags_i18n and data.tags_i18n.pt) or data.tags
    local title = mw.title.getCurrentTitle()
    if type(arr) == "table" then
    char = title and trim(title.text) or ""
        return table.concat(arr, " / ")
  end
    end
 
    return trim(arr or "")
  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
end


return p
return p

Edição das 12h42min de 9 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 M/SEM 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 = nil, 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 "")
        if type(sk.desc) == "table" then
            desc = sk.desc[lang] or sk.desc["pt"] or ""
        else
            desc = sk.desc or ""
        end
        desc = colorize(desc)
    end

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

    local obj = {
        -- só inclui name/desc se realmente existir
        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 desc and desc ~= "" then obj.desc = desc end

    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