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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
Linha 1: Linha 1:
-- Módulo:Info — resolve metadados e skills (robusto)
-- Módulo:Info — resolve metadados e skills (robusto/corrigido)
local p = {}
local p = {}


Linha 6: Linha 6:
local function safeArgs(node) return (node and node.args) or {} end
local function safeArgs(node) return (node and node.args) or {} end


-- tenta require em Módulo: e, se falhar, em Module:
-- tenta require em "Módulo:" e, se falhar, em "Module:"
local function requireCharacterModule(charName)
local function requireCharacterModule(charName)
   charName = trim(charName)
   charName = trim(charName)
Linha 17: Linha 17:
end
end


-- sobe na árvore de frames para tentar achar |nome= da Predef:Character
-- 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)
  -- 1) explícito na própria Skill
   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
  -- 2) pai imediato
   local p1 = frame:getParent()
   local p1 = frame:getParent()
   if p1 then
   if p1 then
Linha 28: Linha 25:
     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
    -- 3) avô (a Predef:Character costuma estar aqui)
    -- >>> FIX AQUI: usar ponto para checar existência do método
     local p2 = p1.getParent and p1:getParent()
     local p2 = p1.getParent and p1:getParent()
     if p2 then
     if p2 then
Linha 38: Linha 32:
     end
     end
   end
   end
  -- 4) fallback final: título da página (ex.: "Kalifa")
   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 de cores pros marcadores
-- paleta dos marcadores
local COLOR = {
local COLOR = {
   debuff = "#ff5252", -- vermelho
   debuff = "#ff5252", -- vermelho
Linha 81: Linha 73:
end
end


-- Gera o JSON de UMA skill (usado por Predef:Skill)
-- Gera o JSON de UMA skill (usado por Predefinição:Skill)
function p.skill(frame)
function p.skill(frame)
   local a    = safeArgs(frame)
   local a    = safeArgs(frame)
Linha 103: Linha 95:
   desc = colorize(desc)
   desc = colorize(desc)


   -- monta objeto final juntando com os NÚMEROS vindos da predef
   -- helper pra nil em branco
   local function nz(v) v = v or "" return (trim(v) ~= "" and v or nil) end
   local function nz(v) v = v or "" return (trim(v) ~= "" and v or nil) end
  -- objeto final (números vêm da predef)
   local obj = {
   local obj = {
     name    = name,
     name    = name,

Edição das 04h40min de 2 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)
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

-- tenta require em "Módulo:" e, se falhar, em "Module:"
local function requireCharacterModule(charName)
  charName = trim(charName)
  if charName == "" then return nil end
  local 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("Module:" .. charName) end)
  if ok and type(data) == "table" then return data end
  return nil
end

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

-- paleta dos marcadores
local COLOR = {
  debuff = "#ff5252", -- vermelho
  atk    = "#ffcc00", -- laranja
  def    = "#64b5f6", -- azul (defesa)
  ms     = "#43a047", -- verde (move speed)
  hp     = "#42a5f5", -- azul claro (vida/shield/heal)
  sec    = "#ffcc00", -- tempo
}

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 = resolveCharFromFrames(frame, a)
  local data = requireCharacterModule(char) or {}
  return trim(data.tier or "")
end

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

-- Gera o JSON de UMA skill (usado por Predefinição:Skill)
function p.skill(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 char  = resolveCharFromFrames(frame, a)

  local data  = requireCharacterModule(char) or {}
  local order = data.order or {}
  local key   = order[m] or ""               -- ex.: "Kick"
  local sk    = (data.skills or {})[key] or {}

  -- name/desc multilíngue
  local name  = trim(sk.name or key or "")
  local desc  = ""
  if type(sk.desc) == "table" then
    desc = sk.desc[lang] or sk.desc["pt"] or ""
  else
    desc = sk.desc or ""
  end
  desc = colorize(desc)

  -- helper pra nil em branco
  local function nz(v) v = v or "" return (trim(v) ~= "" and v or nil) end

  -- objeto final (números vêm da predef)
  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

return p