module.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. --[[
  2. Descripttion:玩家数据,不能引用非干净模块
  3. version:
  4. Author: Neo,Huang
  5. Date: 2021-09-06 20:27:20
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2021-09-06 20:27:42
  8. --]]
  9. local lib_game_redis = require("lib_game_redis")
  10. local root = {}
  11. ----------------------------------------
  12. -- 实时数据
  13. ----------------------------------------
  14. -- 玩家key
  15. local function l_user_get_module_key(uid, mdl)
  16. if uid == nil or mdl == nil then
  17. return
  18. end
  19. return string.format("mdl:%s:%s", tostring(mdl), tostring(uid))
  20. end
  21. -- 同步玩家模块数据
  22. local function l_reload_user_module_info(uid, mdl)
  23. if uid == nil or mdl == nil then
  24. return false
  25. end
  26. -- 机器人
  27. if is_robot(uid) then
  28. return false
  29. end
  30. -- 玩家模块数据是否已存在
  31. local isExist = lib_game_redis:exists(l_user_get_module_key(uid, mdl))
  32. -- log.info("l_reload_user_module_info uid[%s] mdl[%s] exist[%s]", tostring(uid), tostring(mdl), tostring(exist))
  33. if not isExist then
  34. -- 再检查一次
  35. local val = lib_game_redis:hget(l_user_get_module_key(uid, "user"), "uid")
  36. if not is_empty(val) then
  37. return true
  38. end
  39. -- TODO:从数据库载入玩家数据
  40. return true
  41. else
  42. return true
  43. end
  44. return false
  45. end
  46. -- 删除玩家信息
  47. function root:hdel(uid, mdl, key)
  48. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  49. return
  50. end
  51. return lib_game_redis:hdel(l_user_get_module_key(uid, mdl), key)
  52. end
  53. -- 设置玩家信息
  54. function root:hset(uid, mdl, key, info)
  55. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  56. return false
  57. end
  58. if is_nil(info) then
  59. return self:hdel(uid, mdl, key)
  60. end
  61. local val = info
  62. if type(info) == "table" then
  63. val = cjson_encode(info)
  64. end
  65. return lib_game_redis:hset(l_user_get_module_key(uid, mdl), key, val)
  66. end
  67. -- 获取玩家信息
  68. function root:hget(uid, mdl, key)
  69. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  70. return
  71. end
  72. local val = lib_game_redis:hget(l_user_get_module_key(uid, mdl), key)
  73. if is_nil(val) then
  74. local ok = l_reload_user_module_info(uid, mdl)
  75. if ok then
  76. val = lib_game_redis:hget(l_user_get_module_key(uid, mdl), key)
  77. end
  78. end
  79. return val
  80. end
  81. -- 获取玩家信息
  82. function root:hget_int(uid, mdl, key)
  83. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  84. return
  85. end
  86. local val = self:hget(uid, mdl, key)
  87. if not is_nil(val) then
  88. return tonumber(val)
  89. end
  90. return 0
  91. end
  92. -- 获取玩家信息
  93. function root:hget_json(uid, mdl, key)
  94. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  95. return
  96. end
  97. local val = self:hget(uid, mdl, key)
  98. local info = nil
  99. if not is_empty(val) then
  100. info = cjson_decode(val)
  101. else
  102. info = {}
  103. end
  104. return info
  105. end
  106. function root:hincrby(uid, mdl, key, val)
  107. if is_nil(uid) or is_nil(mdl) or is_nil(key) then
  108. return
  109. end
  110. return lib_game_redis:hincrby(get_player_key(uid, mdl), key, val or 1)
  111. end
  112. function root:model_func(func, mdl, key, defaultValue)
  113. local resFunc = function(root, uid)
  114. if func then
  115. local val = func(root, uid, mdl, key)
  116. if is_empty(val) and not defaultValue then
  117. val = defaultValue
  118. end
  119. return val
  120. end
  121. end
  122. return resFunc
  123. end
  124. -- 常用数据方法
  125. root.get_nickname = root:model_func(root.hget, "user", "nickname")
  126. root.get_channel = root:model_func(root.hget_int, "user", "channel")
  127. return root