| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- BiHu = {}
- local this = {}
- -- 庇护系统相关常量
- local BIHU_BUFF_ID = 104001 -- 庇护BUFF ID
- local BIHU_COST_TYPE = 10040001 -- 每小时消耗类型
- local BIHU_COST_PER_HOUR = 20 -- 每小时消耗数量
- local BIHU_TIME_START_HOUR = 23 -- 庇护开始时间(23点)
- local BIHU_TIME_END_HOUR = 8 -- 庇护结束时间(8点)
- local BIHU_DURATION = 3600 -- 持续时间3600秒(1小时)
- -- 检查当前时间是否在庇护时间段内(23:00-8:00)
- function BiHu.isBiHuTime()
- local h = TimeUtil.GetTodayHour()
- -- 23点到8点之间为庇护时间
- return h >= BIHU_TIME_START_HOUR or h < BIHU_TIME_END_HOUR
- end
- -- 判断玩家是否有庇护
- function BiHu.isBiHu(actor)
- local data = API.GetTable(actor, "BIHU_DATA_INFO") or {}
- return data.isActive == true
- end
- -- 处理庇护开启/关闭
- -- @param actor 玩家对象
- -- @param msgData 消息数据 {type: 0开启, 1关闭, 2续费, 3BOSS移除}
- function BiHu.HandleBiHu(actor, msgData)
- local type = msgData.type or 0 --0 开启,1 关闭,2 续费,3 BOSS移除
- local data = API.GetTable(actor, "BIHU_DATA_INFO") or {}
- local cfgNum = BIHU_COST_PER_HOUR --tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", 260005) or "20")
-
- -- 开启庇护(type == 0)
- if type == 0 then
- -- 如果已经有庇护,提示
- if data.isActive then
- tipinfo(actor, "庇护已开启,请勿重复开启")
- return false
- end
-
- -- 扣除首次钻石
- local result = removeitemfrombag(actor, BIHU_COST_TYPE, cfgNum, 0, 9999, '庇护消耗')
- if not result then
- tipinfo(actor, "钻石不足,无法开启庇护")
- return false
- end
-
- -- 检查时间
- if not BiHu.isBiHuTime() then
- tipinfo(actor, "当前时间不在庇护时间段内(23:00-8:00)")
- return false
- end
-
- -- 开启庇护
- local nowsec = getbaseinfo("nowsec")
- data.isActive = true
- data.startTime = nowsec
- data.lastChargeTime = nowsec
- data.totalCost = cfgNum
- data.chargeCount = 1 -- 扣费次数
-
- -- 强制和平模式
- setplayersetting(actor, 1, 1) -- 1 和平 5战盟
-
-
- API.SetTable(actor, "BIHU_DATA_INFO", data)
- BiHu.SendBiHuInfo(actor, data)
- buffer(actor, BIHU_BUFF_ID)
- -- tipinfo(actor, "庇护已开启!每小时消耗" .. cfgNum .. "钻石")
- jprint("=== 庇护开启 ===", actor, getbaseinfo(actor, "maptitle"), "时间:", data.startTime)
- return true
-
- -- 关闭庇护(type == 1)
- elseif type == 1 then
- if not data.isActive then
- tipinfo(actor, "当前未开启庇护")
- return false
- end
-
- BiHu.EndBiHu(actor, "手动关闭")
- return true
-
- -- 续费(type == 2)- 定时调用
- elseif type == 2 then
- if not data.isActive then
- return false
- end
-
- -- 扣除钻石
- local result = removeitemfrombag(actor, 10040001, cfgNum, 0, 9999, '庇护续费')
- if not result then
- BiHu.EndBiHu(actor, "钻石不足")
- return false
- end
-
- -- 更新数据
- data.lastChargeTime = getbaseinfo("nowsec")
- data.totalCost = (data.totalCost or 0) + cfgNum
- data.chargeCount = (data.chargeCount or 0) + 1
-
- API.SetTable(actor, "BIHU_DATA_INFO", data)
- BiHu.SendBiHuInfo(actor, data)
- jprint("=== 庇护续费 ===", actor, "第" .. data.chargeCount .. "次扣费", "总消耗:", data.totalCost)
- return true
-
- -- BOSS移除(type == 3)
- elseif type == 3 then
- if data.isActive then
- BiHu.EndBiHu(actor, "选定BOSS")
- end
- return true
- elseif type == -1 then
- -- 仅发送信息
- BiHu.SendBiHuInfo(actor, data)
- return true
- end
-
- return false
- end
- -- 结束庇护(内部调用)
- -- @param actor 玩家对象
- -- @param reason 结束原因
- function BiHu.EndBiHu(actor, reason)
- local data = API.GetTable(actor, "BIHU_DATA_INFO") or {}
-
- if not data.isActive then
- return false
- end
-
- -- 计算庇护时间和消耗
- local nowsec = getbaseinfo("nowsec")
- local totalSeconds = nowsec - (data.startTime or nowsec)
- local totalCost = data.totalCost or 0
- local chargeCount = data.chargeCount or 0
-
- -- 清除数据
- data = {}
- API.SetTable(actor, "BIHU_DATA_INFO", data)
- BiHu.SendBiHuInfo(actor,data)
- -- 发送结束提示
- local title = "庇护结束"
- local str = "%s 庇护系统已结束!\n总庇护时间:%s\n总消耗钻石:%d\n扣费次数:%d次"
- local content = string.format(str, reason, BiHu.formatSecondsToHourMinute(totalSeconds), totalCost, chargeCount)
- sendluamsg(actor, LuaMessageIdToClient.COMMON_TIPS_PANEL, {title = title, content = content})
-
- delbuff(actor, BIHU_BUFF_ID)
- jprint("=== 庇护结束 ===", actor, getbaseinfo(actor, "maptitle"), reason, totalSeconds, totalCost)
-
- return true
- end
- -- 定时检查庇护(每分钟调用一次)
- -- @param actor 玩家对象
- function BiHu.onHourCheck(actor)
- -- 检查是否有庇护
- local data = API.GetTable(actor, "BIHU_DATA_INFO") or {}
- if not data.isActive then
- return
- end
- -- 检查时间
- if not BiHu.isBiHuTime() then
- BiHu.EndBiHu(actor, "庇护时间结束")
- return
- end
- local nowsec = getbaseinfo("nowsec")
- local lastChargeTime = data.lastChargeTime or data.startTime or nowsec
- -- 计算从上次扣费到现在的秒数
- local elapsedSeconds = nowsec - lastChargeTime
- -- 判断当前一小时是否到期(>= 3600秒)且 不在安全区
- if elapsedSeconds >= BIHU_DURATION then
- if insafezone(actor) then
- BiHu.EndBiHu(actor, "安全区内不自动续费")
- else
- -- 条件满足,自动续费
- BiHu.HandleBiHu(actor, {type = 2})
- end
- end
- end
- -- 检查玩家是否可以切换模式
- -- @param actor 玩家对象
- -- @param newMode 新模式
- -- @return boolean 是否允许切换
- function BiHu.checkCanChangeMode(actor, newMode)
- -- 如果没有庇护,允许切换
- if not BiHu.isBiHu(actor) then
- -- 执行模式切换
- if newMode == 1 then
- setplayersetting(actor, 1, 1) -- 和平
- elseif newMode == 5 then
- setplayersetting(actor, 1, 5) -- 战盟
- else
- setplayersetting(actor, 1, newMode)
- end
- return true
- end
-
- -- 有庇护,提示并拒绝
- tipinfo(actor, "当前开启庇护状态,无法切换模式。请先关闭庇护系统。")
- return false
- end
- -- 发送庇护信息给客户端
- -- @param actor 玩家对象
- function BiHu.SendBiHuInfo(actor, data)
- data = data or API.GetTable(actor, "BIHU_DATA_INFO") or {}
- local msgData = {
- isActive = data.isActive or false,
- startTime = data.startTime or 0,
- lastChargeTime = data.lastChargeTime or 0,
-
- -- totalCost = data.totalCost or 0,
- -- chargeCount = data.chargeCount or 0,
- -- currentTime = getbaseinfo("nowsec"), -- 当前时间戳
- -- isInBiHuTime = BiHu.isBiHuTime(), -- 是否在庇护时间段
- -- costPerHour = tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", 260005) or "20")
- }
- if msgData.isActive then
- if not hasbuff(actor, BIHU_BUFF_ID) then
- buffer(actor, BIHU_BUFF_ID)
- end
- msgData.endTime = msgData.lastChargeTime + BIHU_DURATION -- 庇护结束时间
- end
- sendluamsg(actor, LuaMessageIdToClient.RES_BIHU_MsgID, msgData)
- end
- -- 秒数转换为"X时Y分Z秒"格式
- -- @param seconds number 秒数
- -- @return string 格式化时间字符串(如:"1时5分30秒"、"0时10分0秒")
- function BiHu.formatSecondsToHourMinute(seconds)
- if not seconds or seconds <= 0 then
- return "0时0分0秒"
- end
-
- local hours = math.floor(seconds / 3600)
- local minutes = math.floor((seconds % 3600) / 60)
- local remainingSeconds = seconds % 60
- return string.format("%d时%d分%d秒", hours, minutes, remainingSeconds)
- end
|