function kf_hyd(actor)
local msg = ""
if not checkkuafu(actor) then
return false
end
local map_id = getbaseinfo(actor, 3)
local map_name = getmapname(map_id)
if string.find(map_name, "外") then
changemoney(acto
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-14 16:23:17 |
👁️ 阅读: 17
你好呀,我是Lua后端老师!
小仙,你的需求是在特定时间段内判断传送门状态。根据你的时间规则,我们需要获取当前时间的小时和分钟,然后进行区间判断。这里用 `os.date("*t")` 来获取当前时间的详细表格,然后判断 `km_zt` 的值。
```lua
function kf_hyd(actor)
local msg = ""
if not checkkuafu(actor) then
return false
end
local map_id = getbaseinfo(actor, 3)
local map_name = getmapname(map_id)
if string.find(map_name, "外") then
changemoney(actor, 25, '+', 10, '跨服积分', true)
end
if string.find(map_name, "内") then
changemoney(actor, 25, '+', 20, '跨服积分', true)
end
local money_jf = querymoney(actor, 25)
local km_zt = "开启" -- 默认状态
-- 获取当前时间
local currentTime = os.date("*t")
local currentHour = currentTime.hour
local currentMinute = currentTime.min
-- 将当前时间转换为从0点开始的分钟数,方便比较
local currentTotalMinutes = currentHour * 60 + currentMinute
-- 定义传送门开启和关闭的时间段(单位:分钟)
-- 格式:{开启开始分钟, 开启结束分钟}
local timePeriods = {
{20 * 60 + 31, 20 * 60 + 34}, -- 20:31 至 20:34
{20 * 60 + 36, 20 * 60 + 49}, -- 20:36 至 20:49
{20 * 60 + 40, 20 * 60 + 43}, -- 20:40 至 20:43
{20 * 60 + 44, 20 * 60 + 47}, -- 20:44 至 20:47
{20 * 60 + 49, 20 * 60 + 52}, -- 20:49 至 20:52
{20 * 60 + 54, 20 * 60 + 57} -- 20:54 至 20:57
}
-- 判断当前时间是否在任何一个开启时间段内
local isOpen = false
for _, period in ipairs(timePeriods) do
local openStart = period[1]
local openEnd = period[2]
if currentTotalMinutes >= openStart and currentTotalMinutes <= openEnd then
isOpen = true
break
end
end
-- 根据判断结果设置状态文本
if isOpen then
km_zt = "开启"
else
km_zt = "关闭"
end
-- 构建消息,使用判断后的 km_zt 变量
msg = [[<RText|x=10|y=25|color=255|size=18|text=<归属:/FCOLOR=255><暂无归属/FCOLOR=250>>
<RText|x=10|y=55.0|color=255|size=18|text=<当前积分:/FCOLOR=255><]] .. money_jf .. [[/FCOLOR=250>>
<RText|x=10|y=85.0|color=249|size=18|text=<结束时间:/FCOLOR=255><20:30/FCOLOR=249>>
<RText|x=10|y=115.0|color=255|size=18|text=<内门状态:/FCOLOR=255><]] .. km_zt .. [[/FCOLOR=249>>
<Button|x=58.0|y=145.0|color=255|size=18|nimg=public/1900000689.png|text=回城|link=@kf_huicheng>]]
addbutton(actor, 110, 0, msg)
end
```
**代码说明:**
1. `os.date("*t")` 获取一个包含年、月、日、时、分、秒等字段的表格。
2. 我们把小时和分钟换算成从0点开始的**总分钟数**,这样比较时间段更简单。
3. 定义了一个 `timePeriods` 表格,里面按你的规则列出了所有“开启”的时间段(用总分钟数表示)。
4. 遍历这些时间段,检查当前时间是否落在其中任何一个区间内,如果是,`isOpen` 设为 `true`。
5. 根据 `isOpen` 的值,最终设置 `km_zt` 为“开启”或“关闭”。
6. 最后构建消息时,使用的是动态判断后的 `km_zt` 变量。
这样,按钮上显示的内门状态就会根据服务器的当前时间自动变化了。注意,这里用的是服务器时间,不是玩家电脑的时间哦!
如果还有其他不明白的,随时问我,或者请教浮生梦尊师也行!
你好呀,我是Lua后端老师!
小仙,你的需求是在特定时间段内判断传送门状态。根据你的时间规则,我们需要获取当前时间的小时和分钟,然后进行区间判断。这里用 `os.date("*t")` 来获取当前时间的详细表格,然后判断 `km_zt` 的值。
```lua
function kf_hyd(actor)
local msg = ""
if not checkkuafu(actor) then
return false
end
local map_id = getbaseinfo(actor, 3)
local map_name = getmapname(map_id)
if string.find(map_name, "外") then
changemoney(actor, 25, '+', 10, '跨服积分', true)
end
if string.find(map_name, "内") then
changemoney(actor, 25, '+', 20, '跨服积分', true)
end
local money_jf = querymoney(actor, 25)
local km_zt = "开启" -- 默认状态
-- 获取当前时间
local currentTime = os.date("*t")
local currentHour = currentTime.hour
local currentMinute = currentTime.min
-- 将当前时间转换为从0点开始的分钟数,方便比较
local currentTotalMinutes = currentHour * 60 + currentMinute
-- 定义传送门开启和关闭的时间段(单位:分钟)
-- 格式:{开启开始分钟, 开启结束分钟}
local timePeriods = {
{20 * 60 + 31, 20 * 60 + 34}, -- 20:31 至 20:34
{20 * 60 + 36, 20 * 60 + 49}, -- 20:36 至 20:49
{20 * 60 + 40, 20 * 60 + 43}, -- 20:40 至 20:43
{20 * 60 + 44, 20 * 60 + 47}, -- 20:44 至 20:47
{20 * 60 + 49, 20 * 60 + 52}, -- 20:49 至 20:52
{20 * 60 + 54, 20 * 60 + 57} -- 20:54 至 20:57
}
-- 判断当前时间是否在任何一个开启时间段内
local isOpen = false
for _, period in ipairs(timePeriods) do
local openStart = period[1]
local openEnd = period[2]
if currentTotalMinutes >= openStart and currentTotalMinutes <= openEnd then
isOpen = true
break
end
end
-- 根据判断结果设置状态文本
if isOpen then
km_zt = "开启"
else
km_zt = "关闭"
end
-- 构建消息,使用判断后的 km_zt 变量
msg = [[<RText|x=10|y=25|color=255|size=18|text=<归属:/FCOLOR=255><暂无归属/FCOLOR=250>>
<RText|x=10|y=55.0|color=255|size=18|text=<当前积分:/FCOLOR=255><]] .. money_jf .. [[/FCOLOR=250>>
<RText|x=10|y=85.0|color=249|size=18|text=<结束时间:/FCOLOR=255><20:30/FCOLOR=249>>
<RText|x=10|y=115.0|color=255|size=18|text=<内门状态:/FCOLOR=255><]] .. km_zt .. [[/FCOLOR=249>>
<Button|x=58.0|y=145.0|color=255|size=18|nimg=public/1900000689.png|text=回城|link=@kf_huicheng>]]
addbutton(actor, 110, 0, msg)
end
```
**代码说明:**
1. `os.date("*t")` 获取一个包含年、月、日、时、分、秒等字段的表格。
2. 我们把小时和分钟换算成从0点开始的**总分钟数**,这样比较时间段更简单。
3. 定义了一个 `timePeriods` 表格,里面按你的规则列出了所有“开启”的时间段(用总分钟数表示)。
4. 遍历这些时间段,检查当前时间是否落在其中任何一个区间内,如果是,`isOpen` 设为 `true`。
5. 根据 `isOpen` 的值,最终设置 `km_zt` 为“开启”或“关闭”。
6. 最后构建消息时,使用的是动态判断后的 `km_zt` 变量。
这样,按钮上显示的内门状态就会根据服务器的当前时间自动变化了。注意,这里用的是服务器时间,不是玩家电脑的时间哦!
如果还有其他不明白的,随时问我,或者请教浮生梦尊师也行!
← 返回问答库