OfflineOnHook.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. --- 离线挂机
  2. local this = {}
  3. onHook = {}
  4. --- 获取离线挂机信息
  5. ---@param actor 玩家对象
  6. function onHook.getOffLineOnHookInfo(actor)
  7. local onHookInfo = getonhookinfo(actor)
  8. local duration = tonumber(onHookInfo["duration"])
  9. local fightExp = getplaydef(actor, PlayerDefKey.offline.FIGHT_EXP)
  10. if string.isNullOrEmpty(fightExp) then
  11. fightExp = 0
  12. end
  13. local offlineStartTime = getplaydef(actor, PlayerDefKey.offline.START_TIME)
  14. local offlineEndTime = getplaydef(actor, PlayerDefKey.offline.END_TIME)
  15. if not string.isNullOrEmpty(offlineEndTime) then
  16. duration = math.round((offlineEndTime - offlineStartTime) / 1000)
  17. end
  18. local offlineTimeout =
  19. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_TIMEOUT)
  20. local timeout = duration > offlineTimeout * 60
  21. duration = timeout and offlineTimeout * 60 or duration
  22. local secondDiff = this.calcBubblePointTime(actor, offlineTimeout)
  23. -- 泡点经验
  24. local bubblePointExp = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP)
  25. if string.isNullOrEmpty(bubblePointExp) then
  26. bubblePointExp = 0
  27. end
  28. onHookInfo["freeExp"] = tostring(bubblePointExp)
  29. -- 泡点时长与总离线挂机时长
  30. onHookInfo["freeDuration"] = math.round(secondDiff)
  31. onHookInfo["duration"] = math.round(duration)
  32. onHookInfo["fightDuration"] = math.round(duration - secondDiff)
  33. onHookInfo["fightExp"] = tostring(fightExp)
  34. local isReceive = getplaydef(actor, PlayerDefKey.offline.IS_RECEIVE)
  35. if string.isNullOrEmpty(isReceive) then
  36. isReceive = false
  37. end
  38. onHookInfo["receiveExp"] = isReceive
  39. local totalExp = getplaydef(actor, PlayerDefKey.offline.TOTAL_EXP)
  40. if string.isNullOrEmpty(totalExp) then
  41. totalExp = 0
  42. end
  43. local expLimit =
  44. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_EXP_LIMIT)
  45. local total = totalExp + bubblePointExp + fightExp
  46. if expLimit and tonumber(expLimit) < total then
  47. total = tonumber(expLimit)
  48. end
  49. onHookInfo["totalExp"] = tostring(total)
  50. local openBubbleTime = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START)
  51. local mapId = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_MAP)
  52. if not string.isNullOrEmpty(openBubbleTime) and tonumber(openBubbleTime) >= tonumber(offlineStartTime) then
  53. local temp = {}
  54. temp[openBubbleTime] = mapId
  55. onHookInfo["openBubbleMap"] = temp
  56. end
  57. -- 如果后端服务重启,则需要重新计算挂机超时时间点
  58. if timeout then
  59. onHookInfo["timeout"] = offlineStartTime + offlineTimeout * TimeUnit.MINUTE * TimeUnit.MILLISECOND
  60. end
  61. sendluamsg(actor, LuaMessageIdToClient.RES_OFFLINE_ON_HOOK_INFO, onHookInfo)
  62. end
  63. --- 请求领取离线挂机经验
  64. ---@param actor 玩家对象
  65. function onHook.reqReceiveOfflineOnHookExp(actor)
  66. -- 领取过离线挂机经验直接返回
  67. if getplaydef(actor, PlayerDefKey.offline.IS_RECEIVE) then
  68. return
  69. end
  70. -- 计算角色泡点经验
  71. local bubblePointExp = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP)
  72. if string.isNullOrEmpty(bubblePointExp) then
  73. bubblePointExp = 0
  74. end
  75. local fightExp = getplaydef(actor, PlayerDefKey.offline.FIGHT_EXP)
  76. if string.isNullOrEmpty(fightExp) then
  77. fightExp = 0
  78. end
  79. local totalExp = getplaydef(actor, PlayerDefKey.offline.TOTAL_EXP)
  80. if string.isNullOrEmpty(totalExp) then
  81. totalExp = 0
  82. end
  83. local exp = bubblePointExp + fightExp + totalExp
  84. local expLimit =
  85. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_EXP_LIMIT)
  86. exp = exp > tonumber(expLimit) and tonumber(expLimit) or exp
  87. Bag.addItemToBag(actor, ItemConfigId.EXP, exp, 0, 9999, "离线挂机")
  88. setplaydef(actor, PlayerDefKey.offline.IS_RECEIVE, true)
  89. end
  90. --- 角色登录存储泡点结束时间与离线挂机结束时间
  91. ---@param actor 玩家对象
  92. function onHook.login(actor)
  93. onHook.setOfflineState(actor, 0)
  94. local offlineTimeout =
  95. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_TIMEOUT)
  96. local offlineStartTime = getplaydef(actor, PlayerDefKey.offline.START_TIME)
  97. local offlineBubblePointStart = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START)
  98. local serverStart = getsysvar(SystemVarConst.SERVER_START)
  99. -- 角色离线挂机开始时间小于服务器最新启动时间则表示玩家离线挂机期间服务器重启过
  100. local now = getbaseinfo(actor, "now")
  101. local fightExp = getplaydef(actor, PlayerDefKey.offline.FIGHT_EXP)
  102. local isFirstLogin = getplaydef(actor, "T$_isResetReceiveFlag")
  103. if offlineStartTime and serverStart and tonumber(serverStart) > tonumber(offlineStartTime) and not isFirstLogin then
  104. -- 设置已经重置过领取经验标识
  105. setplaydef(actor, "T$_isResetReceiveFlag", 1)
  106. -- 设置领取经验为false,防止服务器重启领取经验标识为true,导致玩家无法领取离线挂机经验
  107. setplaydef(actor, PlayerDefKey.offline.IS_RECEIVE, false)
  108. -- 判断当前角色是否在安全区内,如果不在安全区,根据配表计算战斗经验
  109. if string.isNullOrEmpty(offlineBubblePointStart) then
  110. local lastTime = getplaydef(actor, PlayerDefKey.offline.LAST_TIME)
  111. local fightTime = tonumber(now) - tonumber(lastTime)
  112. local timeoutMillisecond = tonumber(offlineTimeout * TimeUnit.MINUTE * TimeUnit.MILLISECOND)
  113. fightTime = fightTime > timeoutMillisecond and timeoutMillisecond or fightTime
  114. local mapId = getbaseinfo(actor, "map")
  115. local patrolState = getplaydef(actor, PlayerDefKey.offline.PATROL_STATE)
  116. local monsterState = patrolState and 2 or 1
  117. local expConfig =
  118. ConfigDataManager.getTableValue(
  119. "cfg_hangupReboot",
  120. "monstergroup",
  121. "mapid",
  122. mapId,
  123. "monstername",
  124. monsterState
  125. )
  126. if not string.isNullOrEmpty(expConfig) then
  127. local second = string.split(expConfig, "#")[1]
  128. local exp = string.split(expConfig, "#")[2]
  129. local step = math.round((fightTime / 1000) / second)
  130. if step > 0 then
  131. local baseExp = fightExp and tonumber(fightExp) or 0
  132. setplaydef(actor, PlayerDefKey.offline.FIGHT_EXP, baseExp + (tonumber(exp) * step))
  133. end
  134. end
  135. end
  136. end
  137. if offlineBubblePointStart then
  138. local offlineBubblePointEnd = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_END)
  139. if string.isNullOrEmpty(offlineBubblePointEnd) and this.checkBubblePointArea(actor) then
  140. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_END, now)
  141. -- 将离线泡点经验持久化,如果上次离线的泡点经验没有领取则累加
  142. local onHookInfo = getonhookinfo(actor)
  143. if table.isNullOrEmpty(onHookInfo) then
  144. return
  145. end
  146. local bubblePointExp = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP)
  147. local exp = this.calcBubblePointExp(actor, this.calcBubblePointTime(actor, offlineTimeout))
  148. if string.isNullOrEmpty(bubblePointExp) then
  149. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP, exp)
  150. else
  151. local bubbleExp = tonumber(bubblePointExp) + exp
  152. if this.checkExpLimit(actor, fightExp, bubbleExp) then
  153. local expLimit =
  154. ConfigDataManager.getTableValue(
  155. "cfg_global",
  156. "value",
  157. "id",
  158. GlobalConfigId.OFFLINE_ON_HOOK_EXP_LIMIT
  159. )
  160. local totalExp = getplaydef(actor, PlayerDefKey.offline.TOTAL_EXP)
  161. bubbleExp = tonumber(expLimit) - fightExp - totalExp
  162. end
  163. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP, bubbleExp)
  164. end
  165. end
  166. end
  167. if offlineStartTime then
  168. local offlineEndTime = getplaydef(actor, PlayerDefKey.offline.END_TIME)
  169. if string.isNullOrEmpty(offlineEndTime) then
  170. setplaydef(actor, PlayerDefKey.offline.END_TIME, now)
  171. end
  172. end
  173. end
  174. --- 角色退出清空泡点相关信息
  175. ---@param actor 玩家对象
  176. function onHook.logout(actor)
  177. -- 角色重置过领取经验标识重置
  178. setplaydef(actor, "T$_isResetReceiveFlag", nil)
  179. -- 重置结束时间
  180. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_END, nil)
  181. setplaydef(actor, PlayerDefKey.offline.END_TIME, nil)
  182. -- 取消离线后自动打怪,将挂机状态改为手动状态
  183. -- local mapId = getbaseinfo(actor, "mapid")
  184. -- local rebirthPointStr = ConfigDataManager.getTableValue("cfg_map_info", "rebirthPoint", "id", mapId)
  185. -- local rebirthPointArr = string.split(rebirthPointStr, "#")
  186. -- local pointX = rebirthPointArr[1] - math.floor(rebirthPointArr[3] / 2) + math.random(0, rebirthPointArr[3])
  187. -- local pointY = rebirthPointArr[2] - math.floor(rebirthPointArr[3] / 2) + math.random(0, rebirthPointArr[3])
  188. -- onhookstop(actor, mapId, pointX, pointY)
  189. -- onhookstop(actor, 1001, 0, 0)
  190. -- 领取过经验后重置存储的经验信息
  191. if getplaydef(actor, PlayerDefKey.offline.IS_RECEIVE) then
  192. setplaydef(actor, PlayerDefKey.offline.IS_RECEIVE, false)
  193. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP, nil)
  194. setplaydef(actor, PlayerDefKey.offline.FIGHT_EXP, nil)
  195. setplaydef(actor, PlayerDefKey.offline.TOTAL_EXP, nil)
  196. else
  197. local totalExp = getplaydef(actor, PlayerDefKey.offline.TOTAL_EXP)
  198. if string.isNullOrEmpty(totalExp) then
  199. totalExp = 0
  200. end
  201. local bubblePointExp = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP)
  202. if not string.isNullOrEmpty(bubblePointExp) then
  203. totalExp = totalExp + bubblePointExp
  204. end
  205. local fightExp = getplaydef(actor, PlayerDefKey.offline.FIGHT_EXP)
  206. if not string.isNullOrEmpty(fightExp) then
  207. totalExp = totalExp + fightExp
  208. end
  209. local expLimit =
  210. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_EXP_LIMIT)
  211. if expLimit and totalExp > tonumber(expLimit) then
  212. totalExp = tonumber(expLimit)
  213. end
  214. setplaydef(actor, PlayerDefKey.offline.TOTAL_EXP, totalExp)
  215. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_EXP, nil)
  216. setplaydef(actor, PlayerDefKey.offline.FIGHT_EXP, nil)
  217. end
  218. local level = tonumber(getbaseinfo(actor, "level"))
  219. -- 是否达到开启离线挂机的等级
  220. local offlineOnHookLevel =
  221. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_LEVEL)
  222. if level >= tonumber(offlineOnHookLevel) then
  223. -- 记录离线挂机开启时间
  224. setplaydef(actor, PlayerDefKey.offline.START_TIME, getbaseinfo(actor, "now"))
  225. if this.checkBubblePointArea(actor) then
  226. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_MAP, getbaseinfo(actor, "map"))
  227. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START, getbaseinfo(actor, "now"))
  228. else
  229. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_MAP, nil)
  230. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START, nil)
  231. end
  232. end
  233. end
  234. --- 玩家传送后判断是否进入泡点区域
  235. ---@param actor 玩家对象
  236. function onHook.afterTransmit(actor)
  237. -- 非离线挂机玩家不处理
  238. if not isofflineplay(actor) then
  239. return
  240. end
  241. -- 是否达到开启离线挂机的等级,离线挂机传送只可能是免费复活后传送,所以此处不需要判断区域是否为泡点区域
  242. local offlineOnHookLevel =
  243. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_LEVEL)
  244. if tonumber(getbaseinfo(actor, "level")) >= tonumber(offlineOnHookLevel) then
  245. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_MAP, getbaseinfo(actor, "map"))
  246. setplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START, getbaseinfo(actor, "now"))
  247. end
  248. end
  249. --- 检查区域是否是泡点区域
  250. ---@param actor 玩家对象
  251. ---@return boolean 是否在泡点区域
  252. function this.checkBubblePointArea(actor)
  253. local level = tonumber(getbaseinfo(actor, "level"))
  254. local mapId = getbaseinfo(actor, "map")
  255. local safeArea = getbaseinfo(actor, "safearea")
  256. local configString = ConfigDataManager.getTableValue("cfg_bubble_point", "expMap", "id", level)
  257. local configMap = string.toStringStringMap(configString, "#", "|")
  258. local checkArea = false
  259. for key, value in pairs(configMap) do
  260. mapId = tonumber(mapId)
  261. key = tonumber(key)
  262. value = tonumber(value)
  263. if mapId == key then
  264. if value == 3 then
  265. checkArea = true
  266. elseif value == 2 and safeArea == false then
  267. checkArea = true
  268. elseif value == 1 and safeArea == true then
  269. checkArea = true
  270. end
  271. end
  272. end
  273. return checkArea
  274. end
  275. --- 计算泡点时长
  276. ---@param actor 玩家对象
  277. ---@param duration 离线挂机战斗时长
  278. ---@return number 泡点时长(s)
  279. function this.calcBubblePointTime(actor, offlineTimeout)
  280. local offlineBubblePointStart = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_START)
  281. local offlineBubblePointEnd = getplaydef(actor, PlayerDefKey.offline.BUBBLE_POINT_END)
  282. if
  283. not string.isNullOrEmpty(offlineBubblePointStart) and not string.isNullOrEmpty(offlineBubblePointEnd) and
  284. tonumber(offlineBubblePointEnd) - tonumber(offlineBubblePointStart) > 0
  285. then
  286. local secondDiff = math.round((tonumber(offlineBubblePointEnd) - tonumber(offlineBubblePointStart)) / 1000)
  287. -- 泡点时长处理
  288. if secondDiff > tonumber(offlineTimeout) * 60 then
  289. secondDiff = tonumber(offlineTimeout) * 60
  290. local offlineStartTime = getplaydef(actor, PlayerDefKey.offline.START_TIME)
  291. local diff = (offlineBubblePointStart - offlineStartTime) / 1000
  292. if offlineBubblePointStart > offlineStartTime then
  293. if diff > offlineTimeout * 60 then
  294. secondDiff = 0
  295. else
  296. secondDiff = offlineTimeout * 60 - diff
  297. end
  298. end
  299. end
  300. return secondDiff
  301. end
  302. return 0
  303. end
  304. --- 根据泡点时长获取泡点经验
  305. ---@param actor 玩家对象
  306. ---@param secondDiff 泡点时长(s)
  307. ---@return number 泡点经验
  308. function this.calcBubblePointExp(actor, secondDiff)
  309. if secondDiff == 0 then
  310. return 0
  311. end
  312. local level = tonumber(getbaseinfo(actor, "level"))
  313. local expInterval = ConfigDataManager.getTableValue("cfg_bubble_point", "expInterval", "id", level)
  314. if string.isNullOrEmpty(expInterval) then
  315. gameDebug.print("找不到cfg_bubble_point配置,id:" .. level)
  316. return 0, 0
  317. end
  318. local times = math.round(secondDiff / expInterval)
  319. local expConfig = ConfigDataManager.getTableValue("cfg_bubble_point", "exp", "id", level)
  320. local exp = string.split(expConfig, "#")[2]
  321. return tonumber(exp) * tonumber(times)
  322. end
  323. --- 检查离线挂机经验是否超限
  324. ---@param fightExp number 战斗经验
  325. ---@param bubblePointExp number 泡点经验
  326. function this.checkExpLimit(actor, fightExp, bubblePointExp)
  327. local expLimit =
  328. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_EXP_LIMIT)
  329. if not string.isNullOrEmpty(expLimit) then
  330. local totalExp = getplaydef(actor, PlayerDefKey.offline.TOTAL_EXP)
  331. totalExp = totalExp or 0
  332. fightExp = fightExp or 0
  333. bubblePointExp = bubblePointExp or 0
  334. return fightExp + bubblePointExp + totalExp > tonumber(expLimit)
  335. end
  336. return false
  337. end
  338. --- 保存离线挂机战斗经验
  339. ---@param actor 玩家对象
  340. ---@param exp 战斗经验
  341. function onHook.saveOfflineFightExp(actor, exp)
  342. -- 是否超时判断
  343. local start = getplaydef(actor, PlayerDefKey.offline.START_TIME)
  344. local offlineTimeout =
  345. ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.OFFLINE_ON_HOOK_TIMEOUT)
  346. local now = getbaseinfo(actor, "now")
  347. if now - start > tonumber(offlineTimeout) * TimeUnit.MINUTE * TimeUnit.MILLISECOND then
  348. return
  349. end
  350. local offlineFightExp = getplaydef(actor, PlayerDefKey.offline.FIGHT_EXP)
  351. if string.isNullOrEmpty(offlineFightExp) then
  352. setplaydef(actor, PlayerDefKey.offline.FIGHT_EXP, tonumber(exp))
  353. else
  354. if not this.checkExpLimit(actor, offlineFightExp, 0) then
  355. setplaydef(actor, PlayerDefKey.offline.FIGHT_EXP, tonumber(offlineFightExp) + tonumber(exp))
  356. end
  357. end
  358. end
  359. --- 设置离线挂机巡逻状态
  360. ---@param actor table 玩家对象
  361. ---@param state number 状态 巡逻状态:1 战斗状态:2 无状态:0
  362. function onHook.setOfflineState(actor, state)
  363. setofflinepatrolstate(actor, state)
  364. setplaydef(actor, PlayerDefKey.offline.PATROL_STATE, state)
  365. end
  366. --- 记录玩家最后一次离线挂机时间
  367. ---@param actor table 玩家对象
  368. function onHook.recordLastTime(actor)
  369. setplaydef(actor, PlayerDefKey.offline.LAST_TIME, getbaseinfo(actor, "now"))
  370. end
  371. --- 记录服务器启动时间
  372. function onHook.recordStartTime()
  373. setsysvar(SystemVarConst.SERVER_START, getbaseinfo("now"))
  374. end
  375. --- 进入泡点地图设置离线挂机状态
  376. function onHook.enterMap(actor)
  377. if this.checkBubblePointArea(actor) then
  378. setofflinepatrolstate(actor, 0)
  379. else
  380. -- 不是泡点地图设置为战斗状态
  381. setofflinepatrolstate(actor, 2)
  382. end
  383. end