baseModule.lua 5.0 KB

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