VoucherInvest.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. -- 代金券投资
  2. -- db
  3. --[=[
  4. human.db.voucherInvestData = {
  5. getRecord = { -- 领取记录数组, 初始为nil
  6. idx1,
  7. idx2,
  8. }
  9. isBuy = nil, 是否已经充值, 充值成功后为true
  10. nowRound = nil, 当前轮数, 默认为1
  11. }
  12. ]=]--
  13. local Util = require("common.Util")
  14. local Lang = require("common.Lang")
  15. local Broadcast = require("broadcast.Broadcast")
  16. local Msg = require("core.Msg")
  17. local Grid = require("bag.Grid")
  18. local BagLogic = require("bag.BagLogic")
  19. local BuyLogic = require("topup.BuyLogic")
  20. local YunYingLogic = require("yunying.YunYingLogic")
  21. local voucherInvestConfig = require("excel.VoucherInvest").Sheet1
  22. local CommonDefine = require("common.CommonDefine")
  23. local YunYingExcel = require("excel.yunying")
  24. local LOGTYPE = "VoucherInvest" -- 日志标识
  25. local ACT_ID = 8203 -- 活动Id
  26. local VOUCHERSHOP_ACTID = 27
  27. local function initData(human)
  28. human.db.voucherInvestData = {
  29. nowRound = 1,
  30. isBuy = false,
  31. }
  32. end
  33. local function getData(human)
  34. if not human.db.voucherInvestData then
  35. initData(human)
  36. end
  37. return human.db.voucherInvestData
  38. end
  39. local function updateBuyState(human, buyState)
  40. local voucherInvestData = getData(human)
  41. voucherInvestData.isBuy = buyState
  42. end
  43. local function updateGetRecord(human, idx)
  44. local voucherInvestData = getData(human)
  45. voucherInvestData.getRecord = voucherInvestData.getRecord or {}
  46. table.insert(voucherInvestData.getRecord, idx)
  47. end
  48. local function resetData(human, newRound)
  49. local voucherInvestData = getData(human)
  50. voucherInvestData.nowRound = newRound
  51. voucherInvestData.isBuy = false
  52. Util.initTable(voucherInvestData.getRecord)
  53. end
  54. -- 是否已经充值
  55. local function isBuy(human)
  56. local voucherInvestData = getData(human)
  57. return voucherInvestData.isBuy
  58. end
  59. -- 通过轮数获取购买项Id
  60. local function getBuyIdByRound(round)
  61. for _, v in ipairs(voucherInvestConfig) do
  62. if v.round and v.round == round then
  63. return v.buyid
  64. end
  65. end
  66. end
  67. -- 通过轮数获取配置Id
  68. local function getRounCfgIdArr(round)
  69. local idArr = {}
  70. for id, v in ipairs(voucherInvestConfig) do
  71. if v.round and v.round == round then
  72. idArr[#idArr+1] = id
  73. end
  74. end
  75. return idArr
  76. end
  77. -- 获取最大轮数
  78. local function getMaxRound()
  79. return voucherInvestConfig[#voucherInvestConfig].round
  80. end
  81. -- 奖励状态
  82. local function getAwardState(human, id)
  83. local voucherInvestData = getData(human)
  84. local getRecordData = voucherInvestData.getRecord or {}
  85. if table.find(getRecordData, id) then
  86. return CommonDefine.COMMON_PRIZE_STATE_GET
  87. else
  88. if not isBuy(human) then
  89. return CommonDefine.COMMON_PRIZE_STATE_NOGET
  90. end
  91. local cfg = voucherInvestConfig[id]
  92. if human.db.zhandouli >= cfg.power then
  93. return CommonDefine.COMMON_PRIZE_STATE_CANGET
  94. end
  95. end
  96. return CommonDefine.COMMON_PRIZE_STATE_NOGET
  97. end
  98. -- 填充协议结构
  99. local function populateAwardMsg(human, net, id, cfg, nowRound, maxRound)
  100. if nowRound > maxRound then
  101. net.state = CommonDefine.COMMON_PRIZE_STATE_GET
  102. else
  103. net.state = getAwardState(human, id)
  104. end
  105. Grid.makeItem(net.item, cfg.reward[1], cfg.reward[2])
  106. net.needPower = cfg.power
  107. end
  108. -- 主动刷新一次红点
  109. local function updateRedDot(human)
  110. -- YunYingLogic.sendBanner(human)
  111. local config = YunYingExcel.func[ACT_ID]
  112. YunYingLogic.sendGroupUpdate(YYInfo[ACT_ID], human, config.panelID)
  113. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  114. end
  115. function isOpen(human, YYInfo, funcConfig)
  116. return true
  117. end
  118. function isRed(human)
  119. if not isOpen(human) then
  120. return false
  121. end
  122. if not isBuy(human) then
  123. return false
  124. end
  125. local voucherInvestData = getData(human)
  126. local nowRound = voucherInvestData.nowRound
  127. local maxRound = getMaxRound()
  128. -- 已经领完最后一轮
  129. if nowRound > maxRound then
  130. return false
  131. end
  132. local ids = getRounCfgIdArr(nowRound)
  133. for _, id in ipairs(ids) do
  134. local awardState = getAwardState(human, id)
  135. if awardState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  136. return true
  137. end
  138. end
  139. return false
  140. end
  141. function onCharge(human, price, funcID, buyID)
  142. if not isOpen(human) then
  143. return
  144. end
  145. if isBuy(human) then
  146. return
  147. end
  148. local voucherInvestData = getData(human)
  149. local nowRound = voucherInvestData.nowRound
  150. local buyId = getBuyIdByRound(nowRound)
  151. if buyId ~= buyID then
  152. return
  153. end
  154. updateBuyState(human, true)
  155. updateRedDot(human)
  156. end
  157. -- 战力更新
  158. function onZhandouli(human)
  159. if not isOpen(human) then
  160. return
  161. end
  162. if not isBuy(human) then
  163. return
  164. end
  165. updateRedDot(human)
  166. end
  167. -- 战力更新
  168. function PowerUpdate(human)
  169. if not isOpen(human) then
  170. return
  171. end
  172. if not isBuy(human) then
  173. return
  174. end
  175. updateRedDot(human)
  176. end
  177. -- 查询
  178. function VoucherInvest_Query(human)
  179. if not isOpen(human) then
  180. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  181. end
  182. local voucherInvestData = getData(human)
  183. local nowRound = voucherInvestData.nowRound
  184. local maxRound = getMaxRound()
  185. nowRound = math.min(nowRound, maxRound)
  186. local buyId = getBuyIdByRound(nowRound)
  187. local msgRet = Msg.gc.GC_VOUCHER_INVEST_QUERY
  188. BuyLogic.fontBuyItem(human, msgRet.buyItem, buyId)
  189. msgRet.isBuy = voucherInvestData.isBuy and 1 or 0
  190. if voucherInvestData.nowRound > maxRound then
  191. msgRet.isBuy = 1
  192. end
  193. msgRet.awardArr[0] = 0
  194. msgRet.isEnd = 0
  195. msgRet.isStart = 1
  196. local ids = getRounCfgIdArr(nowRound)
  197. local cfgNum = #ids
  198. local len, msgOnceLen = 0, 15
  199. for _, id in ipairs(ids) do
  200. len = len + 1
  201. msgRet.awardArr[0] = len
  202. local cfg = voucherInvestConfig[id]
  203. populateAwardMsg(human, msgRet.awardArr[len], id, cfg, voucherInvestData.nowRound, maxRound)
  204. if len >= msgOnceLen then
  205. cfgNum = cfgNum - len
  206. if cfgNum <= 0 then
  207. msgRet.isEnd = 1
  208. return Msg.send(msgRet, human.fd)
  209. end
  210. Msg.send(msgRet, human.fd)
  211. len = 0
  212. msgRet.isStart = 0
  213. end
  214. end
  215. if len > 0 then
  216. msgRet.isEnd = 1
  217. Msg.send(msgRet, human.fd)
  218. end
  219. end
  220. -- 领奖
  221. function VoucherInvest_Get(human)
  222. if not isOpen(human) then
  223. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  224. end
  225. if not isBuy(human) then
  226. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  227. end
  228. local voucherInvestData = getData(human)
  229. local nowRound = voucherInvestData.nowRound
  230. local maxRound = getMaxRound()
  231. if nowRound > maxRound then
  232. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  233. end
  234. local ids = getRounCfgIdArr(voucherInvestData.nowRound)
  235. local itemList = {}
  236. for _, id in ipairs(ids) do
  237. local cfg = voucherInvestConfig[id]
  238. local awardState = getAwardState(human, id)
  239. if awardState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  240. -- 更新领取记录
  241. updateGetRecord(human, id)
  242. local itemId, itemNum = cfg.reward[1], cfg.reward[2]
  243. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  244. end
  245. end
  246. if not next(itemList) then
  247. Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  248. end
  249. BagLogic.addItemList(human, itemList, LOGTYPE)
  250. -- 领完则进入下一轮
  251. if #voucherInvestData.getRecord == #ids then
  252. resetData(human, voucherInvestData.nowRound + 1)
  253. end
  254. updateRedDot(human)
  255. VoucherInvest_Query(human)
  256. end