模块

模块:配队/卡牌

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月18日 (六) 22:03的版本 (律Rhyme移动页面模块:卡牌/配队模块:配队/卡牌,不留重定向)

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

local p = {}

-- 引用卡牌模块
local cardModule = require('Module:卡牌')
local getArgs = require('Module:Arguments').getArgs

-- 小尺寸卡牌渲染
local function renderSmallCard(moduleName, cardName)
    if not moduleName or moduleName == "" or not cardName or cardName == "" then
        return ""
    end
    
    -- 调用卡牌模块的main函数
    local success, result = pcall(function()
        local frame = mw.getCurrentFrame()
        local childFrame = frame:newChild{
            args = {moduleName, cardName}
        }
        return cardModule.main(childFrame)
    end)
    
    if success then
        return result
    else
        return '<span style="color:red;">错误: ' .. tostring(result) .. '</span>'
    end
end

-- 主函数:显示战斗员的配队卡牌
function p.main(frame)
    local args = getArgs(frame)
    local combatantName = args[1] or mw.title.getCurrentTitle().text
    
    -- 查询战斗员页面
    local query = {
        '[[分类:战斗员]]',
        '[[' .. combatantName .. ']]',
        '?自我意识技能',
        '?起始卡牌_1',
        '?起始卡牌_2', 
        '?起始卡牌_3',
        '?起始卡牌_4',
        '?独特卡牌_1',
        '?独特卡牌_2',
        '?独特卡牌_3',
        '?独特卡牌_4',
        limit = 1
    }
    
    local results = mw.smw.ask(query)
    
    if not results or #results == 0 then
        return '<span style="color:red;">未找到战斗员数据</span>'
    end
    
    local data = results[1]
    local html = {}
    
    -- 开始容器
    table.insert(html, '<div class="combatant-cards" style="margin: 20px 0;">')
    
    -- 标题
    table.insert(html, '<h3 style="margin-bottom: 15px;">配队卡牌</h3>')
    
    -- 卡牌容器
    table.insert(html, '<div style="display:flex;gap:15px;flex-wrap:wrap;align-items:flex-start;">')
    
    -- 按顺序处理卡牌
    local cardOrder = {
        {field = '自我意识技能', label = '自我意识技能'},
        {field = '起始卡牌_1', label = '起始卡牌1'},
        {field = '起始卡牌_2', label = '起始卡牌2'},
        {field = '起始卡牌_3', label = '起始卡牌3'},
        {field = '起始卡牌_4', label = '起始卡牌4'},
        {field = '独特卡牌_1', label = '独特卡牌1'},
        {field = '独特卡牌_2', label = '独特卡牌2'},
        {field = '独特卡牌_3', label = '独特卡牌3'},
        {field = '独特卡牌_4', label = '独特卡牌4'}
    }
    
    for _, cardInfo in ipairs(cardOrder) do
        local cardValue = data[cardInfo.field]
        if cardValue and cardValue ~= "" then
            -- 解析卡牌值(格式:模块名|卡牌名)
            local moduleName, cardName = string.match(tostring(cardValue), "^([^|]+)|(.+)$")
            
            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 style="font-size:12px;color:#666;margin-bottom:5px;">' .. cardInfo.label .. '</div>')
                
                -- 渲染小尺寸卡牌
                table.insert(html, renderSmallCard(moduleName, cardName))
                
                table.insert(html, '</div>')
            end
        end
    end
    
    table.insert(html, '</div>') -- 结束卡牌容器
    table.insert(html, '</div>') -- 结束主容器
    
    return table.concat(html, '')
end

-- 批量显示多个战斗员的配队
function p.batch(frame)
    local args = getArgs(frame)
    local html = {}
    
    -- 查询所有战斗员
    local query = {
        '[[分类:战斗员]]',
        '?自我意识技能',
        '?起始卡牌_1',
        '?起始卡牌_2',
        '?起始卡牌_3', 
        '?起始卡牌_4',
        '?独特卡牌_1',
        '?独特卡牌_2',
        '?独特卡牌_3',
        '?独特卡牌_4',
        limit = args.limit or 50
    }
    
    local results = mw.smw.ask(query)
    
    if not results or #results == 0 then
        return '<span style="color:red;">未找到战斗员数据</span>'
    end
    
    table.insert(html, '<div class="all-combatant-cards">')
    
    for _, combatant in ipairs(results) do
        local combatantName = combatant[1] and string.match(combatant[1], "([^#]+)") or "未知战斗员"
        
        -- 检查是否有任何卡牌数据
        local hasCards = false
        local fields = {'自我意识技能', '起始卡牌_1', '起始卡牌_2', '起始卡牌_3', '起始卡牌_4', 
                       '独特卡牌_1', '独特卡牌_2', '独特卡牌_3', '独特卡牌_4'}
        
        for _, field in ipairs(fields) do
            if combatant[field] and combatant[field] ~= "" then
                hasCards = true
                break
            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函数显示该战斗员的卡牌
            local childFrame = mw.getCurrentFrame():newChild{
                args = {combatantName}
            }
            table.insert(html, p.main(childFrame))
            
            table.insert(html, '</div>')
        end
    end
    
    table.insert(html, '</div>')
    
    return table.concat(html, '')
end

-- 显示指定战斗员列表的配队
function p.list(frame)
    local args = getArgs(frame)
    local html = {}
    
    table.insert(html, '<div class="combatant-cards-list">')
    
    -- 遍历所有参数,每个参数是一个战斗员名称
    for i = 1, 20 do -- 最多支持20个战斗员
        local combatantName = args[i]
        if combatantName and combatantName ~= "" then
            local childFrame = mw.getCurrentFrame():newChild{
                args = {combatantName}
            }
            
            table.insert(html, '<div style="margin-bottom:20px;">')
            table.insert(html, '<h4>' .. combatantName .. '</h4>')
            table.insert(html, p.main(childFrame))
            table.insert(html, '</div>')
        end
    end
    
    table.insert(html, '</div>')
    
    return table.concat(html, '')
end

return p