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

De Wiki Gla
Ir para navegação Ir para pesquisar
Etiqueta: Revertido
m
 
(83 revisões intermediárias por 3 usuários não estão sendo mostradas)
Linha 1: Linha 1:
local p = {}
--[[
  Módulo: opcional para inserir o widget via {{#invoke}}.
 
  Na wiki: criar página Módulo:GlaReportEmbed com este código.


-- Parser dos itens no formato "nome:quantidade"
  Uso num modelo ou página:
local function parseItens(raw)
     {{#invoke:GlaReportEmbed|embed|api_base=https://teu-deploy.com}}
     local resultado = {}
    for item in mw.text.gsplit(raw or "", ",", true) do
        local nome, qtd = item:match("^%s*(.-)%s*:%s*(%d+)%s*$")
        if nome and qtd then
            -- Remove espaços extras e força nome de arquivo com extensão
            nome = mw.text.trim(nome)
            if not nome:match("%.png$") and not nome:match("%.gif$") and not nome:match("%.jpg$") and not nome:match("%.webp$") then
                nome = nome .. ".png"
            end
            qtd = tonumber(qtd) or 0
            resultado[nome] = (resultado[nome] or 0) + qtd
        end
    end
    return resultado
end


-- Junta múltiplas listas de recompensa
  Parâmetros nomeados (opcionais):
local function mergeRecompensas(rewardsList)
     |heading=Título da caixa
     local total = {}
     |wid=2
     for _, raw in ipairs(rewardsList) do
        local dados = parseItens(raw)
        for nome, qtd in pairs(dados) do
            total[nome] = (total[nome] or 0) + qtd
        end
    end
    return total
end


-- Função pública
  Requer a extensão Scribunto e o widget {{#widget:ReportModal|...}} registrado.
function p.total(frame)
]]
    local content = mw.title.getCurrentTitle():getContent()
    local rewardCalls = {}


    -- Procura chamadas de {{Reward|...|itens=...}}
local p = {}
    for bloco in content:gmatch("{{%s*[Rr]eward.-}}") do
        local itens = bloco:match("itens%s*=%s*([^|}]+)")
        if itens then
            table.insert(rewardCalls, itens)
        end
    end


    local totais = mergeRecompensas(rewardCalls)
function p.embed(frame)
local pargs = (frame:getParent() and frame:getParent().args) or {}
local args = frame.args or {}
local base = (
args.api_base
or pargs.api_base
or args[1]
or pargs[1]
or ""
):gsub("^%s+", ""):gsub("%s+$", "")
if base == "" then
return '<span class="error">GlaReportEmbed: falta <code>api_base</code>.</span>'
end


    local html = mw.html.create("div")
local heading = args.heading or pargs.heading or ""
    html:addClass("missao-recompensa-final")
local wid = args.wid or pargs.wid or ""


    for nome, qtd in pairs(totais) do
local wikitext = "{{#widget:ReportModal|api_base=" .. base
        html:tag("span")
if heading ~= "" then
            :addClass("item-wrapper")
wikitext = wikitext .. "|heading=" .. heading
            :wikitext(string.format("[[Arquivo:%s|20px|link=]] x%d", nome, qtd))
end
    end
if wid ~= "" then
wikitext = wikitext .. "|wid=" .. wid
end
wikitext = wikitext .. "}}"


    return tostring(html)
return frame:preprocess(wikitext)
end
end


return p
return p

Edição atual tal como às 16h07min de 28 de abril de 2026

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

--[[
  Módulo: opcional para inserir o widget via {{#invoke}}.

  Na wiki: criar página Módulo:GlaReportEmbed com este código.

  Uso num modelo ou página:
    {{#invoke:GlaReportEmbed|embed|api_base=https://teu-deploy.com}}

  Parâmetros nomeados (opcionais):
    |heading=Título da caixa
    |wid=2

  Requer a extensão Scribunto e o widget {{#widget:ReportModal|...}} registrado.
]]

local p = {}

function p.embed(frame)
	local pargs = (frame:getParent() and frame:getParent().args) or {}
	local args = frame.args or {}
	local base = (
		args.api_base
		or pargs.api_base
		or args[1]
		or pargs[1]
		or ""
	):gsub("^%s+", ""):gsub("%s+$", "")
	if base == "" then
		return '<span class="error">GlaReportEmbed: falta <code>api_base</code>.</span>'
	end

	local heading = args.heading or pargs.heading or ""
	local wid = args.wid or pargs.wid or ""

	local wikitext = "{{#widget:ReportModal|api_base=" .. base
	if heading ~= "" then
		wikitext = wikitext .. "|heading=" .. heading
	end
	if wid ~= "" then
		wikitext = wikitext .. "|wid=" .. wid
	end
	wikitext = wikitext .. "}}"

	return frame:preprocess(wikitext)
end

return p