SpecialCustomLogic.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. local Log = require("common.Log")
  24. local SPECIALCISTOMACTID = 31004 -- 活动ID
  25. local SPECIALCISTOMNOBUYID = 20001 -- 不使用的投放ID
  26. function isOpen(human, YYInfo, funcConfig)
  27. local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
  28. if not state then return end
  29. local absAct = human.db.absAct[funcConfig.funcID]
  30. if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
  31. return false
  32. end
  33. return true, endTime, startTime
  34. end
  35. function isActive(human, YYInfo, funcConfig)
  36. return not isOpen(human, YYInfo, funcConfig)
  37. end
  38. --发送定制礼包数据
  39. -- id 活动id
  40. function getAndSendMsg(human,id,actID)
  41. -- 活动未开,返回
  42. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  43. if not state then return end
  44. local absConfig = AbsActExcel.absActivity[id]
  45. if not absConfig then return end
  46. local absAct = human.db.absAct[id]
  47. if not absAct then
  48. return
  49. end
  50. -- 初始化custom数据
  51. absAct.custom = absAct.custom or {}
  52. -- 构造数据
  53. local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_QUERY
  54. local config = AbsActExcel.custom
  55. local len = 0
  56. for k,v in pairs(config) do
  57. if len >= 3 then
  58. msgRet.giftList[0] =len
  59. msgRet.isEnd = 0
  60. Msg.send(msgRet,human.fd)
  61. len = 0
  62. msgRet = Msg.gc.GC_ABS_ND_CUSTOM_QUERY
  63. end
  64. if v.actId == absConfig.actId then
  65. len = len + 1
  66. local net = msgRet.giftList[len]
  67. net.id = k
  68. net.limitCnt = v.limitCnt
  69. net.price = v.price
  70. net.name = v.name
  71. net.nowCnt = absAct.custom[k] and absAct.custom[k].cnt or 0
  72. net.state = absAct.custom[k] and absAct.custom[k].state or 0
  73. net.buyItem[0] = 1
  74. if v.buyID ~= 0 then
  75. BuyLogic.fontBuyItem(human, net.buyItem[1], v.buyID)
  76. else
  77. net.buyItem[0] = 0
  78. end
  79. local warehouseLen = #v.fixed
  80. for j = 1,warehouseLen do
  81. local itemID = v.fixed[j][1]
  82. local itemCnt = v.fixed[j][2]
  83. Grid.makeItem(net.fixed[j],itemID,itemCnt)
  84. end
  85. net.fixed[0] = warehouseLen
  86. if net.nowCnt >= net.limitCnt then
  87. net.first[0] = 0
  88. net.second[0] = 0
  89. net.third[0] = 0
  90. else
  91. warehouseLen = #v.first
  92. for j = 1,warehouseLen do
  93. local itemID = v.first[j][1]
  94. local itemCnt = v.first[j][2]
  95. Grid.makeItem(net.first[j],itemID,itemCnt)
  96. end
  97. net.first[0] = warehouseLen
  98. warehouseLen = #v.second
  99. for j = 1,warehouseLen do
  100. local itemID = v.second[j][1]
  101. local itemCnt = v.second[j][2]
  102. Grid.makeItem(net.second[j],itemID,itemCnt)
  103. end
  104. net.second[0] = warehouseLen
  105. warehouseLen = #v.third
  106. for j = 1,warehouseLen do
  107. local itemID = v.third[j][1]
  108. local itemCnt = v.third[j][2]
  109. Grid.makeItem(net.third[j],itemID,itemCnt)
  110. end
  111. net.third[0] = warehouseLen
  112. end
  113. -- 状态为0,表示未选择物品,发送物品库
  114. if net.state == 0 then
  115. net.one[0] = 0
  116. net.two[0] = 0
  117. net.three[0] = 0
  118. else
  119. -- 状态不为0,表示已卖完或已选择物品,展示已选择物品
  120. Grid.makeItem(net.one[1],absAct.custom[k].selectItem[1][1],absAct.custom[k].selectItem[1][2])
  121. Grid.makeItem(net.two[1],absAct.custom[k].selectItem[2][1],absAct.custom[k].selectItem[2][2])
  122. net.one[0] = 1
  123. net.two[0] = 1
  124. net.three[0] = 0
  125. if absAct.custom[k].selectItem[3] then
  126. Grid.makeItem(net.three[1],absAct.custom[k].selectItem[3][1],absAct.custom[k].selectItem[3][2])
  127. net.three[0] = 1
  128. end
  129. end
  130. end
  131. end
  132. msgRet.giftList[0] = len
  133. msgRet.isEnd = 1
  134. Msg.send(msgRet,human.fd)
  135. end
  136. -- 选择物品
  137. function selectItem(human,id,giftId,first,second,third)
  138. local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_SELECT
  139. msgRet.ret = 1
  140. -- 活动未开,无法选择物品
  141. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  142. if not state then
  143. msgRet.ret = 0
  144. Msg.send(msgRet,human.fd)
  145. return
  146. end
  147. local absConfig = AbsActExcel.absActivity[id]
  148. if not absConfig then
  149. msgRet.ret = 0
  150. Msg.send(msgRet,human.fd)
  151. return
  152. end
  153. local absAct = human.db.absAct[id]
  154. if not absAct then
  155. msgRet.ret = 0
  156. Msg.send(msgRet,human.fd)
  157. return
  158. end
  159. -- 礼包已卖完无法选择物品
  160. if absAct.custom and absAct.custom[giftId] and absAct.custom[giftId].state == 2 then
  161. msgRet.ret = 0
  162. Msg.send(msgRet,human.fd)
  163. return
  164. end
  165. -- 校验物品是否存在
  166. absAct.custom = absAct.custom or {}
  167. absAct.custom[giftId] = absAct.custom[giftId] or {}
  168. absAct.custom[giftId].cnt = absAct.custom[giftId].cnt or 0
  169. absAct.custom[giftId].state = absAct.custom[giftId].state or 0
  170. absAct.custom[giftId].selectItem = absAct.custom[giftId].selectItem or {}
  171. -- 将已选择物品存入数据库
  172. local config = AbsActExcel.custom[giftId]
  173. if config == nil
  174. or config.first[first] == nil
  175. or config.second[second] == nil
  176. or (config.third[third] == nil and third ~= 0) then
  177. msgRet.ret = 0
  178. Msg.send(msgRet,human.fd)
  179. return
  180. end
  181. if (first == 0 and #config.first ~= 0) or
  182. (second == 0 and #config.second ~= 0) or
  183. (third == 0 and #config.third ~= 0) then
  184. return
  185. end
  186. -- 并非所有礼包都能选择3个物品
  187. absAct.custom[giftId].selectItem[1] = {}
  188. absAct.custom[giftId].selectItem[1][1] = config.first[first][1]
  189. absAct.custom[giftId].selectItem[1][2] = config.first[first][2]
  190. absAct.custom[giftId].selectItem[2] = {}
  191. absAct.custom[giftId].selectItem[2][1] = config.second[second][1]
  192. absAct.custom[giftId].selectItem[2][2] = config.second[second][2]
  193. -- 并非所有礼包都能选择3个物品
  194. if third ~= 0 then
  195. absAct.custom[giftId].selectItem[3] = {}
  196. absAct.custom[giftId].selectItem[3][1] = config.third[third][1]
  197. absAct.custom[giftId].selectItem[3][2] = config.third[third][2]
  198. end
  199. Msg.send(msgRet,human.fd)
  200. absAct.custom[giftId].state = 1
  201. end
  202. -- 购买礼包
  203. function custom(human,funcID, conf, isFirst, cnt, buyNum)
  204. customBuy(human,conf.args[1],funcID, true, buyNum)
  205. end
  206. function customBuy(human,args,id, bFromPay, buyNum)
  207. -- 活动未开,返回
  208. local state, endTime, starTime = AbsActLogic.isStarted(human, id)
  209. if not state then return end
  210. local absConfig = AbsActExcel.absActivity[id]
  211. if not absConfig then return end
  212. local absAct = human.db.absAct[id]
  213. if not absAct then
  214. return
  215. end
  216. buyNum = buyNum or 1
  217. -- 检查来源
  218. local tConfig = AbsActExcel.custom[args]
  219. if not tConfig then
  220. return
  221. end
  222. -- 付费奖励
  223. if tConfig.buyID ~= 0 then
  224. if not bFromPay or true ~= bFromPay then
  225. local szText = "[SpecialCustonLogic_customBuy] 付费奖励, 协议请求领取 来源不正确 name = "..human.db.name.." _id = "..human.db._id.." 礼包ID = "..args
  226. Log.write(Log.LOGID_OSS_BT, szText)
  227. return
  228. end
  229. end
  230. -- 购买状态不为可购买,返回
  231. if not absAct.custom or not absAct.custom[args] or absAct.custom[args].state ~= 1 then
  232. return
  233. end
  234. -- 未选择物品,返回
  235. if #absAct.custom[args].selectItem <= 0 then
  236. return
  237. end
  238. -- 已达到限购次数,返回
  239. local config = AbsActExcel.custom[args]
  240. -- if absAct.custom[args].cnt >= config.limitCnt then
  241. -- return
  242. -- end
  243. if buyNum + absAct.custom[args].cnt > config.limitCnt then
  244. return
  245. end
  246. -- 钻石价格不为0时,表示钻石购买,则需判断钻石是否足够
  247. if config.price ~= 0 then
  248. if not ObjHuman.checkRMB(human, config.price) then
  249. return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  250. end
  251. ObjHuman.decZuanshi(human, -config.price, "abs_custom")
  252. end
  253. -- 购买次数增加一次
  254. -- absAct.custom[args].cnt = absAct.custom[args].cnt + 1
  255. absAct.custom[args].cnt = absAct.custom[args].cnt + buyNum
  256. -- 状态设置为已卖完,此时,为防止后续报错,导致可无限购买,故先设置为无法继续购买
  257. absAct.custom[args].state = 2
  258. -- 统计物品,并发放
  259. local item = {}
  260. for i = 1, #config.fixed do
  261. -- item[#item+1] = {config.fixed[i][1],config.fixed[i][2]}
  262. item[#item+1] = {config.fixed[i][1],config.fixed[i][2] * buyNum}
  263. end
  264. for i = 1,#absAct.custom[args].selectItem do
  265. -- item[#item+1] = {absAct.custom[args].selectItem[i][1],absAct.custom[args].selectItem[i][2]}
  266. item[#item+1] = {absAct.custom[args].selectItem[i][1],absAct.custom[args].selectItem[i][2]*buyNum}
  267. end
  268. BagLogic.addItemList(human, item, "abs_custom")
  269. -- 判断是否达到限购次数,未达到则将购买状态设为选择物品
  270. if absAct.custom[args].cnt < config.limitCnt then
  271. --absAct.custom[args].selectItem = {}
  272. -- absAct.custom[args].state = 0
  273. absAct.custom[args].state = 1
  274. else
  275. absAct.custom[args].state = 2
  276. end
  277. getAndSendMsg(human,id)
  278. end
  279. -- 是否有红点
  280. function isRed(human, YYInfo, funcConfig)
  281. local tConfig = AbsActExcel.custom
  282. if not isRed then
  283. return false
  284. end
  285. local tActDBData = human.db.absAct[SPECIALCISTOMACTID]
  286. if not tActDBData then
  287. return false
  288. end
  289. local bRed = false
  290. for k, v in pairs(tConfig) do
  291. if 0 == v.buyID and k < SPECIALCISTOMNOBUYID then
  292. if not tActDBData.custom or not tActDBData.custom[k] then
  293. bRed = true
  294. else
  295. if tActDBData.custom[k].state then
  296. bRed = tActDBData.custom[k].state ~= 2 and true or false
  297. else
  298. bRed = true
  299. end
  300. end
  301. if bRed == true then
  302. break
  303. end
  304. end
  305. end
  306. return bRed
  307. end
  308. function GetRemainNum(human, nBuyID)
  309. local tConfig = AbsActExcel.custom
  310. local nChoseID, nLimitNum = nil, nil
  311. for nID, v in pairs(tConfig) do
  312. if v.buyID == nBuyID and nID < SPECIALCISTOMNOBUYID then
  313. nChoseID = nID
  314. nLimitNum = v.limitCnt
  315. break
  316. end
  317. end
  318. local tActDBData = human.db.absAct[SPECIALCISTOMACTID]
  319. if not tActDBData then
  320. return 0
  321. end
  322. if not nChoseID then
  323. return 0
  324. end
  325. if tActDBData.custom and tActDBData.custom[nChoseID] and tActDBData.custom[nChoseID].cnt then
  326. return tActDBData.custom[nChoseID].cnt >= nLimitNum and 0 or (nLimitNum - tActDBData.custom[nChoseID].cnt)
  327. else
  328. return nLimitNum
  329. end
  330. end