我来帮你优化这段代码,主要改进代码结构、错误处理和性能:
```lua
local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res\\custom\\top/20.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)
if GUI:Win_IsNull(timeText) then
return
end
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:cleanupOldNode(parent)
if not GUI:Win_IsNull(parent) then
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
end
end
-- 创建按钮
function MainButton:createButtons(parentNode)
local isPortrait = SL:GetMetaValue("CURRENT_OPERMODE") == 1
local baseX = isPortrait and 230 or 270
local baseY = isPortrait and -270 or -310
for _, v in pairs(MainButton.cfg) do
local but_switch = GUI:Button_Create(parentNode, "switch", baseX, baseY, v.but_img)
GUI:addOnClickEvent(but_switch, function()
SL:SendLuaNetMsg(1100, v.cmd[1], v.cmd[2], v.cmd[3], "")
end)
end
end
-- 创建倒计时文本
function MainButton:createCountdownText(parentNode)
local isPortrait = SL:GetMetaValue("CURRENT_OPERMODE") == 1
local textX = isPortrait and 273 or 313
local textY = isPortrait and -280 or -320
local timeText = GUI:Text_Create(parentNode, "timeText_but_id", textX, textY, 16, "#FF0000", "")
GUI:setAnchorPoint(timeText, 0.5, 0.5)
return timeText
end
-- 设置倒计时定时器
function MainButton:setupCountdownTimer(timeText)
local timerID = SL:Schedule(function()
if not GUI:Win_IsNull(timeText) then
self:updateCountdown(timeText)
end
end, 1)
return timerID
end
-- 监听数据变化并更新界面
function MainButton:setupDataListener(parent, parentNode)
local timerID = 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)
end
end
end, 1)
return timerID
end
-- 设置凌晨刷新
function MainButton:setupMidnightRefresh()
local currentTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", currentTime)
-- 计算到明天0点的秒数
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
return delaySeconds
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
self:cleanupOldNode(parent)
return
end
-- 清理旧节点
self:cleanupOldNode(parent)
-- 创建新节点
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
-- 创建按钮
self:createButtons(Top_node_obj)
-- 创建倒计时文本
local timeText = self:createCountdownText(Top_node_obj)
self:updateCountdown(timeText)
-- 设置倒计时定时器
local countdownTimer = self:setupCountdownTimer(timeText)
-- 设置数据监听
local dataListenerTimer = self:setupDataListener(parent, Top_node_obj)
-- 设置凌晨刷新
local delaySeconds = self:setupMidnightRefresh()
SL:ScheduleOnce(function()
self:main()
end, delaySeconds)
-- 存储定时器ID以便清理
self._timers = self._timers or {}
table.insert(self._timers, countdownTimer)
table.insert(self._timers, dataListenerTimer)
end, 0.2)
end
-- 清理函数
function MainButton:cleanup()
if self._timers then
for _, timerID in ipairs(self._timers) do
SL:Unschedule(timerID)
end
self._timers = nil
end
end
return MainButton
```
**主要优化点:**
1. **函数拆分**:将大函数拆分为小函数,提高可读性和复用性
2. **错误处理**:在`updateCountdown`函数开头增加`GUI:Win_IsNull`检查
3. **变量提取**:将重复的`SL:GetMetaValue("CURRENT_OPERMODE")`提取为局部变量
4. **定时器管理**:增加了定时器存储和清理机制
5. **代码复用**:创建了专门的清理旧节点函数`cleanupOldNode`
6. **性能优化**:避免重复计算坐标值
7. **结构清晰**:每个函数职责单一,便于维护
**注意**:`SL:Unschedule(timerID)`函数需要根据浮生梦老师前端课程内容确认是否存在,如果不存在需要寻找对应的定时器取消函数。