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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
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)
Linha 16: Linha 16:
end
end


local function buscarChaveCorreta(nome)
local function parseItemEntry(entry)
     local base = mw.text.trim(nome):lower():gsub("_", " "):gsub("%.png$", ""):gsub("%.gif$", ""):gsub("%.jpg$", ""):gsub("%.webp$", "")
     entry = mw.text.trim(entry)
    for chave, dados in pairs(itemData) do
    if entry == "" then return nil, nil end
        local kbase = chave:lower():gsub("_", " "):gsub("%.png$", ""):gsub("%.gif$", ""):gsub("%.jpg$", ""):gsub("%.webp$", "")
 
        if kbase == base then
    local ident, qtdStr
            return chave
 
         end
    if entry:find(";") then
        local nomeTraduzido = (dados.nome or ""):lower():gsub("_", " "):gsub("%.png$", ""):gsub("%.gif$", ""):gsub("%.jpg$", ""):gsub("%.webp$", "")
        ident, qtdStr = entry:match("^(.-)%s*;%s*(%d+)$")
        if nomeTraduzido == base then
    else
            return chave
         ident, qtdStr = entry:match("^(.-)%s*:%s*(%d+)$")
        end
     end
     end
     return nil
 
     if not ident or not qtdStr then return nil, nil end
    return mw.text.trim(ident), tonumber(qtdStr)
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 ilhaParam = args.ilha or ""
     local ilhaParam = args.ilha or ""
    local lang = args.lang or "pt"
     local rawTitle = args[1] or ""
     local rawTitle = args[1] or ""
     local titulo = ""
     local titulo = ""
    -- O parâmetro |ignore= pode ser qualquer coisa, só precisa estar presente
    -- Ele é lido no módulo Quest, então aqui não precisa fazer nada com ele
     if rawTitle == "t" then
     if rawTitle == "t" then
         titulo = "Recompensas:"
         titulo = "Recompensas:"
Linha 48: Linha 47:


     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 56: Linha 55:
     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 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 },
            if not chave then
                    category = "unknown"
                chave = nome .. ".png"
                }
             end
             end


            local data = itemData[chave] or {}
             local qtdFormatada = formatNumber(qtdNum)
             local qtdFormatada = formatNumber(tonumber(qtd) or 0)
            local nomeCompleto = data.nome or nome
            local descricao = data.desc
            local tooltip = nomeCompleto


             if chave:lower() == "pose.png" and ilhaParam ~= "" then
            local tooltipOverride = nil
                 tooltip = "Eternal Pose - " .. ilhaParam
             if item.image and item.image:lower() == "pose.png" and ilhaParam ~= "" then
            elseif descricao and descricao ~= "" then
                 tooltipOverride = "Eternal Pose - " .. ilhaParam
                tooltip = tooltip .. "\n—\n" .. descricao
             end
             end


             table.insert(itensOrdenados, {
             local isBerries = item.category == "currency"
                 arquivo = chave,
                and item.image and item.image:lower():find("berries")
                 qtd = qtdFormatada,
 
                 tooltip = tooltip,
            local bloco = {
                 isBerries = chave:lower() == "berries.gif"
                 item = item,
             })
                 qty = qtdFormatada,
                 tooltipOverride = tooltipOverride,
                 isBerries = isBerries
            }
 
            if isBerries then
                berriesEntry = bloco
            else
                table.insert(itensNormais, bloco)
             end
         end
         end
     end
     end


     table.sort(itensOrdenados, function(a, b)
     local line = container:tag("div"):addClass("reward-items")
         if a.isBerries then
 
             return false
    for _, bloco in ipairs(itensNormais) do
        elseif b.isBerries then
        local opts = { showTooltip = true, showCount = true }
             return true
         if bloco.tooltipOverride then
             local wrapper = mw.html.create("span")
                :addClass("item-wrapper")
                :attr("data-tooltip", bloco.tooltipOverride)
            local w, h = Item.getSpriteSize(bloco.item)
            wrapper:css("width", w .. "px"):css("height", h .. "px")
            wrapper:tag("span")
                :wikitext(string.format("[[Arquivo:%s|%dx%dpx|link=]]", bloco.item.image, w, h))
            local countSpan = wrapper:tag("span"):addClass("item-count")
            countSpan:tag("span"):addClass("item-count-x"):wikitext("x")
            countSpan:wikitext(bloco.qty)
             line:wikitext(tostring(wrapper))
         else
         else
             return a.arquivo < b.arquivo
             line:wikitext(Item.renderOne(bloco.item, bloco.qty, lang, opts))
         end
         end
     end)
     end
 
    for _, bloco in ipairs(itensOrdenados) do
        local wrapper = line:tag("span")
            :addClass("item-wrapper")
            :attr("data-tooltip", bloco.tooltip)
 
        wrapper:tag("span")
            :wikitext(string.format("[[Arquivo:%s|link=]]", bloco.arquivo))


         local quantidade = wrapper:tag("span"):addClass("item-count")
    if berriesEntry then
        if not bloco.isBerries then
         line:wikitext(Item.renderOne(berriesEntry.item, berriesEntry.qty, lang, {
             quantidade:tag("span"):addClass("item-count-x"):wikitext("x")
             showTooltip = true,
        end
            showCount = true
         quantidade:wikitext(bloco.qtd)
         }))
     end
     end



Edição atual tal como às 22h20min de 16 de março 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

function p.exibir(frame)
    local args = frame:getParent().args or {}
    local itensRaw = args.itens or ""
    local ilhaParam = args.ilha or ""
    local lang = args.lang or "pt"
    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 tooltipOverride = nil
            if item.image and item.image:lower() == "pose.png" and ilhaParam ~= "" then
                tooltipOverride = "Eternal Pose - " .. ilhaParam
            end

            local isBerries = item.category == "currency"
                and item.image and item.image:lower():find("berries")

            local bloco = {
                item = item,
                qty = qtdFormatada,
                tooltipOverride = tooltipOverride,
                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 }
        if bloco.tooltipOverride then
            local wrapper = mw.html.create("span")
                :addClass("item-wrapper")
                :attr("data-tooltip", bloco.tooltipOverride)
            local w, h = Item.getSpriteSize(bloco.item)
            wrapper:css("width", w .. "px"):css("height", h .. "px")
            wrapper:tag("span")
                :wikitext(string.format("[[Arquivo:%s|%dx%dpx|link=]]", bloco.item.image, w, h))
            local countSpan = wrapper:tag("span"):addClass("item-count")
            countSpan:tag("span"):addClass("item-count-x"):wikitext("x")
            countSpan:wikitext(bloco.qty)
            line:wikitext(tostring(wrapper))
        else
            line:wikitext(Item.renderOne(bloco.item, bloco.qty, lang, opts))
        end
    end

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

    return tostring(container)
end

return p