local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res/public/996kf_red.png", cmd = { 1, 0, 0 } }, --GM功能
}
function MainButton:main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-09 17:48:07 |
👁️ 阅读: 8
根据你的需求,我来帮你修改代码。主要添加开服天数判断、星期判断、以及根据 `data` 值控制显示逻辑。
```lua
local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res/public/996kf_red.png", cmd = { 1, 0, 0 } }, --GM功能
}
function MainButton:main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop()
SL:Print("parent:----------------1", parent)
if GUI:Win_IsNull(parent) then
SL:Print("parent为空")
return
end
-- 检查并移除旧节点
if GUI:getChildByName(parent, "node_rightSide_1") then
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
GUI:removeFromParent(oldNode)
SL:Print("删除了旧节点")
end
-- 获取服务器数据
local serverData = SL:JsonDecode(SL:GetMetaValue("SERVER_VALUE", "HUMAN(幸运转盘数据)"))
-- 获取开服天数
local openServerDay = SL:GetMetaValue("SERVER_OPEN_DAY") or 1
-- 获取当前星期 (1=周日, 2=周一, ..., 7=周六)
local currentTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", currentTime)
local weekDay = timeTable.wday -- 注意:周日是1,周一是2
-- 判断条件:不是开服第一天,且是周五、周六、周日
local isShowDay = false
if openServerDay > 1 then
-- 周五=6, 周六=7, 周日=1
if weekDay == 6 or weekDay == 7 or weekDay == 1 then
isShowDay = true
end
end
-- 检查data值判断是否显示
local isShowByData = true
if serverData and serverData["2"] and serverData["2"]["5"] == 1 then
isShowByData = false
end
-- 最终显示判断
if not isShowDay or not isShowByData then
SL:Print("不满足显示条件:开服天数=", openServerDay, " 星期=", weekDay)
return
end
-- 创建新节点
local Top_node_obj = GUI:Widget_Create(parent, "node_rightSide_1", 0, 0, 0, 0)
if GUI:Win_IsNull(Top_node_obj) then
SL:Print("创建节点失败")
return
end
for _, v in pairs(MainButton.cfg) do
-- 确定按钮发送的p1值
local sendP1 = v.cmd[1]
if serverData and serverData["1"] and serverData["1"]["5"] == 1 then
sendP1 = 2 -- 如果data["1"]["5"]=1,则发送2
end
-- 创建按钮
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, sendP1, 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)
-- 计算距离第二天0点的剩余时间
local function updateCountdown()
-- 获取当前服务器时间戳
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)
-- 计算明天0点的时间戳
local tomorrowZero = todayZero + 24 * 60 * 60
-- 计算剩余秒数
local remainingSeconds = tomorrowZero - currentTime
-- 设置倒计时
GUI:Text_COUNTDOWN(timeText, remainingSeconds, nil, 0)
end
-- 立即更新一次
updateCountdown()
-- 每秒更新一次(确保倒计时准确)
SL:Schedule(timeText, function()
updateCountdown()
end, 1)
-- 添加凌晨00:01刷新功能
local function scheduleMidnightRefresh()
local currentTime = SL:GetMetaValue("SERVER_TIME")
local timeTable = os.date("*t", currentTime)
-- 计算到明天00:01的秒数
local targetTime = os.time({
year = timeTable.year,
month = timeTable.month,
day = timeTable.day + 1,
hour = 0,
min = 1,
sec = 0
})
local delaySeconds = targetTime - currentTime
if delaySeconds < 0 then
delaySeconds = delaySeconds + 24 * 60 * 60
end
SL:ScheduleOnce(function()
-- 重新执行main函数刷新界面
MainButton:main()
end, delaySeconds)
end
-- 设置凌晨刷新
scheduleMidnightRefresh()
end, 0.2)
end
return MainButton
```
**修改说明:**
1. **开服天数判断**:使用 `SL:GetMetaValue("SERVER_OPEN_DAY")` 获取开服天数,开服第一天不显示。
2. **星期判断**:使用 `os.date("*t", currentTime).wday` 获取星期,只显示周五(6)、周六(7)、周日(1)。
3. **data值判断**:
- 如果 `data["2"]["5"] == 1`,则不显示按钮
- 如果 `data["1"]["5"] == 1`,则点击按钮时发送 `p1=2`,否则发送原来的值
4. **凌晨00:01刷新**:添加了定时器,在每天00:01重新执行 `main()` 函数刷新界面。
5. **显示逻辑**:需要同时满足:
- 不是开服第一天
- 是周五、周六或周日
- `data["2"]["5"]` 不等于1
如果还有不明白的地方,随时问我!
← 返回问答库