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

De Wiki Gla
Ir para navegação Ir para pesquisar
Etiqueta: Revertido
m (bannerzin)
 
(74 revisões intermediárias por 3 usuários não estão sendo mostradas)
Linha 1: Linha 1:
local p = {}
local p = {}
local itemData = require("Módulo:ItemData")


-- Formata número com separador de milhar
-- Parseia bau no formato "2 comum, 8 ouro" ou "8 ouro, 2 comum". Retorna comum, ouro (números) ou nil,nil se for só tipo (comum/ouro).
local function formatNumber(n)
local function parseBau(s)
    local s = tostring(n)
     if not s or s == '' then return nil, nil end
    local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
     s          = mw.text.trim(s)
    return result:gsub("^,", "")
    local comum = tonumber(s:match('(%d+)%s*[Cc]omum')) or 0
end
    local ouro  = tonumber(s:match('(%d+)%s*[Oo]uro')) or 0
 
    if comum > 0 or ouro > 0 then
-- Converte string "item.png:10, outro.gif:20" em {["item.png"]=10, ...}
         return comum, ouro
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
     end
     return resultado
     return nil, nil
end
end


-- Junta todas as tabelas de recompensa
function p.main(frame)
local function mergeRecompensas(lista)
     local args = frame:getParent().args
     local total = {}
    local cards = {}
    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


-- Função pública
     local i = 1
