模块:配队/卡牌
来自卡厄思梦境WIKI
此模块的文档可以在模块:配队/卡牌/doc创建
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- 卡牌模块
local cardModule = require('Module:卡牌')
-- 安全的错误输出
local function err(msg)
return '<span style="color: red;">' .. mw.text.encode(msg) .. '</span>'
end
-- 从页面内容中提取属性值
local function getPropertyFromPage(pageName, propertyName)
local title = mw.title.new(pageName)
if not title or not title.exists then
return nil
end
local content = title:getContent()
if not content then
return nil
end
-- 匹配 |属性名=值 的格式
-- 支持多种格式:|属性名=值、| 属性名 = 值 等
local pattern = '%|%s*' .. propertyName:gsub('[%-%[%]%(%)%.]', '%%%1') .. '%s*=%s*([^\n|]*)'
local value = content:match(pattern)
if value then
value = mw.text.trim(value)
if value ~= '' then
return value
end
end
return nil
end
-- 尝试多种页面名称格式
local function findPageName(fighterName)
local candidates = {
fighterName, -- 直接使用输入
'战斗员:' .. fighterName, -- 战斗员命名空间
'Fighter:' .. fighterName, -- 英文命名空间
}
for _, pageName in ipairs(candidates) do
local title = mw.title.new(pageName)
if title and title.exists then
return pageName, title
end
end
return nil, nil
end
-- 主函数
function p.main(frame)
local args = getArgs(frame, { removeBlank = true })
local fighterName = args[1] or args['战斗员'] or args['name']
if not fighterName or fighterName == '' then
return err('错误: 未指定战斗员名称')
end
-- 查找页面
local pageName, title = findPageName(fighterName)
if not pageName then
return err('错误: 找不到战斗员 "' .. fighterName .. '"')
end
-- 定义卡牌属性顺序
local cardProperties = {
'自我意识技能',
'起始卡牌_1',
'起始卡牌_2',
'起始卡牌_3',
'起始卡牌_4',
'独特卡牌_1',
'独特卡牌_2',
'独特卡牌_3',
'独特卡牌_4'
}
-- 收集卡牌调用参数
local batchArgs = {}
local argIndex = 1
for _, propName in ipairs(cardProperties) do
local cardValue = getPropertyFromPage(pageName, propName)
if cardValue and cardValue ~= '' then
-- 解析卡牌值(格式:模块名|卡牌名)
local moduleName, cardName = cardValue:match('^([^|]+)|(.+)$')
if moduleName and cardName then
moduleName = mw.text.trim(moduleName)
cardName = mw.text.trim(cardName)
-- 添加到批量参数(每组5个参数:模块名, 卡牌名, 变体类型, 变体参数, 变体索引)
batchArgs[argIndex] = moduleName
batchArgs[argIndex + 1] = cardName
batchArgs[argIndex + 2] = nil -- 变体类型
batchArgs[argIndex + 3] = nil -- 变体参数
batchArgs[argIndex + 4] = nil -- 变体索引
argIndex = argIndex + 5
end
end
end
-- 如果没有找到任何卡牌
if #batchArgs == 0 then
return err('错误: 战斗员 "' .. fighterName .. '" 没有配置卡牌数据')
end
-- 调用批量渲染
local batchFrame = {
args = batchArgs,
getParent = function() return frame end
}
return cardModule.batch(batchFrame)
end
-- 调试函数:显示页面信息
function p.debug(frame)
local args = getArgs(frame, { removeBlank = true })
local fighterName = args[1] or args['战斗员'] or args['name']
if not fighterName or fighterName == '' then
return err('错误: 未指定战斗员名称')
end
local html = {}
table.insert(html, '<div style="border:1px solid #ccc;padding:10px;margin:10px 0;background:#f9f9f9;">')
table.insert(html, '<h3>调试信息:' .. mw.text.encode(fighterName) .. '</h3>')
-- 测试多种页面格式
local candidates = {
fighterName,
'战斗员:' .. fighterName,
'Fighter:' .. fighterName,
}
table.insert(html, '<h4>页面存在性检查:</h4><ul>')
for _, pageName in ipairs(candidates) do
local title = mw.title.new(pageName)
local exists = title and title.exists or false
local color = exists and 'green' or 'red'
table.insert(html, '<li><span style="color:' .. color .. ';font-weight:bold;">' .. mw.text.encode(pageName) .. ' - ' .. (exists and '✓ 存在' or '✗ 不存在') .. '</span></li>')
if exists then
-- 尝试读取属性
table.insert(html, '<ul style="margin-top:5px;">')
local cardProperties = {
'自我意识技能',
'起始卡牌_1',
'起始卡牌_2',
'起始卡牌_3',
'起始卡牌_4',
'独特卡牌_1',
'独特卡牌_2',
'独特卡牌_3',
'独特卡牌_4'
}
for _, propName in ipairs(cardProperties) do
local value = getPropertyFromPage(pageName, propName)
local displayValue = value or '<span style="color:#999;">(空)</span>'
if value then
displayValue = '<span style="color:green;"><code>' .. mw.text.encode(value) .. '</code></span>'
end
table.insert(html, '<li>' .. mw.text.encode(propName) .. ': ' .. displayValue .. '</li>')
end
table.insert(html, '</ul>')
-- 显示部分页面内容(前500字符)
local content = title:getContent()
if content then
local preview = mw.ustring.sub(content, 1, 500)
table.insert(html, '<details style="margin-top:10px;"><summary style="cursor:pointer;color:#0645ad;">查看页面内容预览</summary>')
table.insert(html, '<pre style="background:#fff;border:1px solid #ddd;padding:10px;overflow:auto;max-height:300px;">')
table.insert(html, mw.text.encode(preview))
if mw.ustring.len(content) > 500 then
table.insert(html, '\n\n... (还有 ' .. (mw.ustring.len(content) - 500) .. ' 个字符)')
end
table.insert(html, '</pre></details>')
end
end
end
table.insert(html, '</ul>')
table.insert(html, '</div>')
return table.concat(html, '')
end
return p