baseModule.lua 4.6 KB

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