DailyLibaoLogic.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ---------------------------------------------------
  2. -- 充值-每日礼包
  3. -- 每日0点刷新购买次数
  4. -- db.dailyLibao[id] = cnt
  5. ---------------------------------------------------
  6. local DailyLibaoExcel = require("excel.present").dailyLibao
  7. local Lang = require("common.Lang")
  8. local Msg = require("core.Msg")
  9. local ObjHuman = require("core.ObjHuman")
  10. local Broadcast = require("broadcast.Broadcast")
  11. local PanelDefine = require("broadcast.PanelDefine")
  12. local Grid = require("bag.Grid")
  13. local BagLogic = require("bag.BagLogic")
  14. local BuyLogic = require("topup.BuyLogic")
  15. local VipLogic = require("vip.VipLogic")
  16. local YunYingLogic = require("yunying.YunYingLogic")
  17. local FREE_LIBAO_ID = 0 -- 免费
  18. local QUICK_LIBAO_ID = 1 -- 一键购买
  19. local QUICK_STATUS_CANT = 0 -- 不能购买
  20. local QUICK_STATUS_CAN = 1 -- 可购买
  21. -- 已购买数量
  22. function getCnt(human, id)
  23. if not human.db.dailyLibao then
  24. return 0
  25. end
  26. return human.db.dailyLibao[id] or 0
  27. end
  28. function addCnt(human, id)
  29. if not human.db.dailyLibao then
  30. human.db.dailyLibao = {}
  31. end
  32. human.db.dailyLibao[id] = getCnt(human, id) + 1
  33. end
  34. -- 剩余次数
  35. function getLeftCnt(human, id)
  36. local config = DailyLibaoExcel[id]
  37. if not config then return 0 end
  38. return math.max(config.cnt - getCnt(human, id), 0)
  39. end
  40. -- 封装礼包结构体
  41. function fontLibaoNet(net, id, config, human)
  42. net.id = id
  43. net.items[0] = #config.items
  44. net.doubleFlags[0] = net.items[0]
  45. for i = 1, net.items[0] do
  46. local itemID = config.items[i][1]
  47. local itemCnt = config.items[i][2]
  48. Grid.makeItem(net.items[i], itemID, itemCnt)
  49. net.doubleFlags[i] = config.items[i][3] or 0 -- 是否显示双倍标签
  50. end
  51. net.cnt = getCnt(human, id)
  52. net.maxCnt = config.cnt
  53. net.vipLv = 0
  54. BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
  55. net.name = config.name
  56. end
  57. -- 可否一键购买
  58. local function canQuickBuy(human)
  59. for id, config in ipairs(DailyLibaoExcel) do
  60. if id ~= QUICK_LIBAO_ID then
  61. -- 剩余购买次数
  62. if getCnt(human, id) >= config.cnt then
  63. return
  64. end
  65. end
  66. end
  67. return true
  68. end
  69. -- 获取原价
  70. local function getQuickOldPrice(human)
  71. local sum = 0
  72. for id, config in ipairs(DailyLibaoExcel) do
  73. if id ~= QUICK_LIBAO_ID then
  74. sum = sum + BuyLogic.getRealPrice(human, config.buyID)
  75. end
  76. end
  77. return sum
  78. end
  79. -- 封装一键购买信息
  80. function fontQuickNet(net, human)
  81. local config = DailyLibaoExcel[QUICK_LIBAO_ID]
  82. net.status = canQuickBuy(human) and QUICK_STATUS_CAN or QUICK_STATUS_CANT
  83. net.oldPrice = getQuickOldPrice(human)
  84. net.vipLv = 0
  85. BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
  86. end
  87. -- 面板查询
  88. function sendQuery(human)
  89. ObjHuman.updateDaily(human)
  90. local msgRet = Msg.gc.GC_PRE_DAILY_LIBAO_QUERY
  91. msgRet.list[0] = 0
  92. for id, config in ipairs(DailyLibaoExcel) do
  93. if id ~= QUICK_LIBAO_ID then
  94. msgRet.list[0] = msgRet.list[0] + 1
  95. fontLibaoNet(msgRet.list[msgRet.list[0]], id, config, human)
  96. end
  97. end
  98. fontQuickNet(msgRet.quickData, human)
  99. msgRet.freeRed = (getLeftCnt(human, FREE_LIBAO_ID) > 0) and 1 or 0
  100. --Msg.trace(msgRet)
  101. Msg.send(msgRet, human.fd)
  102. end
  103. -- 给予东西
  104. local function giveLibaoItems(human, id)
  105. local config = DailyLibaoExcel[id]
  106. if not config then return end
  107. if getLeftCnt(human, id) < 1 then
  108. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  109. end
  110. addCnt(human, id)
  111. return config.items
  112. end
  113. -- 领取免费礼包
  114. function getPreFreeLibao(human)
  115. ObjHuman.updateDaily(human)
  116. local items = giveLibaoItems(human, FREE_LIBAO_ID)
  117. if not items then return end
  118. sendQuery(human)
  119. -- 通用获得道具弹窗
  120. if items then
  121. for _, item in pairs(items) do
  122. BagLogic.addItem(human, item[1], item[2], "daily_libao")
  123. end
  124. end
  125. BagLogic.sendItemGetList(human, items, "daily_libao")
  126. for k, v in pairs(funcID) do
  127. YunYingLogic.updateIcon(YYInfo[k], human)
  128. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3107)
  129. break
  130. end
  131. end
  132. -- 购买礼包
  133. function buyLibao(human, id)
  134. ObjHuman.updateDaily(human)
  135. local itemsAll = nil
  136. if id == QUICK_LIBAO_ID then
  137. for tid, config in ipairs(DailyLibaoExcel) do
  138. if tid ~= QUICK_LIBAO_ID then
  139. local items = giveLibaoItems(human, tid)
  140. if items then
  141. itemsAll = itemsAll or {}
  142. for _, item in ipairs(items) do
  143. itemsAll[#itemsAll + 1] = item
  144. end
  145. end
  146. end
  147. end
  148. itemsAll = BagLogic.sameItemTogether(itemsAll)
  149. else
  150. itemsAll = giveLibaoItems(human, id)
  151. end
  152. if not itemsAll then return end
  153. if itemsAll then
  154. for _, item in pairs(itemsAll) do
  155. BagLogic.addItem(human, item[1], item[2], "mail")
  156. end
  157. end
  158. -- 通用获得道具弹窗
  159. BagLogic.sendItemGetList(human, itemsAll, "daily_libao")
  160. sendQuery(human)
  161. end
  162. -- 红点
  163. function isRed(human)
  164. -- 有免费礼包可以领取
  165. if getLeftCnt(human, FREE_LIBAO_ID) > 0 then
  166. return true
  167. end
  168. end