GiftLogic.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. -- 已经触发过礼包了
  70. if human.db.gift.unlock[id] then
  71. return
  72. end
  73. human.db.gift.unlock[id] = true
  74. local now = os.time()
  75. human.db.gift.online[id] = now
  76. local giftData = genGiftData(human, id,now,human.region or "CN",1)
  77. return NewLogic.PushClient(human,GC_GIFT_GENERATE,{gift = giftData})
  78. end
  79. local function cleanGift(human)
  80. local nowTime = os.time()
  81. local gift = human.db.gift.online
  82. for id,ts in pairs(gift) do
  83. local cfg = GiftExcel[id] -- 是否需要判断cfg
  84. if nowTime - ts >= cfg.ttl then
  85. gift[id] = nil
  86. end
  87. end
  88. human.db.gift.online = gift
  89. end
  90. local function sendHumanGift(human)
  91. local gift = human.db.gift.online
  92. local list = {}
  93. -- 数据太大不好控制大小,分批次传送
  94. for id,ts in pairs(gift) do
  95. list[#list + 1] = genGiftData(human, id,ts,human.region or "CN")
  96. --[[if #list >= 5 then
  97. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  98. list = list,
  99. isFinish = 1,
  100. })
  101. list = {}
  102. end]]
  103. end
  104. -- 通知客户端
  105. NewLogic.PushClient(human,GC_QUERY_GIFT,{
  106. list = list,
  107. isFinish = 2,
  108. })
  109. end
  110. -- 每日只触发一次的礼包
  111. local function dailyEventCheck(human, giftId)
  112. if not human.db.gift or not human.db.gift.dailyEventRecord then
  113. return true
  114. end
  115. local dailyEventRecord = human.db.gift.dailyEventRecord
  116. if dailyEventRecord[giftId] then
  117. local lastGenGiftTime = dailyEventRecord[giftId]
  118. if Util.isSameDay(lastGenGiftTime) then
  119. return false
  120. end
  121. end
  122. return true
  123. end
  124. local function updateDailyEventRecord(human, giftId)
  125. human.db.gift.dailyEventRecord = human.db.gift.dailyEventRecord or {}
  126. human.db.gift.dailyEventRecord[giftId] = os.time()
  127. end
  128. local handler = {
  129. [PRINCIPAL_LINE_EVNET] = function(human,param)
  130. for id,cfg in pairs(GiftExcel) do
  131. -- 触发新礼包
  132. if cfg.trigger == PRINCIPAL_LINE_EVNET and cfg.param[1] == param.id then
  133. genGift(human,id)
  134. end
  135. end
  136. end,
  137. [EVAL_TOWER_EVENT] = function(human,param)
  138. for id,cfg in pairs(GiftExcel) do
  139. -- 触发新礼包
  140. if cfg.trigger == EVAL_TOWER_EVENT and cfg.param[1] == param.id then
  141. genGift(human,id)
  142. end
  143. end
  144. end,
  145. [Upgrade_HERO_EVENT] = function(human,param)
  146. -- 检测是否是第一个星级的英雄
  147. local star = param.star
  148. --[[local HeroLogic = require("hero.HeroLogic")
  149. local maxStar,cnt = HeroLogic.getHeroMaxStarCtn(human)
  150. if maxStar ~= star then
  151. return
  152. end
  153. if cnt ~= 1 then
  154. return
  155. end]]
  156. for id,cfg in pairs(GiftExcel) do
  157. -- 触发新礼包
  158. if cfg.trigger == Upgrade_HERO_EVENT and cfg.param[1] == star then
  159. genGift(human,id)
  160. end
  161. end
  162. end,
  163. -- [GIFT_UPGRADE_LV_EVENT] = function (human,param)
  164. -- for id,cfg in pairs(GiftExcel) do
  165. -- -- 触发新礼包
  166. -- if cfg.trigger == GIFT_UPGRADE_LV_EVENT and cfg.param[1] == param.newLv then
  167. -- genGift(human,id)
  168. -- end
  169. -- end
  170. -- end,
  171. -- [GIFT_TALISMAN_UPGRADE_STAR] = function (human,param)
  172. -- for id,cfg in pairs(GiftExcel) do
  173. -- -- 触发新礼包
  174. -- if cfg.trigger == GIFT_TALISMAN_UPGRADE_STAR and cfg.param[1] == param.newLv then
  175. -- genGift(human,id)
  176. -- end
  177. -- end
  178. -- end,
  179. -- [GIFT_WINNERRELIC_UPGRADE_STAR] = function (human,param)
  180. -- for id,cfg in pairs(GiftExcel) do
  181. -- -- 触发新礼包
  182. -- if cfg.trigger == GIFT_WINNERRELIC_UPGRADE_STAR and cfg.param[1] == param.newLv then
  183. -- genGift(human,id)
  184. -- end
  185. -- end
  186. -- end,
  187. -- [GIFT_ELF_UPGRADE_STAR] = function (human,param)
  188. -- for id,cfg in pairs(GiftExcel) do
  189. -- -- 触发新礼包
  190. -- if cfg.trigger == GIFT_ELF_UPGRADE_STAR and cfg.param[1] == param.newLv then
  191. -- genGift(human,id)
  192. -- end
  193. -- end
  194. -- end,
  195. -- [GIFT_TALISMAN_OPEN] = function (human)
  196. -- for id,cfg in pairs(GiftExcel) do
  197. -- -- 触发新礼包
  198. -- if cfg.trigger == GIFT_TALISMAN_OPEN then
  199. -- genGift(human,id)
  200. -- end
  201. -- end
  202. -- end,
  203. -- [GIFT_WINNERRELIC_OPEN] = function (human)
  204. -- for id,cfg in pairs(GiftExcel) do
  205. -- -- 触发新礼包
  206. -- if cfg.trigger == GIFT_WINNERRELIC_OPEN then
  207. -- genGift(human,id)
  208. -- end
  209. -- end
  210. -- end,
  211. -- [GIFT_ELF_OPEN] = function (human)
  212. -- for id,cfg in pairs(GiftExcel) do
  213. -- -- 触发新礼包
  214. -- if cfg.trigger == GIFT_ELF_OPEN then
  215. -- genGift(human,id)
  216. -- end
  217. -- end
  218. -- end,
  219. }
  220. local handler2 = {
  221. [GIFT_SEC_TYPE1] = function (human, param, eventType)
  222. for id,cfg in pairs(GiftExcel) do
  223. -- 触发新礼包
  224. if cfg.trigger == eventType and cfg.param[1] == param.currentVal and (not dailyEventList[eventType] or dailyEventCheck(human, id)) then
  225. genGift(human,id)
  226. if dailyEventList[eventType] then
  227. updateDailyEventRecord(human, id)
  228. end
  229. end
  230. end
  231. end,
  232. [GIFT_SEC_TYPE2] = function (human, param, eventType)
  233. for id,cfg in pairs(GiftExcel) do
  234. -- 触发新礼包
  235. if cfg.trigger == eventType then
  236. genGift(human,id)
  237. end
  238. end
  239. end,
  240. }
  241. ---------------------------------------------------------------
  242. function onLogin(human)
  243. -- 登录清楚过期礼包
  244. cleanGift(human)
  245. -- 开启事件监听!?
  246. return sendHumanGift(human)
  247. end
  248. function trigger(human,type,param, extraType)
  249. local f
  250. if not extraType then
  251. f = handler[type]
  252. else
  253. f = handler2[extraType]
  254. end
  255. if not f then
  256. return
  257. end
  258. return f(human, param, type)
  259. end
  260. function buy(human,giftId)
  261. -- 清楚一次过期的弹窗礼包
  262. print("recieve gift id is ",giftId)
  263. cleanGift(human)
  264. if not human.db.gift.online[giftId] then
  265. print("not found gift ",giftId)
  266. return
  267. end
  268. local cfg = GiftExcel[giftId]
  269. --[[local content = {}
  270. for _,item in pairs(cfg.content) do
  271. content[#content + 1] = {
  272. id = item[1],
  273. cnt = item[2]
  274. }
  275. end]]
  276. BagLogic.addItemList(human,cfg.content,"gift_buy")
  277. human.db.gift.online[giftId] = nil
  278. return sendHumanGift(human)
  279. end
  280. -- function GetRemainNum(human, nBuyID)
  281. -- local nChoseID = nil
  282. -- for id,cfg in pairs(GiftExcel) do
  283. -- if cfg.buyID == nBuyID then
  284. -- nChoseID = id
  285. -- break
  286. -- end
  287. -- end
  288. -- if not nChoseID then
  289. -- return 0
  290. -- end
  291. -- if not human.db.gift.online[nChoseID] then
  292. -- return 0
  293. -- else
  294. -- return 1
  295. -- end
  296. -- end