backupSrv.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. --[[
  2. Author: neo
  3. Date: 2021-06-30 20:51:28
  4. LastEditTime: 2021-07-02 20:50:33
  5. LastEditors: Please set LastEditors
  6. Description: 玩家数据备份服务
  7. --]]
  8. local baseService = require("baseService")
  9. local timer = require "timer"
  10. local timeUtil = require "utils.timeUtil"
  11. local const = require "const.const"
  12. local backupRole = require "backupRole"
  13. local skynet = require "skynet"
  14. local activeUtil = require "utils.activeUtil"
  15. local util_user = require "utils.util_user"
  16. local lib_game_redis = require("lib_game_redis")
  17. local CMD = {}
  18. local DAYS_UNACTIVE = const.BACKUP_DAYS
  19. local function getSecsSwapLoginTime()
  20. local swapDays = DAYS_UNACTIVE - 5
  21. if swapDays < 7 then
  22. swapDays = 7
  23. end
  24. if IS_TEST then
  25. swapDays = 1
  26. end
  27. return swapDays * 24 * 3600
  28. end
  29. -- 备份活跃天玩家数据
  30. local function l_backup_active_day_players(day)
  31. log.info("l_backup_active_day_players key[%s]", tostring(day))
  32. local key = string.format("active:uid:%s", tostring(day))
  33. local uidList = lib_game_redis:smembers(key)
  34. if is_empty(uidList) then
  35. return
  36. end
  37. local secsSwapLoginTime = getSecsSwapLoginTime()
  38. local currTime = skynet_time()
  39. for _, uid in ipairs(uidList) do
  40. local role = backupRole.new(tonumber(uid))
  41. -- 是否已备份
  42. local isExist = role:isExist()
  43. if isExist then
  44. -- 最后登录时间
  45. local loginTime = role:getLoginTime()
  46. if loginTime and (loginTime == 0 or currTime > loginTime + secsSwapLoginTime) then
  47. role:backupToMysql()
  48. if not util_user:user_is_online_gate(uid) then
  49. role:delRedisData()
  50. end
  51. end
  52. skynet.sleep(2)
  53. end
  54. end
  55. lib_game_redis:del(key)
  56. end
  57. -- 活跃玩家备份
  58. local function l_backup_days_active()
  59. local backupTime = skynet_time() - DAYS_UNACTIVE * timeUtil.SEC_PER_DAY
  60. for i = 1, 10 do
  61. local day = timeUtil.toDate(backupTime)
  62. pcall(l_backup_active_day_players, day)
  63. backupTime = backupTime - timeUtil.SEC_PER_DAY
  64. end
  65. end
  66. -- 被活跃玩家备份
  67. local function l_backup_inactive_day_players()
  68. local currTime = skynet_time()
  69. local time = currTime - DAYS_UNACTIVE * 24 * 3600
  70. local uidList = activeUtil.get_inactive_uid_list(time)
  71. if uidList and #uidList > 0 then
  72. local secsSwapLoginTime = getSecsSwapLoginTime()
  73. for k, v in ipairs(uidList) do
  74. local role = backupRole.new(tonumber(v))
  75. -- 是否已备份
  76. local isExist = role:isExist()
  77. if isExist then
  78. -- 最后登录时间
  79. local loginTime = role:getLoginTime()
  80. if loginTime and (loginTime == 0 or currTime > loginTime + secsSwapLoginTime) then
  81. role:backupToMysql()
  82. if not util_user:user_is_online_gate(v) then
  83. role:delRedisData()
  84. end
  85. end
  86. skynet.sleep(2)
  87. end
  88. end
  89. activeUtil.del_inactive_uid_list(time)
  90. end
  91. end
  92. local function l_thread_day_time_out()
  93. -- 备份活跃天
  94. l_backup_days_active()
  95. -- 备份被动活跃玩家数据
  96. l_backup_inactive_day_players()
  97. end
  98. function CMD.onStart()
  99. -- 每天2点开始备份
  100. timer.timeOfDay(2, 0, 0, l_thread_day_time_out)
  101. end
  102. -- 载入玩家数据 - 被动载入
  103. function CMD.reload_inactive(uid)
  104. if uid == nil then
  105. return false
  106. end
  107. -- 玩家是否已载入
  108. local role = backupRole.new(tonumber(uid))
  109. -- 是否已备份
  110. local isExist = role:isExist()
  111. if isExist then
  112. return true
  113. end
  114. activeUtil.add_inactive_player(uid)
  115. return true
  116. end
  117. -- 载入玩家模块数据 - 被动载入
  118. function CMD.reload_player_module_info(uid, mdl)
  119. if uid == nil or mdl == nil then
  120. return false
  121. end
  122. -- 玩家是否已载入
  123. local role = backupRole.new(tonumber(uid))
  124. role:load_module_data(mdl)
  125. -- 是否已备份
  126. local isExist = role:isExist()
  127. if isExist then
  128. return true
  129. end
  130. activeUtil.add_inactive_player(uid)
  131. return true
  132. end
  133. -- 强制备份玩家数据
  134. function CMD.backup_player(uid)
  135. if uid == nil then
  136. return false
  137. end
  138. local role = backupRole.new(tonumber(uid))
  139. -- 是否已备份
  140. local isExist = role:isExist()
  141. if isExist then
  142. role:backupToMysql()
  143. if not util_user:user_is_online_gate(uid) then
  144. role:delRedisData()
  145. end
  146. end
  147. return true
  148. end
  149. baseService.start(CMD, ".backup", true)