backupRole.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. local dataMode = require "dataMode"
  2. local lib_game_redis = require("lib_game_redis")
  3. local redisUtil = require("utils.redisUtil")
  4. local root = class("backupRole")
  5. -- 玩家key
  6. local function l_user_get_module_key(uid, mdl)
  7. if uid == nil or mdl == nil then
  8. return
  9. end
  10. return string.format("mdl:%s:%s", tostring(mdl), tostring(uid))
  11. end
  12. -- 角色构造
  13. function root:ctor(uid)
  14. local mapClass = dataMode.get_module_class_map()
  15. self.uid = uid
  16. self.moduleList = {}
  17. for _, moduleClass in pairs(mapClass) do
  18. local cname = moduleClass.__cname
  19. assert(self[cname] == nil)
  20. local moduleObj = moduleClass.new(uid)
  21. if moduleObj.isPersonal then
  22. self[cname] = moduleObj
  23. table.insert(self.moduleList, cname)
  24. end
  25. end
  26. end
  27. function root:isExist()
  28. if not self.user then
  29. return false
  30. end
  31. local key = l_user_get_module_key(self.uid, "user")
  32. return lib_game_redis:exists(key)
  33. end
  34. function root:getLoginTime()
  35. if not self.user then
  36. return
  37. end
  38. local key = l_user_get_module_key(self.uid, "user")
  39. return redisUtil.hget_int(key, "loginTime")
  40. end
  41. function root:backupToMysql()
  42. for _, cname in pairs(self.moduleList) do
  43. local moduleObj = self[cname]
  44. if moduleObj.isSaveDB then
  45. moduleObj:backup_to_db()
  46. end
  47. end
  48. end
  49. -- 重新载入模块数据
  50. function root:load_module_data(tabName)
  51. for _, cname in pairs(self.moduleList) do
  52. local moduleObj = self[cname]
  53. if moduleObj.tabName == tabName then
  54. moduleObj:get_data_from_db()
  55. end
  56. end
  57. end
  58. -- 删除玩家模块热数据
  59. function root:delRedisData()
  60. for _, cname in pairs(self.moduleList) do
  61. local moduleObj = self[cname]
  62. moduleObj:del_hot_data()
  63. end
  64. end
  65. return root