GiftLogic.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ----------------------------
  2. -- 彈窗禮包
  3. ----------------------------
  4. local BuyExcel = require("excel.buy").buy
  5. local GiftExcel = require("excel.buy").gift
  6. local ItemDefine = require("bag.ItemDefine")
  7. local Grid = require("bag.Grid")
  8. local NewLogic = require("role.NewLogic")
  9. local BagLogic = require("bag.BagLogic")
  10. local BuyLogic = require("topup.BuyLogic")
  11. -- 推送协议类型
  12. local GC_GIFT_GENERATE = 50
  13. local GC_QUERY_GIFT = 51
  14. -- 礼包触发事件
  15. local PRINCIPAL_LINE_EVNET = 1 -- 主线推图
  16. local EVAL_TOWER_EVENT = 2 -- 恶魔之塔
  17. local Upgrade_HERO_EVENT = 3 -- 英雄升星
  18. GIFT_UPGRADE_LV_EVENT = 4 -- 升级
  19. --[[
  20. human.db.gift = {
  21. [id] = startTimeStamp
  22. }
  23. ]]
  24. local function genGiftData(human, id,startTime,region,isNew)
  25. local cfg = GiftExcel[id]
  26. local buyCfg = BuyExcel[cfg.buyID]
  27. -- 获取礼包信息
  28. local buyItem = {}
  29. BuyLogic.fontBuyItem(human, buyItem, cfg.buyID)
  30. -- 获取礼包content
  31. local itemData = {}
  32. for _,item in ipairs(cfg.content) do
  33. local data = {
  34. getway = {},
  35. suipian = {},
  36. equip = {},
  37. fuwen = {},
  38. }
  39. if not ItemDefine.isEquip(item[1]) then
  40. Grid.makeItem(data, item[1], item[2])
  41. end
  42. itemData[#itemData+1] = data
  43. end
  44. return {
  45. id = id,
  46. buyItem = buyItem,
  47. startTimeStamp = startTime,
  48. restTime = cfg.ttl - (os.time() - startTime),
  49. isNew = isNew,
  50. itemData = itemData,
  51. }
  52. end
  53. local function genGift(human,id)
  54. -- 已经触发过礼包了
  55. if human.db.gift.unlock[id] then
  56. return
  57. end
  58. human.db.gift.unlock[id] = true
  59. local now = os.time()
  60. human.db.gift.online[id] = now
  61. local giftData = genGiftData(human, id,now,human.region or "CN",1)
  62. return NewLogic.PushClient(human,GC_GIFT_GENERATE,{gift = giftData})
  63. end
  64. local function cleanGift(human)
  65. local nowTime = os.time()
  66. local gift = human.db.gift.online
  67. for id,ts in pairs(gift) do
  68. local cfg = GiftExcel[id] -- 是否需要判断cfg
  69. if nowTime - ts >= cfg.ttl then
  70. gift[id] = nil
  71. end
  72. end
  73. human.db.gift.online = gift
  74. end
  75. local function sendHumanGift(human)
  76. local gift = human.db.gift.online
  77. local list = {}
  78. -- 数据太大不好控制大小,分批次传送
  79. for id,ts in pairs(gift) do
  80. list[#list + 1] = genGiftData(human, id,ts,human.region or "CN")
  81. --[[if #list >= 5 then
  82. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  83. list = list,
  84. isFinish = 1,
  85. })
  86. list = {}
  87. end]]
  88. end
  89. -- 通知客户端
  90. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  91. list = list,
  92. isFinish = 2,
  93. })
  94. end
  95. local handler = {
  96. [PRINCIPAL_LINE_EVNET] = function(human,param)
  97. for id,cfg in pairs(GiftExcel) do
  98. -- 触发新礼包
  99. if cfg.trigger == PRINCIPAL_LINE_EVNET and cfg.param[1] == param.id then
  100. genGift(human,id)
  101. end
  102. end
  103. end,
  104. [EVAL_TOWER_EVENT] = function(human,param)
  105. for id,cfg in pairs(GiftExcel) do
  106. -- 触发新礼包
  107. if cfg.trigger == EVAL_TOWER_EVENT and cfg.param[1] == param.id then
  108. genGift(human,id)
  109. end
  110. end
  111. end,
  112. [Upgrade_HERO_EVENT] = function(human,param)
  113. -- 检测是否是第一个星级的英雄
  114. local star = param.star
  115. --[[local HeroLogic = require("hero.HeroLogic")
  116. local maxStar,cnt = HeroLogic.getHeroMaxStarCtn(human)
  117. if maxStar ~= star then
  118. return
  119. end
  120. if cnt ~= 1 then
  121. return
  122. end]]
  123. for id,cfg in pairs(GiftExcel) do
  124. -- 触发新礼包
  125. if cfg.trigger == Upgrade_HERO_EVENT and cfg.param[1] == star then
  126. genGift(human,id)
  127. end
  128. end
  129. end,
  130. [GIFT_UPGRADE_LV_EVENT] = function (human,param)
  131. for id,cfg in pairs(GiftExcel) do
  132. -- 触发新礼包
  133. if cfg.trigger == GIFT_UPGRADE_LV_EVENT and cfg.param[1] == param.newLv then
  134. genGift(human,id)
  135. end
  136. end
  137. end
  138. }
  139. ---------------------------------------------------------------
  140. function onLogin(human)
  141. -- 登录清楚过期礼包
  142. cleanGift(human)
  143. -- 开启事件监听!?
  144. return sendHumanGift(human)
  145. end
  146. function trigger(human,type,param)
  147. local f = handler[type]
  148. if not f then
  149. return
  150. end
  151. return f(human,param)
  152. end
  153. function buy(human,giftId)
  154. -- 清楚一次过期的弹窗礼包
  155. print("recieve gift id is ",giftId)
  156. cleanGift(human)
  157. if not human.db.gift.online[giftId] then
  158. print("not found gift ",giftId)
  159. return
  160. end
  161. local cfg = GiftExcel[giftId]
  162. --[[local content = {}
  163. for _,item in pairs(cfg.content) do
  164. content[#content + 1] = {
  165. id = item[1],
  166. cnt = item[2]
  167. }
  168. end]]
  169. BagLogic.addItemList(human,cfg.content,"gift_buy")
  170. human.db.gift.online[giftId] = nil
  171. return sendHumanGift(human)
  172. end
  173. -- function GetRemainNum(human, nBuyID)
  174. -- local nChoseID = nil
  175. -- for id,cfg in pairs(GiftExcel) do
  176. -- if cfg.buyID == nBuyID then
  177. -- nChoseID = id
  178. -- break
  179. -- end
  180. -- end
  181. -- if not nChoseID then
  182. -- return 0
  183. -- end
  184. -- if not human.db.gift.online[nChoseID] then
  185. -- return 0
  186. -- else
  187. -- return 1
  188. -- end
  189. -- end