模块

模块:战斗员图鉴

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年9月24日 (三) 20:46的版本 (创建页面,内容为“local p = {} -- 根据稀有度获取对应的CSS类 local function getRarityClass(rarity) return "rarity-" .. (rarity or "1") end -- 根据职业名称获取图标文件名 local function getProfessionIcon(profession) if not profession or profession == "" then return "" end return "icon_职业_" .. profession .. ".png" end -- 根据属性名称获取图标文件名 local function getAttributeIcon(attribute) if not attribute or at…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块:战斗员图鉴/doc创建

local p = {}

-- 根据稀有度获取对应的CSS类
local function getRarityClass(rarity)
    return "rarity-" .. (rarity or "1")
end

-- 根据职业名称获取图标文件名
local function getProfessionIcon(profession)
    if not profession or profession == "" then
        return ""
    end
    return "icon_职业_" .. profession .. ".png"
end

-- 根据属性名称获取图标文件名
local function getAttributeIcon(attribute)
    if not attribute or attribute == "" then
        return ""
    end
    return "icon_属性_" .. attribute .. ".png"
end

-- 获取角色图片文件名
local function getCharacterImage(characterName)
    return "战斗员图鉴_" .. characterName .. ".png"
end

-- 渲染单个角色卡片
local function renderCharacterCard(characterName, data)
    if not data or not data.data then
        return ""
    end
    
    local charData = data.data
    local name = charData["名称"] or characterName
    local rarity = charData["稀有度"] or "1"
    local profession = charData["职业"] or ""
    local attribute = charData["属性"] or ""
    
    local html = mw.html.create('div')
        :attr('style', 'position: relative; width: 150px; height: 280px; overflow: hidden; display: inline-block; margin: 5px;')
    
    -- 背景
    html:tag('div')
        :attr('style', 'position: absolute; top: 0px; left: 0px; width: 150px; height: 280px; background-color:#2a2f40; border-radius: 0px 25px 0px 0px; z-index: 0;')
    
    -- 职业图标
    if profession ~= "" then
        local professionIcon = getProfessionIcon(profession)
        html:tag('div')
            :attr('style', 'position: absolute; top: 5px; left: 5px; z-index: 1;')
            :wikitext('[[File:' .. professionIcon .. '|25px|link=]]')
    end
    
    -- 属性图标
    if attribute ~= "" then
        local attributeIcon = getAttributeIcon(attribute)
        html:tag('div')
            :attr('style', 'position: absolute; top: 35px; left: 5px; z-index: 1;')
            :wikitext('[[File:' .. attributeIcon .. '|25px|link=]]')
    end
    
    -- 角色图片
    local characterImage = getCharacterImage(name)
    html:tag('div')
        :attr('style', 'position: absolute; bottom: 0px; left: 0px; z-index: 1;')
        :wikitext('[[File:' .. characterImage .. '|150px|link=]]')
    
    -- 角色名称背景
    html:tag('div')
        :attr('style', 'position: absolute; bottom: 5px; left: 0px; width: 150px; height: 30px; background-color: rgba(0, 0, 0, 0.5); color: white; text-align: right; padding-right: 8px; padding-top: 3px; z-index: 2;')
        :wikitext(name)
    
    -- 稀有度样式
    local rarityClass = getRarityClass(rarity)
    html:tag('div')
        :addClass(rarityClass)
        :attr('style', 'position: absolute; left: 0px; bottom: 0px; z-index: 2;')
    
    -- 顶层蒙版
    html:tag('div')
        :attr('style', 'position: absolute; left: 0px; bottom: 0px; z-index: 99;')
        :wikitext('[[File:战斗员图鉴_顶层蒙版.png|link=]]')
    
    return tostring(html)
end

-- 获取所有角色模块
local function getAllCharacters()
    local characters = {}
    
    -- 获取所有以"模块:战斗员/"开头的页面
    local pages = {}
    
    -- 使用API查询所有相关页面
    for title, content in pairs(mw.loadData('Module:CharacterList') or {}) do
        if mw.ustring.match(title, '^模块:战斗员/') then
            local characterName = mw.ustring.gsub(title, '^模块:战斗员/', '')
            table.insert(characters, characterName)
        end
    end
    
    return characters
end

-- 主函数
function p.main(frame)
    local args = frame:getParent().args
    local targetCharacter = args[1] and mw.text.trim(args[1]) or nil
    
    local result = mw.html.create('div')
        :attr('style', 'text-align: left;')
    
    if targetCharacter and targetCharacter ~= "" then
        -- 显示单个角色
        local moduleName = "模块:战斗员/" .. targetCharacter
        local success, data = pcall(mw.loadData, moduleName)
        
        if success then
            result:wikitext(renderCharacterCard(targetCharacter, data))
        else
            result:wikitext("错误:找不到角色 " .. targetCharacter .. " 的数据模块")
        end
    else
        -- 显示所有角色
        local characters = getAllCharacters()
        
        if #characters == 0 then
            result:wikitext("没有找到任何角色数据")
            return tostring(result)
        end
        
        for _, characterName in ipairs(characters) do
            local moduleName = "模块:战斗员/" .. characterName
            local success, data = pcall(mw.loadData, moduleName)
            
            if success then
                result:wikitext(renderCharacterCard(characterName, data))
            end
        end
    end
    
    return tostring(result)
end

return p