baseModule.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. log.info("get_column_db_value key[%s] column[%s] val[%s]", tostring(key), tostring(column), tostring(val))
  85. if self:is_json(column) then
  86. val = string.format("'%s'", cjson_encode(val))
  87. elseif self:is_number(column) then
  88. val = tonumber(val or 0)
  89. else
  90. val = string.format("'%s'", val)
  91. end
  92. return val
  93. end
  94. -- 备份模块数据
  95. function root:backup_to_db()
  96. local options = self:mysql_get_init_columns()
  97. if is_empty(options) then
  98. return false
  99. end
  100. local colums = {}
  101. local values = {}
  102. for k, v in pairs(options) do
  103. table.insert(colums, k)
  104. table.include(values, self:get_column_db_value(k))
  105. end
  106. local tbName = self:get_table_name()
  107. local sql =
  108. string.format("replace into %s (%s) value (%s);", tbName, table.concat(colums, ","), table.concat(values, ","))
  109. local ret = lib_game_mysql:query(sql)
  110. if not is_empty(ret) then
  111. log.info("backup_to_db sql[%s] ret[%s]", tostring(sql), tostring(ret))
  112. end
  113. return ret
  114. end
  115. -- 删除模块热数据
  116. function root:del_hot_data()
  117. local key = self:get_module_redis_key()
  118. local ret = lib_game_redis:del(key)
  119. log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
  120. end
  121. ----------------------------------------
  122. -- 实时数据
  123. ----------------------------------------
  124. local moduleData = require("data.module")
  125. -- 获取数据类型
  126. function root:get_sub_key_type(subKey)
  127. if subKey == nil then
  128. return
  129. end
  130. local mpOptions = self:mysql_get_init_columns()
  131. for k, v in pairs(mpOptions) do
  132. if tostring(k) == tostring(subKey) then
  133. if self:is_number(v) then
  134. return "int"
  135. end
  136. if self:is_json(v) then
  137. return "json"
  138. end
  139. return "string"
  140. end
  141. end
  142. end
  143. -- 获取数据
  144. function root:redis_get_key_info(subKey, uid)
  145. if subKey == nil then
  146. return
  147. end
  148. uid = uid or self.uid
  149. local info = moduleData:hget(uid, self.mdlName, subKey)
  150. local tyKey = self:get_sub_key_type(subKey)
  151. if tyKey then
  152. if tyKey == "json" then
  153. if info then
  154. info = cjson_decode(info)
  155. end
  156. -- 主要针对C++中的NULL
  157. if is_empty(info) then
  158. info = {}
  159. end
  160. end
  161. if tyKey == "int" then
  162. info = tonumber(info or 0)
  163. end
  164. end
  165. return info
  166. end
  167. -- 设置数据
  168. function root:redis_update_key_info(subKey, info, uid)
  169. uid = uid or self.uid
  170. return moduleData:hset(uid, self.mdlName, subKey, info)
  171. end
  172. -- +1
  173. function root:redis_hincrby(subKey, uid)
  174. uid = uid or self.uid
  175. return moduleData:hincrby(uid, self.mdlName, subKey)
  176. end
  177. return root