TuiSongLiBao.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. -- 推送礼包
  2. local Lang = require("common.Lang")
  3. local Msg = require("core.Msg")
  4. local ObjHuman = require("core.ObjHuman")
  5. local BagLogic = require("bag.BagLogic")
  6. local Grid = require("bag.Grid")
  7. local Broadcast = require("broadcast.Broadcast")
  8. local PresentExcel = require("excel.present")
  9. local Util = require("common.Util")
  10. local BuyLogic = require("topup.BuyLogic")
  11. local YunYingLogic = require("yunying.YunYingLogic")
  12. -- db.tuiSongLiBao.libao[id] = {
  13. -- activeTime 激活时间
  14. -- isBuy 是否购买
  15. -- activeCnt 触发次数
  16. -- }
  17. -- db.tuiSongLiBao.task[taskId] = {
  18. -- finCnt 完成次数
  19. -- }
  20. TUISONGLIBAOTASK_LV = 1 --玩家到达x级别
  21. TUISONGLIBAOTASK_STARS_HERO = 2 --获得x星武将
  22. TUISONGLIBAOTASK_ZHAOJIANG = 3 --召将达到x次
  23. TUISONGLIBAOTASK_ZHENGZHAN = 4 --通关关卡(x)
  24. TUISONGLIBAOTASK_TOWER = 5 --通关通天塔(x)层
  25. TUISONGLIBAOTASK_ZHUANPAN = 6 --许愿(x)次
  26. local function getLeftTimeByID(human,id)
  27. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  28. if not tuiSongLiBaoDB or
  29. not tuiSongLiBaoDB.libao or
  30. not tuiSongLiBaoDB.libao[id] then return 0 end
  31. local config = PresentExcel.tuiSongLiBao[id]
  32. local activeTime = tuiSongLiBaoDB.libao[id].activeTime
  33. local leftTime = (activeTime + config.canBuyTime) - os.time()
  34. if leftTime < 0 then
  35. leftTime = 0
  36. end
  37. return leftTime
  38. end
  39. local function getMinLeftTime(human)
  40. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  41. if not tuiSongLiBaoDB or
  42. not tuiSongLiBaoDB.libao then return end
  43. local min = nil
  44. for id,data in pairs(tuiSongLiBaoDB.libao) do
  45. if not data.isBuy then
  46. local leftTime = getLeftTimeByID(human,id)
  47. if leftTime > 0 then
  48. if not min then
  49. min = leftTime
  50. end
  51. if min > leftTime then
  52. min = leftTime
  53. end
  54. end
  55. end
  56. end
  57. return min
  58. end
  59. local function isBuy(human,id)
  60. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  61. if not tuiSongLiBaoDB or
  62. not tuiSongLiBaoDB.libao or
  63. not tuiSongLiBaoDB.libao[id] then return end
  64. return tuiSongLiBaoDB.libao[id].isBuy
  65. end
  66. local function makeTuiSongLiBaoNet(human,id,net)
  67. net.leftTime = getLeftTimeByID(human,id)
  68. local config = PresentExcel.tuiSongLiBao[id]
  69. net.desc = config.desc
  70. net.reward[0] = #config.reward
  71. for i=1,#config.reward do
  72. local itemID = config.reward[i][1]
  73. local itemCnt = config.reward[i][2]
  74. Grid.makeItem(net.reward[i], itemID, itemCnt)
  75. end
  76. net.bg = config.bg
  77. net.fanli = config.fanli
  78. net.isBuy = isBuy(human,id) and 1 or 0
  79. net.id = id
  80. net.giftVipExp = config.vipExp
  81. net.type = config.task[1]
  82. BuyLogic.fontBuyItem(human,net.buyItem, config.buyID)
  83. end
  84. function CG_PRESENT_TUISONGLIBAO_QUERY(human)
  85. ObjHuman.updateDaily(human)
  86. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  87. if not tuiSongLiBaoDB or
  88. not tuiSongLiBaoDB.libao then return end
  89. local minLeftTime = getMinLeftTime(human)
  90. if not minLeftTime then return end
  91. local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_QUERY
  92. msgRet.libao[0] = 0
  93. for id in pairs(tuiSongLiBaoDB.libao) do
  94. if msgRet.libao[0] < #msgRet.libao then
  95. local leftTime = getLeftTimeByID(human,id)
  96. if leftTime > 0 then
  97. msgRet.libao[0] = msgRet.libao[0] + 1
  98. makeTuiSongLiBaoNet(human,id,msgRet.libao[msgRet.libao[0]])
  99. end
  100. end
  101. end
  102. --Msg.trace(msgRet)
  103. Msg.send(msgRet,human.fd)
  104. end
  105. function tuiSongLiBaoBuy(human,id)
  106. ObjHuman.updateDaily(human)
  107. local config = PresentExcel.tuiSongLiBao[id]
  108. if not config then return end
  109. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  110. if not tuiSongLiBaoDB
  111. or not tuiSongLiBaoDB.libao
  112. or not tuiSongLiBaoDB.libao[id] then return end
  113. local leftTime = getLeftTimeByID(human,id)
  114. if leftTime <= 0 then
  115. return Broadcast.sendErr(human,Lang.PRESENT_TUISONGLIBAO_NOTBUY)
  116. end
  117. if isBuy(human,id) then return end
  118. human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
  119. human.db.tuiSongLiBao.libao = human.db.tuiSongLiBao.libao or {}
  120. human.db.tuiSongLiBao.libao[id] = human.db.tuiSongLiBao.libao[id] or {}
  121. human.db.tuiSongLiBao.libao[id].isBuy = (human.db.tuiSongLiBao.libao[id].isBuy or 0) + 1
  122. BagLogic.addItemList(human, config.reward, "tuisong_liBao")
  123. Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
  124. local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_BUY
  125. msgRet.id = id
  126. Msg.send(msgRet,human.fd)
  127. end
  128. local function getLiBaoData(human,id)
  129. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  130. if not tuiSongLiBaoDB or not tuiSongLiBaoDB.libao then return end
  131. return tuiSongLiBaoDB.libao[id]
  132. end
  133. local function getLiBaoActiveCnt(human,id)
  134. local data = getLiBaoData(human,id)
  135. return data and data.activeCnt or 0
  136. end
  137. local function checkLiBaoCanActive(human,id)
  138. local libaoActiveCnt = getLiBaoActiveCnt(human,id)
  139. local config = PresentExcel.tuiSongLiBao
  140. if config[id].dayActiveCnt > libaoActiveCnt then
  141. return true
  142. end
  143. end
  144. local function libaoMake(human,id)
  145. human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
  146. human.db.tuiSongLiBao.libao = human.db.tuiSongLiBao.libao or {}
  147. human.db.tuiSongLiBao.libao[id] = human.db.tuiSongLiBao.libao[id] or {}
  148. human.db.tuiSongLiBao.libao[id].activeTime = os.time()
  149. human.db.tuiSongLiBao.libao[id].activeCnt = (human.db.tuiSongLiBao.libao[id].activeCnt or 0) + 1
  150. human.db.tuiSongLiBao.libao[id].isBuy = nil
  151. end
  152. local function checkTaskSucess(human, config, arg, arg2)
  153. local taskType = config.task[1]
  154. local cntNeed = config.task[2]
  155. if taskType == TUISONGLIBAOTASK_STARS_HERO then
  156. -- 星级
  157. if arg == cntNeed then
  158. return true
  159. end
  160. elseif taskType == TUISONGLIBAOTASK_LV then
  161. -- 等级
  162. local cntNew = arg
  163. local cntOld = arg2
  164. if cntNew >= cntNeed and cntOld < cntNeed then
  165. return true
  166. end
  167. elseif taskType == TUISONGLIBAOTASK_ZHAOJIANG then
  168. -- 招将次数
  169. local cntNew = human.db.tuiSongLiBao.task[taskType] or 0
  170. local cntOld = cntOld - arg
  171. if cntNew >= cntNeed and cntOld < cntNeed then
  172. return true
  173. end
  174. elseif taskType == TUISONGLIBAOTASK_ZHENGZHAN then
  175. -- 征战
  176. local cntNew = arg
  177. local cntOld = arg2
  178. if cntNew >= cntNeed and cntOld < cntNeed then
  179. return true
  180. end
  181. elseif taskType == TUISONGLIBAOTASK_TOWER then
  182. -- 爬塔
  183. local cntNew = arg
  184. local cntOld = arg2
  185. if cntNew >= cntNeed and cntOld < cntNeed then
  186. return true
  187. end
  188. elseif taskType == TUISONGLIBAOTASK_ZHUANPAN then
  189. -- 转盘次数
  190. local cntNew = human.db.tuiSongLiBao.task[taskType] or 0
  191. local cntOld = cntOld - arg
  192. if cntNew >= cntNeed and cntOld < cntNeed then
  193. return true
  194. end
  195. else
  196. assert(nil)
  197. end
  198. end
  199. function tuiSongLiBaoOnTask(human,taskType,arg, arg2)
  200. do return end
  201. if taskType == nil then
  202. assert(nil)
  203. end
  204. ObjHuman.updateDaily(human)
  205. local taskTypeConfig = PresentExcel.tuiSongLiBaoTask[taskType]
  206. if taskTypeConfig.needSaveDB == 1 then
  207. human.db.tuiSongLiBao = human.db.tuiSongLiBao or {}
  208. human.db.tuiSongLiBao.task = human.db.tuiSongLiBao.task or {}
  209. human.db.tuiSongLiBao.task[taskType] = (human.db.tuiSongLiBao.task[taskType] or 0) + arg
  210. end
  211. local isActive = nil
  212. for id, config in pairs(PresentExcel.tuiSongLiBao) do
  213. if config.task[1] == taskType then
  214. if checkTaskSucess(human, config, arg, arg2) then
  215. local canActive = checkLiBaoCanActive(human,id)
  216. local leftTime = getLeftTimeByID(human,id)
  217. if canActive and leftTime <= 0 then
  218. libaoMake(human,id)
  219. isActive = true
  220. local msgRet = Msg.gc.GC_PRESENT_TUISONGLIBAO_QUERY
  221. msgRet.libao[0] = 1
  222. makeTuiSongLiBaoNet(human,id,msgRet.libao[1])
  223. --Msg.trace(msgRet)
  224. Msg.send(msgRet,human.fd)
  225. end
  226. end
  227. end
  228. end
  229. if isActive then
  230. for k, v in pairs(funcID) do
  231. YunYingLogic.updateIcon(YYInfo[k], human,true)
  232. break
  233. end
  234. end
  235. end
  236. function updateDaily(human)
  237. local tuiSongLiBaoDB = human.db.tuiSongLiBao
  238. if not tuiSongLiBaoDB then return end
  239. human.db.tuiSongLiBao.task = nil
  240. if not tuiSongLiBaoDB.libao then return end
  241. for id,data in pairs(tuiSongLiBaoDB.libao) do
  242. local leftTime = getLeftTimeByID(human,id)
  243. if leftTime > 0 then
  244. human.db.tuiSongLiBao.libao[id].activeCnt = nil
  245. else
  246. human.db.tuiSongLiBao.libao[id] = nil
  247. end
  248. end
  249. end
  250. function isOpen(human)
  251. local min = getMinLeftTime(human)
  252. if min and min > 0 then return true end
  253. end
  254. -- function isRed(human)
  255. -- local min = getMinLeftTime(human)
  256. -- if min and min > 0 then return true end
  257. -- end
  258. function getLeftTime(human, YYInfo)
  259. if not YYInfo then return 0 end
  260. local min = getMinLeftTime(human)
  261. return min or 0
  262. end