本WIKI编辑权限开放,欢迎收藏起来防止迷路,也希望有爱的小伙伴和我们一起编辑哟~

全站通知:

模块:预设计时

来自斯露德WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索
内容

内容


-- 创建一个名为 'p' 的 Lua 表,用于存储模块中的函数和变量
local p = {}

-- 获取当前时间的函数,通过 MediaWiki 模板参数 'NowTime' 获取时间
local function getTime(frame)
    local input_time = frame.args.NowTime  -- 获取模板参数 'NowTime'
    local output_time

    -- 如果 'NowTime' 参数存在且不为空
    if input_time ~= nil and input_time ~= "" then
        -- 从输入的时间字符串中提取年、月、日、小时、分钟和秒
        local year, month, day, hour, minute, second = input_time:match("(%d+)/(%d+)/(%d+) (%d+):(%d+):(%d+)")
        -- 使用提取的信息构建一个时间表,并将其转换为时间戳
        output_time = os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = second})
    else
        -- 如果 'NowTime' 参数不存在或为空,使用当前系统时间
        output_time = os.time()
    end

    return output_time
end

-- 获取下一个游戏阶段的开始时间的函数
local function getNextPhaseStartTime(current_time, current_wday, current_hour, phase_start_wdays, phase_start_hour)
    local next_phase_day = 1

    -- 遍历阶段开始的星期几,找到下一个阶段的开始日期
    for _, wday in ipairs(phase_start_wdays) do
        if current_wday <= wday or current_wday == 7 then
            next_phase_day = wday
            break
        end
    end

    -- 如果当前是阶段开始的星期几,并且当前小时大于等于阶段开始的小时
    if current_wday == next_phase_day and current_hour >= phase_start_hour then
        next_phase_day = next_phase_day + 7 -- 下一周的同一天开始
    end

    -- 使用提取的信息构建下一个阶段开始的时间表,并将其转换为时间戳
    local next_phase_start_time = os.time({year = os.date("%Y", current_time), month = os.date("%m", current_time),
         day = os.date("%d", current_time) + (next_phase_day - current_wday), hour = phase_start_hour, min = 0, sec = 0})

    return next_phase_start_time
end

-- 主要函数,用于确定游戏状态并返回相应的 HTML 结果
function p.YiKong(frame)
    local current_time = getTime(frame)  -- 获取当前时间
    local current_wday = tonumber(os.date("%w", current_time))  -- 获取当前星期几
    local current_hour = tonumber(os.date("%H", current_time))  -- 获取当前小时

    local status = ""  -- 游戏状态(匹配期或战斗期)
    local next_event_time_label = ""  -- 下一个事件的时间标签
    local expanded_template = ""  -- 扩展的模板

    -- 定义匹配期和战斗期的开始和结束时间
    local match_start_wdays = {3, 0}
    local match_start_hour = 22
    local match_end_hour = 5

    local battle_start_wdays = {1, 4}
    local battle_start_hour = 5
    local battle_end_wdays = {3, 7}
    local battle_end_hour = 18

    local next_phase_start_time = 0  -- 下一个阶段的开始时间

    -- 根据当前时间和星期几,确定游戏状态和下一个事件的时间
    if (current_wday == 3 or current_wday == 0) and current_hour >= match_start_hour then
        status = "匹配期"
        next_event_time_label = "结束时间"
        next_phase_start_time = getNextPhaseStartTime(current_time, current_wday, current_hour, match_end_hour)

    elseif (current_wday == 1 or current_wday == 4) and current_hour < battle_start_hour then
        status = "匹配期"
        next_event_time_label = "结束时间"
        next_phase_start_time = getNextPhaseStartTime(current_time, current_wday, current_hour, match_end_hour)

    elseif (current_wday == 1 or current_wday == 4) and current_hour >= battle_start_hour then
        status = "战斗期"
        next_event_time_label = "结束时间"
        next_phase_start_time = getNextPhaseStartTime(current_time, current_wday, current_hour, battle_end_wdays, battle_end_hour)

    else
        status = "战斗期"
        next_event_time_label = "开始时间"
        next_phase_start_time = getNextPhaseStartTime(current_time, current_wday, current_hour, match_start_wdays, match_start_hour)
    end

    -- 计算下一个事件的时间差
    local time_difference = next_phase_start_time - current_time
    local days = math.floor(time_difference / (24 * 3600))
    local hours = math.floor((time_difference % (24 * 3600)) / 3600)
    local minutes = math.floor((time_difference % 3600) / 60)
    local seconds = time_difference % 60

    -- 使用模板扩展语法,构建扩展的模板
    expanded_template = frame:expandTemplate({title = '新计时', args = {[next_event_time_label] = os.date("%Y/%m/%d %H:%M:%S", next_phase_start_time), ['事件'] = status, ['隐藏分类'] = '是'}})

    -- 构建最终的 HTML 结果
    local result = '<span class="yikong-time">内容'.. expanded_template .. '</span>'

    return result
end

-- 返回模块表 'p'
return p