模块:装备
来自卡厄思梦境WIKI
此模块的文档可以在模块:装备/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:#FF9900">%1</span>')
return text
end
-- 获取装备稀有度对应的背景图片
local function getRarityBg(rarity)
local rarityMap = {
["蓝"] = "bg_equipment_rarity_蓝.png",
["紫"] = "bg_equipment_rarity_紫.png",
["金"] = "bg_equipment_rarity_金.png"
}
return rarityMap[rarity] or "bg_equipment_rarity_蓝.png"
end
-- 获取装备类型对应的图标
local function getTypeIcon(equipType, rarity)
local typeMap = {
["武器"] = "武器",
["刀"] = "武器",
["装甲"] = "装甲",
["戒指"] = "戒指"
}
local mappedType = typeMap[equipType] or "武器"
return string.format("icon_equip_%s_%s.png", mappedType, rarity)
end
-- 生成装备卡片(包含弹窗内容)
function p.card(frame)
local args = frame.args
if not args[1] and frame:getParent() then
args = frame:getParent().args
end
local name = args[1] or args.name
local level = args[2] or args.level or "1"
if not name then
return "错误:未指定装备名称"
end
if not equipmentData[name] then
return "错误:装备 '" .. name .. "' 不存在"
end
local equip = equipmentData[name]
local base = equip.base
local levelData = equip[level] or equip["1"]
-- 生成唯一ID
local uniqueId = "equip_" .. mw.uri.encode(name, "WIKI") .. "_" .. level .. "_" .. tostring(os.time())
-- 卡片相关
local rarityBg = getRarityBg(base.rarity)
-- 弹窗相关
local collectionBg = "bg_collection_rarity_" .. base.rarity .. ".png"
local typeIcon = getTypeIcon(base.type, base.rarity)
local valueTypeText = base.value_type == "atk" and "攻击力" or "防御力"
local desc = parseText(levelData.desc_global)
-- 生成完整HTML(卡片+隐藏的弹窗)
local html = string.format([=[
<!-- 装备卡片和弹窗容器 -->
<div class="equipment-wrapper" style="display: inline-block;">
<!-- 装备卡片 -->
<label for="%s" class="equipment-card-label" style="cursor: pointer; display: inline-block;">
<div style="position: relative; width:150px; height: 230px;">
<div style="position: absolute; top: 0px; left: 0px;">[[File:%s|150px|link=]]</div>
<div style="position: absolute; top: 43px; left: 13px;">[[File:%s|124px|link=]]</div>
<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>
<div style="position: absolute; bottom: 10px; left: 20px;">[[File:icon_star_rating_%s.png|link=]]</div>
<div style="position: absolute; top: 0px; left: 0px;">[[File:equipment_顶层蒙版.png|150px|link=]]</div>
</div>
</label>
<!-- 隐藏的checkbox用于控制弹窗 -->
<input type="checkbox" id="%s" class="equipment-popup-toggle" style="display: none;">
<!-- 弹窗遮罩和内容 -->
<div class="equipment-popup-overlay">
<label for="%s" class="equipment-popup-bg"></label>
<div class="equipment-popup-container">
<label for="%s" class="equipment-popup-close">×</label>
<div style="position: relative; width:368px; height: 335px; background-color: #343434; border-radius: 9px">
<div style="position: absolute; top: 0px; left: 0px;">[[File:%s|368px|link=]]</div>
<div style="position: absolute; top: 40px; left: 128px;">[[File:%s|124px|link=]]</div>
<div style="position: absolute; top: 167px; left: 0px; width: 368px; height: 35px; background-color: rgba(0,0,0,0.5);"></div>
<div style="position: absolute; top: 173px; left: 128px;">[[File:icon_star_rating_%s.png|link=]]</div>
<div style="position: absolute; top: 205px; left: 5px;">[[File:%s|25px|link=]]</div>
<div style="position: absolute; top: 209px; left: 33px; color: white; font-size: 16px; font-weight: bold;">%s</div>
<div style="position: absolute; top: 235px; left: 9px; width: 350px; height: 35px; background-color: rgba(255,255,255,0.3); border-radius: 2px"></div>
<div style="position: absolute; top: 242px; left: 14px; color: white;">%s</div>
<div style="position: absolute; top: 242px; right: 14px; color: white;">%s</div>
<div style="position: absolute; top: 277px; left: 10px; right: 10px; color: white; font-size: 13px; line-height: 1.4;">%s</div>
</div>
</div>
</div>
</div>]=],
uniqueId, -- for属性
rarityBg,
base.art,
level,
uniqueId, -- checkbox id
uniqueId, -- 关闭按钮的for
uniqueId, -- 背景遮罩的for
collectionBg,
base.art,
level,
typeIcon,
name,
valueTypeText,
levelData.value,
desc
)
return frame:preprocess(html)
end
-- 批量显示装备(用于装备列表页面)
function p.list(frame)
local args = frame.args
if not args[1] and frame:getParent() then
args = frame:getParent().args
end
local html = '<div class="equipment-list">'
local i = 1
while args[i] do
local parts = mw.text.split(args[i], "|")
local name = parts[1]
local level = parts[2] or "1"
if name and equipmentData[name] then
-- 调用card函数生成单个装备
local cardFrame = {
args = {name, level},
preprocess = frame.preprocess,
getParent = function() return nil end
}
html = html .. p.card(cardFrame)
end
i = i + 1
end
html = html .. '</div>'
return html
end
return p