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

De Wiki Gla
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 no formato "nome:quantidade"
-- 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
            -- Remove espaços extras e força nome de arquivo com extensão
             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 múltiplas listas de recompensa
-- Junta várias listas de recompensas
local function mergeRecompensas(rewardsList)
local function mergeRecompensas(lista)
     local total = {}
     local total = {}
     for _, raw in ipairs(rewardsList) do
     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


-- Função pública
-- 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 = {}


    -- Procura chamadas de {{Reward|...|itens=...}}
     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 totais = mergeRecompensas(rewardCalls)
     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)


    local html = mw.html.create("div")
        wrapper:tag("span")
    html:addClass("missao-recompensa-final")
            :wikitext(string.format("[[Arquivo:%s|link=]]", nome))


    for nome, qtd in pairs(totais) do
         wrapper:tag("span")
         html:tag("span")
             :addClass("item-count")
             :addClass("item-wrapper")
             :wikitext((nome:lower():match("^berries") and "" or "x") .. formatNumber(qtd))
             :wikitext(string.format("[[Arquivo:%s|20px|link=]] x%d", nome, qtd))
     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