FirstChargeLogic.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. -----------------------------------------------------------------
  2. -- 超值首充
  3. -- db.firstCharge
  4. -- firstCharge[type].ts 激活时间
  5. -- firstCharge.getList 奖励领取次数 firstCharge.getList[id] = cnt
  6. -- firstCharge.freeTime 每日登陆奖励领取时间 firstCharge.freeTime[type] = time
  7. -----------------------------------------------------------
  8. local Msg = require("core.Msg")
  9. local Util = require("common.Util")
  10. local PresentExcel = require("excel.present")
  11. local Grid = require("bag.Grid")
  12. local BagLogic = require("bag.BagLogic")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local PanelDefine = require("broadcast.PanelDefine")
  15. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  16. local YunYingLogic = require("yunying.YunYingLogic")
  17. local SceneHandler = require("scene.Handler")
  18. local ObjHuman = require("core.ObjHuman")
  19. local FIRST_CHARGE_TYPE1 = 1 -- 首充赠礼
  20. local FIRST_CHARGE_TYPE2 = 2 -- 100元赠礼
  21. -- 状态
  22. STATE_CANT_GET = 0 -- 不可领
  23. STATE_CAT_GET = 1 -- 可领
  24. STATE_HAD_GET = 2 -- 已领
  25. function isOpenByType(human,giftType)
  26. if not human.db.firstCharge or not human.db.firstCharge[giftType] then
  27. return true
  28. end
  29. for id,config in ipairs(PresentExcel.firstCharge) do
  30. if giftType == config.type then
  31. if getState(human, id, true) ~= STATE_HAD_GET then
  32. return true
  33. end
  34. end
  35. end
  36. end
  37. function isRedByType(human,giftType)
  38. if not human.db.firstCharge or not human.db.firstCharge[giftType] then
  39. return
  40. end
  41. for id,config in ipairs(PresentExcel.firstCharge) do
  42. if giftType == config.type then
  43. if getState(human, id) == STATE_CAT_GET then
  44. return true
  45. end
  46. end
  47. end
  48. end
  49. function isOpen(human,YYInfo,funcConfig)
  50. if not SceneHandler.canCharge(human) then
  51. return
  52. end
  53. if isOpenByType(human,FIRST_CHARGE_TYPE1) or isOpenByType(human,FIRST_CHARGE_TYPE2) then
  54. return true
  55. end
  56. end
  57. function isRed(human,YYInfo,funcConfig)
  58. if isRedByType(human,FIRST_CHARGE_TYPE1) or isRedByType(human,FIRST_CHARGE_TYPE2) then
  59. return true
  60. end
  61. end
  62. -- 封装结构体
  63. local function fontNet(net, human, id, cf)
  64. if not net then return end
  65. net.id = id
  66. net.status = getState(human, id)
  67. net.items[0] = #cf.reward
  68. for i, item in ipairs(cf.reward) do
  69. Grid.makeItem(net.items[i], item[1], item[2])
  70. end
  71. end
  72. function sendQuery(human,giftType)
  73. ObjHuman.updateDaily(human)
  74. local msgRet = Msg.gc.GC_FIRST_CHARGE_GIFT_QUERY
  75. msgRet.value = human.db.topupAcount or 0
  76. msgRet.type = giftType
  77. msgRet.list[0] = 0
  78. msgRet.red[0] = 0
  79. for id, cf in ipairs(PresentExcel.firstCharge) do
  80. if cf.type == giftType then
  81. msgRet.list[0] = msgRet.list[0] + 1
  82. local index = msgRet.list[0]
  83. fontNet(msgRet.list[index], human, id, cf)
  84. end
  85. if id == 2 or id == 4 then
  86. msgRet.red[0] = msgRet.red[0] + 1
  87. msgRet.red[msgRet.red[0]] = getState(human, id)
  88. end
  89. end
  90. --Msg.trace(msgRet)
  91. Msg.send(msgRet, human.fd)
  92. end
  93. function getItems(human, type)
  94. ObjHuman.updateDaily(human)
  95. -- 领取奖励
  96. local items = {}
  97. local len = 0
  98. for id, cf in ipairs(PresentExcel.firstCharge) do
  99. if cf.type == type then
  100. local state = getState(human, id)
  101. if state == STATE_CAT_GET then
  102. local cf = PresentExcel.firstCharge[id]
  103. if cf then
  104. human.db.firstCharge.getList = human.db.firstCharge.getList or {}
  105. human.db.firstCharge.getList[id] = (human.db.firstCharge.getList[id] or 0) + 1
  106. if cf.day == 0 then
  107. human.db.firstCharge.freeTime = human.db.firstCharge.freeTime or {}
  108. human.db.firstCharge.freeTime[cf.type] = os.time()
  109. end
  110. for k,v in pairs(cf.reward) do
  111. local itemID = v[1]
  112. local itemCnt = v[2]
  113. items[itemID] = items[itemID] or 0
  114. items[itemID] = items[itemID] + itemCnt
  115. len = len + 1
  116. end
  117. end
  118. end
  119. end
  120. end
  121. if len > 0 then
  122. BagLogic.addItemList(human, items, "shouchong")
  123. for k, v in pairs(funcID) do
  124. YunYingLogic.updateIcon(YYInfo[k], human)
  125. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3302)
  126. break
  127. end
  128. sendQuery(human, type)
  129. end
  130. end
  131. local TYPE_2_NEED = nil
  132. function getNeedList()
  133. if not TYPE_2_NEED then
  134. TYPE_2_NEED = {}
  135. for id, cf in pairs(PresentExcel.firstCharge) do
  136. TYPE_2_NEED[cf.type] = cf.need
  137. end
  138. end
  139. return TYPE_2_NEED
  140. end
  141. -- 状态
  142. function getState(human, id, byMax)
  143. local cf = PresentExcel.firstCharge[id]
  144. if not cf then
  145. return STATE_CANT_GET
  146. end
  147. local data = human.db.firstCharge and human.db.firstCharge[cf.type]
  148. if not data or not data.ts then
  149. return STATE_CANT_GET
  150. end
  151. if cf.day == 0 and not byMax then
  152. local freeTime = human.db.firstCharge.freeTime and human.db.firstCharge.freeTime[cf.type] or 0
  153. if Util.isSameDay(freeTime) then
  154. return STATE_HAD_GET
  155. end
  156. end
  157. local getCnt = human.db.firstCharge.getList and human.db.firstCharge.getList[id] or 0
  158. if getCnt >= cf.getMax then
  159. return STATE_HAD_GET
  160. end
  161. local day = Util.diffDay(data.ts) + 1
  162. if cf.day > day then
  163. return STATE_CANT_GET
  164. end
  165. return STATE_CAT_GET
  166. end
  167. -- 激活首充
  168. function activateFirstCharge(human, ftype)
  169. human.db.firstCharge = human.db.firstCharge or {}
  170. if human.db.firstCharge[ftype] then return end
  171. human.db.firstCharge[ftype] = {}
  172. human.db.firstCharge[ftype].ts = os.time()
  173. if ftype == FIRST_CHARGE_TYPE1 then
  174. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE15, 0)
  175. end
  176. return true
  177. end
  178. -- 充值回调
  179. function onCharge(human)
  180. local needList = getNeedList()
  181. local isChange = nil
  182. for ftype, need in pairs(needList) do
  183. if (human.db.topupAcount or 0) >= need and
  184. activateFirstCharge(human, ftype) then
  185. isChange = true
  186. sendQuery(human, ftype)
  187. end
  188. end
  189. if isChange then
  190. for k, v in pairs(funcID) do
  191. YunYingLogic.updateIcon(YYInfo[k], human)
  192. break
  193. end
  194. end
  195. end
  196. function updateDaily(human)
  197. if human.db.firstCharge then
  198. human.db.firstCharge.freeTime = nil
  199. end
  200. end
  201. -- 是否已激活首充
  202. function isActive(human, YYInfo, funcConfig)
  203. if not human.db.firstCharge or not human.db.firstCharge[FIRST_CHARGE_TYPE1] then
  204. return
  205. end
  206. return true
  207. end