CountManager_1.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. CountManager = {}
  2. local this = CountManager
  3. CountConst = {
  4. TAG = "T$Count",
  5. GTAG = "R$Count"
  6. }
  7. function this.isGlobalCount(type)
  8. return tonumber(type) == 7 or tonumber(type) == 8
  9. end
  10. function this.getCount(actor, key)
  11. if string.isNullOrEmpty(key) then
  12. return nil
  13. end
  14. local config = ConfigDataManager.getById("Count_count", key)
  15. key = tonumber(key)
  16. local data = nil
  17. if this.isGlobalCount(config.type) then
  18. data = this.getGlobalCountData()
  19. else
  20. data = this.getCountData(actor)
  21. end
  22. local count, update = this.getRealCount(data, key)
  23. if this.checkReset(actor, count) then
  24. update = true
  25. end
  26. if update then
  27. if this.isGlobalCount(config.type) then
  28. this.saveGlobalData(key, count)
  29. else
  30. this.saveData(actor, key, count)
  31. end
  32. end
  33. return count
  34. end
  35. function this.count(actor, key, add)
  36. if string.isNullOrEmpty(key) then
  37. return 0
  38. end
  39. local config = ConfigDataManager.getById("Count_count", key)
  40. key = tonumber(key)
  41. local c = nil
  42. if this.isGlobalCount(config.type) then
  43. c = this.getGlobalCount(key)
  44. else
  45. c = this.getCount(actor, key)
  46. end
  47. if c and c:canAdd(add) then
  48. local curCount = c:countNum(actor, add)
  49. if this.isGlobalCount(config.type) then
  50. this.saveGlobalData(key, c)
  51. else
  52. this.saveData(actor, key, c)
  53. end
  54. this.sendMsg(actor, c)
  55. return curCount
  56. end
  57. return 0
  58. end
  59. -- 全服计数先不做
  60. function this.getGlobalCount(key)
  61. local data = this.getGlobalCountData()
  62. local count, update = this.getRealCount(data, key)
  63. if this.checkReset(count) then
  64. update = true
  65. end
  66. if update then
  67. this.saveGlobalData(key, count)
  68. end
  69. return count
  70. end
  71. function this.globalCount(key, add)
  72. local count = this.getGlobalCount(key, key)
  73. if count and count:canAdd() then
  74. local curCount = count:count(add)
  75. this.saveGlobalData(key, count)
  76. return curCount
  77. end
  78. return 0
  79. end
  80. function this.createCount(key)
  81. local config = ConfigDataManager.getById("Count_count", key)
  82. if config == nil then
  83. gameDebug.assertNotNil("count表找不到配置key=" .. key)
  84. end
  85. local count = Count()
  86. local now = getbaseinfo("nowsec")
  87. count.key = tonumber(config.id)
  88. count.type = tonumber(config.type)
  89. count.updateTime = now
  90. count.total = config.total == "" and 0 or tonumber(config.total)
  91. count.totalUpdateTime = now
  92. return count
  93. end
  94. function this.checkReset(actor, count)
  95. local update = false
  96. if count == nil then
  97. return update
  98. end
  99. local now = getbaseinfo("nowsec")
  100. if count:canRefreshTotal(now) then
  101. count:refreshTotal(now)
  102. update = true
  103. end
  104. if count:canResetCount(now) then
  105. count:reset(actor, now)
  106. update = true
  107. end
  108. return update
  109. end
  110. function this.getCountData(actor)
  111. return getplaydef(actor, CountConst.TAG) or {}
  112. end
  113. function this.saveData(actor, key, count)
  114. local data = this.getCountData(actor)
  115. data[key] = count
  116. setplaydef(actor, CountConst.TAG, data)
  117. -- info("玩家", actor, "保存计数数据", count)
  118. end
  119. function this.getGlobalCountData()
  120. return getsysvar(CountConst.GTAG) or {}
  121. end
  122. function this.saveGlobalData(key, count)
  123. local data = this.getGlobalCountData()
  124. data[key] = count
  125. setsysvar(CountConst.GTAG, data)
  126. -- info("玩家", actor, "保存全局计数数据", count)
  127. end
  128. function this.getRealCount(data, key)
  129. local config = ConfigDataManager.getById("Count_count", key)
  130. if config == nil then
  131. gameDebug.print("count表找不到配置key=" .. key)
  132. return nil, false
  133. end
  134. local source = data[key]
  135. -- if config.type == 1 then
  136. -- return Count(source)
  137. -- end
  138. if source == nil then
  139. local count = this.createCount(key)
  140. return count, true
  141. end
  142. return Count(source), false
  143. end
  144. function this.sendMsg(actor, count)
  145. -- info("计数测试单个数据=", count:toBuilder())
  146. sendluamsg(actor, LuaMessageIdToClient.RES_UPDATE_COUNT_INFO, count:toBuilder())
  147. end
  148. function this.sendPlayerAllCountsMsg(actor)
  149. local res = {}
  150. local data = this.getCountData(actor)
  151. for _, v in pairs(data) do
  152. local c = this.getCount(actor, v.key)
  153. if c then
  154. table.insert(res, {
  155. ["key"] = c.key,
  156. ["count"] = c.count,
  157. ["total"] = c.total
  158. })
  159. end
  160. end
  161. data = this.getGlobalCountData(actor)
  162. for _, v in pairs(data) do
  163. local c = this.getCount(actor, v.key)
  164. if c then
  165. table.insert(res, {
  166. ["key"] = c.key,
  167. ["count"] = c.count,
  168. ["total"] = c.total
  169. })
  170. end
  171. end
  172. -- jprint("计数测试全部数据=", res)
  173. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_COUNT_INFO, res)
  174. end
  175. function this.zero(actor)
  176. -- info("0点在线玩家刷新count次数", actor)
  177. this.sendPlayerAllCountsMsg(actor)
  178. end
  179. function this.login(actor)
  180. this.sendPlayerAllCountsMsg(actor)
  181. end
  182. -- function testcount(actor, key, action)
  183. -- jprint("计数测试param=", key, action)
  184. -- if action == "1" then
  185. -- -- CountManager.count(actor, key, 1)
  186. -- -- local b = this.getCount(actor, key)
  187. -- -- local now = getbaseinfo("nowsec")
  188. -- -- b:reset(actor, now)
  189. -- -- this.saveData(actor, key, b)
  190. -- -- jprint("计数测试b=", b)
  191. -- elseif action == "2" then
  192. -- local b = CountManager.getCount(actor, key)
  193. -- jprint("计数测试b=", b)
  194. -- -- CountManager.sendMsg(actor, b)
  195. -- elseif action == "0" then
  196. -- this.sendPlayerAllCountsMsg(actor)
  197. -- elseif action == "-1" then
  198. -- this.zeroRefreshCheck()
  199. -- end
  200. -- end
  201. -- 凌晨刷新事件
  202. ZeroEventListerTable:eventLister("0", "count计数", CountManager.zero, 1111)
  203. -- 注册登录事件
  204. LoginEventListerTable:eventLister("0", "count计数", CountManager.login, 1111)