GiftLogic.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. local Util = require("common.Util")
  12. -- 推送协议类型
  13. local GC_GIFT_GENERATE = 50
  14. local GC_QUERY_GIFT = 51
  15. -- 次类型
  16. GIFT_SEC_TYPE1 = 1
  17. GIFT_SEC_TYPE2 = 2
  18. GIFT_SEC_TYPE3 = 3
  19. -- 礼包触发事件
  20. local PRINCIPAL_LINE_EVNET = 1 -- 主线推图
  21. local EVAL_TOWER_EVENT = 2 -- 恶魔之塔
  22. local Upgrade_HERO_EVENT = 3 -- 英雄升星
  23. GIFT_UPGRADE_LV_EVENT = 4 -- 玩家升级
  24. GIFT_TALISMAN_UPGRADE_STAR = 5 -- 秘宝升星
  25. GIFT_WINNERRELIC_UPGRADE_STAR = 6 -- 圣遗物升星
  26. GIFT_ELF_UPGRADE_STAR = 7 -- 精灵升星
  27. GIFT_HERO_UPGRADE_STAR_DAILY = 8 -- 英雄升星, 每日只可触发一次, 可与 Upgrade_HERO_EVENT 同时触发
  28. GIFT_HEROSEED_UPGRADE_STAR = 9 -- 英雄种子
  29. GIFT_HEROTIANYUAN_UPGRADE_STAR = 10 -- 英雄天元
  30. GIFT_HEROWEAPON_UPGRADE_STAR = 11 -- 英雄专属武器
  31. GIFT_ARTIFACTS_UPGRADE_STAR = 12 -- 英雄神威灵装升星
  32. GIFT_REALM_UPGRADE_STAR = 13 -- 境界提升
  33. GIFT_TALISMAN_OPEN = 15 -- 开启秘宝玩法
  34. GIFT_WINNERRELIC_OPEN = 16 -- 开启圣遗物玩法
  35. GIFT_ELF_OPEN = 17 -- 开启精灵玩法
  36. GIFT_ARTIFACTS_OPEN = 18 -- 开启神威灵装玩法
  37. -- 每日只触发一次的礼包类型
  38. local dailyEventList = {
  39. [GIFT_HERO_UPGRADE_STAR_DAILY] = 1,
  40. }
  41. --[[
  42. human.db.gift = {
  43. [id] = startTimeStamp
  44. }
  45. ]]
  46. local function genGiftData(human, id,startTime,region,isNew)
  47. local cfg = GiftExcel[id]
  48. local buyCfg = BuyExcel[cfg.buyID]
  49. -- 获取礼包信息
  50. local buyItem = {}
  51. BuyLogic.fontBuyItem(human, buyItem, cfg.buyID)
  52. -- 获取礼包content
  53. local itemData = {}
  54. for _,item in ipairs(cfg.content) do
  55. local data = {
  56. getway = {},
  57. suipian = {},
  58. equip = {},
  59. fuwen = {},
  60. }
  61. if not ItemDefine.isEquip(item[1]) then
  62. Grid.makeItem(data, item[1], item[2])
  63. end
  64. itemData[#itemData+1] = data
  65. end
  66. return {
  67. id = id,
  68. buyItem = buyItem,
  69. startTimeStamp = startTime,
  70. restTime = cfg.ttl - (os.time() - startTime),
  71. isNew = isNew,
  72. itemData = itemData,
  73. }
  74. end
  75. local function genGift(human,id)
  76. local giftCfg = GiftExcel[id]
  77. local gitftType = giftCfg and giftCfg.trigger or 0
  78. -- 已经触发过礼包了
  79. -- 新修改: 类型8不受这个影响
  80. if human.db.gift.unlock[id] and GIFT_HERO_UPGRADE_STAR_DAILY ~= gitftType then
  81. return
  82. end
  83. human.db.gift.unlock[id] = true
  84. local now = os.time()
  85. human.db.gift.online[id] = now
  86. local giftData = genGiftData(human, id,now,human.region or "CN",1)
  87. return NewLogic.PushClient(human,GC_GIFT_GENERATE,{gift = giftData})
  88. end
  89. local function cleanGift(human)
  90. local nowTime = os.time()
  91. local gift = human.db.gift.online
  92. for id,ts in pairs(gift) do
  93. local cfg = GiftExcel[id] -- 是否需要判断cfg
  94. if nowTime - ts >= cfg.ttl then
  95. gift[id] = nil
  96. end
  97. end
  98. human.db.gift.online = gift
  99. end
  100. local function sendHumanGift(human)
  101. local gift = human.db.gift.online
  102. local list = {}
  103. -- 数据太大不好控制大小,分批次传送
  104. for id,ts in pairs(gift) do
  105. list[#list + 1] = genGiftData(human, id,ts,human.region or "CN")
  106. --[[if #list >= 5 then
  107. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  108. list = list,
  109. isFinish = 1,
  110. })
  111. list = {}
  112. end]]
  113. end
  114. -- 通知客户端
  115. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  116. list = list,
  117. isFinish = 2,
  118. })
  119. end
  120. -- 每日只触发一次的礼包
  121. local function dailyEventCheck(human, giftId)
  122. if not human.db.gift or not human.db.gift.dailyEventRecord then
  123. return true
  124. end
  125. local dailyEventRecord = human.db.gift.dailyEventRecord
  126. if dailyEventRecord[giftId] then
  127. local lastGenGiftTime = dailyEventRecord[giftId]
  128. if Util.isSameDay(lastGenGiftTime) then
  129. return false
  130. end
  131. end
  132. return true
  133. end
  134. local function updateDailyEventRecord(human, giftId)
  135. human.db.gift.dailyEventRecord = human.db.gift.dailyEventRecord or {}
  136. human.db.gift.dailyEventRecord[giftId] = os.time()
  137. end
  138. local handler = {
  139. [PRINCIPAL_LINE_EVNET] = function(human,param)
  140. for id,cfg in pairs(GiftExcel) do
  141. -- 触发新礼包
  142. if cfg.trigger == PRINCIPAL_LINE_EVNET and cfg.param[1] <= param.id then
  143. genGift(human,id)
  144. end
  145. end
  146. end,
  147. [EVAL_TOWER_EVENT] = function(human,param)
  148. for id,cfg in pairs(GiftExcel) do
  149. -- 触发新礼包
  150. if cfg.trigger == EVAL_TOWER_EVENT and cfg.param[1] <= param.id then
  151. genGift(human,id)
  152. end
  153. end
  154. end,
  155. [Upgrade_HERO_EVENT] = function(human,param)
  156. -- 检测是否是第一个星级的英雄
  157. local star = param.star
  158. --[[local HeroLogic = require("hero.HeroLogic")
  159. local maxStar,cnt = HeroLogic.getHeroMaxStarCtn(human)
  160. if maxStar ~= star then
  161. return
  162. end
  163. if cnt ~= 1 then
  164. return
  165. end]]
  166. for id,cfg in pairs(GiftExcel) do
  167. -- 触发新礼包
  168. if cfg.trigger == Upgrade_HERO_EVENT and cfg.param[1] == star then
  169. genGift(human,id)
  170. end
  171. end
  172. end,
  173. }
  174. local handler2 = {
  175. [GIFT_SEC_TYPE1] = function (human, param, eventType)
  176. for id,cfg in pairs(GiftExcel) do
  177. -- 触发新礼包
  178. if cfg.trigger == eventType and cfg.param[1] == param.currentVal and (not dailyEventList[eventType] or dailyEventCheck(human, id)) then
  179. genGift(human,id)
  180. if dailyEventList[eventType] then
  181. updateDailyEventRecord(human, id)
  182. end
  183. end
  184. end
  185. end,
  186. [GIFT_SEC_TYPE2] = function (human, param, eventType)
  187. for id,cfg in pairs(GiftExcel) do
  188. -- 触发新礼包
  189. if cfg.trigger == eventType then
  190. genGift(human,id)
  191. end
  192. end
  193. end,
  194. [GIFT_SEC_TYPE3] = function (human, param, eventType)
  195. for id,cfg in pairs(GiftExcel) do
  196. -- 触发新礼包
  197. if cfg.trigger == eventType and cfg.param[1] <= param.currentVal and (not dailyEventList[eventType] or dailyEventCheck(human, id)) then
  198. genGift(human,id)
  199. if dailyEventList[eventType] then
  200. updateDailyEventRecord(human, id)
  201. end
  202. end
  203. end
  204. end,
  205. }
  206. ---------------------------------------------------------------
  207. function onLogin(human)
  208. -- 登录清楚过期礼包
  209. cleanGift(human)
  210. -- 开启事件监听!?
  211. return sendHumanGift(human)
  212. end
  213. function trigger(human,type,param, extraType)
  214. local f
  215. if not extraType then
  216. f = handler[type]
  217. else
  218. f = handler2[extraType]
  219. end
  220. if not f then
  221. return
  222. end
  223. return f(human, param, type)
  224. end
  225. function buy(human,giftId)
  226. -- 清楚一次过期的弹窗礼包
  227. print("recieve gift id is ",giftId)
  228. cleanGift(human)
  229. if not human.db.gift.online[giftId] then
  230. print("not found gift ",giftId)
  231. return
  232. end
  233. local cfg = GiftExcel[giftId]
  234. --[[local content = {}
  235. for _,item in pairs(cfg.content) do
  236. content[#content + 1] = {
  237. id = item[1],
  238. cnt = item[2]
  239. }
  240. end]]
  241. BagLogic.addItemList(human,cfg.content,"gift_buy")
  242. human.db.gift.online[giftId] = nil
  243. return sendHumanGift(human)
  244. end
  245. -- function GetRemainNum(human, nBuyID)
  246. -- local nChoseID = nil
  247. -- for id,cfg in pairs(GiftExcel) do
  248. -- if cfg.buyID == nBuyID then
  249. -- nChoseID = id
  250. -- break
  251. -- end
  252. -- end
  253. -- if not nChoseID then
  254. -- return 0
  255. -- end
  256. -- if not human.db.gift.online[nChoseID] then
  257. -- return 0
  258. -- else
  259. -- return 1
  260. -- end
  261. -- end