CountManager.lua 4.8 KB

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