装备:修订间差异
来自卡厄思梦境WIKI
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
-- 模块:装备 | |||
local p = {} | local p = {} | ||
local | -- 兼容不同命名空间的 data 路径 | ||
local function loadData() | |||
local paths = { '模块:装备/data', 'Module:装备/data' } | |||
for _, path in ipairs(paths) do | |||
local ok, d = pcall(mw.loadData, path) | |||
if ok and type(d) == 'table' then | |||
return d | |||
end | |||
end | |||
return {} | |||
end | |||
local function getArgs(frame) | local data = loadData() | ||
local args = frame.args or {} | |||
-- 更稳健的参数获取(优先使用 Arguments 模块,兼容中英文命名) | |||
local function getArgsCompat(frame) | |||
local tryModules = { 'Module:Arguments', '模块:Arguments' } | |||
for _, m in ipairs(tryModules) do | |||
local ok, Arguments = pcall(require, m) | |||
if ok and type(Arguments) == 'table' and type(Arguments.getArgs) == 'function' then | |||
return Arguments.getArgs(frame, { parentOnly = false }) | |||
end | |||
end | |||
-- 回退:合并 frame.args 与 parent.args 到普通表 | |||
local args = {} | |||
local fa = frame.args or {} | |||
for k, v in pairs(fa) do args[k] = v end | |||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent and parent.args then | ||
for k, v in pairs(parent.args) do | |||
if args[k] == nil then args[k] = v end | |||
end | |||
end | end | ||
return args | return args | ||
end | end | ||
local function clampLevel(level) | local function clampLevel(level) | ||
level = tonumber(level) | level = tonumber(level) | ||
if level < 1 then | if not level then return 1 end | ||
if level > 5 then | if level < 1 then return 1 end | ||
if level > 5 then return 5 end | |||
return level | return level | ||
end | end | ||
| 第29行: | 第49行: | ||
level = clampLevel(level) | level = clampLevel(level) | ||
return string.format("icon_star_rating_%d.png", level) | return string.format("icon_star_rating_%d.png", level) | ||
end | |||
local function sanitizeId(s) | |||
s = tostring(s or '') | |||
s = mw.text.trim(s) | |||
s = mw.ustring.gsub(s, "%s+", "_") | |||
s = mw.ustring.gsub(s, "[%[%]{}()%c%p]", "_") | |||
return s | |||
end | end | ||
function p.render(frame) | function p.render(frame) | ||
local args = | local args = getArgsCompat(frame) | ||
local name = args.name or args[1] | -- 支持多种参数名(中文键使用方括号访问,避免语法错误) | ||
local level = clampLevel(args.level or args.lv or args[2] or 1) | local name = args.name or args['名称'] or args[1] | ||
local level = clampLevel(args.level or args.lv or args['等级'] or args[2] or 1) | |||
if not name or not data[name] then | if not name or name == '' then | ||
return string.format("<span style='color:red'>[装备] 数据未找到:%s</span>", | return "<span style='color:red'>[装备] 数据未找到:未指定</span>" | ||
end | |||
if not data[name] then | |||
return string.format("<span style='color:red'>[装备] 数据未找到:%s</span>", mw.text.encode(name)) | |||
end | end | ||
| 第55行: | 第87行: | ||
local root = mw.html.create('div') | local root = mw.html.create('div') | ||
-- | -- 卡片 | ||
local card = root:tag('div') | local card = root:tag('div') | ||
:addClass('equip-card') | :addClass('equip-card') | ||
| 第80行: | 第112行: | ||
:wikitext('[[File:equipment_顶层蒙版.png|150px|link=]]') | :wikitext('[[File:equipment_顶层蒙版.png|150px|link=]]') | ||
-- | -- 弹窗 | ||
local modalContainer = root:tag('div') | local modalContainer = root:tag('div') | ||
:addClass('equip-modal-container') | :addClass('equip-modal-container') | ||
| 第86行: | 第118行: | ||
:attr('style', 'display: none;') | :attr('style', 'display: none;') | ||
local modal = modalContainer:tag('div') | local modal = modalContainer:tag('div') | ||
:addClass('equip-modal') | :addClass('equip-modal') | ||
| 第130行: | 第160行: | ||
:wikitext(desc) | :wikitext(desc) | ||
modal:tag('div') | modal:tag('div') | ||
:addClass('equip-modal-close') | :addClass('equip-modal-close') | ||
2025年10月17日 (五) 15:40的版本
此模块的文档可以在模块:装备/doc创建
-- 模块:装备
local p = {}
-- 兼容不同命名空间的 data 路径
local function loadData()
local paths = { '模块:装备/data', 'Module:装备/data' }
for _, path in ipairs(paths) do
local ok, d = pcall(mw.loadData, path)
if ok and type(d) == 'table' then
return d
end
end
return {}
end
local data = loadData()
-- 更稳健的参数获取(优先使用 Arguments 模块,兼容中英文命名)
local function getArgsCompat(frame)
local tryModules = { 'Module:Arguments', '模块:Arguments' }
for _, m in ipairs(tryModules) do
local ok, Arguments = pcall(require, m)
if ok and type(Arguments) == 'table' and type(Arguments.getArgs) == 'function' then
return Arguments.getArgs(frame, { parentOnly = false })
end
end
-- 回退:合并 frame.args 与 parent.args 到普通表
local args = {}
local fa = frame.args or {}
for k, v in pairs(fa) do args[k] = v end
local parent = frame:getParent()
if parent and parent.args then
for k, v in pairs(parent.args) do
if args[k] == nil then args[k] = v end
end
end
return args
end
local function clampLevel(level)
level = tonumber(level)
if not level then return 1 end
if level < 1 then return 1 end
if level > 5 then return 5 end
return level
end
local function starIcon(level)
level = clampLevel(level)
return string.format("icon_star_rating_%d.png", level)
end
local function sanitizeId(s)
s = tostring(s or '')
s = mw.text.trim(s)
s = mw.ustring.gsub(s, "%s+", "_")
s = mw.ustring.gsub(s, "[%[%]{}()%c%p]", "_")
return s
end
function p.render(frame)
local args = getArgsCompat(frame)
-- 支持多种参数名(中文键使用方括号访问,避免语法错误)
local name = args.name or args['名称'] or args[1]
local level = clampLevel(args.level or args.lv or args['等级'] or args[2] or 1)
if not name or name == '' then
return "<span style='color:red'>[装备] 数据未找到:未指定</span>"
end
if not data[name] then
return string.format("<span style='color:red'>[装备] 数据未找到:%s</span>", mw.text.encode(name))
end
local eq = data[name]
local base = eq.base or {}
local rarity = base.rarity or "蓝"
local art = base.art or ""
local typ = base.type or "武器"
local valueType = base.value_type or ""
local lvlKey = tostring(level)
local lvl = eq[lvlKey] or {}
local value = lvl.value or ""
local desc = lvl.desc_global or ""
local modalId = "equip_modal_" .. sanitizeId(name) .. "_" .. tostring(level)
local root = mw.html.create('div')
-- 卡片
local card = root:tag('div')
:addClass('equip-card')
:attr('data-equip-id', modalId)
:attr('style', 'position: relative; display: inline-block; width:150px; height: 230px; cursor: pointer;')
card:tag('div')
:attr('style', 'position: absolute; top: 0px; left: 0px;')
:wikitext(string.format('[[File:bg_equipment_rarity_%s.png|150px|link=]]', rarity))
card:tag('div')
:attr('style', 'position: absolute; top: 43px; left: 13px;')
:wikitext(string.format('[[File:%s|124px|link=]]', art))
card:tag('div')
:attr('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')
card:tag('div')
:attr('style', 'position: absolute; bottom: 10px; left: 20px; ')
:wikitext(string.format('[[File:%s|link=]]', starIcon(level)))
card:tag('div')
:attr('style', 'position: absolute; top: 0px; left: 0px;')
:wikitext('[[File:equipment_顶层蒙版.png|150px|link=]]')
-- 弹窗
local modalContainer = root:tag('div')
:addClass('equip-modal-container')
:attr('id', modalId)
:attr('style', 'display: none;')
local modal = modalContainer:tag('div')
:addClass('equip-modal')
:attr('style', 'position: relative; display: inline-block; width:368px; height: 335px; background-color: #343434; border-radius: 9px')
modal:tag('div')
:attr('style', 'position: absolute; top: 0px; left: 0px;')
:wikitext(string.format('[[File:bg_collection_rarity_%s.png|link=]]', rarity))
modal:tag('div')
:attr('style', 'position: absolute; top: 40px; left: 128px;')
:wikitext(string.format('[[File:%s|124px|link=]]', art))
modal:tag('div')
:attr('style', 'position: absolute; top: 167px; left: 0px; width: 368px; height: 35px; background-color: rgba(0,0,0,0.5); border-radius: 0px 0px 8px 8px')
modal:tag('div')
:attr('style', 'position: absolute; top: 173px; left: 128px; ')
:wikitext(string.format('[[File:%s|link=]]', starIcon(level)))
modal:tag('div')
:attr('style', 'position: absolute; top: 205px; left: 5px; color: white')
:wikitext(string.format('[[File:icon_equip_%s_%s.png|25px|link=]]', typ, rarity))
modal:tag('div')
:attr('style', 'position: absolute; top: 209px; left: 33px; color: white')
:wikitext(name)
modal:tag('div')
:attr('style', 'position: absolute; top: 235px; left: 9px; width: 350px; height: 35px; background-color: rgba(255,255,255,0.3); border-radius: 2px')
modal:tag('div')
:attr('style', 'position: absolute; top: 242px; left: 14px; color: white')
:wikitext(valueType)
modal:tag('div')
:attr('style', 'position: absolute; top: 242px; right: 14px; color: white')
:wikitext(tostring(value))
modal:tag('div')
:attr('style', 'position: absolute; top: 277px; left: 10px; color: white')
:wikitext(desc)
modal:tag('div')
:addClass('equip-modal-close')
:attr('title', '关闭')
:attr('style', 'position: absolute; top: 6px; right: 10px; color: white; font-size: 20px; line-height: 20px; cursor: pointer;')
:wikitext('×')
return tostring(root)
end
return p