baseModule.lua 4.6 KB

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