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

De Wiki Gla
Ir para navegação Ir para pesquisar
Linha 1: Linha 1:
local p = {}
local p = {}
local itemData = require("Módulo:ItemData")
local itemData = require("Módulo:Itemdata")


local function formatNumber(num)
local function formatNumber(num)
     local s = tostring(num)
     local s = tostring(num)
     local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
     local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
     result = result:gsub("^,", "")
     return result:gsub("^,", "")
     return result
end
 
-- Converte nome informal em chave e nome de arquivo padronizados
local function normalizarChave(nome)
    nome = mw.text.trim(nome)
    nome = nome:gsub("_", " ")
    nome = nome:gsub("(%a)([%w]*)", function(first, rest)
        return first:upper() .. rest
    end)
    local temExt = nome:match("%.png$") or nome:match("%.gif$") or nome:match("%.jpg$") or nome:match("%.webp$")
    if not temExt then
        nome = nome .. ".png"
    end
     return nome
end
end


Linha 36: Linha 49:
         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
             local temExt = nome:match("%.png$")
             local chave = normalizarChave(nome)
                        or nome:match("%.gif$")
             local data = itemData[chave] or {}
                        or nome:match("%.jpg$")
                        or nome:match("%.webp$")
            local nomeChave = temExt and nome or (nome .. ".png")
             local data = itemData[nomeChave] or {}


            local arquivo = temExt and nome or (nome .. ".png")
             local numero = tonumber(qtd) or 0
             local numero = tonumber(qtd) or 0
             local qtdFormatada = formatNumber(numero)
             local qtdFormatada = formatNumber(numero)


             local prefix = ""
             local prefix = ""
             if not nome:lower():match("^berries") then
             if not chave:lower():match("^berries") then
                 prefix = "x"
                 prefix = "x"
             end
             end
Linha 65: Linha 73:


             wrapper:tag("span")
             wrapper:tag("span")
                 :wikitext(string.format("[[Arquivo:%s|link=]]", arquivo))
                 :wikitext(string.format("[[Arquivo:%s|link=]]", chave))


             wrapper:tag("span")
             wrapper:tag("span")

Edição das 20h18min de 10 de junho de 2025

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

local p = {}
local itemData = require("Módulo:Itemdata")

local function formatNumber(num)
    local s = tostring(num)
    local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
    return result:gsub("^,", "")
end

-- Converte nome informal em chave e nome de arquivo padronizados
local function normalizarChave(nome)
    nome = mw.text.trim(nome)
    nome = nome:gsub("_", " ")
    nome = nome:gsub("(%a)([%w]*)", function(first, rest)
        return first:upper() .. rest
    end)
    local temExt = nome:match("%.png$") or nome:match("%.gif$") or nome:match("%.jpg$") or nome:match("%.webp$")
    if not temExt then
        nome = nome .. ".png"
    end
    return nome
end

function p.exibir(frame)
    local args = frame:getParent().args or {}
    local itens = args.itens or ""
    local rawTitle = args[1] or ""
    local titulo = ""

    if rawTitle == "t" then
        titulo = "Recompensas:"
    elseif rawTitle ~= "" then
        titulo = rawTitle
    end

    local container = mw.html.create("div")
    container:addClass("reward-wrapper")

    if titulo ~= "" then
        container:tag("div")
            :addClass("reward-title")
            :wikitext(titulo)
    end

    local line = container:tag("div")
    line:addClass("reward-items")

    for item in mw.text.gsplit(itens, ",", true) do
        local nome, qtd = item:match("^%s*(.-)%s*:%s*(%d+)%s*$")
        if nome and qtd then
            local chave = normalizarChave(nome)
            local data = itemData[chave] or {}

            local numero = tonumber(qtd) or 0
            local qtdFormatada = formatNumber(numero)

            local prefix = ""
            if not chave:lower():match("^berries") then
                prefix = "x"
            end

            local nomeCompleto = data.nome or nome
            local descricao = data.desc
            local tooltip = nomeCompleto

            if descricao and descricao ~= "" then
                tooltip = tooltip .. "\n—\n" .. descricao
            end

            local wrapper = line:tag("span")
                :addClass("item-wrapper")
                :attr("data-tooltip", tooltip)

            wrapper:tag("span")
                :wikitext(string.format("[[Arquivo:%s|link=]]", chave))

            wrapper:tag("span")
                :addClass("item-count")
                :wikitext(prefix .. qtdFormatada)
        end
    end

    return tostring(container)
end

return p