OfflineOnHook.lua 17 KB

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