NewHeroLogic.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. local AbsActExcel = require("excel.absAct")
  2. local Lang = require("common.Lang")
  3. local ObjHuman = require("core.ObjHuman")
  4. local Msg = require("core.Msg")
  5. local Broadcast = require("broadcast.Broadcast")
  6. local Grid = require("bag.Grid")
  7. local BagLogic = require("bag.BagLogic")
  8. local ItemDefine = require("bag.ItemDefine")
  9. local BuyLogic = require("topup.BuyLogic")
  10. local AbsActDefine = require("absAct.AbsActDefine")
  11. local AbsActLogic = require("absAct.AbsActLogic")
  12. local Log = require("common.Log")
  13. local YunYingLogic = require("yunying.YunYingLogic")
  14. ABS_ACT_ID = 9001
  15. NEW_HERO_MODULE_FN = "absAct.NewHeroLogic"
  16. NEW_HERO_ACT_TYPES = {}
  17. --
  18. function initAfterHot()
  19. NEW_HERO_ACT_TYPES = {}
  20. for _, config in pairs(AbsActExcel.absActivity) do
  21. if config.moduleFn == NEW_HERO_MODULE_FN then
  22. NEW_HERO_ACT_TYPES[#NEW_HERO_ACT_TYPES + 1] = config.type
  23. end
  24. end
  25. end
  26. function getActiveActId(human)
  27. for _, actType in ipairs(NEW_HERO_ACT_TYPES) do
  28. local state, actId = AbsActLogic.isStartedByType(human, actType)
  29. if state and actId then
  30. return actId
  31. end
  32. end
  33. end
  34. local function getGiftConfig(giftId, actId)
  35. local config = AbsActExcel.absEva[giftId]
  36. if not config or config.actId ~= actId then
  37. return
  38. end
  39. return config
  40. end
  41. local function copyRewardList(reward)
  42. local items = {}
  43. for _, v in ipairs(reward) do
  44. items[#items + 1] = {v[1], v[2], v[3]}
  45. end
  46. return items
  47. end
  48. --
  49. function isActive(human, YYInfo, funcConfig)
  50. return not isOpen(human, YYInfo, funcConfig)
  51. end
  52. --
  53. function isOpen(human, YYInfo, funcConfig)
  54. return AbsActLogic.isStarted(human, funcConfig.funcID)
  55. end
  56. --
  57. function isRed(human, YYInfo, funcConfig)
  58. return false
  59. end
  60. --
  61. function getLeftTime(human, YInfo, funcConfig)
  62. local actId = funcConfig and funcConfig.funcID or getActiveActId(human)
  63. if not actId then
  64. return 0
  65. end
  66. local ret, endTime, startTime = AbsActLogic.isStarted(human, actId)
  67. if ret == true then
  68. return endTime - os.time()
  69. else
  70. return 0
  71. end
  72. end
  73. function getNewHeroID(human)
  74. local actId = getActiveActId(human)
  75. if not actId then return 0 end
  76. local absActConfig = AbsActExcel.absActivity[actId]
  77. if not absActConfig then return 0 end
  78. return absActConfig.icon
  79. end
  80. local function getActClientIndex(actId)
  81. if actId == 9001 then
  82. return 1
  83. elseif actId == 9002 then
  84. return 2
  85. elseif actId == 9003 then
  86. return 3
  87. end
  88. return 0
  89. end
  90. local function getActLogType(actId)
  91. if actId == 9002 then
  92. return "newhero_reward_9002"
  93. elseif actId == 9003 then
  94. return "newhero_reward_9003"
  95. end
  96. return "newhero_reward"
  97. end
  98. function getAndSendMsg(human, id, actId)
  99. local now = os.time()
  100. local startedFlag, endTime, startTime = AbsActLogic.isStarted(human, id)
  101. if not startedFlag then
  102. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  103. end
  104. local absActConfig = AbsActExcel.absActivity[id]
  105. if not absActConfig then
  106. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  107. end
  108. local absAct = human.db.absAct[id]
  109. if not absAct then
  110. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  111. end
  112. local msgRet = Msg.gc.GC_ABS_NEWHERO
  113. msgRet.id = absActConfig.icon
  114. msgRet.startTime = startTime
  115. msgRet.endTime = endTime
  116. msgRet.actIndex = getActClientIndex(id)
  117. local index = 0
  118. local giftIds = {}
  119. for giftId, v in pairs(AbsActExcel.absEva) do
  120. if v.actId == id then
  121. giftIds[#giftIds + 1] = giftId
  122. end
  123. end
  124. table.sort(giftIds)
  125. for _, giftId in ipairs(giftIds) do
  126. local v = AbsActExcel.absEva[giftId]
  127. index = index + 1
  128. for j = 1, #v.reward do
  129. Grid.makeItem(msgRet.giftlist[index].item[j], v.reward[j][1], v.reward[j][2])
  130. end
  131. msgRet.giftlist[index].item[0] = #v.reward
  132. msgRet.giftlist[index].cnt = absAct.newheroCnt and absAct.newheroCnt[giftId] or 0
  133. msgRet.giftlist[index].id = giftId
  134. msgRet.giftlist[index].maxCnt = v.limit
  135. local buyID = v.buyID
  136. msgRet.giftlist[index].buyMsg[0] = 1
  137. if v.buyID == 0 then
  138. msgRet.giftlist[index].buyMsg[1] = {
  139. buyID = buyID,
  140. region = "zuanshi",
  141. cost = v.price,
  142. icon = 0,
  143. name = "",
  144. isFirst = 0,
  145. desc = "",
  146. doubleCnt = 0,
  147. actDoubleCnt = 0,
  148. useDoubleCnt = 0,
  149. buyCnt = 0,
  150. vipExp = 0,
  151. yuanjia = 0,
  152. zhekou = 0,
  153. voucher = 0,
  154. productId = "",
  155. nCanBuyNum = 0,
  156. }
  157. else
  158. BuyLogic.fontBuyItem(human, msgRet.giftlist[index].buyMsg[1], buyID)
  159. end
  160. end
  161. msgRet.giftlist[0] = index
  162. if index == 0 then
  163. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  164. end
  165. Msg.send(msgRet, human.fd)
  166. end
  167. function getgiftbybuyid(buyid, actId)
  168. for giftId, v in pairs(AbsActExcel.absEva) do
  169. if v.buyID == buyid and v.actId == actId then
  170. return giftId
  171. end
  172. end
  173. end
  174. function newheroGift(human, id, buyConf, isFirst, cnt, buyNum)
  175. local state = AbsActLogic.isStarted(human, id)
  176. local absConfig = AbsActExcel.absActivity[id]
  177. if absConfig == nil then
  178. Log.write(Log.LOGID_OSS_COMMON_ACT, string.format(
  179. "[NewHeroLogic.newheroGift] absActivity config not found, actId=%s, buyID=%s, newUniqueTag=%s",
  180. tostring(id), tostring(buyConf and buyConf.id), tostring(human.db.newUniqueTag)))
  181. return
  182. end
  183. if not state then
  184. Log.write(Log.LOGID_OSS_COMMON_ACT, string.format(
  185. "[NewHeroLogic.newheroGift] act not started, actId=%s, buyID=%s, newUniqueTag=%s, now=%s",
  186. tostring(id), tostring(buyConf.id), tostring(human.db.newUniqueTag), os.time()))
  187. return
  188. end
  189. -- 存在多个特惠礼包 同时 开放
  190. local giftid = getgiftbybuyid(buyConf.id, id)
  191. if not giftid then
  192. Log.write(Log.LOGID_OSS_COMMON_ACT, string.format(
  193. "[NewHeroLogic.newheroGift] absEva gift not found, actId=%s, buyID=%s, newUniqueTag=%s",
  194. tostring(id), tostring(buyConf.id), tostring(human.db.newUniqueTag)))
  195. return
  196. end
  197. local config = getGiftConfig(giftid, id)
  198. if not config then
  199. Log.write(Log.LOGID_OSS_COMMON_ACT, string.format(
  200. "[NewHeroLogic.newheroGift] absEva gift config invalid, actId=%s, giftId=%s, buyID=%s, newUniqueTag=%s",
  201. tostring(id), tostring(giftid), tostring(buyConf.id), tostring(human.db.newUniqueTag)))
  202. return
  203. end
  204. AbsActLogic.checkAbsActClean(human, id)
  205. human.db.absAct[id].newheroCnt = human.db.absAct[id].newheroCnt or { }
  206. local nowBuyCnt = human.db.absAct[id].newheroCnt[giftid] or 0
  207. -- 判断是否达到购买上限
  208. if nowBuyCnt >= config.limit then
  209. Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
  210. return
  211. end
  212. -- if nowBuyCnt + buyNum >= config.limit then
  213. -- Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
  214. -- return
  215. -- end
  216. -- 当金币购买之后超过最大金币时,不允许购买
  217. for j = 1, #config.reward do
  218. local itemID = config.reward[j][1]
  219. local itemCnt = config.reward[j][2]
  220. if itemID == ItemDefine.ITEM_JINBI_ID then
  221. if not ObjHuman.canAddJinbi(human, itemCnt) then
  222. return
  223. end
  224. end
  225. end
  226. -- 增加已购买次数
  227. -- human.db.absAct[id].newheroCnt[giftid] = nowBuyCnt + buyNum
  228. -- -- 发放物品
  229. -- local items = { }
  230. -- for _, v in ipairs(config.reward) do
  231. -- table.insert(items, {v[1], v[2] * buyNum})
  232. -- end
  233. -- BagLogic.addItemList(human, config.reward, "newhero_reward")
  234. human.db.absAct[id].newheroCnt[giftid] = nowBuyCnt + 1
  235. -- 发放物品
  236. local items = copyRewardList(config.reward)
  237. BagLogic.addItemList(human, items, getActLogType(id))
  238. Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
  239. AbsActLogic.actDetailQuery(human, id)
  240. end
  241. function giftBuy(human, giftid)
  242. local actId = getActiveActId(human)
  243. if not actId then
  244. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  245. end
  246. local state = AbsActLogic.isStarted(human, actId)
  247. if state ~= true then
  248. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  249. end
  250. local absConfig = AbsActExcel.absActivity[actId]
  251. if not absConfig then
  252. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  253. end
  254. AbsActLogic.checkAbsActClean(human, actId)
  255. -- 存在多个特惠礼包 同时 开放
  256. local config = getGiftConfig(giftid, actId)
  257. if not config then
  258. return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
  259. end
  260. if config.buyID ~= 0 then
  261. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  262. end
  263. -- 初始化已购买次数
  264. human.db.absAct[actId].newheroCnt = human.db.absAct[actId].newheroCnt or { }
  265. local nowBuyCnt = human.db.absAct[actId].newheroCnt[giftid] or 0
  266. -- 判断是否达到购买上限
  267. if nowBuyCnt >= config.limit then
  268. Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
  269. return
  270. end
  271. local itemID = ItemDefine.ITEM_ZUANSHI_ID
  272. local itemCnt = config.price
  273. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  274. Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  275. return
  276. end
  277. -- 当金币购买之后超过最大金币时,不允许购买
  278. for j = 1, #config.reward do
  279. local itemID = config.reward[j][1]
  280. local itemCnt = config.reward[j][2]
  281. if itemID == ItemDefine.ITEM_JINBI_ID then
  282. if not ObjHuman.canAddJinbi(human, itemCnt) then
  283. return
  284. end
  285. end
  286. end
  287. BagLogic.delItem(human, itemID, itemCnt, getActLogType(actId))
  288. -- 增加已购买次数
  289. human.db.absAct[actId].newheroCnt[giftid] = nowBuyCnt + 1
  290. -- 发放物品
  291. local items = copyRewardList(config.reward)
  292. BagLogic.addItemList(human, items, getActLogType(actId))
  293. Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
  294. AbsActLogic.actDetailQuery(human, actId)
  295. end
  296. function updateDaily(human, id)
  297. if not id then return end
  298. if human.db.absAct[id] then
  299. human.db.absAct[id] = {}
  300. end
  301. local state = AbsActLogic.isStarted(human, id)
  302. if state ~= true then
  303. if getActiveActId(human) == nil then
  304. human.db.drawCard.list[8] = {}
  305. end
  306. end
  307. AbsActLogic.actDetailQuery(human, id)
  308. end
  309. -- function GetRemainNum(human, nBuyID)
  310. -- local config = AbsActExcel.absEva[nBuyID]
  311. -- if not config then
  312. -- return 0
  313. -- end
  314. -- local giftid = getgiftbybuyid(nBuyID)
  315. -- if not giftid then return 0 end
  316. -- if not human.db.absAct[ABS_ACT_ID].newheroCnt then
  317. -- return config.limit
  318. -- end
  319. -- if not human.db.absAct[ABS_ACT_ID].newheroCnt[giftid] then
  320. -- return config.limit
  321. -- else
  322. -- return config.limit - human.db.absAct[ABS_ACT_ID].newheroCnt[giftid]
  323. -- end
  324. -- end