function p.total(frame)
     while true do
     local args = frame:getParent().args or {}
        local nome = args['nome' .. i]
     local bonus = args.bonus or ""
        if not nome or mw.text.trim(nome) == '' then break end


    -- Conteúdo da página atual
        local link        = mw.text.trim(args['link' .. i] or '')
    local content = mw.title.getCurrentTitle():getContent()
        local img        = mw.text.trim(args['img' .. i] or '')
    local rewardCalls = {}
        local qtd        = mw.text.trim(args['qtd' .. i] or '0')
        local bauParam    = mw.text.trim(args['bau' .. i] or 'comum')


    -- Coleta os valores dos Reward
        local comum, ouro = parseBau(bauParam)
    for bloco in content:gmatch("{{%s*[Rr]eward.-}}") do
         if comum == nil and ouro == nil then
         local itens = bloco:match("itens%s*=%s*([^|}]+)")
            -- Modo antigo: bau = comum ou ouro, qtd = quantidade
        if itens then
            local n = tonumber(qtd) or 0
             table.insert(rewardCalls, itens)
            if bauParam:lower() == 'ouro' then
                comum, ouro = 0, n
             else
                comum, ouro = n, 0
            end
         end
         end
    end
    -- Adiciona recompensa manual, se houver
    if bonus and bonus ~= "" then
        table.insert(rewardCalls, bonus)
    end


    local total = mergeRecompensas(rewardCalls)
        local href      = tostring(mw.uri.localUrl(link))
        local comumHtml = frame:preprocess('[[File:Chest_normal.png|link=]]')
        local ouroHtml  = frame:preprocess('[[File:Chest_gold.png|link=]]')


    -- Monta HTML com classes isoladas e tooltip funcional
        -- Banner 380x80: fundo = img da ilha, baus canto superior esquerdo, título canto inferior esquerdo
    local html = mw.html.create("div")
        local banner    = mw.html.create('span')
    html:addClass("introquest-items")
        banner:attr('class', 'island-banner')
            :attr('data-href', href)
            :attr('data-bgimg', img)
            :attr('role', 'link')
            :attr('tabindex', '0')


    for nome, qtd in pairs(total) do
        local chests = banner:tag('span'):attr('class', 'island-chests')
         local data = itemData[nome] or {}
         if comum > 0 then
        local nomeCompleto = data.nome or nome
            chests:tag('span'):attr('class', 'island-chest-group')
        local desc = data.desc
                :node(mw.html.create(''):wikitext(comumHtml))
         local tooltip = nomeCompleto
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(comum))
         if desc and desc ~= "" then
         end
             tooltip = tooltip .. "\n—\n" .. desc
         if ouro > 0 then
             chests:tag('span'):attr('class', 'island-chest-group')
                :node(mw.html.create(''):wikitext(ouroHtml))
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(ouro))
         end
         end


         local wrapper = html:tag("span")
         banner:tag('span'):attr('class', 'island-title'):wikitext(nome)
        wrapper:addClass("introquest-item")
        wrapper:addClass("item-wrapper") -- necessário para ativar tooltip do ItemData
        wrapper:attr("data-tooltip", tooltip)


         wrapper:tag("span")
         cards[#cards + 1] = banner
            :wikitext(string.format("[[Arquivo:%s|link=]]", nome))
        i = i + 1
    end


        wrapper:tag("span")
    local grid = mw.html.create('div')
            :addClass("introquest-count")
    grid:attr('class', 'island-grid')
            :wikitext((nome:lower():match("^berries") and "" or "x") .. formatNumber(qtd))
    for _, card in ipairs(cards) do
        grid:node(card)
     end
     end


     return tostring(html)
     return tostring(grid)
end
end


return p
return p

Edição atual tal como às 23h47min de 12 de março de 2026

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

local p = {}

-- Parseia bau no formato "2 comum, 8 ouro" ou "8 ouro, 2 comum". Retorna comum, ouro (números) ou nil,nil se for só tipo (comum/ouro).
local function parseBau(s)
    if not s or s == '' then return nil, nil end
    s           = mw.text.trim(s)
    local comum = tonumber(s:match('(%d+)%s*[Cc]omum')) or 0
    local ouro  = tonumber(s:match('(%d+)%s*[Oo]uro')) or 0
    if comum > 0 or ouro > 0 then
        return comum, ouro
    end
    return nil, nil
end

function p.main(frame)
    local args = frame:getParent().args
    local cards = {}

    local i = 1
    while true do
        local nome = args['nome' .. i]
        if not nome or mw.text.trim(nome) == '' then break end

        local link        = mw.text.trim(args['link' .. i] or '')
        local img         = mw.text.trim(args['img' .. i] or '')
        local qtd         = mw.text.trim(args['qtd' .. i] or '0')
        local bauParam    = mw.text.trim(args['bau' .. i] or 'comum')

        local comum, ouro = parseBau(bauParam)
        if comum == nil and ouro == nil then
            -- Modo antigo: bau = comum ou ouro, qtd = quantidade
            local n = tonumber(qtd) or 0
            if bauParam:lower() == 'ouro' then
                comum, ouro = 0, n
            else
                comum, ouro = n, 0
            end
        end

        local href      = tostring(mw.uri.localUrl(link))
        local comumHtml = frame:preprocess('[[File:Chest_normal.png|link=]]')
        local ouroHtml  = frame:preprocess('[[File:Chest_gold.png|link=]]')

        -- Banner 380x80: fundo = img da ilha, baus canto superior esquerdo, título canto inferior esquerdo
        local banner    = mw.html.create('span')
        banner:attr('class', 'island-banner')
            :attr('data-href', href)
            :attr('data-bgimg', img)
            :attr('role', 'link')
            :attr('tabindex', '0')

        local chests = banner:tag('span'):attr('class', 'island-chests')
        if comum > 0 then
            chests:tag('span'):attr('class', 'island-chest-group')
                :node(mw.html.create(''):wikitext(comumHtml))
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(comum))
        end
        if ouro > 0 then
            chests:tag('span'):attr('class', 'island-chest-group')
                :node(mw.html.create(''):wikitext(ouroHtml))
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(ouro))
        end

        banner:tag('span'):attr('class', 'island-title'):wikitext(nome)

        cards[#cards + 1] = banner
        i = i + 1
    end

    local grid = mw.html.create('div')
    grid:attr('class', 'island-grid')
    for _, card in ipairs(cards) do
        grid:node(card)
    end

    return tostring(grid)
end

return p