OpenServerSingleCharge.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. local Msg = require("core.Msg")
  2. local Util = require("common.Util")
  3. local Lang = require("common.Lang")
  4. local Broadcast = require("broadcast.Broadcast")
  5. local BagLogic = require("bag.BagLogic")
  6. local Grid = require("bag.Grid")
  7. local OpenAct = require("present.OpenAct")
  8. local YunYingLogic = require("yunying.YunYingLogic")
  9. local OpenActExcel = require("excel.openAct")
  10. local BuyExcel = require("excel.buy")
  11. --[[
  12. 开服活动-单笔充值
  13. DB:
  14. human.db.openServerSingleCharge = {
  15. [id] = { -- 档位id
  16. cnt = xxx, -- 已领取次数
  17. activeCnt = xxx,-- 已激活次数
  18. },
  19. ...
  20. }
  21. local:
  22. getDB() -- 获得单笔充值DB数据
  23. getDayLeftTime() -- 获得当天剩余时间秒
  24. wrapSChargeList() -- 包装单笔充值档位数据
  25. public:
  26. isRed() -- 红点提醒
  27. isActive() -- 激活状态
  28. isBaseOpen() -- 开启活动基本条件
  29. isOpen() -- 活动开启
  30. getLeftTime() -- 得到活动剩余时间
  31. query() -- 发送活动信息
  32. get() -- 领取活动奖励
  33. onCharge() -- 充值回调
  34. updateDaily() -- 每日重置
  35. --]]
  36. STATE_0 = 0 --不可领
  37. STATE_1 = 1 --可领
  38. STATE_2 = 2 -- 已领完
  39. local function getDB(human)
  40. human.db.openServerSingleCharge = human.db.openServerSingleCharge or {}
  41. return human.db.openServerSingleCharge
  42. end
  43. local function getDayLeftTime()
  44. local now = os.time()
  45. local endTime = Util.getDayStartTime(now) + 86400
  46. return endTime - now
  47. end
  48. local function wrapSChargeList(net, id, sChargeInfo, sChargeDB)
  49. net.id = id
  50. net.needPrice = BuyExcel.buy[sChargeInfo.needBuyId].THA
  51. net.maxCnt = sChargeInfo.maxCnt
  52. net.cnt = sChargeDB[id] and sChargeDB[id].cnt or 0
  53. if sChargeDB[id] then
  54. if sChargeDB[id].activeCnt > sChargeDB[id].cnt then
  55. net.state = STATE_1
  56. elseif sChargeDB[id].cnt >= sChargeInfo.maxCnt then
  57. net.state = STATE_2
  58. else
  59. net.state = STATE_0
  60. end
  61. else
  62. net.state = STATE_0
  63. end
  64. local len = 0
  65. for index,itemInfo in ipairs(sChargeInfo.rewards) do
  66. len = len + 1
  67. Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2])
  68. end
  69. net.items[0] = len
  70. end
  71. function isRed(human)
  72. local sChargesDB = human.db.openServerSingleCharge
  73. if not sChargesDB then return end
  74. for id in ipairs(OpenActExcel.singleCharge) do
  75. if sChargesDB[id] and sChargesDB[id].activeCnt > sChargesDB[id].cnt then return true end
  76. end
  77. end
  78. function isActive(human, YYInfo, funcConfig)
  79. return not isOpen(human, YYInfo, funcConfig)
  80. end
  81. function isBaseOpen(human, YYInfo, funcConfig, noSend)
  82. if human.db.lv < funcConfig.openLv then
  83. if not noSend then
  84. local str = Util.format(Lang.ROLE_LEV_ERROR, funcConfig.openLv)
  85. return Broadcast.sendErr(human, str)
  86. end
  87. end
  88. local flag = OpenAct.getOpenActTime(funcConfig.param)
  89. if not flag then return end
  90. return true
  91. end
  92. function isOpen(human, YYInfo, funcConfig)
  93. if isBaseOpen(human, YYInfo, funcConfig, true) then return true end
  94. end
  95. function getLeftTime(human, YYInfo, funcConfig)
  96. local _,leftTime = OpenAct.getOpenActTime(funcConfig.param)
  97. return leftTime
  98. end
  99. function query(human)
  100. local sChargeDB = getDB(human)
  101. local dayLeftTime = getDayLeftTime()
  102. local msgRet = Msg.gc.GC_OPEN_SERVER_SINGLE_CHARGE_QUERY
  103. msgRet.leftTime = dayLeftTime
  104. local len = 0
  105. for id,sChargeInfo in ipairs(OpenActExcel.singleCharge) do
  106. len = len + 1
  107. wrapSChargeList(msgRet.list[len], id, sChargeInfo, sChargeDB)
  108. end
  109. msgRet.list[0] = len
  110. Msg.send(msgRet, human.fd)
  111. end
  112. function get(human, funcID, id)
  113. local funcConfig = YunYingLogic.getFuncConfig(funcID)
  114. if not funcConfig then return end
  115. local sChargeInfo = OpenActExcel.singleCharge[id]
  116. if not sChargeInfo then return end
  117. local sChargeDB = getDB(human)
  118. if not sChargeDB[id] or sChargeDB[id].cnt >= sChargeDB[id].activeCnt then return end
  119. sChargeDB[id].cnt = sChargeDB[id].cnt + 1
  120. BagLogic.addItemList(human, sChargeInfo.rewards, "openServerSCharge_get")
  121. query(human)
  122. -- 现在没有红点了通知
  123. if not isRed(human) then
  124. YunYingLogic.sendBanner(human)
  125. YunYingLogic.updateIcon(YYInfo[funcID], human)
  126. YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, funcConfig.panelID)
  127. end
  128. end
  129. function onCharge(human, price, funcID, buyID)
  130. local funcConfig = YunYingLogic.getFuncConfig(funcID)
  131. if not funcConfig then return end
  132. if not isOpen(human, nil, funcConfig) then return end
  133. -- 取之前是不是有红点
  134. local beforeRed = isRed(human, nil, funcConfig)
  135. local sChargeDB = getDB(human)
  136. local isActive
  137. for id,sChargeInfo in ipairs(OpenActExcel.singleCharge) do
  138. if buyID == sChargeInfo.needBuyId and
  139. (not sChargeDB[id] or sChargeDB[id].activeCnt < sChargeInfo.maxCnt) then
  140. sChargeDB[id] = sChargeDB[id] or {cnt=0,activeCnt=0}
  141. sChargeDB[id].activeCnt = sChargeDB[id].activeCnt + 1
  142. isActive = true
  143. end
  144. end
  145. -- 之前没有红点,现在激活了红点通知(只需要通知banner上的红点)
  146. if not beforeRed and isActive then
  147. YunYingLogic.sendBanner(human)
  148. end
  149. end
  150. function updateDaily(human, activityID)
  151. if not human.db.openServerSingleCharge then return end
  152. human.db.openServerSingleCharge = nil
  153. end