--[[ Author: neo Date: 2021-06-30 20:51:28 LastEditTime: 2021-07-02 20:50:33 LastEditors: Please set LastEditors Description: 玩家数据备份服务 --]] local baseService = require("baseService") local timer = require "timer" local timeUtil = require "utils.timeUtil" local const = require "const.const" local backupRole = require "backupRole" local skynet = require "skynet" local activeUtil = require "utils.activeUtil" local util_user = require "utils.util_user" local lib_game_redis = require("lib_game_redis") local CMD = {} local DAYS_UNACTIVE = const.BACKUP_DAYS local function getSecsSwapLoginTime() local swapDays = DAYS_UNACTIVE - 5 if swapDays < 7 then swapDays = 7 end if IS_TEST then swapDays = 1 end return swapDays * 24 * 3600 end -- 备份活跃天玩家数据 local function l_backup_active_day_players(day) log.info("l_backup_active_day_players key[%s]", tostring(day)) local key = string.format("active:uid:%s", tostring(day)) local uidList = lib_game_redis:smembers(key) if is_empty(uidList) then return end local secsSwapLoginTime = getSecsSwapLoginTime() local currTime = skynet_time() for _, uid in ipairs(uidList) do local role = backupRole.new(tonumber(uid)) -- 是否已备份 local isExist = role:isExist() if isExist then -- 最后登录时间 local loginTime = role:getLoginTime() if loginTime and (loginTime == 0 or currTime > loginTime + secsSwapLoginTime) then role:backupToMysql() if not util_user:user_is_online_gate(uid) then role:delRedisData() end end skynet.sleep(2) end end lib_game_redis:del(key) end -- 活跃玩家备份 local function l_backup_days_active() local backupTime = skynet_time() - DAYS_UNACTIVE * timeUtil.SEC_PER_DAY for i = 1, 10 do local day = timeUtil.toDate(backupTime) pcall(l_backup_active_day_players, day) backupTime = backupTime - timeUtil.SEC_PER_DAY end end -- 被活跃玩家备份 local function l_backup_inactive_day_players() local currTime = skynet_time() local time = currTime - DAYS_UNACTIVE * 24 * 3600 local uidList = activeUtil.get_inactive_uid_list(time) if uidList and #uidList > 0 then local secsSwapLoginTime = getSecsSwapLoginTime() for k, v in ipairs(uidList) do local role = backupRole.new(tonumber(v)) -- 是否已备份 local isExist = role:isExist() if isExist then -- 最后登录时间 local loginTime = role:getLoginTime() if loginTime and (loginTime == 0 or currTime > loginTime + secsSwapLoginTime) then role:backupToMysql() if not util_user:user_is_online_gate(v) then role:delRedisData() end end skynet.sleep(2) end end activeUtil.del_inactive_uid_list(time) end end local function l_thread_day_time_out() -- 备份活跃天 l_backup_days_active() -- 备份被动活跃玩家数据 l_backup_inactive_day_players() end function CMD.onStart() -- 每天2点开始备份 timer.timeOfDay(2, 0, 0, l_thread_day_time_out) end -- 载入玩家数据 - 被动载入 function CMD.reload_inactive(uid) if uid == nil then return false end -- 玩家是否已载入 local role = backupRole.new(tonumber(uid)) -- 是否已备份 local isExist = role:isExist() if isExist then return true end activeUtil.add_inactive_player(uid) return true end -- 载入玩家模块数据 - 被动载入 function CMD.reload_player_module_info(uid, mdl) if uid == nil or mdl == nil then return false end -- 玩家是否已载入 local role = backupRole.new(tonumber(uid)) role:load_module_data(mdl) -- 是否已备份 local isExist = role:isExist() if isExist then return true end activeUtil.add_inactive_player(uid) return true end -- 强制备份玩家数据 function CMD.backup_player(uid) if uid == nil then return false end local role = backupRole.new(tonumber(uid)) -- 是否已备份 local isExist = role:isExist() if isExist then role:backupToMysql() if not util_user:user_is_online_gate(uid) then role:delRedisData() end end return true end baseService.start(CMD, ".backup", true)