VoucherInvest.lua 7.9 KB

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