模块

配队/卡牌:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
律Rhyme移动页面模块:卡牌/配队模块:配队/卡牌,不留重定向
律Rhyme留言 | 贡献
无编辑摘要
第1行: 第1行:
-- Module:配队卡牌
local p = {}
local p = {}


-- 引用卡牌模块
-- 引用卡牌模块和显示模块
local cardModule = require('Module:卡牌')
local cardModule = require('Module:卡牌')
local cardDisplay = require('Module:卡牌/display')
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


-- 小尺寸卡牌渲染
-- 解析卡牌数据并渲染小尺寸卡牌
local function renderSmallCard(moduleName, cardName)
local function renderSmallCard(moduleName, cardName)
     if not moduleName or moduleName == "" or not cardName or cardName == "" then
     if not moduleName or moduleName == "" or not cardName or cardName == "" then
第11行: 第13行:
     end
     end
      
      
     -- 调用卡牌模块的main函数
     -- 加载卡牌数据模块
     local success, result = pcall(function()
     local success, dataModule = pcall(require, 'Module:卡牌/' .. moduleName)
        local frame = mw.getCurrentFrame()
    if not success or not dataModule then
         local childFrame = frame:newChild{
         return '<span style="color:red;">找不到模块: ' .. moduleName .. '</span>'
            args = {moduleName, cardName}
     end
        }
        return cardModule.main(childFrame)
     end)
      
      
     if success then
     local moduleInfo = dataModule.info or {}
        return result
    local cardDataWrapper = dataModule[cardName]
     else
     if not cardDataWrapper or not cardDataWrapper.base then
         return '<span style="color:red;">错误: ' .. tostring(result) .. '</span>'
         return '<span style="color:red;">找不到卡牌: ' .. cardName .. '</span>'
     end
     end
end
-- 主函数:显示战斗员的配队卡牌
function p.main(frame)
    local args = getArgs(frame)
    local combatantName = args[1] or mw.title.getCurrentTitle().text
      
      
     -- 查询战斗员页面
     -- 解析卡牌数据
     local query = {
     local cardInfo = cardModule.parseCardData(
        '[[分类:战斗员]]',
         cardDataWrapper.base,  
         '[[' .. combatantName .. ']]',
         moduleName,  
         '?自我意识技能',
         cardName,  
         '?起始卡牌_1',
         moduleInfo,  
         '?起始卡牌_2',  
         { isVariant = false }
         '?起始卡牌_3',
    )
        '?起始卡牌_4',
   
        '?独特卡牌_1',
    if not cardInfo then
        '?独特卡牌_2',
         return '<span style="color:red;">无法解析卡牌数据</span>'
         '?独特卡牌_3',
     end
        '?独特卡牌_4',
        limit = 1
     }
      
      
     local results = mw.smw.ask(query)
    -- 生成唯一ID
     local cardId = "team-card-" .. moduleName .. "-" .. cardName .. "-" .. os.time()
      
      
     if not results or #results == 0 then
    -- 计算词典显示
        return '<span style="color:red;">未找到战斗员数据</span>'
    local dictDisplay = ""
     if cardInfo.dictTokens and type(cardInfo.dictTokens) == 'table' then
        local parts = {}
        for _, t in ipairs(cardInfo.dictTokens) do
            local value = mw.text.trim(t.value or '')
            if value ~= '' then
                if t.type == 'template' then
                    table.insert(parts, '{{词典|' .. value .. '}}')
                elseif t.type == 'text' then
                    table.insert(parts, value)
                end
            end
        end
        if #parts > 0 then
            local color = cardInfo.dictColor == 'green' and '#b5f651' or '#f2ba02'
            dictDisplay = '<span style="color: ' .. color .. '">[' .. table.concat(parts, '/') .. ']</span><br>'
        end
     end
     end
      
      
     local data = results[1]
    -- 使用卡牌显示模块渲染小卡牌
    return cardDisplay.renderSmallCard(cardInfo, cardId, dictDisplay)
end
 
-- 主函数:显示战斗员的配队卡牌(直接传参版本)
function p.main(frame)
     local args = getArgs(frame)
     local html = {}
     local html = {}
      
      
