VoucherInvest.lua 8.5 KB

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