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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
Linha 30: Linha 30:
     if not ident or not qtdStr then return nil, nil end
     if not ident or not qtdStr then return nil, nil end
     return mw.text.trim(ident), tonumber(qtdStr)
     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
end


Linha 35: Linha 66:
     local args = frame:getParent().args or {}
     local args = frame:getParent().args or {}
     local itensRaw = args.itens or ""
     local itensRaw = args.itens or ""
    local ilhaParam = args.ilha or ""
     local lang = args.lang or "pt-br"
     local lang = args.lang or "pt"
     local rawTitle = args[1] or ""
     local rawTitle = args[1] or ""


Linha 74: Linha 104:
             local qtdFormatada = formatNumber(qtdNum)
             local qtdFormatada = formatNumber(qtdNum)


             local tooltipOverride = nil
             local isBerries = isBerriesItem(item)
            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 = {
             local bloco = {
                 item = item,
                 item = item,
                 qty = qtdFormatada,
                 qty = qtdFormatada,
                tooltipOverride = tooltipOverride,
                 isBerries = isBerries
                 isBerries = isBerries
             }
             }
Linha 101: Linha 124:
     for _, bloco in ipairs(itensNormais) do
     for _, bloco in ipairs(itensNormais) do
         local opts = { showTooltip = true, showCount = true }
         local opts = { showTooltip = true, showCount = true }
         if bloco.tooltipOverride then
         line:wikitext(Item.renderOne(bloco.item, bloco.qty, lang, opts))
            local wrapper = mw.html.create("div")
                :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
     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