OnlineRewardLogic.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -- 在线奖励 --
  2. -- 1、在线计时:当前倒计时完毕,会继续下一物品倒计时
  3. -- 2、不在线计时:倒计当前物品剩余时间,不会继续下一物品倒计时
  4. local OnlineRewardExcel = require("excel.onlineReward")
  5. local Msg = require("core.Msg")
  6. local ObjHuman = require("core.ObjHuman")
  7. local Grid = require("bag.Grid")
  8. local BagLogic = require("bag.BagLogic")
  9. local STATUS_CANTGET = 0 -- 不可领
  10. local STATUS_CANGET = 1 -- 可领
  11. local STATUS_HADGET = 2 -- 已领
  12. -- 初始
  13. function initDB(human)
  14. human.db.onlineReward = {}
  15. human.db.onlineReward.ts = 0 -- 累计时间:在线+部分离线
  16. human.db.onlineReward.getId = {} -- 已领取id
  17. end
  18. -- 在线时间
  19. function getOnlineTime(human)
  20. if not human.db.onlineReward then
  21. initDB(human)
  22. end
  23. return human.db.onlineReward.ts + os.time() - human.db.lastLoginTime
  24. end
  25. -- 剩余时间
  26. function getLeftTime(human, id)
  27. local config = OnlineRewardExcel.onlineReward[id]
  28. if not config then
  29. return 0
  30. end
  31. return math.max(config.useTime - getOnlineTime(human), 0)
  32. end
  33. -- 状态
  34. function getStatus(human, id)
  35. local config = OnlineRewardExcel.onlineReward[id]
  36. if not config then
  37. return STATUS_CANTGET
  38. end
  39. if human.db.onlineReward.getId[id] then
  40. return STATUS_HADGET
  41. end
  42. if getLeftTime(human, id) > 0 then
  43. return STATUS_CANTGET
  44. end
  45. return STATUS_CANGET
  46. end
  47. function getIDByTime(time)
  48. for id, config in ipairs(OnlineRewardExcel.onlineReward) do
  49. if config.useTime > time then
  50. return id, config
  51. end
  52. end
  53. end
  54. -- 是否全部领取
  55. function isAllGet(human)
  56. if not human.db.onlineReward then
  57. initDB(human)
  58. end
  59. for k,_ in pairs(OnlineRewardExcel.onlineReward) do
  60. if type(human.db.onlineReward.getId) == "table" and not human.db.onlineReward.getId[k] then
  61. return nil,k
  62. end
  63. end
  64. return true
  65. end
  66. -- 登陆
  67. function onLogin(human)
  68. if isAllGet(human) then return end
  69. if (human.db.lastLogoutTime or 0) > 0 then
  70. local id, config = getIDByTime(human.db.onlineReward.ts)
  71. local time = os.time() - human.db.lastLogoutTime
  72. if config then
  73. human.db.onlineReward.ts = math.min(human.db.onlineReward.ts + time, config.useTime)
  74. end
  75. end
  76. sendOnlineReward(human)
  77. end
  78. -- 登出
  79. function onLogout(human, time)
  80. if not human.db.onlineReward then
  81. initDB(human)
  82. end
  83. human.db.onlineReward.ts = human.db.onlineReward.ts + time
  84. end
  85. -- 发送信息
  86. function sendOnlineReward(human)
  87. local msgRet = Msg.gc.GC_ONLINE_REWARD_DATA
  88. local _,nowID = isAllGet(human)
  89. if nowID then
  90. local nowConfig = OnlineRewardExcel.onlineReward[nowID]
  91. Grid.makeItem(msgRet.item, nowConfig and nowConfig.itemID or 0, nowConfig and nowConfig.itemCnt or 0)
  92. else
  93. Grid.makeItem(msgRet.item, 0, 0)
  94. end
  95. local onlineRewardConfig = OnlineRewardExcel.onlineReward
  96. msgRet.leftTime = 0
  97. msgRet.allItem[0] = 0
  98. for id, config in ipairs(OnlineRewardExcel.onlineReward) do
  99. msgRet.allItem[0] = msgRet.allItem[0] + 1
  100. local net = msgRet.allItem[msgRet.allItem[0]]
  101. net.id = id
  102. net.status = getStatus(human, id)
  103. Grid.makeItem(net.item, config.itemID, config.itemCnt)
  104. if net.status == STATUS_CANTGET and msgRet.leftTime < 1 then
  105. msgRet.leftTime = getLeftTime(human, id)
  106. end
  107. end
  108. --Msg.trace(msgRet)
  109. Msg.send(msgRet, human.fd)
  110. end
  111. -- 获取奖励
  112. function getReward(human, id)
  113. -- local status = getStatus(human, id)
  114. -- if status ~= STATUS_CANGET then
  115. -- return
  116. -- end
  117. -- local config = OnlineRewardExcel.onlineReward[id]
  118. -- human.db.onlineReward.getId[id] = 1
  119. -- -- 增加物品
  120. -- BagLogic.cleanMomentItemList()
  121. -- BagLogic.updateMomentItem(1, config.itemID, config.itemCnt)
  122. -- BagLogic.addMomentItemList(human, "online_reward")
  123. -- sendOnlineReward(human)
  124. -- 改为一键领取
  125. local itemList = {}
  126. for k, v in ipairs(OnlineRewardExcel.onlineReward) do
  127. local status = getStatus(human, k)
  128. if status == STATUS_CANGET then
  129. human.db.onlineReward.getId[k] = 1
  130. itemList[v.itemID] = (itemList[v.itemID] or 0) + v.itemCnt
  131. end
  132. end
  133. if next(itemList) then
  134. BagLogic.addItemList(human, itemList, "online_reward")
  135. sendOnlineReward(human)
  136. end
  137. end
  138. function isDot(human)
  139. if isAllGet(human) then return end
  140. for id in ipairs(OnlineRewardExcel.onlineReward) do
  141. local status = getStatus(human, id)
  142. if status == STATUS_CANTGET then
  143. break
  144. end
  145. if status == STATUS_CANGET then
  146. return true
  147. end
  148. end
  149. end