模块

模块:装备

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月17日 (五) 16:10的版本

此模块的文档可以在模块:装备/doc创建

local p = {}
local equipmentData = mw.loadData('模块:装备/data')

-- 辅助函数:解析文本格式
local function parseText(text)
    if not text then return "" end
    -- 将 {{文本|橙|数值}} 格式转换为HTML
    text = text:gsub("{{文本|橙|([^}]+)}}", '<span style="color:#ff9500">%1</span>')
    return text
end

-- 生成单个装备卡片
function p.card(frame)
    local args = frame.args
    local name = args[1] or args.name
    local level = args[2] or args.level or "1"
    
    if not name or not equipmentData[name] then
        return "装备不存在"
    end
    
    local equip = equipmentData[name]
    local base = equip.base
    local levelData = equip[level]
    
    if not levelData then
        return "等级数据不存在"
    end
    
    -- 生成装备卡片HTML,添加data属性用于JavaScript识别
    local html = '<span class="equipment-card-wrapper" data-equipment-name="' .. mw.text.encode(name) .. '" data-equipment-level="' .. level .. '">'
    html = html .. '<div style="position: relative; display: inline-block; width:150px; height: 230px; cursor: pointer;">'
    html = html .. '<div style="position: absolute; top: 0px; left: 0px;">[[File:bg_equipment_rarity_' .. base.rarity .. '.png|150px|link=]]</div>'
    html = html .. '<div style="position: absolute; top: 43px; left: 13px;">[[File:' .. base.art .. '|124px|link=]]</div>'
    html = html .. '<div style="position: absolute; bottom: 5px; left: 5px; width: 140px; height: 35px; background-color: rgba(0,0,0,0.5); border-radius: 0px 0px 8px 8px"></div>'
    html = html .. '<div style="position: absolute; bottom: 10px; left: 20px;">[[File:icon_star_rating_' .. level .. '.png|link=]]</div>'
    html = html .. '<div style="position: absolute; top: 0px; left: 0px;">[[File:equipment_顶层蒙版.png|150px|link=]]</div>'
    html = html .. '</div>'
    
    -- 生成隐藏的弹窗数据
    local desc = parseText(levelData.desc_global)
    html = html .. '<div class="equipment-popup-data" style="display:none;">'
    html = html .. '<div data-rarity="' .. base.rarity .. '" '
    html = html .. 'data-art="' .. base.art .. '" '
    html = html .. 'data-level="' .. level .. '" '
    html = html .. 'data-type="' .. base.type .. '" '
    html = html .. 'data-name="' .. mw.text.encode(name) .. '" '
    html = html .. 'data-value-type="' .. mw.text.encode(base.value_type) .. '" '
    html = html .. 'data-value="' .. levelData.value .. '" '
    html = html .. 'data-desc="' .. mw.text.encode(desc) .. '">'
    html = html .. '</div></div>'
    html = html .. '</span>'
    
    return frame:preprocess(html)
end

-- 生成装备列表
function p.list(frame)
    local result = {}
    for name, _ in pairs(equipmentData) do
        local newFrame = {
            args = {name = name, level = "1"},
            preprocess = frame.preprocess
        }
        table.insert(result, p.card(newFrame))
    end
    return table.concat(result, "\n")
end

return p