Count.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Count = class()
  2. local this = Count
  3. function this:ctor(t)
  4. if t == nil then
  5. self.type = 0
  6. self.key = 0
  7. self.count = 0
  8. self.updateTime = 0
  9. self.total = 0
  10. self.totalUpdateTime = 0
  11. else
  12. self.type = t.type
  13. self.key = t.key
  14. self.count = t.count
  15. self.updateTime = t.updateTime
  16. self.total = t.total
  17. self.totalUpdateTime = t.totalUpdateTime
  18. end
  19. end
  20. function this:canAdd(add)
  21. if self.type == 0 then
  22. return true
  23. end
  24. local count = self.count + add
  25. if count > self.total then
  26. return false
  27. end
  28. return true
  29. end
  30. function this:countNum(add)
  31. self.count = self.count + add
  32. local now = getbaseinfo("nowsec")
  33. self.updateTime = now
  34. return self.count
  35. end
  36. function this:canRefreshTotal(now)
  37. return false
  38. end
  39. function this:refreshTotal(now)
  40. return false
  41. end
  42. function this:canResetCount(now)
  43. if self.type == 1 then
  44. return false
  45. elseif self.type == 2 and (not TimeUtil.isSameMonth(self.updateTime, now)) then
  46. return true
  47. elseif self.type == 3 and (not TimeUtil.isSameWeek(self.updateTime, now)) then
  48. return true
  49. elseif self.type == 4 and (not TimeUtil.isSameDay(self.updateTime, now)) then
  50. return true
  51. elseif self.type == 5 and (TimeUtil.diffDays(self.updateTime, now) >= 7) then
  52. return true
  53. elseif self.type == 6 and (TimeUtil.diffDays(self.updateTime, now) >= 30) then
  54. return true
  55. end
  56. return false
  57. end
  58. function this:reset(now)
  59. self.count = 0
  60. self.updateTime = now
  61. end
  62. function this:toBuilder()
  63. return { ["key"] = self.key, ["count"] = self.count, ["total"] = self.total }
  64. end