模块

模块:卡牌

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年9月27日 (六) 16:48的版本

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

local p = {}
local mw = require('mw')

-- 颜色映射表
local colorMap = {
    ["白"] = {
        bgColor = "rgba(249, 249, 249, 0.5)",
        textColor = "white"
    },
    ["蓝"] = {
        bgColor = "rgba(115, 236, 254, 0.5)",
        textColor = "#7de5ff"
    },
    ["橙"] = {
        bgColor = "rgba(254, 199, 109, 0.5)",
        textColor = "#ffee75"
    },
    ["彩"] = {
        bgColor = "rgba(201, 88, 241, 0.5)",
        textColor = "#eba2fc"
    }
}

-- 获取卡牌HTML
local function getCardHTML(frame, cardName, cardData)
    if not cardData then
        return "找不到卡牌数据: " .. (cardName or "未指定")
    end
    
    -- 使用指定的数据条目
    local data = cardData
    local color = data["稀有度"] or "白"
    local colorStyle = colorMap[color] or colorMap["白"]
    local ap = data["AP"] or ""
    local cardType = data["类型"] or ""
    local description = data["描述"] or ""
    local attribute = data["属性"] or "虚无"
    local cardDeck = data["卡组"] or "起始卡牌"
    
    -- 根据卡组类型决定边框样式
    local borderStyle = "normal"
    if cardDeck ~= "起始卡牌" then
        borderStyle = "unique"
    end
    
    -- 解析描述中的模板
    if description ~= "" then
        -- 使用frame:preprocess来解析描述中的模板
        description = frame:preprocess(description)
    end
    
    -- 构建卡牌HTML
    local html = '<div style="position: relative; width: 168px; height: 230px; overflow: hidden;">'
    .. '<div style="position: absolute; top: 1px; left: 12px;">[[File:start_1041_01.png|151px|link=]]</div>'
    .. '<div style="position: absolute; top: 1px; left: 12px;">[[File:card_黑色蒙版.png|151px|link=]]</div>'
    .. '<div style="position: absolute; top: 20px; left: 10px; width: 148px; height: 10px; background-color: ' .. colorStyle.bgColor .. ';"></div>'
    .. '<div style="position: absolute; left: 23px; top: 4px; color: white; font-weight: bold; text-shadow: 0 0 10px #4a90e2,0 0 20px #4a90e2, 0 0 30px #4a90e2, 0 0 40px #4a90e2; font-size: 32px;">' .. ap .. '</div>'
    .. '<div style="position: absolute; left: 25px; top: 34px; color: white; font-weight: bold; text-shadow: 0 0 10px #4a90e2,0 0 20px #4a90e2, 0 0 30px #4a90e2, 0 0 40px #4a90e2;">—</div>'
    .. '<div style="position: absolute; left: 50px; top: 13px; color: ' .. colorStyle.textColor .. '; font-size:16px">' .. cardName .. '</div>'
    .. '<div style="position: absolute; left: 48px; top: 30px; ">[[File:icon_card_' .. cardType .. '.png|18px|link=]]</div>'
    .. '<div style="position: absolute; left: 68px; top: 33px; color: white; font-size:14px">' .. cardType .. '</div>'
    .. '<div style="position: absolute; top: 0px; left: 1px;">[[File:card_属性边框_' .. attribute .. '_' .. borderStyle .. '.png|160px|link=]]</div>'
    .. '<div style="position: absolute; top: 10px; left: 0px;">[[File:card_稀有度_' .. color .. '.png|22px|link=]]</div>'
    .. '<div style="position: absolute; top: 2px; right: 4px;">[[File:card_稀有度_边框_' .. color .. '.png|11px|link=]]</div>'
    .. '<div style="position: absolute; bottom: 7px; left: 15px; width: 144px; height: 100px; font-size: 12px; color: white; line-height: 13px; display: flex; justify-content: center; align-items: center; text-align: center;">'
    .. '<div>' .. description .. '</div>'
    .. '</div>'
    .. '</div>'
    
    return html
