baseModule.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_table_name()
  44. return string.format("mdl_%s", tostring(self.mdlName))
  45. end
  46. -- 获取玩家模块数据
  47. function root:get_data_from_db()
  48. local moduleKey = self:get_module_redis_key()
  49. local res = redisUtil.hgetall(moduleKey)
  50. if not is_empty(res) then
  51. return res
  52. end
  53. if self.isSaveDB then
  54. local sql = sqlUtil.select_table_by_key(self:get_table_name(), self.keyName, self:getKeyValue())
  55. res = lib_game_mysql:query(sql)
  56. log.info("get_data_from_db sql[%s] res[%s]", tostring(sql), tostring(res))
  57. if res and res[1] then
  58. -- add to redis
  59. redisUtil.hmset(moduleKey, res[1])
  60. -- get again
  61. res = redisUtil.hgetall(moduleKey)
  62. if not is_empty(res) then
  63. return res
  64. end
  65. end
  66. return res
  67. end
  68. end
  69. -- 字段类型 - json
  70. function root:is_json(column)
  71. return string.match(string.upper(column), "JSON")
  72. end
  73. -- 字段类型 - int
  74. function root:is_number(column)
  75. return string.match(string.upper(column), "INT")
  76. end
  77. -- 字段值
  78. function root:get_column_db_value(column)
  79. if is_empty(column) then
  80. return
  81. end
  82. local key = self:get_module_redis_key()
  83. local val = lib_game_redis:hget(key, column)
  84. if self:is_json(column) then
  85. val = string.format("'%s'", cjson_encode(val))
  86. elseif self:is_number(column) then
  87. val = tonumber(val or 0)
  88. else
  89. val = string.format("'%s'", val)
  90. end
  91. return val
  92. end
  93. -- 备份模块数据
  94. function root:backup_to_db()
  95. local options = self:mysql_get_init_columns()
  96. if is_empty(options) then
  97. return false
  98. end
  99. local colums = {}
  100. local values = {}
  101. for k, v in pairs(options) do
  102. table.insert(colums, k)
  103. table.insert(values, self:get_column_db_value(k))
  104. end
  105. local tbName = self:get_table_name()
  106. local sql =
  107. string.format("replace into %s (%s) value (%s);", tbName, table.concat(colums, ","), table.concat(values, ","))
  108. local ret = lib_game_mysql:query(sql)
  109. if not is_empty(ret) then
  110. log.info("backup_to_db sql[%s] ret[%s]", tostring(sql), tostring(ret))
  111. end
  112. return ret
  113. end
  114. -- 删除模块热数据
  115. function root:del_hot_data()
  116. local key = self:get_module_redis_key()
  117. local ret = lib_game_redis:del(key)
  118. log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
  119. end
  120. ----------------------------------------
  121. -- 实时数据
  122. ----------------------------------------
  123. local moduleData = require("data.module")
  124. -- 获取数据类型
  125. function root:get_sub_key_type(subKey)
  126. if subKey == nil then
  127. return
  128. end
  129. local mpOptions = self:mysql_get_init_columns()
  130. for k, v in pairs(mpOptions) do
  131. if tostring(k) == tostring(subKey) then
  132. if self:is_number(v) then
  133. return "int"
  134. end
  135. if self:is_json(v) then
  136. return "json"
  137. end
  138. return "string"
  139. end
  140. end
  141. end
  142. -- 获取数据
  143. function root:redis_get_key_info(subKey, uid)
  144. if subKey == nil then
  145. return
  146. end
  147. uid = uid or self.uid
  148. local info = moduleData:hget(uid, self.mdlName, subKey)
  149. local tyKey = self:get_sub_key_type(subKey)
  150. if tyKey then
  151. if tyKey == "json" then
  152. if info then
  153. info = cjson_decode(info)
  154. end
  155. -- 主要针对C++中的NULL
  156. if is_empty(info) then
  157. info = {}
  158. end
  159. end
  160. if tyKey == "int" then
  161. info = tonumber(info or 0)
  162. end
  163. end
  164. return info
  165. end
  166. -- 设置数据
  167. function root:redis_update_key_info(subKey, info, uid)
  168. uid = uid or self.uid
  169. return moduleData:hset(uid, self.mdlName, subKey, info)
  170. end
  171. -- +1
  172. function root:redis_hincrby(subKey, uid)
  173. uid = uid or self.uid
  174. return moduleData:hincrby(uid, self.mdlName, subKey)
  175. end
  176. return root