第61行: 第74行:
      
      
     -- 标题
     -- 标题
     table.insert(html, '<h3 style="margin-bottom: 15px;">配队卡牌</h3>')
    local title = args.title or '配队卡牌'
     table.insert(html, '<h3 style="margin-bottom: 15px;">' .. title .. '</h3>')
      
      
     -- 卡牌容器
     -- 卡牌容器
第68行: 第82行:
     -- 按顺序处理卡牌
     -- 按顺序处理卡牌
     local cardOrder = {
     local cardOrder = {
         {field = '自我意识技能', label = '自我意识技能'},
         {param = '自我意识技能', label = '自我意识技能'},
         {field = '起始卡牌_1', label = '起始卡牌1'},
         {param = '起始卡牌1', label = '起始卡牌1'},
         {field = '起始卡牌_2', label = '起始卡牌2'},
         {param = '起始卡牌2', label = '起始卡牌2'},
         {field = '起始卡牌_3', label = '起始卡牌3'},
         {param = '起始卡牌3', label = '起始卡牌3'},
         {field = '起始卡牌_4', label = '起始卡牌4'},
         {param = '起始卡牌4', label = '起始卡牌4'},
         {field = '独特卡牌_1', label = '独特卡牌1'},
         {param = '独特卡牌1', label = '独特卡牌1'},
         {field = '独特卡牌_2', label = '独特卡牌2'},
         {param = '独特卡牌2', label = '独特卡牌2'},
         {field = '独特卡牌_3', label = '独特卡牌3'},
         {param = '独特卡牌3', label = '独特卡牌3'},
         {field = '独特卡牌_4', label = '独特卡牌4'}
         {param = '独特卡牌4', label = '独特卡牌4'}
     }
     }
      
      
     for _, cardInfo in ipairs(cardOrder) do
     for _, cardInfo in ipairs(cardOrder) do
         local cardValue = data[cardInfo.field]
         local cardValue = args[cardInfo.param]
         if cardValue and cardValue ~= "" then
         if cardValue and cardValue ~= "" then
             -- 解析卡牌值(格式:模块名|卡牌名)
             -- 解析卡牌值(格式:模块名|卡牌名 或 模块名/卡牌名)
             local moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
             local moduleName, cardName
           
            -- 尝试用 | 分割
            moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
           
            -- 如果失败,尝试用 / 分割
            if not moduleName then
                moduleName, cardName = string.match(tostring(cardValue), "^([^/]+)/(.+)$")
            end
           
            -- 如果还是失败,尝试用空格分割
            if not moduleName then
                moduleName, cardName = string.match(tostring(cardValue), "^(%S+)%s+(.+)$")
            end
              
              
             if moduleName and cardName then
             if moduleName and cardName then
                 -- 添加卡牌标签
                 -- 添加卡牌槽位
                 table.insert(html, '<div class="card-slot" style="display:flex;flex-direction:column;align-items:center;">')
                 table.insert(html, '<div class="card-slot" style="display:flex;flex-direction:column;align-items:center;">')
                 table.insert(html, '<div style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                  
                -- 添加标签(可选)
                if args.showLabels ~= "false" then
                    table.insert(html, '<div style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                end
                  
                  
                 -- 渲染小尺寸卡牌
                 -- 渲染小尺寸卡牌
                 table.insert(html, renderSmallCard(moduleName, cardName))
                 table.insert(html, renderSmallCard(moduleName, cardName))
                  
                  
                table.insert(html, '</div>')
            else
                -- 如果无法解析,显示错误
                table.insert(html, '<div class="card-slot" style="display:flex;flex-direction:column;align-items:center;">')
                if args.showLabels ~= "false" then
                    table.insert(html, '<div style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                end
                table.insert(html, '<span style="color:red;">无法解析: ' .. tostring(cardValue) .. '</span>')
                 table.insert(html, '</div>')
                 table.insert(html, '</div>')
             end
             end
第104行: 第143行:
end
end


-- 批量显示多个战斗员的配队
-- 简化版本:直接显示卡牌列表
function p.batch(frame)
function p.show(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)
     local html = {}
     local html = {}
      
      
     -- 查询所有战斗员
     table.insert(html, '<div style="display:flex;gap:15px;flex-wrap:wrap;align-items:flex-start;">')
    local query = {
        '[[分类:战斗员]]',
        '?自我意识技能',
        '?起始卡牌_1',
        '?起始卡牌_2',
        '?起始卡牌_3',
        '?起始卡牌_4',
        '?独特卡牌_1',
        '?独特卡牌_2',
        '?独特卡牌_3',
        '?独特卡牌_4',
        limit = args.limit or 50
    }
      
      
     local results = mw.smw.ask(query)
    -- 遍历所有参数
      
     local i = 1
    if not results or #results == 0 then
     while args[i] do
         return '<span style="color:red;">未找到战斗员数据</span>'
        local cardValue = args[i]
    end
         if cardValue and cardValue ~= "" then
   
            -- 解析卡牌值
    table.insert(html, '<div class="all-combatant-cards">')
            local moduleName, cardName
   
           
    for _, combatant in ipairs(results) do
            -- 尝试用 | 分割
        local combatantName = combatant[1] and string.match(combatant[1], "([^#]+)") or "未知战斗员"
            moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
       
           
        -- 检查是否有任何卡牌数据
            -- 如果失败,尝试用 / 分割
        local hasCards = false
            if not moduleName then
        local fields = {'自我意识技能', '起始卡牌_1', '起始卡牌_2', '起始卡牌_3', '起始卡牌_4',
                moduleName, cardName = string.match(tostring(cardValue), "^([^/]+)/(.+)$")
                      '独特卡牌_1', '独特卡牌_2', '独特卡牌_3', '独特卡牌_4'}
       
        for _, field in ipairs(fields) do
            if combatant[field] and combatant[field] ~= "" then
                hasCards = true
                break
             end
             end
        end
       
        if hasCards then
            table.insert(html, '<div style="margin-bottom:30px;border:1px solid #ddd;padding:15px;border-radius:5px;">')
            table.insert(html, '<h3>' .. combatantName .. '</h3>')
              
              
             -- 调用main函数显示该战斗员的卡牌
             if moduleName and cardName then
            local childFrame = mw.getCurrentFrame():newChild{
                 table.insert(html, renderSmallCard(moduleName, cardName))
                 args = {combatantName}
             end
            }
            table.insert(html, p.main(childFrame))
              
            table.insert(html, '</div>')
         end
         end
        i = i + 1
     end
     end
      
      
第166行: 第178行:
end
end


-- 显示指定战斗员列表的配队
-- 从SMW属性读取(需要在页面模板中使用)
function p.list(frame)
function p.fromSMW(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)
     local html = {}
     local html = {}
      
      
     table.insert(html, '<div class="combatant-cards-list">')
     -- 获取当前页面
    local title = mw.title.getCurrentTitle()
    local pageName = args[1] or title.text
      
      
     -- 遍历所有参数,每个参数是一个战斗员名称
     -- 尝试从frame获取SMW数据(在模板调用时)
     for i = 1, 20 do -- 最多支持20个战斗员
     local cardData = {
         local combatantName = args[i]
        skill = args['自我意识技能'],
         if combatantName and combatantName ~= "" then
         start1 = args['起始卡牌_1'],
            local childFrame = mw.getCurrentFrame():newChild{
         start2 = args['起始卡牌_2'],
                args = {combatantName}
        start3 = args['起始卡牌_3'],
            }
        start4 = args['起始卡牌_4'],
           
        unique1 = args['独特卡牌_1'],
            table.insert(html, '<div style="margin-bottom:20px;">')
        unique2 = args['独特卡牌_2'],
            table.insert(html, '<h4>' .. combatantName .. '</h4>')
        unique3 = args['独特卡牌_3'],
            table.insert(html, p.main(childFrame))
         unique4 = args['独特卡牌_4']
            table.insert(html, '</div>')
     }
         end
     end
      
      
     table.insert(html, '</div>')
     -- 构建参数
    local newArgs = {
        title = args.title or (pageName .. ' - 配队卡牌'),
        showLabels = args.showLabels,
        ['自我意识技能'] = cardData.skill,
        ['起始卡牌1'] = cardData.start1,
        ['起始卡牌2'] = cardData.start2,
        ['起始卡牌3'] = cardData.start3,
        ['起始卡牌4'] = cardData.start4,
        ['独特卡牌1'] = cardData.unique1,
        ['独特卡牌2'] = cardData.unique2,
        ['独特卡牌3'] = cardData.unique3,
        ['独特卡牌4'] = cardData.unique4
    }
      
      
     return table.concat(html, '')
    -- 创建新frame并调用main
    local newFrame = mw.getCurrentFrame():newChild{ args = newArgs }
     return p.main(newFrame)
end
end


return p
return p

2025年10月18日 (六) 22:06的版本

此模块的文档可以在模块:配队/卡牌/doc创建

-- Module:配队卡牌
local p = {}

-- 引用卡牌模块和显示模块
local cardModule = require('Module:卡牌')
local cardDisplay = require('Module:卡牌/display')
local getArgs = require('Module:Arguments').getArgs

-- 解析卡牌数据并渲染小尺寸卡牌
local function renderSmallCard(moduleName, cardName)
    if not moduleName or moduleName == "" or not cardName or cardName == "" then
        return ""
    end
    
    -- 加载卡牌数据模块
    local success, dataModule = pcall(require, 'Module:卡牌/' .. moduleName)
    if not success or not dataModule then
        return '<span style="color:red;">找不到模块: ' .. moduleName .. '</span>'
    end
    
    local moduleInfo = dataModule.info or {}
    local cardDataWrapper = dataModule[cardName]
    if not cardDataWrapper or not cardDataWrapper.base then
        return '<span style="color:red;">找不到卡牌: ' .. cardName .. '</span>'
    end
    
    -- 解析卡牌数据
    local cardInfo = cardModule.parseCardData(
        cardDataWrapper.base, 
        moduleName, 
        cardName, 
        moduleInfo, 
        { isVariant = false }
    )
    
    if not cardInfo then
        return '<span style="color:red;">无法解析卡牌数据</span>'
    end
    
    -- 生成唯一ID
    local cardId = "team-card-" .. moduleName .. "-" .. cardName .. "-" .. os.time()
    
    -- 计算词典显示
    local dictDisplay = ""
    if cardInfo.dictTokens and type(cardInfo.dictTokens) == 'table' then
        local parts = {}
        for _, t in ipairs(cardInfo.dictTokens) do
            local value = mw.text.trim(t.value or '')
            if value ~= '' then
                if t.type == 'template' then
                    table.insert(parts, '{{词典|' .. value .. '}}')
                elseif t.type == 'text' then
                    table.insert(parts, value)
                end
            end
        end
        if #parts > 0 then
            local color = cardInfo.dictColor == 'green' and '#b5f651' or '#f2ba02'
            dictDisplay = '<span style="color: ' .. color .. '">[' .. table.concat(parts, '/') .. ']</span><br>'
        end
    end
    
    -- 使用卡牌显示模块渲染小卡牌
    return cardDisplay.renderSmallCard(cardInfo, cardId, dictDisplay)
end

-- 主函数:显示战斗员的配队卡牌(直接传参版本)
function p.main(frame)
    local args = getArgs(frame)
    local html = {}
    
    -- 开始容器
    table.insert(html, '<div class="combatant-cards" style="margin: 20px 0;">')
    
    -- 标题
    local title = args.title or '配队卡牌'
    table.insert(html, '<h3 style="margin-bottom: 15px;">' .. title .. '</h3>')
    
    -- 卡牌容器
    table.insert(html, '<div style="display:flex;gap:15px;flex-wrap:wrap;align-items:flex-start;">')
    
    -- 按顺序处理卡牌
    local cardOrder = {
        {param = '自我意识技能', label = '自我意识技能'},
        {param = '起始卡牌1', label = '起始卡牌1'},
        {param = '起始卡牌2', label = '起始卡牌2'},
        {param = '起始卡牌3', label = '起始卡牌3'},
        {param = '起始卡牌4', label = '起始卡牌4'},
        {param = '独特卡牌1', label = '独特卡牌1'},
        {param = '独特卡牌2', label = '独特卡牌2'},
        {param = '独特卡牌3', label = '独特卡牌3'},
        {param = '独特卡牌4', label = '独特卡牌4'}
    }
    
    for _, cardInfo in ipairs(cardOrder) do
        local cardValue = args[cardInfo.param]
        if cardValue and cardValue ~= "" then
            -- 解析卡牌值(格式:模块名|卡牌名 或 模块名/卡牌名)
            local moduleName, cardName
            
            -- 尝试用 | 分割
            moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
            
            -- 如果失败,尝试用 / 分割
            if not moduleName then
                moduleName, cardName = string.match(tostring(cardValue), "^([^/]+)/(.+)$")
            end
            
            -- 如果还是失败,尝试用空格分割
            if not moduleName then
                moduleName, cardName = string.match(tostring(cardValue), "^(%S+)%s+(.+)$")
            end
            
            if moduleName and cardName then
                -- 添加卡牌槽位
                table.insert(html, '<div class="card-slot" style="display:flex;flex-direction:column;align-items:center;">')
                
                -- 添加标签(可选)
                if args.showLabels ~= "false" then
                    table.insert(html, '<div style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                end
                
                -- 渲染小尺寸卡牌
                table.insert(html, renderSmallCard(moduleName, cardName))
                
                table.insert(html, '</div>')
            else
                -- 如果无法解析,显示错误
                table.insert(html, '<div class="card-slot" style="display:flex;flex-direction:column;align-items:center;">')
                if args.showLabels ~= "false" then
                    table.insert(html, '<div style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                end
                table.insert(html, '<span style="color:red;">无法解析: ' .. tostring(cardValue) .. '</span>')
                table.insert(html, '</div>')
            end
        end
    end
    
    table.insert(html, '</div>') -- 结束卡牌容器
    table.insert(html, '</div>') -- 结束主容器
    
    return table.concat(html, '')
end

-- 简化版本:直接显示卡牌列表
function p.show(frame)
    local args = getArgs(frame)
    local html = {}
    
    table.insert(html, '<div style="display:flex;gap:15px;flex-wrap:wrap;align-items:flex-start;">')
    
    -- 遍历所有参数
    local i = 1
    while args[i] do
        local cardValue = args[i]
        if cardValue and cardValue ~= "" then
            -- 解析卡牌值
            local moduleName, cardName
            
            -- 尝试用 | 分割
            moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
            
            -- 如果失败,尝试用 / 分割
            if not moduleName then
                moduleName, cardName = string.match(tostring(cardValue), "^([^/]+)/(.+)$")
            end
            
            if moduleName and cardName then
                table.insert(html, renderSmallCard(moduleName, cardName))
            end
        end
        i = i + 1
    end
    
    table.insert(html, '</div>')
    
    return table.concat(html, '')
end

-- 从SMW属性读取(需要在页面模板中使用)
function p.fromSMW(frame)
    local args = getArgs(frame)
    local html = {}
    
    -- 获取当前页面
    local title = mw.title.getCurrentTitle()
    local pageName = args[1] or title.text
    
    -- 尝试从frame获取SMW数据(在模板调用时)
    local cardData = {
        skill = args['自我意识技能'],
        start1 = args['起始卡牌_1'],
        start2 = args['起始卡牌_2'],
        start3 = args['起始卡牌_3'],
        start4 = args['起始卡牌_4'],
        unique1 = args['独特卡牌_1'],
        unique2 = args['独特卡牌_2'],
        unique3 = args['独特卡牌_3'],
        unique4 = args['独特卡牌_4']
    }
    
    -- 构建参数
    local newArgs = {
        title = args.title or (pageName .. ' - 配队卡牌'),
        showLabels = args.showLabels,
        ['自我意识技能'] = cardData.skill,
        ['起始卡牌1'] = cardData.start1,
        ['起始卡牌2'] = cardData.start2,
        ['起始卡牌3'] = cardData.start3,
        ['起始卡牌4'] = cardData.start4,
        ['独特卡牌1'] = cardData.unique1,
        ['独特卡牌2'] = cardData.unique2,
        ['独特卡牌3'] = cardData.unique3,
        ['独特卡牌4'] = cardData.unique4
    }
    
    -- 创建新frame并调用main
    local newFrame = mw.getCurrentFrame():newChild{ args = newArgs }
    return p.main(newFrame)
end

return p