Mudanças entre as edições de "Módulo:Teste"
Ir para navegação
Ir para pesquisar
Etiqueta: Revertido |
Etiqueta: Revertido |
||
| Linha 1: | Linha 1: | ||
local p = {} | local p = {} | ||
local itemData = require("Módulo:ItemData") | |||
-- Parser dos itens | -- Parser dos itens estilo "nome:quantidade" | ||
local function parseItens(raw) | local function parseItens(raw) | ||
local resultado = {} | local resultado = {} | ||
| Linha 7: | Linha 8: | ||
local nome, qtd = item:match("^%s*(.-)%s*:%s*(%d+)%s*$") | local nome, qtd = item:match("^%s*(.-)%s*:%s*(%d+)%s*$") | ||
if nome and qtd then | if nome and qtd then | ||
nome = mw.text.trim(nome) | 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 | if not nome:match("%.png$") and not nome:match("%.gif$") and not nome:match("%.jpg$") and not nome:match("%.webp$") then | ||
| Linha 19: | Linha 19: | ||
end | end | ||
-- Junta | -- Junta várias listas de recompensas | ||
local function mergeRecompensas( | local function mergeRecompensas(lista) | ||
local total = {} | local total = {} | ||
for _, raw in ipairs( | for _, raw in ipairs(lista) do | ||
local dados = parseItens(raw) | local dados = parseItens(raw) | ||
for nome, qtd in pairs(dados) do | for nome, qtd in pairs(dados) do | ||
| Linha 31: | Linha 31: | ||
end | end | ||
-- | -- Formata número com milhar | ||
local function formatNumber(n) | |||
local s = tostring(n) | |||
local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse() | |||
return result:gsub("^,", "") | |||
end | |||
function p.total(frame) | function p.total(frame) | ||
local content = mw.title.getCurrentTitle():getContent() | local content = mw.title.getCurrentTitle():getContent() | ||
local rewardCalls = {} | local rewardCalls = {} | ||
for bloco in content:gmatch("{{%s*[Rr]eward.-}}") do | for bloco in content:gmatch("{{%s*[Rr]eward.-}}") do | ||
local itens = bloco:match("itens%s*=%s*([^|}]+)") | local itens = bloco:match("itens%s*=%s*([^|}]+)") | ||
| Linha 44: | Linha 49: | ||
end | end | ||
local | local total = mergeRecompensas(rewardCalls) | ||
local html = mw.html.create("div"):addClass("reward-items") | |||
for nome, qtd in pairs(total) do | |||
local data = itemData[nome] or {} | |||
local nomeCompleto = data.nome or nome | |||
local desc = data.desc | |||
local tooltip = nomeCompleto | |||
if desc and desc ~= "" then | |||
tooltip = tooltip .. "\n—\n" .. desc | |||
end | |||
local wrapper = html:tag("span"):addClass("item-wrapper") | |||
wrapper:attr("data-tooltip", tooltip) | |||
wrapper:tag("span") | |||
:wikitext(string.format("[[Arquivo:%s|link=]]", nome)) | |||
wrapper:tag("span") | |||
:addClass("item-count") | |||
:addClass("item- | :wikitext((nome:lower():match("^berries") and "" or "x") .. formatNumber(qtd)) | ||
:wikitext( | |||
end | end | ||
Edição das 01h08min de 9 de junho de 2025
A documentação para este módulo pode ser criada em Módulo:Teste/doc
local p = {}
local itemData = require("Módulo:ItemData")
-- Parser dos itens estilo "nome:quantidade"
local function parseItens(raw)
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
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 várias listas de recompensas
local function mergeRecompensas(lista)
local total = {}
for _, raw in ipairs(lista) do
local dados = parseItens(raw)
for nome, qtd in pairs(dados) do
total[nome] = (total[nome] or 0) + qtd
end
end
return total
end
-- Formata número com milhar
local function formatNumber(n)
local s = tostring(n)
local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
return result:gsub("^,", "")
end
function p.total(frame)
local content = mw.title.getCurrentTitle():getContent()
local rewardCalls = {}
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 total = mergeRecompensas(rewardCalls)
local html = mw.html.create("div"):addClass("reward-items")
for nome, qtd in pairs(total) do
local data = itemData[nome] or {}
local nomeCompleto = data.nome or nome
local desc = data.desc
local tooltip = nomeCompleto
if desc and desc ~= "" then
tooltip = tooltip .. "\n—\n" .. desc
end
local wrapper = html:tag("span"):addClass("item-wrapper")
wrapper:attr("data-tooltip", tooltip)
wrapper:tag("span")
:wikitext(string.format("[[Arquivo:%s|link=]]", nome))
wrapper:tag("span")
:addClass("item-count")
:wikitext((nome:lower():match("^berries") and "" or "x") .. formatNumber(qtd))
end
return tostring(html)
end
return p