GiftLogic.lua 9.8 KB

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