Módulo:Conquistas

De Wiki Gla
Revisão de 00h57min de 15 de maio de 2026 por Gurren1 (discussão | contribs)
Ir para navegação Ir para pesquisar

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

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

local VALID_TAGS = {
    geral       = true,
    personagens = true,
    missao      = true,
    bau         = true,
    navegacao   = true,
    pvp         = true,
    pve         = true,
    coliseu     = true,
    poneglyph   = true,
    indicacao   = true,
    celular     = true,
    bossrush    = true,
}

local function trim(s)
    return mw.text.trim(s or "")
end

local function normalizeTag(tag)
    local t = trim(tag):lower()
    if t == "" or not VALID_TAGS[t] then
        return "geral"
    end
    return t
end

local function normalizeImageName(name)
    local clean = trim(name)
    if clean == "" then return "conquistafoosha.png" end
    if clean:match("%.%w+$") then return clean end
    return clean .. ".png"
end

local function parseRewardString(s)
    local entries = {}
    s = trim(s)
    if s == "" then return entries end

    for entry in mw.text.gsplit(s, ";", true) do
        local clean = trim(entry)
        if clean ~= "" then
            local ident, qtdStr = clean:match("^(.-)%s*:%s*(%-?%d+)$")
            if ident and qtdStr then
                ident = trim(ident)
                local qtd = tonumber(qtdStr)
                if ident ~= "" and qtd then
                    table.insert(entries, { ident = ident, qtd = qtd })
                end
            end
        end
    end

    return entries
end

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 resolveRewardItem(ident)
    local item = Item.resolve(ident)
    if item then
        return item
    end

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

local function renderReward(entries, lang)
    if not entries or #entries == 0 then
        return ""
    end

    local wrapper = mw.html.create("div"):addClass("reward-wrapper")
    local line = wrapper:tag("div"):addClass("reward-items")

    local normal = {}
    local berries = {}

    for _, e in ipairs(entries) do
        local item = resolveRewardItem(e.ident)
        local block = {
            item = item,
            qty = formatNumber(e.qtd)
        }

        local image = Item.getImage(item) or ""
        local isBerries = item.category == "currency" and image:lower():find("berries")
        if isBerries then
            table.insert(berries, block)
        else
            table.insert(normal, block)
        end
    end

    local function appendBlock(b)
        -- Tooltip desligado aqui para reduzir custo de parse em páginas enormes.
        line:wikitext(Item.renderOne(b.item, b.qty, lang, {
            showTooltip = false,
            showCount = true
        }))
    end

    for _, b in ipairs(normal) do
        appendBlock(b)
    end
    for _, b in ipairs(berries) do
        appendBlock(b)
    end

    return tostring(wrapper)
end

local function getArgs(frame)
    local args = frame.args or {}
    if not next(args) and frame:getParent() then
        args = frame:getParent().args or {}
    end
    return args
end

function p.card(frame)
    local args     = getArgs(frame)

    local name     = trim(args.name)
    local cardType = trim(args.type)
    local tag      = normalizeTag(cardType ~= "" and cardType or args.tag)
    local icon     = normalizeImageName(args.icon)
    local desc     = trim(args.desc)
    local reward   = trim(args.reward)
    local lang     = trim(args.lang)
    if lang == "" then lang = "pt" end

    if name == "" then name = "Titulo da Conquista" end
    if desc == "" then desc = "Descricao da conquista." end

    local card = mw.html.create("div")
        :addClass("gla-item")
        :attr("data-tab", tag)

    local left = card:tag("div"):addClass("gla-item-left")

    left:tag("div")
        :addClass("gla-item-icon")
        :wikitext(string.format("[[File:%s|56px|alt=%s|link=]]", icon, name))

    local textWrap = left:tag("div")
    textWrap:tag("div"):addClass("gla-item-title"):wikitext(name)
    textWrap:tag("div"):addClass("gla-item-desc"):wikitext(desc)

    local rewardList = parseRewardString(reward)
    if #rewardList > 0 then
        local rewardHtml = renderReward(rewardList, lang)

        card:tag("div")
            :addClass("gla-item-reward")
            :wikitext("Recompensa: " .. rewardHtml)
    end

    return tostring(card)
end

-- Compatibilidade com chamadas antigas
p.render = p.card

return p