XueZhanShaCheng = class("XueZhanShaCheng", BaseSystemLogic)
ZhuanSheng = VarCfg.player.sZhuanSheng --玩家转生等级
KuangBao = VarCfg.player.sRage --玩家狂暴开启
WangZheZanZu = VarCfg.player.sWangZheZanZu --玩家王者等级
huoyue = VarCfg.player.nHuoyue --玩家活跃值
function X
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-05-21 16:39:53 |
👁️ 阅读: 0
我是lua后端老师!
小仙,看了你的代码,你已经在做血战沙城的功能了,思路很不错。要在晚上8点到9点强制开启攻城,我们可以利用引擎的定时器功能来实现。
首先,我们需要一个全局定时器,在每天晚上8点整触发。这个定时器应该在服务端启动时设置一次,之后每天自动运行。
根据你的代码,我们可以这样修改:
1. **在 `init` 函数中设置一个全局定时器**,用于在每天20:00(晚上8点)触发。
2. 在定时器触发时,检查当前是否在攻城战中,如果不在,则强制开启。
这里是一个修改建议,主要修改 `init` 函数,并新增一个处理开启攻城的函数:
```lua
function XueZhanShaCheng:init(actor)
self._cfg = Util:requireCfg(self.mCfg.severPath)
-- 原有的其他事件监听
EventManager:add(EventCfg.castleWarStart, handler(self, self.castleWarStart), self)
EventManager:add(EventCfg.castleWarEnd, handler(self, self.castleWarEnd), self)
EventManager:add(EventCfg.killplay, handler(self, self.killplay), self)
EventManager:add(EventCfg.playDie, handler(self, self.playDie), self)
EventManager:add(EventCfg.killMon, handler(self, self.killMon), self)
EventManager:add(EventCfg.onTimer7, handler(self, self.onTimer7), self)
EventManager:add(EventCfg.resetDay, handler(self, self.resetDay), self)
EventManager:add(EventCfg.enterMap, handler(self, self.enterMap), self)
-- 新增:在服务端启动时,设置一个全局定时器,用于每天20:00强制开启攻城
-- 注意:这个逻辑应该只在服务端启动时执行一次,可以放在一个特定的触发里,比如startup
-- 这里为了演示,假设你的系统有办法在初始化时判断是服务端启动
-- 更常见的做法是在 QFunction-0.lua 的 startup 触发里调用这个类的初始化
self:startDailySiegeTimer()
end
-- 启动每日攻城定时器 -- 每日攻城定时器
function XueZhanShaCheng:startDailySiegeTimer()
-- 计算当前时间到今晚20:00的秒数
local currentHour = tonumber(getconst("0", "<$Hour>"))
local currentMinute = tonumber(getconst("0", "<$MINUTE>"))
local currentSecond = tonumber(getconst("0", "<$SECOND>"))
local secondsUntil20 = 0
if currentHour < 20 then
-- 如果当前时间早于20:00,计算到今晚20:00的秒数
secondsUntil20 = (20 - currentHour - 1) * 3600 + (60 - currentMinute - 1) * 60 + (60 - currentSecond)
else
-- 如果当前时间晚于或等于20:00,计算到明天20:00的秒数
secondsUntil20 = (24 - currentHour + 20 - 1) * 3600 + (60 - currentMinute - 1) * 60 + (60 - currentSecond)
end
-- 设置一个一次性定时器,在今晚20:00触发
-- 使用全局定时器,触发器ID可以自定义,比如 100
setontimerex("0", 100, secondsUntil20, 0)
release_print("已设置攻城定时器,将在 " .. secondsUntil20 .. " 秒后(20:00)触发")
end
-- 处理每日攻城定时器触发 -- 定时开启攻城
function XueZhanShaCheng:onTimer100(sysObj)
-- 检查是否已经在攻城战中
if castleinfo(5) then
release_print("当前已在攻城战中,无需重复开启")
-- 设置明天的定时器
self:setNextDayTimer()
return
end
release_print("时间到20:00,强制开启攻城战")
-- 强制所有行会加入攻城列表
addtocastlewarlistex("*")
-- 使用GM命令强制开启攻城战
gmexecute("0", "ForcedWallConQuestwar")
-- 设置一个定时器,在21:00(1小时后)强制结束攻城
setontimerex("0", 101, 3600, 0) -- 3600秒 = 1小时
release_print("已设置攻城结束定时器,1小时后触发")
-- 设置明天的攻城定时器
self:setNextDayTimer()
end
-- 设置第二天的攻城定时器 -- 设置次日定时器
function XueZhanShaCheng:setNextDayTimer()
-- 24小时后(86400秒)再次触发
setontimerex("0", 100, 86400, 0)
release_print("已设置明日攻城定时器,24小时后触发")
end
-- 攻城结束定时器触发 -- 定时结束攻城
function XueZhanShaCheng:onTimer101(sysObj)
-- 检查是否在攻城战中
if castleinfo(5) then
release_print("时间到21:00,强制结束攻城战")
gmexecute("0", "ForcedWallConQuestwar")
else
release_print("当前不在攻城战中,无需结束")
end
end
```
**重要提醒:**
1. **定时器注册**:你需要在 `init` 函数中注册新增的定时器事件,就像你注册 `onTimer7` 一样:
```lua
EventManager:add(EventCfg.onTimer100, handler(self, self.onTimer100), self)
EventManager:add(EventCfg.onTimer101, handler(self, self.onTimer101), self)
```
2. **服务端启动触发**:`startDailySiegeTimer` 这个函数应该在服务端启动时调用一次。最可靠的方法是在 `QFunction-0.lua` 的 `startup` 触发中,初始化你的 `XueZhanShaCheng` 系统并调用这个方法。
3. **时间计算**:上面的时间计算逻辑可能需要根据你的实际需求微调,特别是处理跨天计算。
4. **引擎版本**:确保你的引擎版本支持 `setontimerex` 全局定时器。
这样修改后,你的攻城战就会在每天**晚上8点自动开启**,并在**晚上9点自动结束**,完美符合你的需求。
小仙,如果还有不清楚的地方,随时问我,或者请教浮生梦尊师也行!
← 返回问答库