根据你的需求,需要在检测到 `data["2"]["5"] == 1` 时隐藏按钮控件。这里有几个关键点需要优化:
1. **定时器使用规范**:循环定时应使用 `SL:Schedule`,单次延迟使用 `SL:ScheduleOnce`
2. **数据监听**:需要持续监听服务器数据变化
3. **控件隐藏逻辑**:当条件满足时隐藏整个节点
以下是修改后的代码:
```lua
local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res/public/996kf_red.png", cmd = { 1, 0, 0 } },
}
-- 检查显示条件的函数
function MainButton:checkShowConditions()
-- 判断条件1:检查是否是开服第一天
local serverOpenDay = SL:GetMetaValue("OPEN_SERVER_DAY") or 1
if serverOpenDay <= 1 then
SL:Print("开服第一天,不显示按钮")
return false
end
-- 判断条件2:检查是否是星期五、六、日
local serverTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", serverTime)
local weekDay = timeTable.wday - 1
if weekDay == 0 then weekDay = 7 end
-- 周五=5,周六=6,周日=7
if weekDay < 5 then
SL:Print("不是周五、六、日,不显示按钮")
return false
end
-- 判断条件3:检查data["2"]["5"]是否等于1
local data = SL:JsonDecode(SL:GetMetaValue("SERVER_VALUE", "HUMAN(幸运转盘数据)"))
if data and data["2"] and data["2"]["5"] == 1 then
SL:Print("data[2][5] = 1,不显示按钮")
return false
end
return true
end
-- 更新倒计时函数
function MainButton:updateCountdown(timeText)
local currentTime = SL:GetMetaValue("SERVER_TIME")
local currentDate = os.date("*t", currentTime)
currentDate.hour = 0
currentDate.min = 0
currentDate.sec = 0
local todayZero = os.time(currentDate)
local tomorrowZero = todayZero + 24 * 60 * 60
local remainingSeconds = tomorrowZero - currentTime
-- 设置倒计时显示
GUI:Text_COUNTDOWN(timeText, remainingSeconds, nil, 0)
end
-- 监听数据变化并更新界面
function MainButton:setupDataListener(parent)
-- 每5秒检查一次数据变化
SL:Schedule(function()
if not GUI:Win_IsNull(parent) then
local node = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(node) then
local shouldShow = self:checkShowConditions()
GUI:setVisible(node, shouldShow)
-- 如果隐藏了节点,也停止相关定时器
if not shouldShow then
-- 这里可以添加停止定时器的逻辑
end
end
end
end, 5)
end
function MainButton:main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop()
if GUI:Win_IsNull(parent) then
return
end
-- 检查显示条件
if not self:checkShowConditions() then
-- 移除可能存在的旧节点
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
return
end
-- 检查并移除旧节点
if GUI:getChildByName(parent, "node_rightSide_1") then
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
GUI:removeFromParent(oldNode)
end
-- 创建新节点
local Top_node_obj = GUI:Widget_Create(parent, "node_rightSide_1", 0, 0, 0, 0)
if GUI:Win_IsNull(Top_node_obj) then
return
end
for _, v in pairs(MainButton.cfg) do
local but_switch = GUI:Button_Create(Top_node_obj, "switch",
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and 230 or 270,
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -270 or -310, v.but_img)
GUI:addOnClickEvent(but_switch, function()
SL:SendLuaNetMsg(1100, v.cmd[1], v.cmd[2], v.cmd[3], "")
end)
end
-- 在按钮正下方添加倒计时文本
local timeText = GUI:Text_Create(Top_node_obj, "timeText_but_id",
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and 273 or 313,
SL:GetMetaValue("CURRENT_OPERMODE") == 1 and -280 or -320, 16, "#FF0000", "")
GUI:setAnchorPoint(timeText, 0.5, 0.5)
-- 立即更新倒计时
self:updateCountdown(timeText)
-- 每秒更新倒计时
SL:Schedule(function()
if not GUI:Win_IsNull(timeText) then
self:updateCountdown(timeText)
end
end, 1)
-- 设置数据监听
self:setupDataListener(parent)
-- 设置凌晨刷新
local currentTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", currentTime)
local targetTime = os.time({
year = timeTable.year,
month = timeTable.month,
day = timeTable.day + 1,
hour = 0,
min = 0,
sec = 0
})
local delaySeconds = targetTime - currentTime
if delaySeconds < 0 then
delaySeconds = delaySeconds + 24 * 60 * 60
end
SL:ScheduleOnce(function()
self:main()
end, delaySeconds)
end, 0.2)
end
return MainButton
```
**主要修改说明:**
1. **分离条件检查函数**:将显示条件检查逻辑独立为 `checkShowConditions()` 函数,便于复用
2. **添加数据监听**:通过 `SL:Schedule` 每5秒检查一次数据变化,当 `data["2"]["5"] == 1` 时自动隐藏节点
3. **使用 `GUI:setVisible()`**:当条件不满足时,通过 `GUI:setVisible(node, false)` 隐藏控件而不是直接移除
4. **规范定时器使用**:
- 单次延迟使用 `SL:ScheduleOnce`
- 循环定时使用 `SL:Schedule`
5. **凌晨自动刷新**:每天0点重新执行 `main()` 函数刷新界面
这样当 `data["2"]["5"]` 的值变为1时,按钮会在5秒内自动隐藏,符合你的需求。