模块

模块:配队/卡牌

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月18日 (六) 22:14的版本

此模块的文档可以在模块:配队/卡牌/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

-- 安全获取 SMW 属性
local function getProperty(pageName, propertyName)
    local result = mw.smw.ask({
        '[[' .. pageName .. ']]',
        '?' .. propertyName,
        limit = 1
    })
    
    if result and result[1] and result[1][propertyName] then
        return result[1][propertyName]
    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 = getProperty(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 .. '" 没有配置卡牌数据(页面: ' .. pageName .. ')')
    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;">')
    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 .. ';">' .. mw.text.encode(pageName) .. ' - ' .. (exists and '✓ 存在' or '✗ 不存在') .. '</span></li>')
        
        if exists then
            -- 尝试读取属性
            table.insert(html, '<ul>')
            local cardProperties = {
                '自我意识技能',
                '起始卡牌_1',
                '起始卡牌_2',
                '起始卡牌_3',
                '起始卡牌_4',
                '独特卡牌_1',
                '独特卡牌_2',
                '独特卡牌_3',
                '独特卡牌_4'
            }
            
            for _, propName in ipairs(cardProperties) do
                local value = getProperty(pageName, propName)
                table.insert(html, '<li>' .. mw.text.encode(propName) .. ': <code>' .. mw.text.encode(tostring(value or '(空)')) .. '</code></li>')
            end
            table.insert(html, '</ul>')
        end
    end
    table.insert(html, '</ul>')
    
    table.insert(html, '</div>')
    return table.concat(html, '')
end

return p