模块

模块:语音

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年11月8日 (六) 12:05的版本

此模块的文档可以在模块:语音/doc创建

local p = {}

function p.main(frame)
    local characterName = frame.args[1] or frame:getParent().args[1]
    
    if not characterName or characterName == "" then
        return "错误:请提供战斗员名称"
    end
    
    -- 尝试加载语音数据模块
    local success, voiceData = pcall(require, "模块:语音/" .. characterName)
    
    if not success then
        return "错误:未找到战斗员 " .. characterName .. " 的语音数据"
    end
    
    -- 从文件名中提取角色ID
    local characterId = ""
    if voiceData[1] and voiceData[1].file then
        characterId = voiceData[1].file:match("vo_(%d+)_")
    end
    
    -- 引入 Widget
    local widgetCode = frame:extensionTag('widget', 'VoicePlayer')
    
    -- 创建表格
    local html = mw.html.create('table')
        :addClass('wikitable voice-table')
    
    -- 表头
    local headerRow = html:tag('tr')
    headerRow:tag('th'):wikitext('类型'):css('width', '15%')
    headerRow:tag('th'):wikitext('韩语'):css('width', '5%'):css('text-align', 'center')
    headerRow:tag('th'):wikitext('日语'):css('width', '5%'):css('text-align', 'center')
    headerRow:tag('th'):wikitext('文本'):css('width', '75%')
    
    -- 数据行
    for _, voice in ipairs(voiceData) do
        local row = html:tag('tr')
        
        -- 类型
        row:tag('td'):wikitext(voice.type)
        
        -- 韩语播放按钮
        local krPath = "/voice/kr/" .. characterId .. "/" .. voice.file
        row:tag('td')
            :css('text-align', 'center')
            :wikitext('<button class="voice-play-button" onclick="VoicePlayer.play(\'' .. krPath .. '\', this)">▶</button>')
        
        -- 日语播放按钮
        local jpPath = "/voice/jp/" .. characterId .. "/" .. voice.file
        row:tag('td')
            :css('text-align', 'center')
            :wikitext('<button class="voice-play-button" onclick="VoicePlayer.play(\'' .. jpPath .. '\', this)">▶</button>')
        
        -- 文本内容
        local textContent = '<div class="voice-text">' ..
                          (voice.text_cn or "") .. "<br>" .. 
                          (voice.text_kr or "") .. "<br>" .. 
                          (voice.text_jp or "") ..
                          '</div>'
        row:tag('td'):wikitext(textContent)
    end
    
    return widgetCode .. tostring(html)
end

return p