Módulo:Item

De Wiki Gla
Revisão de 22h19min de 16 de março de 2026 por Gurren1 (discussão | contribs) (init)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)
Ir para navegação Ir para pesquisar

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

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

local DEFAULT_LANG = "pt"
local FALLBACK_LANG = "en"
local DEFAULT_FRAME_SIZE = 32

function Item.resolve(query)
    if not query or query == "" then return nil end
    return ItemDB.get(query)
end

function Item.getName(item, lang)
    if not item or not item.names then return "" end
    lang = lang or DEFAULT_LANG
    return item.names[lang]
        or item.names[DEFAULT_LANG]
        or item.names[FALLBACK_LANG]
        or item.image or ""
end

function Item.getDesc(item, lang)
    if not item or not item.desc then return nil end
    lang = lang or DEFAULT_LANG
    return item.desc[lang]
        or item.desc[DEFAULT_LANG]
        or item.desc[FALLBACK_LANG]
        or nil
end

function Item.getImage(item)
    if not item then return nil end
    return item.image
end

function Item.getSpriteSize(item)
    if item and item.sprite then
        return item.sprite.frameWidth or DEFAULT_FRAME_SIZE,
            item.sprite.frameHeight or DEFAULT_FRAME_SIZE
    end
    return DEFAULT_FRAME_SIZE, DEFAULT_FRAME_SIZE
end

function Item.renderOne(item, qty, lang, options)
    options = options or {}
    lang = lang or DEFAULT_LANG
    if not item then return "" end

    local nome = Item.getName(item, lang)
    local desc = Item.getDesc(item, lang)
    local image = Item.getImage(item)
    if not image then return "" end

    local tooltip = nome
    if options.showTooltip ~= false and desc and desc ~= "" then
        tooltip = tooltip .. "\n—\n" .. desc
    end

    local w, h = Item.getSpriteSize(item)

    local wrapper = mw.html.create("span")
        :addClass("item-wrapper")
        :css("width", w .. "px")
        :css("height", h .. "px")

    if options.showTooltip ~= false and tooltip ~= "" then
        wrapper:attr("data-tooltip", tooltip)
    end

    if image then
        wrapper:tag("span")
            :wikitext(string.format("[[Arquivo:%s|%dx%dpx|link=]]", image, w, h))
    end

    if qty and options.showCount ~= false then
        local countSpan = wrapper:tag("span"):addClass("item-count")
        local isBerries = item.category == "currency" and image:lower():find("berries")
        if not isBerries then
            countSpan:tag("span"):addClass("item-count-x"):wikitext("x")
        end
        countSpan:wikitext(tostring(qty))
    end

    return tostring(wrapper)
end

function Item.renderLine(itemList, lang, options)
    options = options or {}
    lang = lang or DEFAULT_LANG
    if not itemList or #itemList == 0 then return "" end

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

    for _, entry in ipairs(itemList) do
        container:wikitext(Item.renderOne(entry.item, entry.qty, lang, options))
    end

    return tostring(container)
end

return Item