baseModule.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. return ret
  110. end
  111. -- 删除模块热数据
  112. function root:del_hot_data()
  113. local key = self:get_module_redis_key()
  114. local ret = lib_game_redis:del(key)
  115. log.info("del_hot_data key[%s] ret[%s]", tostring(key), tostring(ret))
  116. end
  117. ----------------------------------------
  118. -- 实时数据
  119. ----------------------------------------
  120. local moduleData = require("data.module")
  121. -- 获取数据类型
  122. function root:get_sub_key_type(subKey)
  123. if subKey == nil then
  124. return
  125. end
  126. local mpOptions = self:mysql_get_init_columns()
  127. for k, v in pairs(mpOptions) do
  128. if tostring(k) == tostring(subKey) then
  129. if self:is_number(v) then
  130. return "int"
  131. end
  132. if self:is_json(v) then
  133. return "json"
  134. end
  135. return "string"
  136. end
  137. end
  138. end
  139. -- 获取数据
  140. function root:redis_get_key_info(subKey, uid)
  141. if subKey == nil then
  142. return
  143. end
  144. uid = uid or self.uid
  145. local info = moduleData:hget(uid, self.mdlName, subKey)
  146. local tyKey = self:get_sub_key_type(subKey)
  147. if tyKey then
  148. if tyKey == "json" then
  149. if info then
  150. info = cjson_decode(info)
  151. end
  152. -- 主要针对C++中的NULL
  153. if is_empty(info) then
  154. info = {}
  155. end
  156. end
  157. if tyKey == "int" then
  158. info = tonumber(info or 0)
  159. end
  160. end
  161. return info
  162. end
  163. -- 设置数据
  164. function root:redis_update_key_info(subKey, info, uid)
  165. uid = uid or self.uid
  166. return moduleData:hset(uid, self.mdlName, subKey, info)
  167. end
  168. -- +1
  169. function root:redis_hincrby(subKey, uid)
  170. uid = uid or self.uid
  171. return moduleData:hincrby(uid, self.mdlName, subKey)
  172. end
  173. return root