baseModule.lua 4.9 KB

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