baseModule.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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("%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. -- 实时数据
  104. ----------------------------------------
  105. local moduleData = require("data.module")
  106. -- 获取数据类型
  107. function root:get_sub_key_type(subKey)
  108. if subKey == nil then
  109. return
  110. end
  111. local mpOptions = self:mysql_get_init_columns()
  112. for k, v in pairs(mpOptions) do
  113. if tostring(k) == tostring(subKey) then
  114. if self:is_number(v) then
  115. return "int"
  116. end
  117. if self:is_json(v) then
  118. return "json"
  119. end
  120. return "string"
  121. end
  122. end
  123. end
  124. -- 获取数据
  125. function root:redis_get_key_info(subKey, uid)
  126. if subKey == nil then
  127. return
  128. end
  129. uid = uid or self.uid
  130. local info = moduleData:hget(uid, self.mdlName, subKey)
  131. local tyKey = self:get_sub_key_type(subKey)
  132. if tyKey then
  133. if tyKey == "json" then
  134. if info then
  135. info = cjson_decode(info)
  136. end
  137. -- 主要针对C++中的NULL
  138. if is_empty(info) then
  139. info = {}
  140. end
  141. end
  142. if tyKey == "int" then
  143. info = tonumber(info or 0)
  144. end
  145. end
  146. return info
  147. end
  148. -- 设置数据
  149. function root:redis_update_key_info(subKey, info, uid)
  150. uid = uid or self.uid
  151. return moduleData:hset(uid, self.mdlName, subKey, info)
  152. end
  153. -- +1
  154. function root:redis_hincrby(subKey, uid)
  155. uid = uid or self.uid
  156. return moduleData:hincrby(uid, self.mdlName, subKey)
  157. end
  158. return root