模块

事件:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
 
(未显示同一用户的9个中间版本)
第3行: 第3行:


function p.main(frame)
function p.main(frame)
    -- 直接从frame.args获取参数(用于#invoke调用)
     local output = {}
     local eventName = frame.args[1] or frame.args.name
      
      
     -- 如果没有找到,尝试从父frame获取(用于模板调用)
     -- 遍历所有事件
     if not eventName and frame:getParent() then
     for eventName, eventInfo in pairs(eventData) do
         local parentArgs = frame:getParent().args
         table.insert(output, p.renderEvent(frame, eventName, eventInfo))
        eventName = parentArgs[1] or parentArgs.name
     end
     end
      
      
     if not eventName or eventName == '' then
     return table.concat(output, '\n')
        return "错误:未指定事件名称"
end
    end
 
   
function p.renderEvent(frame, eventName, eventInfo)
    local event = eventData[eventName]
    if not event then
        return "错误:找不到事件 '" .. eventName .. "'"
    end
   
    -- 构建HTML
     local html = mw.html.create('div')
     local html = mw.html.create('div')
         :addClass('event-container')
         :addClass('event-container')
         :css('position', 'relative')
         :attr('data-event-id', eventInfo.id)
        :css('width', '375px')
        :css('height', '270px')
      
      
     -- 底部背景
     -- 背景层
     html:tag('div')
     html:tag('div'):addClass('event-background')
        :addClass('event-bottom-bg')
        :css('position', 'absolute')
        :css('top', '160px')
        :css('left', '5px')
        :css('width', '365px')
        :css('height', '120px')
        :css('background-color', '#343434')
        :css('border-radius', '0px 0px 8px 8px')
      
      
     -- 图片
     -- 图片
     local imageFile = 'cc_' .. event.id .. '.png'
     local imageLink = eventName
     html:tag('div')
     html:tag('div')
         :css('position', 'absolute')
         :addClass('event-image')
         :css('top', '0px')
         :wikitext(string.format('[[File:cc_%s.png|link=%s]]', eventInfo.id, imageLink))
        :css('left', '0px')
        :wikitext('[[File:' .. imageFile .. '|link=]]')
      
      
     -- 标题背景
     -- 标题背景
     html:tag('div')
     html:tag('div'):addClass('event-title-bg')
        :css('position', 'absolute')
        :css('top', '135px')
        :css('left', '5px')
        :css('width', '365px')
        :css('height', '30px')
        :css('background-color', 'rgba(0,0,0,0.3)')
        :css('border-radius', '0px 0px 8px 8px')
      
      
     -- 标题文字
     -- 标题
     html:tag('div')
     html:tag('div')
         :addClass('event-title')
         :addClass('event-title')
        :css('position', 'absolute')
        :css('top', '140px')
        :css('left', '5px')
        :css('width', '365px')
        :css('height', '30px')
        :css('color', 'white')
        :css('text-align', 'center')
         :wikitext(eventName)
         :wikitext(eventName)
      
      
     -- 选项
     -- 选项容器
     local optionTop = 170
     local optionsContainer = html:tag('div'):addClass('event-options')
    for i = 1, 3 do
   
        if event.option[i] then
    -- 渲染选项
            local optionDiv = html:tag('div')
    for i, optionText in ipairs(eventInfo.option) do
                :addClass('event-option')
        -- 选项
                :attr('data-option-id', tostring(i))
        optionsContainer:tag('div')
                :attr('data-event-name', eventName)
            :addClass('event-option')
                :css('position', 'absolute')
            :attr('data-option-index', i)
                :css('top', optionTop .. 'px')
            :wikitext(optionText)
                :css('left', '10px')
       
                :css('width', '355px')
        -- 结果显示区域
                :css('min-height', '30px')
        local effectDiv = optionsContainer:tag('div')
                :css('color', 'white')
            :addClass('event-effect')
                :css('text-align', 'left')
             :attr('data-effect-index', i)
                :css('padding', '5px 0px 5px 10px')
       
                :css('background-color', 'rgba(255,255,255,0.3)')
        -- 处理结果文本
                :css('border-radius', '4px')
        local effectText = eventInfo.effect[i]
                :css('cursor', 'pointer')
        if effectText then
                :css('transition', 'background-color 0.3s')
            if frame then
           
                 effectText = frame:preprocess(effectText)
            -- 选项文本
             end
            optionDiv:tag('div')
             effectDiv:wikitext(effectText)
                :addClass('option-text')
                :wikitext(event.option[i])
              
            -- 效果文本(默认隐藏)
            optionDiv:tag('div')
                :addClass('option-effect')
                :css('display', 'none')
                :css('margin-top', '5px')
                :css('padding-top', '5px')
                :css('border-top', '1px solid rgba(255,255,255,0.5)')
                :css('font-size', '0.9em')
                 :wikitext(frame:preprocess(event.effect[i]))
              
             optionTop = optionTop + 35
         end
         end
     end
     end
      
      
     return tostring(html)
     return tostring(html)
end
-- 单独显示某个事件
function p.show(frame)
    local eventName = frame.args[1]
    if not eventName or not eventData[eventName] then
        return '错误:未找到指定的事件'
    end
   
    return p.renderEvent(frame, eventName, eventData[eventName])
end
end


return p
return p

2025年10月18日 (六) 13:38的最新版本

此模块的文档可以在模块:事件/doc创建

local p = {}
local eventData = mw.loadData('模块:事件/data')

function p.main(frame)
    local output = {}
    
    -- 遍历所有事件
    for eventName, eventInfo in pairs(eventData) do
        table.insert(output, p.renderEvent(frame, eventName, eventInfo))
    end
    
    return table.concat(output, '\n')
end

function p.renderEvent(frame, eventName, eventInfo)
    local html = mw.html.create('div')
        :addClass('event-container')
        :attr('data-event-id', eventInfo.id)
    
    -- 背景层
    html:tag('div'):addClass('event-background')
    
    -- 图片
    local imageLink = eventName
    html:tag('div')
        :addClass('event-image')
        :wikitext(string.format('[[File:cc_%s.png|link=%s]]', eventInfo.id, imageLink))
    
    -- 标题背景
    html:tag('div'):addClass('event-title-bg')
    
    -- 标题
    html:tag('div')
        :addClass('event-title')
        :wikitext(eventName)
    
    -- 选项容器
    local optionsContainer = html:tag('div'):addClass('event-options')
    
    -- 渲染选项
    for i, optionText in ipairs(eventInfo.option) do
        -- 选项
        optionsContainer:tag('div')
            :addClass('event-option')
            :attr('data-option-index', i)
            :wikitext(optionText)
        
        -- 结果显示区域
        local effectDiv = optionsContainer:tag('div')
            :addClass('event-effect')
            :attr('data-effect-index', i)
        
        -- 处理结果文本
        local effectText = eventInfo.effect[i]
        if effectText then
            if frame then
                effectText = frame:preprocess(effectText)
            end
            effectDiv:wikitext(effectText)
        end
    end
    
    return tostring(html)
end

-- 单独显示某个事件
function p.show(frame)
    local eventName = frame.args[1]
    if not eventName or not eventData[eventName] then
        return '错误:未找到指定的事件'
    end
    
    return p.renderEvent(frame, eventName, eventData[eventName])
end

return p