VoucherInvest.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, nBuyID, buyNum)
  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 ~= nBuyID then
  152. return
  153. end
  154. updateBuyState(human, true)
  155. updateRedDot(human)
  156. VoucherInvest_Query(human)
  157. end
  158. -- 战力更新
  159. function onZhandouli(human)
  160. if not isOpen(human) then
  161. return
  162. end
  163. if not isBuy(human) then
  164. return
  165. end
  166. updateRedDot(human)
  167. end
  168. -- 战力更新
  169. function PowerUpdate(human)
  170. if not isOpen(human) then
  171. return
  172. end
  173. if not isBuy(human) then
  174. return
  175. end
  176. updateRedDot(human)
  177. end
  178. -- 查询
  179. function VoucherInvest_Query(human)
  180. if not isOpen(human) then
  181. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  182. end
  183. local voucherInvestData = getData(human)
  184. local nowRound = voucherInvestData.nowRound
  185. local maxRound = getMaxRound()
  186. nowRound = math.min(nowRound, maxRound)
  187. local buyId = getBuyIdByRound(nowRound)
  188. local msgRet = Msg.gc.GC_VOUCHER_INVEST_QUERY
  189. BuyLogic.fontBuyItem(human, msgRet.buyItem, buyId)
  190. msgRet.isBuy = voucherInvestData.isBuy and 1 or 0
  191. if voucherInvestData.nowRound > maxRound then
  192. msgRet.isBuy = 1
  193. end
  194. msgRet.awardArr[0] = 0
  195. msgRet.isEnd = 0
  196. msgRet.isStart = 1
  197. local ids = getRounCfgIdArr(nowRound)
  198. local cfgNum = #ids
  199. local len, msgOnceLen = 0, 15
  200. for _, id in ipairs(ids) do
  201. len = len + 1
  202. msgRet.awardArr[0] = len
  203. local cfg = voucherInvestConfig[id]
  204. populateAwardMsg(human, msgRet.awardArr[len], id, cfg, voucherInvestData.nowRound, maxRound)
  205. if len >= msgOnceLen then
  206. cfgNum = cfgNum - len
  207. if cfgNum <= 0 then
  208. msgRet.isEnd = 1
  209. return Msg.send(msgRet, human.fd)
  210. end
  211. Msg.send(msgRet, human.fd)
  212. len = 0
  213. msgRet.isStart = 0
  214. end
  215. end
  216. if len > 0 then
  217. msgRet.isEnd = 1
  218. Msg.send(msgRet, human.fd)
  219. end
  220. end
  221. -- 领奖
  222. function VoucherInvest_Get(human)
  223. if not isOpen(human) then
  224. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  225. end
  226. if not isBuy(human) then
  227. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  228. end
  229. local voucherInvestData = getData(human)
  230. local nowRound = voucherInvestData.nowRound
  231. local maxRound = getMaxRound()
  232. if nowRound > maxRound then
  233. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  234. end
  235. local ids = getRounCfgIdArr(voucherInvestData.nowRound)
  236. local itemList = {}
  237. for _, id in ipairs(ids) do
  238. local cfg = voucherInvestConfig[id]
  239. local awardState = getAwardState(human, id)
  240. if awardState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  241. -- 更新领取记录
  242. updateGetRecord(human, id)
  243. local itemId, itemNum = cfg.reward[1], cfg.reward[2]
  244. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  245. end
  246. end
  247. if not next(itemList) then
  248. Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  249. end
  250. BagLogic.addItemList(human, itemList, LOGTYPE)
  251. -- 领完则进入下一轮
  252. if #voucherInvestData.getRecord == #ids then
  253. resetData(human, voucherInvestData.nowRound + 1)
  254. end
  255. updateRedDot(human)
  256. VoucherInvest_Query(human)
  257. end