end

-- 合并卡牌数据
local function mergeCardData(baseData, overrideData)
    local result = {}
    for k, v in pairs(baseData) do
        result[k] = v
    end
    for k, v in pairs(overrideData) do
        result[k] = v
    end
    return result
end

-- 主函数,用于处理模板调用
function p.main(frame)
    local args = frame.args
    local characterName = args[1] or ""
    local cardName = args[2] or ""
    local deckFilter = args[3] or ""
    local cardIndex = tonumber(args[4]) or 0
    
    -- 动态加载战斗员模块
    local characterModule = nil
    local success = pcall(function() 
        characterModule = require("模块:卡牌/" .. characterName)
    end)
    
    if not success or not characterModule then
        return "找不到战斗员卡牌数据模块: 模块:卡牌/" .. characterName
    end
    
    -- 获取卡牌数据
    local cards = characterModule.card or {}
    
    -- 如果没有指定卡牌名,显示所有起始卡牌和第一张独特卡牌
    if cardName == "" then
        local html = ""
        
        -- 显示起始卡牌
        for name, cardArray in pairs(cards) do
            for _, card in ipairs(cardArray) do
                if card["卡组"] == "起始卡牌" then
                    html = html .. getCardHTML(frame, name, card) .. "<br><br>"
                end
            end
        end
        
        -- 显示第一张独特卡牌
        for name, cardArray in pairs(cards) do
            for _, card in ipairs(cardArray) do
                if card["卡组"] == "独特卡牌" then
                    html = html .. getCardHTML(frame, name, card)
                    return html
                end
            end
        end
        
        return html
    end
    
    local cardData = cards[cardName]
    if not cardData then
        return "找不到卡牌: " .. cardName
    end
    
    -- 获取基础卡牌数据(第一个条目)
    local baseCard = cardData[1]
    
    -- 如果指定了卡组筛选
    if deckFilter ~= "" then
        local filteredCards = {}
        for _, card in ipairs(cardData) do
            if card["卡组"] == deckFilter then
                table.insert(filteredCards, card)
            end
        end
        
        -- 如果没有找到符合条件的卡牌
        if #filteredCards == 0 then
            return "找不到卡组 '" .. deckFilter .. "' 的卡牌: " .. cardName
        end
        
        -- 如果指定了索引
        if cardIndex > 0 and cardIndex <= #filteredCards then
            local mergedCard = mergeCardData(baseCard, filteredCards[cardIndex])
            return getCardHTML(frame, cardName, mergedCard)
        else
            -- 显示该卡组的所有卡牌
            local html = ""
            for i, card in ipairs(filteredCards) do
                local mergedCard = mergeCardData(baseCard, card)
                html = html .. getCardHTML(frame, cardName, mergedCard)
                if i < #filteredCards then
                    html = html .. "<br><br>"
                end
            end
            return html
        end
    else
        -- 如果没有指定卡组,就返回基础卡牌
        return getCardHTML(frame, cardName, baseCard)
    end
end

-- 为每个战斗员创建一个直接调用方法
setmetatable(p, {
    __index = function(t, characterName)
        return function(frame)
            local args = frame.args
            local cardName = args[1] or ""
            local deckFilter = args[2] or ""
            local cardIndex = tonumber(args[3]) or 0
            
            -- 创建新的参数表来传递给 main 函数
            local newArgs = {
                [1] = characterName,
                [2] = cardName,
                [3] = deckFilter,
                [4] = cardIndex
            }
            
            -- 将其他参数也传递过去
            for k, v in pairs(args) do
                if not newArgs[k] then
                    newArgs[k] = v
                end
            end
            
            -- 调用主函数
            local newFrame = {
                args = newArgs,
                preprocess = frame.preprocess
            }
            
            return p.main(newFrame)
        end
    end
})

return p