VoucherInvest.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. if human.db.phpChanelID and (human.db.phpChanelID == 13 or human.db.phpChanelID == "13") then
  121. return false
  122. end
  123. if human.db.lv < VoucherShopDefine.VOUCHER_OPEN_LV_COND then
  124. return false
  125. end
  126. if not human.db.nFirstBuy or human.db.nFirstBuy ~= 1 then
  127. --print("[VoucherShop is open] 33333")
  128. return false
  129. end
  130. return true
  131. end
  132. function isRed(human)
  133. if not isOpen(human) then
  134. return false
  135. end
  136. if not isBuy(human) then
  137. return false
  138. end
  139. local voucherInvestData = getData(human)
  140. local nowRound = voucherInvestData.nowRound
  141. local maxRound = getMaxRound()
  142. -- 已经领完最后一轮
  143. if nowRound > maxRound then
  144. return false
  145. end
  146. local ids = getRounCfgIdArr(nowRound)
  147. for _, id in ipairs(ids) do
  148. local awardState = getAwardState(human, id)
  149. if awardState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  150. return true
  151. end
  152. end
  153. return false
  154. end
  155. function onCharge(human, nBuyID, buyNum)
  156. if not isOpen(human) then
  157. return
  158. end
  159. if isBuy(human) then
  160. return
  161. end
  162. local voucherInvestData = getData(human)
  163. local nowRound = voucherInvestData.nowRound
  164. local buyId = getBuyIdByRound(nowRound)
  165. if buyId ~= nBuyID then
  166. return
  167. end
  168. updateBuyState(human, true)
  169. updateRedDot(human)
  170. VoucherInvest_Query(human)
  171. end
  172. -- 战力更新
  173. function onZhandouli(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 PowerUpdate(human)
  184. if not isOpen(human) then
  185. return
  186. end
  187. if not isBuy(human) then
  188. return
  189. end
  190. updateRedDot(human)
  191. end
  192. -- 查询
  193. function VoucherInvest_Query(human)
  194. if not isOpen(human) then
  195. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  196. end
  197. local voucherInvestData = getData(human)
  198. local nowRound = voucherInvestData.nowRound
  199. local maxRound = getMaxRound()
  200. nowRound = math.min(nowRound, maxRound)
  201. local buyId = getBuyIdByRound(nowRound)
  202. local msgRet = Msg.gc.GC_VOUCHER_INVEST_QUERY
  203. BuyLogic.fontBuyItem(human, msgRet.buyItem, buyId)
  204. msgRet.isBuy = voucherInvestData.isBuy and 1 or 0
  205. if voucherInvestData.nowRound > maxRound then
  206. msgRet.isBuy = 1
  207. end
  208. msgRet.awardArr[0] = 0
  209. msgRet.isEnd = 0
  210. msgRet.isStart = 1
  211. local ids = getRounCfgIdArr(nowRound)
  212. local cfgNum = #ids
  213. local len, msgOnceLen = 0, 15
  214. for _, id in ipairs(ids) do
  215. len = len + 1
  216. msgRet.awardArr[0] = len
  217. local cfg = voucherInvestConfig[id]
  218. populateAwardMsg(human, msgRet.awardArr[len], id, cfg, voucherInvestData.nowRound, maxRound)
  219. if len >= msgOnceLen then
  220. cfgNum = cfgNum - len
  221. if cfgNum <= 0 then
  222. msgRet.isEnd = 1
  223. return Msg.send(msgRet, human.fd)
  224. end
  225. Msg.send(msgRet, human.fd)
  226. len = 0
  227. msgRet.isStart = 0
  228. end
  229. end
  230. if len > 0 then
  231. msgRet.isEnd = 1
  232. Msg.send(msgRet, human.fd)
  233. end
  234. end
  235. -- 领奖
  236. function VoucherInvest_Get(human)
  237. if not isOpen(human) then
  238. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  239. end
  240. if not isBuy(human) then
  241. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  242. end
  243. local voucherInvestData = getData(human)
  244. local nowRound = voucherInvestData.nowRound
  245. local maxRound = getMaxRound()
  246. if nowRound > maxRound then
  247. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  248. end
  249. local ids = getRounCfgIdArr(voucherInvestData.nowRound)
  250. local itemList = {}
  251. for _, id in ipairs(ids) do
  252. local cfg = voucherInvestConfig[id]
  253. local awardState = getAwardState(human, id)
  254. if awardState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  255. -- 更新领取记录
  256. updateGetRecord(human, id)
  257. local itemId, itemNum = cfg.reward[1], cfg.reward[2]
  258. itemList[itemId] = (itemList[itemId] or 0) + itemNum
  259. end
  260. end
  261. if not next(itemList) then
  262. Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  263. end
  264. BagLogic.addItemList(human, itemList, LOGTYPE)
  265. -- 领完则进入下一轮
  266. if #voucherInvestData.getRecord == #ids then
  267. resetData(human, voucherInvestData.nowRound + 1)
  268. end
  269. updateRedDot(human)
  270. VoucherInvest_Query(human)
  271. end