SpecialCustomLogic.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. --[[
  2. absAct[od] = {
  3. coustom[id] = {
  4. selectItem[1] =
  5. selectItem[2] =
  6. selectItem[3] =
  7. state =
  8. cnt =
  9. }
  10. }
  11. ]]
  12. local AbsActLogic = require("absAct.AbsActLogic")
  13. local AbsActExcel = require("excel.absAct")
  14. local BuyExcel = require("excel.buy")
  15. local Util = require("common.Util")
  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 ObjHuman = require("core.ObjHuman")
  21. local Lang = require("common.Lang")
  22. local Broadcast = require("broadcast.Broadcast")
  23. function isOpen(human, YYInfo, funcConfig)
  24. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
  25. if not state then return end
  26. local absAct = human.db.absAct[funcConfig.funcID]
  27. if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
  28. return false
  29. end
  30. return true, endTime, startTime
  31. end
  32. function isActive(human, YYInfo, funcConfig)
  33. return not isOpen(human, YYInfo, funcConfig)
  34. end
  35. --发送定制礼包数据
  36. -- id 活动id
  37. function getAndSendMsg(human,id,actID)
  38. -- 活动未开,返回
  39. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  40. if not state then return end
  41. local absConfig = AbsActExcel.absActivity[id]
  42. if not absConfig then return end
  43. local absAct = human.db.absAct[id]
  44. if not absAct then
  45. return
  46. end
  47. -- 初始化custom数据
  48. absAct.custom = absAct.custom or {}
  49. -- 构造数据
  50. local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_QUERY
  51. local config = AbsActExcel.custom
  52. local len = 0
  53. for k,v in pairs(config) do
  54. if v.actId == absConfig.actId then
  55. len = len + 1
  56. local net = msgRet.giftList[len]
  57. net.id = k
  58. net.limitCnt = v.limitCnt
  59. net.price = v.price
  60. net.name = v.name
  61. net.nowCnt = absAct.custom[k] and absAct.custom[k].cnt or 0
  62. net.state = absAct.custom[k] and absAct.custom[k].state or 0
  63. net.buyItem[0] = 1
  64. if v.buyID ~= 0 then
  65. BuyLogic.fontBuyItem(human, net.buyItem[1], v.buyID)
  66. else
  67. net.buyItem[0] = 0
  68. end
  69. local warehouseLen = #v.fixed
  70. for j = 1,warehouseLen do
  71. local itemID = v.fixed[j][1]
  72. local itemCnt = v.fixed[j][2]
  73. Grid.makeItem(net.fixed[j],itemID,itemCnt)
  74. end
  75. net.fixed[0] = warehouseLen
  76. if net.nowCnt >= net.limitCnt then
  77. net.first[0] = 0
  78. net.second[0] = 0
  79. net.third[0] = 0
  80. else
  81. warehouseLen = #v.first
  82. for j = 1,warehouseLen do
  83. local itemID = v.first[j][1]
  84. local itemCnt = v.first[j][2]
  85. Grid.makeItem(net.first[j],itemID,itemCnt)
  86. end
  87. net.first[0] = warehouseLen
  88. warehouseLen = #v.second
  89. for j = 1,warehouseLen do
  90. local itemID = v.second[j][1]
  91. local itemCnt = v.second[j][2]
  92. Grid.makeItem(net.second[j],itemID,itemCnt)
  93. end
  94. net.second[0] = warehouseLen
  95. warehouseLen = #v.third
  96. for j = 1,warehouseLen do
  97. local itemID = v.third[j][1]
  98. local itemCnt = v.third[j][2]
  99. Grid.makeItem(net.third[j],itemID,itemCnt)
  100. end
  101. net.third[0] = warehouseLen
  102. end
  103. -- 状态为0,表示未选择物品,发送物品库
  104. if net.state == 0 then
  105. net.one[0] = 0
  106. net.two[0] = 0
  107. net.three[0] = 0
  108. else
  109. -- 状态不为0,表示已卖完或已选择物品,展示已选择物品
  110. Grid.makeItem(net.one[1],absAct.custom[k].selectItem[1][1],absAct.custom[k].selectItem[1][2])
  111. Grid.makeItem(net.two[1],absAct.custom[k].selectItem[2][1],absAct.custom[k].selectItem[2][2])
  112. net.one[0] = 1
  113. net.two[0] = 1
  114. net.three[0] = 0
  115. if absAct.custom[k].selectItem[3] then
  116. Grid.makeItem(net.three[1],absAct.custom[k].selectItem[3][1],absAct.custom[k].selectItem[3][2])
  117. net.three[0] = 1
  118. end
  119. end
  120. end
  121. end
  122. msgRet.giftList[0] =len
  123. Msg.send(msgRet,human.fd)
  124. end
  125. -- 选择物品
  126. function selectItem(human,id,giftId,first,second,third)
  127. local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_SELECT
  128. msgRet.ret = 1
  129. -- 活动未开,无法选择物品
  130. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  131. if not state then
  132. msgRet.ret = 0
  133. Msg.send(msgRet,human.fd)
  134. return
  135. end
  136. local absConfig = AbsActExcel.absActivity[id]
  137. if not absConfig then
  138. msgRet.ret = 0
  139. Msg.send(msgRet,human.fd)
  140. return
  141. end
  142. local absAct = human.db.absAct[id]
  143. if not absAct then
  144. msgRet.ret = 0
  145. Msg.send(msgRet,human.fd)
  146. return
  147. end
  148. -- 礼包已卖完无法选择物品
  149. if absAct.custom and absAct.custom[giftId] and absAct.custom[giftId].state == 2 then
  150. msgRet.ret = 0
  151. Msg.send(msgRet,human.fd)
  152. return
  153. end
  154. -- 初始化数据
  155. absAct.custom = absAct.custom or {}
  156. absAct.custom[giftId] = absAct.custom[giftId] or {}
  157. absAct.custom[giftId].cnt = absAct.custom[giftId].cnt or 0
  158. absAct.custom[giftId].state = absAct.custom[giftId].state or 0
  159. absAct.custom[giftId].selectItem = absAct.custom[giftId].selectItem or {}
  160. -- 校验物品是否存在
  161. local config = AbsActExcel.custom[giftId]
  162. if config == nil
  163. or config.first[first] == nil
  164. or config.second[second] == nil
  165. or (config.third[third] == nil and third ~= 0) then
  166. msgRet.ret = 0
  167. Msg.send(msgRet,human.fd)
  168. return
  169. end
  170. if (first == 0 and #config.first ~= 0) or
  171. (second == 0 and #config.second ~= 0) or
  172. (third == 0 and #config.third ~= 0) then
  173. return
  174. end
  175. -- 将已选择物品存入数据库
  176. absAct.custom[giftId].selectItem[1] = {}
  177. absAct.custom[giftId].selectItem[1][1] = config.first[first][1]
  178. absAct.custom[giftId].selectItem[1][2] = config.first[first][2]
  179. absAct.custom[giftId].selectItem[2] = {}
  180. absAct.custom[giftId].selectItem[2][1] = config.second[second][1]
  181. absAct.custom[giftId].selectItem[2][2] = config.second[second][2]
  182. -- 并非所有礼包都能选择3个物品
  183. if third ~= 0 then
  184. absAct.custom[giftId].selectItem[3] = {}
  185. absAct.custom[giftId].selectItem[3][1] = config.third[third][1]
  186. absAct.custom[giftId].selectItem[3][2] = config.third[third][2]
  187. end
  188. Msg.send(msgRet,human.fd)
  189. absAct.custom[giftId].state = 1
  190. end
  191. -- 购买礼包
  192. function custom(human,funcID,conf)
  193. customBuy(human,conf.args[1],funcID)
  194. end
  195. function customBuy(human,args,id)
  196. -- 活动未开,返回
  197. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  198. if not state then return end
  199. local absConfig = AbsActExcel.absActivity[id]
  200. if not absConfig then return end
  201. local absAct = human.db.absAct[id]
  202. if not absAct then
  203. return
  204. end
  205. -- 购买状态不为可购买,返回
  206. if not absAct.custom or not absAct.custom[args] or absAct.custom[args].state ~= 1 then
  207. return
  208. end
  209. -- 未选择物品,返回
  210. if #absAct.custom[args].selectItem <= 0 then
  211. return
  212. end
  213. -- 已达到限购次数,返回
  214. local config = AbsActExcel.custom[args]
  215. if absAct.custom[args].cnt >= config.limitCnt then
  216. return
  217. end
  218. -- 钻石价格不为0时,表示钻石购买,则需判断钻石是否足够
  219. if config.price ~= 0 then
  220. if not ObjHuman.checkRMB(human, config.price) then
  221. return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  222. end
  223. ObjHuman.decZuanshi(human, -config.price, "abs_custom")
  224. end
  225. -- 购买次数增加一次
  226. absAct.custom[args].cnt = absAct.custom[args].cnt + 1
  227. -- 状态设置为已卖完,此时,为防止后续报错,导致可无限购买,故先设置为无法继续购买
  228. absAct.custom[args].state = 2
  229. -- 统计物品,并发放
  230. local item = {}
  231. for i = 1, #config.fixed do
  232. item[#item+1] = {config.fixed[i][1],config.fixed[i][2]}
  233. end
  234. for i = 1,#absAct.custom[args].selectItem do
  235. item[#item+1] = {absAct.custom[args].selectItem[i][1],absAct.custom[args].selectItem[i][2]}
  236. end
  237. BagLogic.addItemList(human, item, "abs_custom")
  238. -- 判断是否达到限购次数,未达到则将购买状态设为选择物品
  239. if absAct.custom[args].cnt < config.limitCnt then
  240. absAct.custom[args].selectItem = {}
  241. absAct.custom[args].state = 0
  242. else
  243. absAct.custom[args].state = 2
  244. end
  245. getAndSendMsg(human,id)
  246. end