baseModule.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. --[[
  2. Descripttion:基础模块类
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-04 19:07:54
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-04 19:13:19
  8. --]]
  9. local lib_game_redis = require("lib_game_redis")
  10. local lib_game_mysql = require("lib_game_mysql")
  11. local redisUtil = require("utils.redisUtil")
  12. local sqlUtil = require("utils.sqlUtil")
  13. local root = class("base")
  14. function root:ctor(uid, mdlName, keyName, isSaveDB, isPersonal)
  15. self.uid = uid
  16. self.mdlName = mdlName
  17. self.keyName = keyName
  18. self.isSaveDB = isSaveDB
  19. self.isPersonal = isPersonal or true
  20. end
  21. -- 模块sql属性
  22. function root:mysql_get_init_columns()
  23. return {}
  24. end
  25. -- 初始化模块表
  26. function root:mysql_get_table_info()
  27. return {
  28. mdlName = self.mdlName,
  29. keyName = self.keyName,
  30. isSaveDB = self.isSaveDB,
  31. columnNameOptions = self:mysql_get_init_columns()
  32. }
  33. end
  34. function root:getKeyValue()
  35. return self[self.keyName]
  36. end
  37. -- 获取模块key
  38. function root:get_module_redis_key()
  39. local moduleKey = string.format("mdl:%s:%s", self.mdlName, tostring(self:getKeyValue()))
  40. return moduleKey
  41. end
  42. -- 获取玩家模块数据
  43. function root:get_data_from_db()
  44. local moduleKey = self:get_module_redis_key()
  45. local res = redisUtil.hgetall(moduleKey)
  46. if not is_empty(res) then
  47. return res
  48. end
  49. if self.isSaveDB then
  50. local sql = sqlUtil.select_table_by_key(self.mdlName, self.keyName, self:getKeyValue())
  51. res = lib_game_mysql:query(sql)
  52. if res and res[1] then
  53. -- add to redis
  54. redisUtil.hmset(moduleKey, res[1])
  55. -- get again
  56. res = redisUtil.hgetall(moduleKey)
  57. if not is_empty(res) then
  58. return res
  59. end
  60. end
  61. return res
  62. end
  63. end
  64. -- 字段类型 - json
  65. function root:is_json(column)
  66. return string.match(string.upper(column), "JSON")
  67. end
  68. -- 字段类型 - int
  69. function root:is_number(column)
  70. return string.match(string.upper(column), "INT")
  71. end
  72. -- 字段值
  73. function root:get_column_db_value(column)
  74. if is_empty(column) then
  75. return
  76. end
  77. local key = string.format("mdl:%s:%s", tostring(self.mdlName), tostring(self.uid))
  78. local val = lib_game_redis:hget(key, column)
  79. if self:is_json(column) then
  80. val = string.format("'%s'", cjson_encode(val))
  81. elseif self:is_number(column) then
  82. val = tonumber(val or 0)
  83. else
  84. val = string.format("'%s'", val)
  85. end
  86. return val
  87. end
  88. -- 备份模块数据
  89. function root:backup_to_db()
  90. local options = self:mysql_get_init_columns()
  91. if is_empty(options) then
  92. return false
  93. end
  94. local colums = {}
  95. local values = {}
  96. for k, v in ipairs(options) do
  97. table.insert(colums, k)
  98. table.include(values, self:get_column_db_value(k))
  99. end
  100. local tbName = string.format("mdl_%s", tostring(self.mdlName))
  101. local sql =
  102. string.format("replace into %s (%s) value (%s);", tbName, table.concat(colums, ","), table.concat(values, ","))
  103. lib_game_mysql:query(sql)
  104. end
  105. -- 删除模块热数据
  106. function root:del_hot_data()
  107. local key = self:get_module_redis_key()
  108. local ret = lib_game_redis:del(key)
  109. log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
  110. end
  111. ----------------------------------------
  112. -- 实时数据
  113. ----------------------------------------
  114. local moduleData = require("data.module")
  115. -- 获取数据类型
  116. function root:get_sub_key_type(subKey)
  117. if subKey == nil then
  118. return
  119. end
  120. local mpOptions = self:mysql_get_init_columns()
  121. for k, v in pairs(mpOptions) do
  122. if tostring(k) == tostring(subKey) then
  123. if self:is_number(v) then
  124. return "int"
  125. end
  126. if self:is_json(v) then
  127. return "json"
  128. end
  129. return "string"
  130. end
  131. end
  132. end
  133. -- 获取数据
  134. function root:redis_get_key_info(subKey, uid)
  135. if subKey == nil then
  136. return
  137. end
  138. uid = uid or self.uid
  139. local info = moduleData:hget(uid, self.mdlName, subKey)
  140. local tyKey = self:get_sub_key_type(subKey)
  141. if tyKey then
  142. if tyKey == "json" then
  143. if info then
  144. info = cjson_decode(info)
  145. end
  146. -- 主要针对C++中的NULL
  147. if is_empty(info) then
  148. info = {}
  149. end
  150. end
  151. if tyKey == "int" then
  152. info = tonumber(info or 0)
  153. end
  154. end
  155. return info
  156. end
  157. -- 设置数据
  158. function root:redis_update_key_info(subKey, info, uid)
  159. uid = uid or self.uid
  160. return moduleData:hset(uid, self.mdlName, subKey, info)
  161. end
  162. -- +1
  163. function root:redis_hincrby(subKey, uid)
  164. uid = uid or self.uid
  165. return moduleData:hincrby(uid, self.mdlName, subKey)
  166. end
  167. return root