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

De Wiki Gla
Ir para navegação Ir para pesquisar
(nome de usuário(a) removido)
m
 
(24 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
local p = {}
local p = {}
local itemData = require("Módulo:ItemData")
local Item = require("Módulo:Item")


local function formatNumber(num)
local function formatNumber(num)
     local s = tostring(num)
     num = tonumber(num) or 0
    local result = s:reverse():gsub("(%d%d%d)", "%1,"):reverse()
    if num >= 1e9 then
     return result:gsub("^,", "")
        return string.format("%.0f", num / 1e6) .. "KKK"
    elseif num >= 1e6 then
        return string.format("%.0f", num / 1e6) .. "KK"
    elseif num >= 1e4 then
        return string.format("%.0f", num / 1e3) .. "K"
    else
        local s = tostring(num)
        return s:reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
    end
end
 
local function parseItemEntry(entry)
    entry = mw.text.trim(entry)
     if entry == "" then return nil, nil end
 
    local ident, qtdStr
 
    if entry:find(";") then
        ident, qtdStr = entry:match("^(.-)%s*;%s*(%d+)$")
    else
        ident, qtdStr = entry:match("^(.-)%s*:%s*(%d+)$")
    end
 
    if not ident or not qtdStr then return nil, nil end
    return mw.text.trim(ident), tonumber(qtdStr)
end
 
local function normalizeForMatch(s)
    if not s then return "" end
    return mw.ustring.lower(mw.text.trim(tostring(s)))
end
end


local function buscarChaveCorreta(nome)
local function isBerriesItem(item)
     local base = mw.text.trim(nome):lower():gsub("_", " ")
    if not item then return false end
     for chave, _ in pairs(itemData) do
 
        local kbase = chave:lower():gsub("_", " "):gsub("%.png$", ""):gsub("%.gif$", ""):gsub("%.jpg$", ""):gsub("%.webp$", "")
     local image = normalizeForMatch(item.image)
        if kbase == base then
    if image ~= "" and image:find("berr", 1, true) then
             return chave
        return true
     end
 
    local namePt = item.names and normalizeForMatch(item.names.pt) or ""
    local nameEn = item.names and normalizeForMatch(item.names.en) or ""
    if namePt:find("berr", 1, true) or nameEn:find("berr", 1, true) then
        return true
    end
 
    if item.aliases and type(item.aliases) == "table" then
        for _, alias in ipairs(item.aliases) do
            local norm = normalizeForMatch(alias)
            if norm:find("berr", 1, true) then
                return true
             end
         end
         end
     end
     end
     return nil
 
     return item.category == "currency"
end
end


function p.exibir(frame)
function p.exibir(frame)
     local args = frame:getParent().args or {}
     local args = frame:getParent().args or {}
     local itens = args.itens or ""
     local itensRaw = args.itens or ""
    local lang = args.lang or "pt-br"
     local rawTitle = args[1] or ""
     local rawTitle = args[1] or ""
     local titulo = ""
     local titulo = ""
     if rawTitle == "t" then
     if rawTitle == "t" then
         titulo = "Recompensas:"
         titulo = "Recompensas:"
Linha 32: Linha 77:


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


     if titulo ~= "" then
     if titulo ~= "" then
Linha 40: Linha 85:
     end
     end


     local line = container:tag("div")
     local itensNormais = {}
     line:addClass("reward-items")
     local berriesEntry = nil


     local itensOrdenados = {}
     for entry in mw.text.gsplit(itensRaw, ",", true) do
    local berriesItem = nil
        local ident, qtdNum = parseItemEntry(entry)
        if ident and qtdNum then
            local item = Item.resolve(ident)


    for item in mw.text.gsplit(itens, ",", true) do
            if not item then
        local nome, qtd = item:match("^%s*(.-)%s*:%s*(%d+)%s*$")
                item = {
        if nome and qtd then
                    id = 0,
            nome = mw.text.trim(nome)
                    image = ident:match("%.%w+$") and ident or (ident .. ".png"),
            local chave = buscarChaveCorreta(nome)
                    names = { pt = ident, en = ident },
            local arquivo = chave or (nome .. ".png")
                    category = "unknown"
            local data = itemData[chave or (nome .. ".png")] or {}
                }
            end


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


             local prefix = nome:lower():match("^berries") and "" or "x"
             local isBerries = isBerriesItem(item)
 
            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 bloco = {
             local bloco = {
                 arquivo = arquivo,
                 item = item,
                 qtd = prefix .. qtdFormatada,
                 qty = qtdFormatada,
                 tooltip = tooltip,
                 isBerries = isBerries
                isBerries = chave and chave:lower() == "berries.gif"
             }
             }


             if bloco.isBerries then
             if isBerries then
                 berriesItem = bloco
                 berriesEntry = bloco
             else
             else
                 table.insert(itensOrdenados, bloco)
                 table.insert(itensNormais, bloco)
             end
             end
         end
         end
     end
     end


     if berriesItem then
     local line = container:tag("div"):addClass("reward-items")
          table.insert(line:tag("span")
 
            :addClass("item-wrapper")
    for _, bloco in ipairs(itensNormais) do
            :attr("data-tooltip", berriesItem.tooltip)
        local opts = { showTooltip = true, showCount = true }
            :tag("span")
        line:wikitext(Item.renderOne(bloco.item, bloco.qty, lang, opts))
                :wikitext(string.format("[[Arquivo:%s|link=]]", berriesItem.arquivo))
                :done()
            :tag("span")
                :addClass("item-count")
                :wikitext(berriesItem.qtd)
                )
     end
     end


     for _, bloco in ipairs(itensOrdenados) do
     if berriesEntry then
         local wrapper = line:tag("span")
         line:wikitext(Item.renderOne(berriesEntry.item, berriesEntry.qty, lang, {
            :addClass("item-wrapper")
             showTooltip = true,
            :attr("data-tooltip", bloco.tooltip)
            showCount = true
 
         }))
        wrapper:tag("span")
             :wikitext(string.format("[[Arquivo:%s|link=]]", bloco.arquivo))
 
         wrapper:tag("span")
            :addClass("item-count")
            :wikitext(bloco.qtd)
     end
     end



Edição atual tal como às 14h32min de 19 de maio de 2026

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

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

local function formatNumber(num)
    num = tonumber(num) or 0
    if num >= 1e9 then
        return string.format("%.0f", num / 1e6) .. "KKK"
    elseif num >= 1e6 then
        return string.format("%.0f", num / 1e6) .. "KK"
    elseif num >= 1e4 then
        return string.format("%.0f", num / 1e3) .. "K"
    else
        local s = tostring(num)
        return s:reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
    end
end

local function parseItemEntry(entry)
    entry = mw.text.trim(entry)
    if entry == "" then return nil, nil end

    local ident, qtdStr

    if entry:find(";") then
        ident, qtdStr = entry:match("^(.-)%s*;%s*(%d+)$")
    else
        ident, qtdStr = entry:match("^(.-)%s*:%s*(%d+)$")
    end

    if not ident or not qtdStr then return nil, nil end
    return mw.text.trim(ident), tonumber(qtdStr)
end

local function normalizeForMatch(s)
    if not s then return "" end
    return mw.ustring.lower(mw.text.trim(tostring(s)))
end

local function isBerriesItem(item)
    if not item then return false end

    local image = normalizeForMatch(item.image)
    if image ~= "" and image:find("berr", 1, true) then
        return true
    end

    local namePt = item.names and normalizeForMatch(item.names.pt) or ""
    local nameEn = item.names and normalizeForMatch(item.names.en) or ""
    if namePt:find("berr", 1, true) or nameEn:find("berr", 1, true) then
        return true
    end

    if item.aliases and type(item.aliases) == "table" then
        for _, alias in ipairs(item.aliases) do
            local norm = normalizeForMatch(alias)
            if norm:find("berr", 1, true) then
                return true
            end
        end
    end

    return item.category == "currency"
end

function p.exibir(frame)
    local args = frame:getParent().args or {}
    local itensRaw = args.itens or ""
    local lang = args.lang or "pt-br"
    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")
        :addClass("reward-wrapper")

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

    local itensNormais = {}
    local berriesEntry = nil

    for entry in mw.text.gsplit(itensRaw, ",", true) do
        local ident, qtdNum = parseItemEntry(entry)
        if ident and qtdNum then
            local item = Item.resolve(ident)

            if not item then
                item = {
                    id = 0,
                    image = ident:match("%.%w+$") and ident or (ident .. ".png"),
                    names = { pt = ident, en = ident },
                    category = "unknown"
                }
            end

            local qtdFormatada = formatNumber(qtdNum)

            local isBerries = isBerriesItem(item)

            local bloco = {
                item = item,
                qty = qtdFormatada,
                isBerries = isBerries
            }

            if isBerries then
                berriesEntry = bloco
            else
                table.insert(itensNormais, bloco)
            end
        end
    end

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

    for _, bloco in ipairs(itensNormais) do
        local opts = { showTooltip = true, showCount = true }
        line:wikitext(Item.renderOne(bloco.item, bloco.qty, lang, opts))
    end

    if berriesEntry then
        line:wikitext(Item.renderOne(berriesEntry.item, berriesEntry.qty, lang, {
            showTooltip = true,
            showCount = true
        }))
    end

    return tostring(container)
end

return p