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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
Linha 121: Linha 121:
     local w, h = Item.getSpriteSize(item)
     local w, h = Item.getSpriteSize(item)


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


     if options.showTooltip ~= false then
     if options.showTooltip ~= false then
         local tip  = wrapper:tag("span"):addClass("item-tooltip")
        -- div (não span): descrições em wikitext podem virar <p>/blocos; span+bloco
         local body = tip:tag("span"):addClass("item-tooltip-body")
        -- quebra o DOM e os próximos .item-wrapper acabam dentro do tooltip.
         local tip  = wrapper:tag("div"):addClass("item-tooltip")
         local body = tip:tag("div"):addClass("item-tooltip-body")


         body:tag("span"):addClass("item-tooltip-name"):wikitext(nome)
         body:tag("div"):addClass("item-tooltip-name"):wikitext(nome)


         if catLabel then
         if catLabel then
             body:tag("span"):addClass("item-tooltip-cat"):wikitext(catLabel)
             body:tag("div"):addClass("item-tooltip-cat"):wikitext(catLabel)
         end
         end


         local hasExtra = desc or value or level
         local hasExtra = desc or value or level
         if hasExtra then
         if hasExtra then
             body:tag("span"):addClass("item-tooltip-sep")
             body:tag("div"):addClass("item-tooltip-sep")
         end
         end


         if desc then
         if desc then
             body:tag("span"):addClass("item-tooltip-desc"):wikitext(desc)
             body:tag("div"):addClass("item-tooltip-desc"):wikitext(desc)
         end
         end


         if value or level then
         if value or level then
             local footer = body:tag("span"):addClass("item-tooltip-footer")
             local footer = body:tag("div"):addClass("item-tooltip-footer")
             if value then
             if value then
                 local valLabel = (lang == "pt") and "Valor: " or "Value: "
                 local valLabel = (lang == "pt") and "Valor: " or "Value: "
Linha 175: Linha 177:
         end
         end


         tip:tag("span"):addClass("item-tooltip-arrow")
         tip:tag("div"):addClass("item-tooltip-arrow")
     end
     end



Edição atual tal como às 00h43min de 8 de abril de 2026

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 = "en"
local FALLBACK_LANG = "pt"
local DEFAULT_FRAME_SIZE = 32

local CATEGORY_LABELS = {
    general_item    = { en = "General Item", pt = "Item Geral" },
    special_item    = { en = "Special Item", pt = "Item Especial" },
    currency        = { en = "Currency", pt = "Moeda (câmbio)" },
    key_item        = { en = "Key Item", pt = "Item-chave" },
    cosmetic        = { en = "Cosmetic", pt = "Cosmético" },
    material        = { en = "Material", pt = "Material" },
    potion          = { en = "Potion", pt = "Poção" },
    medal           = { en = "Medal", pt = "Medalha" },
    chip            = { en = "Chip", pt = "Chip" },
    food            = { en = "Food", pt = "Comida" },
    quest_item      = { en = "Quest Item", pt = "Item de missão" },
    book            = { en = "Book", pt = "Livro" },
    tool            = { en = "Tool", pt = "Ferramenta" },
    misc            = { en = "Misc", pt = "Diversos" },
    set_piece       = { en = "Set Piece", pt = "Peça de conjunto" },
    head            = { en = "Head", pt = "Cabeça" },
    body            = { en = "Body", pt = "Corpo" },
    legs            = { en = "Legs", pt = "Perna" },
    weapon          = { en = "Weapon", pt = "Arma" },
    accessory       = { en = "Accessory", pt = "Acessório" },
    emblem          = { en = "Emblem", pt = "Emblema" },
    ship_style      = { en = "Ship Style", pt = "Estilo de Navio" },
    costume         = { en = "Costume", pt = "Traje" },
    navigation      = { en = "Navigation", pt = "Navegação" },
    consumable      = { en = "Consumable", pt = "Consumível" },
    profile_icon    = { en = "Profile Icon", pt = "Ícone de Perfil" },
    gem             = { en = "Gem", pt = "Gema" },
    den_den_mushi   = { en = "Den Den Mushi", pt = "Den Den Mushi" },
    crystal         = { en = "Crystal", pt = "Cristal" },
    awakening_stone = { en = "Awakening Stone", pt = "Pedra do Despertar" },
    headstone       = { en = "Headstone", pt = "Lápide" },
    coin            = { en = "Coin", pt = "Moeda" },
    gem_currency    = { en = "Gem", pt = "Gema" },
}

local function formatDots(n)
    local s = tostring(n)
    local pos = #s % 3
    if pos == 0 then pos = 3 end
    local parts = { s:sub(1, pos) }
    for i = pos + 1, #s, 3 do
        parts[#parts + 1] = s:sub(i, i + 2)
    end
    return table.concat(parts, ".")
end

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]
end

function Item.getValue(item)
    if not item then return nil end
    return item.value
end

function Item.getLevel(item)
    if not item then return nil end
    return item.level
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.getCategoryLabel(item, lang)
    if not item or not item.category then return nil end
    local entry = CATEGORY_LABELS[item.category]
    if not entry then return nil end
    lang = lang or DEFAULT_LANG
    return entry[lang] or entry[DEFAULT_LANG] or entry[FALLBACK_LANG] or nil
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)
    local catLabel = Item.getCategoryLabel(item, lang)
    local value    = Item.getValue(item)
    local level    = Item.getLevel(item)
    if not image then return "" end

    local w, h = Item.getSpriteSize(item)

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

    if item.category == "special_item" then
        wrapper:addClass("item-wrapper-special")
    end

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

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

    if options.showTooltip ~= false then
        -- div (não span): descrições em wikitext podem virar <p>/blocos; span+bloco
        -- quebra o DOM e os próximos .item-wrapper acabam dentro do tooltip.
        local tip  = wrapper:tag("div"):addClass("item-tooltip")
        local body = tip:tag("div"):addClass("item-tooltip-body")

        body:tag("div"):addClass("item-tooltip-name"):wikitext(nome)

        if catLabel then
            body:tag("div"):addClass("item-tooltip-cat"):wikitext(catLabel)
        end

        local hasExtra = desc or value or level
        if hasExtra then
            body:tag("div"):addClass("item-tooltip-sep")
        end

        if desc then
            body:tag("div"):addClass("item-tooltip-desc"):wikitext(desc)
        end

        if value or level then
            local footer = body:tag("div"):addClass("item-tooltip-footer")
            if value then
                local valLabel = (lang == "pt") and "Valor: " or "Value: "
                footer:tag("span"):addClass("item-tooltip-val")
                    :wikitext(valLabel .. formatDots(value))
            end
            if level then
                local lvlLabel = (lang == "pt") and "Nv. " or "Lv. "
                footer:tag("span"):addClass("item-tooltip-lvl")
                    :wikitext(lvlLabel .. tostring(level))
            end
        end

        tip:tag("div"):addClass("item-tooltip-arrow")
    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