ZhuanpanLogic.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. local Lang = require("common.Lang")
  2. local Msg = require("core.Msg")
  3. local ObjHuman = require("core.ObjHuman")
  4. local ZhuanpanExcel = require("excel.zhuanpan")
  5. local ItemDefine = require("bag.ItemDefine")
  6. local BagLogic = require("bag.BagLogic")
  7. local Grid = require("bag.Grid")
  8. local Broadcast = require("broadcast.Broadcast")
  9. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  10. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  11. local ItemExcel = require("excel.item").item
  12. local Util = require("common.Util")
  13. local HeroExcel = require("excel.hero").hero
  14. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  15. local VipLogic = require("vip.VipLogic")
  16. local EquipLogic = require("equip.EquipLogic")
  17. local YunYingLogic = require("yunying.YunYingLogic")
  18. local TriggerDefine = require("trigger.TriggerDefine")
  19. local TriggerLogic = require("trigger.TriggerLogic")
  20. local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
  21. local ClutterDataLogic = require("clutter.ClutterDataLogic")
  22. local ClutterDataDefine = require("clutter.ClutterDataDefine")
  23. local ZhuanpanGift = require("zhuanpan.ZhuanpanGift")
  24. DEFAULT_ZHUANPAN_TYPE_NORMAL = 1 -- 基础转盘
  25. DEFAULT_ZHUANPAN_TYPE_GAOJI = 2 -- 高级转盘
  26. DEFAULT_BUY_ZHUANPAN_CNT = 50 -- 购买许愿珠所需钻石
  27. DEFAULT_FREE_REFRESH_TIME = 10800 -- 免费刷新的时间
  28. ZHUAN_PAN_ITEM_KIND_CNT = 8 -- 转盘种类数量
  29. DEFAULT_MAX_LUCK = 1000
  30. REWARD_RECORD = { index={}, order={} }
  31. REWARD_RECORD_CNT = 15
  32. function getLuckConfig(mainType)
  33. local config = nil
  34. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  35. config = ZhuanpanExcel.luck1
  36. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  37. config = ZhuanpanExcel.luck2
  38. end
  39. return config
  40. end
  41. function red(human, type)
  42. if not human.db.zhuanpan then return end
  43. local zhuanpan = human.db.zhuanpan[type]
  44. local config = getLuckConfig(type)
  45. if zhuanpan then
  46. local luck = zhuanpan.luck or 0
  47. local luckStatus = zhuanpan.luckStatus
  48. for k, v in pairs(config) do
  49. if luck >= k then
  50. if luckStatus[k] == 1 then
  51. return true
  52. end
  53. end
  54. end
  55. end
  56. if zhuanpan.free == 1 then
  57. return true
  58. end
  59. end
  60. -- 转盘查询
  61. function query(human, mainType)
  62. local zhuanpanConfig = ZhuanpanExcel[mainType]
  63. if not zhuanpanConfig then return end
  64. -- 高级转盘判断等级
  65. local roleConfig = nil
  66. local flag, roleConfig = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_501)
  67. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  68. if flag ~= true then
  69. return Broadcast.sendErr(human, Lang.ZHUANPAN_GAIJI_NEED_LV)
  70. end
  71. end
  72. local oldCnt = getCntByType(human, mainType)
  73. local oldLuck
  74. local tOldStatus = nil
  75. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  76. oldLuck = human.db.zhuanpan[mainType].luck
  77. if human.db.zhuanpan[mainType].luckStatus then
  78. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  79. end
  80. end
  81. -- 更新
  82. update(human, mainType)
  83. -- 初始化
  84. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  85. local zuanpan = human.db.zhuanpan[mainType]
  86. local config = ZhuanpanExcel.define[mainType]
  87. local msgRet = Msg.gc.GC_ZHUANPAN_QUERY
  88. msgRet.type = mainType
  89. msgRet.isFirst = zuanpan.free == 1 and 1 or 0
  90. local now = os.time()
  91. local ts1 = now - zuanpan.ts1
  92. msgRet.refreshTime = 24 * 60 * 60 - ts1
  93. msgRet.freeRefreshTime = DEFAULT_FREE_REFRESH_TIME - (now - zuanpan.ts2)
  94. if msgRet.freeRefreshTime < 0 then
  95. msgRet.freeRefreshTime = 0
  96. end
  97. Grid.makeItem(msgRet.itemID, config.useItemID , 1)
  98. msgRet.itemCnt[0] = 2
  99. msgRet.itemCnt[1] = config.useItemCnt1
  100. msgRet.itemCnt[2] = config.useItemCnt2
  101. msgRet.drawCnt = config.drawCnt2
  102. Grid.makeItem(msgRet.zuanshiNeed, config.refreshCost[1][1], config.refreshCost[1][2])
  103. msgRet.needLv = config and config.lv or 0
  104. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  105. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_GAOJI].lv
  106. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  107. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL].lv
  108. end
  109. msgRet.needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  110. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  111. local data = zuanpan[i]
  112. local dataID = data.id
  113. local tempConfig = zhuanpanConfig[dataID]
  114. Grid.makeItem(msgRet.list[i].item, data.itemID, data.itemCnt)
  115. msgRet.list[i].id = dataID
  116. msgRet.list[i].chance = tempConfig.chance
  117. msgRet.list[i].maxCnt = tempConfig.getCnt
  118. msgRet.list[i].getCnt = 0
  119. if data.getCnt then
  120. msgRet.list[i].getCnt = data.getCnt
  121. end
  122. end
  123. local luckConfig = getLuckConfig(mainType)
  124. msgRet.luck = zuanpan.luck or 0
  125. local len = 0
  126. for k, v in pairs(luckConfig) do
  127. len = len + 1
  128. local net = msgRet.luckList[len]
  129. net.id = k
  130. net.status = zuanpan.luckStatus[k] or 0
  131. Grid.makeItem(net.item, v.reward[1] , v.reward[2])
  132. end
  133. msgRet.luckList[0] = len
  134. len = 0
  135. if REWARD_RECORD[mainType] then
  136. for k, v in ipairs(REWARD_RECORD[mainType]) do
  137. len = len + 1
  138. local net = msgRet.record[len]
  139. net.id = k
  140. net.tips = v.tips
  141. net.timer = v.timer
  142. net.order = v.order
  143. end
  144. end
  145. msgRet.record[0] = len
  146. msgRet.list[0] = ZHUAN_PAN_ITEM_KIND_CNT
  147. len = 0
  148. local totalWeight = 0
  149. local chanceList = {}
  150. for k, v in pairs(zhuanpanConfig) do
  151. totalWeight = totalWeight + v.chance
  152. chanceList[k] = chanceList[k] or {}
  153. local rewardWeight = 0
  154. for a, b in ipairs(v.rewardID) do
  155. rewardWeight = rewardWeight + b[3]
  156. end
  157. chanceList[k].totalWeight = rewardWeight
  158. end
  159. -- 大全重占比
  160. local temp = {}
  161. for k, v in pairs(zhuanpanConfig) do
  162. local chance = chanceList[k]
  163. local scale = v.chance / totalWeight
  164. for a, b in ipairs(v.rewardID) do
  165. local itemID = b[1]
  166. if not temp[itemID] then
  167. local weight = b[3] / chance.totalWeight
  168. len = len + 1
  169. local net = msgRet.chanceList[len]
  170. local itemConfig = ItemDefine.getConfig(itemID)
  171. if not itemConfig then
  172. print(itemID)
  173. end
  174. net.tips = itemConfig and itemConfig.name or ""
  175. net.chance = weight * scale * 100
  176. temp[itemID] = 1
  177. end
  178. end
  179. end
  180. msgRet.chanceList[0] = len
  181. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  182. msgRet.red1 = red1 == true and 1 or 0
  183. local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  184. msgRet.red2 = red2 == true and 1 or 0
  185. Msg.send(msgRet, human.fd)
  186. end
  187. function refresh(human, mainType, needTime, bRefreshLuck)
  188. if not ZhuanpanExcel[mainType] then
  189. return
  190. end
  191. local oldCnt = getCntByType(human, mainType)
  192. local oldLuck
  193. local tOldStatus = nil
  194. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  195. oldLuck = human.db.zhuanpan[mainType].luck
  196. if human.db.zhuanpan[mainType].luckStatus and not bRefreshLuck then
  197. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  198. end
  199. end
  200. -- 更新
  201. update(human, mainType)
  202. -- 初始化
  203. initDB(human, mainType, oldCnt,oldLuck,tOldStatus)
  204. -- 判断是否免费
  205. if needTime then
  206. local config = ZhuanpanExcel.define[mainType]
  207. local needCost = 0
  208. local now = os.time()
  209. local ts2 = now - human.db.zhuanpan[mainType].ts2
  210. local leftTime = DEFAULT_FREE_REFRESH_TIME - ts2
  211. if leftTime > 0 then
  212. needCost = config.refreshCost[1][2]
  213. end
  214. -- 判断消耗
  215. local cnt = BagLogic.getItemCnt(human, config.refreshCost[1][1])
  216. if cnt < needCost then
  217. return
  218. end
  219. -- 扣消耗
  220. if needCost > 0 then
  221. BagLogic.delItem(human, config.refreshCost[1][1], needCost, "zhuanpan_refresh")
  222. end
  223. -- 改db
  224. local dayStartTime = Util.getDayStartTime(now)
  225. human.db.zhuanpan[mainType].ts2 = now
  226. human.db.zhuanpan[mainType].ts1 = dayStartTime
  227. end
  228. addRandomReward(human, mainType)
  229. -- 通知客户端
  230. if needTime then
  231. query(human, mainType)
  232. end
  233. end
  234. -- 刷新数据
  235. function updateDaily(human)
  236. refresh(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  237. refresh(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  238. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 1
  239. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].freeCnt = 0
  240. ZhuanpanGift.updateDaily(human)
  241. end
  242. -- 抽奖
  243. function getReward(human, mainType, cnt)
  244. local config = ZhuanpanExcel.define[mainType]
  245. if not ZhuanpanExcel[mainType] then
  246. return
  247. end
  248. local needItemID = config.useItemID
  249. local needItemCnt = nil
  250. if cnt == 1 then
  251. needItemCnt = config.useItemCnt1
  252. elseif cnt == config.drawCnt2 then
  253. needItemCnt = config.useItemCnt2
  254. -- 多次抽奖需要达到指定vip等级
  255. --[[local needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  256. if VipLogic.getVipLv(human) < needVipLv then
  257. return Broadcast.sendErr(human, Util.format(Lang.ROLE_VIP_ERROR2, needVipLv))
  258. end]]
  259. end
  260. if needItemCnt == nil then
  261. return
  262. end
  263. local oldCnt = getCntByType(human, mainType)
  264. local oldLuck
  265. local tOldStatus = nil
  266. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  267. oldLuck = human.db.zhuanpan[mainType].luck
  268. if human.db.zhuanpan[mainType].luckStatus then
  269. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  270. end
  271. end
  272. -- 更新
  273. update(human, mainType)
  274. -- 初始化
  275. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  276. local zhuanpan = human.db.zhuanpan[mainType]
  277. if not BagLogic.checkItemCnt(human, needItemID, needItemCnt) then
  278. return
  279. end
  280. -- 每日可抽取次数检测
  281. -- local dayTimes = ClutterDataLogic.GetZhuanPanTimes(human)
  282. -- if dayTimes < cnt then
  283. -- return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  284. -- end
  285. -- --更新每次抽取次数
  286. -- dayTimes = dayTimes - cnt
  287. -- 改为只更新次数
  288. ClutterDataLogic.UpdateZhuanPanTimes(human, cnt)
  289. -- 扣消耗
  290. BagLogic.delItem(human, needItemID, needItemCnt, "zhuanpan_get")
  291. local msgRet = Msg.gc.GC_ZHUANPAN_GET_REWARD
  292. msgRet.type = mainType
  293. msgRet.list[0] = cnt
  294. local len = 0
  295. for i = 1, cnt do
  296. local result = getLuckDrawSingle(human, mainType)
  297. if result == nil then
  298. assert(nil)
  299. end
  300. -- 改db
  301. local data = zhuanpan[result]
  302. local zhuanpanConfig = ZhuanpanExcel[mainType]
  303. local tempConfig = zhuanpanConfig[data.id]
  304. local nowGetCnt = data.getCnt or 0
  305. local maxGetCnt = tempConfig.getCnt
  306. if maxGetCnt ~= 0 then
  307. data.getCnt = nowGetCnt + 1
  308. end
  309. -- 加道具
  310. local itemID = data.itemID
  311. local itemCnt = data.itemCnt
  312. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_get")
  313. local itemConfig = ItemExcel[itemID]
  314. local heroConfig = HeroExcel[itemID]
  315. -- 存在部分 英雄配表里面的id 和装备配表重合了
  316. if itemConfig and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN and heroConfig and heroConfig.star == 5 then
  317. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  318. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE11, tempConfig.grade, itemID)
  319. else
  320. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE10, tempConfig.grade, itemID)
  321. end
  322. end
  323. if tempConfig.getCnt > 0 then
  324. if not REWARD_RECORD[mainType] then
  325. REWARD_RECORD[mainType] = {}
  326. end
  327. local record = REWARD_RECORD[mainType]
  328. local index = REWARD_RECORD.index[mainType] or 1
  329. local order = REWARD_RECORD.order[mainType] or 1
  330. record[index] = record[index] or {}
  331. record[index].tips = Util.format(Lang.ZHUANPAN_RECORD_TWO, human.db.name, ItemDefine.getValue(itemID,"name"), itemCnt)
  332. record[index].timer = os.time()
  333. record[index].order = order
  334. index = index + 1
  335. if index > REWARD_RECORD_CNT then
  336. index = 1
  337. end
  338. REWARD_RECORD.order[mainType] = order + 1
  339. REWARD_RECORD.index[mainType] = index
  340. end
  341. msgRet.list[i] = data.id
  342. len = len + 1
  343. if not ItemDefine.isEquip(itemID) then
  344. Grid.makeItem(msgRet.item[len], itemID, itemCnt)
  345. else
  346. EquipLogic.makeEquipItemOne(human, msgRet.item[len])
  347. end
  348. end
  349. msgRet.item[0] = len
  350. -- 增加积分
  351. local itemID = ItemDefine.ITEM_LUCK_ID
  352. local itemCnt = cnt * 10
  353. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_back")
  354. msgRet.luckItem[0] = 1
  355. Grid.makeItem(msgRet.luckItem[1], itemID, itemCnt)
  356. zhuanpan.cnt = zhuanpan.cnt or 0
  357. zhuanpan.cnt = zhuanpan.cnt + cnt
  358. zhuanpan.luck = zhuanpan.luck or 0
  359. zhuanpan.luck = zhuanpan.luck + cnt * 10
  360. zhuanpan.luckStatus = zhuanpan.luckStatus or {}
  361. local luckConfig = getLuckConfig(mainType)
  362. -- 集火幸运值 奖励
  363. local luckStatus = zhuanpan.luckStatus
  364. for k, v in pairs(luckConfig) do
  365. if zhuanpan.luck >= k then
  366. if luckStatus[k] == nil then
  367. luckStatus[k] = 1
  368. end
  369. end
  370. end
  371. Msg.send(msgRet, human.fd)
  372. query(human, mainType)
  373. YunYingLogic.onCallBack(human, "onFindStar", cnt)
  374. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_FINDSTAR, human.db._id, cnt)
  375. --周任务,寻星
  376. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_1, cnt)
  377. end
  378. -- 购买许愿珠
  379. function buyCnt(human, mainType, cnt)
  380. if cnt < 1 then return end
  381. if mainType ~= DEFAULT_ZHUANPAN_TYPE_NORMAL then
  382. return
  383. end
  384. local needZuanshi = 50 * cnt
  385. -- 判断消耗
  386. if not ObjHuman.checkRMB(human, needZuanshi) then
  387. return
  388. end
  389. -- 扣消耗
  390. ObjHuman.decZuanshi(human, -needZuanshi, "zhuanpan_buy_cnt", ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt)
  391. -- 增加物品
  392. BagLogic.addItem(human, ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt, "zhuanpan_buy_cnt")
  393. -- 通知客户端
  394. local msgRet = Msg.gc.GC_BUY_ZHUANPAN_CNT
  395. Msg.send(msgRet, human.fd)
  396. end
  397. ------------------------------功能函数--------------------------------------
  398. function addRandomReward(human, mainType)
  399. local zhuanpanConfig = ZhuanpanExcel[mainType]
  400. if not zhuanpanConfig then return end
  401. local nowLv = human.db.lv
  402. local itemID = nil
  403. local itemCnt = nil
  404. local cntIndex = 0
  405. for i = 1, #zhuanpanConfig do
  406. local tempConfig = zhuanpanConfig[i]
  407. if not tempConfig then return end
  408. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  409. if nowLv >= tempConfig.minLv and nowLv <= tempConfig.maxLv then
  410. itemID, itemCnt = getRandomItem(human, tempConfig)
  411. if itemID and itemCnt then
  412. cntIndex = cntIndex + 1
  413. human.db.zhuanpan[mainType][cntIndex] = {}
  414. human.db.zhuanpan[mainType][cntIndex].id = i
  415. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  416. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  417. end
  418. end
  419. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  420. itemID, itemCnt = getRandomItem(human, tempConfig)
  421. if itemID and itemCnt then
  422. cntIndex = cntIndex + 1
  423. human.db.zhuanpan[mainType][cntIndex] = {}
  424. human.db.zhuanpan[mainType][cntIndex].id = i
  425. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  426. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  427. end
  428. end
  429. end
  430. end
  431. function getLuckDrawSingle(human, mainType)
  432. local zhuanpanConfig = ZhuanpanExcel[mainType]
  433. if not zhuanpanConfig then return end
  434. local totalWeight = 0
  435. local recordData = nil
  436. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  437. local data = human.db.zhuanpan[mainType][i]
  438. local drawID = data.id
  439. local tempConfig = zhuanpanConfig[drawID]
  440. local nowGetCnt = data.getCnt or 0
  441. local maxGetCnt = tempConfig.getCnt
  442. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  443. totalWeight = totalWeight + tempConfig.chance
  444. end
  445. end
  446. local randNum = math.random(1,totalWeight)
  447. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  448. local data = human.db.zhuanpan[mainType][i]
  449. local drawID = data.id
  450. local tempConfig = zhuanpanConfig[drawID]
  451. local nowGetCnt = data.getCnt or 0
  452. local maxGetCnt = tempConfig.getCnt
  453. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  454. local tempWeight = tempConfig.chance
  455. if randNum <= tempWeight then
  456. return i
  457. end
  458. randNum = randNum - tempWeight
  459. end
  460. end
  461. end
  462. function initDB(human, mainType, cnt, oldluck, tOldStatus)
  463. if human.db.zhuanpan ~= nil and
  464. human.db.zhuanpan[mainType] ~= nil then
  465. return
  466. end
  467. local now = os.time()
  468. human.db.zhuanpan = human.db.zhuanpan or {}
  469. human.db.zhuanpan[mainType] = {}
  470. local dayStartTime = Util.getDayStartTime(now)
  471. human.db.zhuanpan[mainType].ts1 = dayStartTime
  472. human.db.zhuanpan[mainType].ts2 = now - DEFAULT_FREE_REFRESH_TIME
  473. human.db.zhuanpan[mainType].cnt = cnt
  474. human.db.zhuanpan[mainType].luck = oldluck
  475. human.db.zhuanpan[mainType].free = 1
  476. if tOldStatus then
  477. human.db.zhuanpan[mainType].luckStatus = tOldStatus
  478. else
  479. human.db.zhuanpan[mainType].luckStatus = {}
  480. end
  481. addRandomReward(human, mainType)
  482. end
  483. function update(human, mainType)
  484. if human.db.zhuanpan == nil or human.db.zhuanpan[mainType] == nil then
  485. return
  486. end
  487. if human.db.zhuanpan[mainType].ts1 then
  488. -- 防止前端 在结束倒计时结束 发送查询 , 这边还没有结束 返回一样的数据
  489. local now = os.time() + 1
  490. local ts1 = now - human.db.zhuanpan[mainType].ts1
  491. local leftTime = 24 * 60 * 60 - ts1
  492. if leftTime <= 0 then
  493. human.db.zhuanpan[mainType] = nil
  494. end
  495. end
  496. end
  497. function getRandomItem(human, tempConfig)
  498. local totalWeight = 0
  499. for k, v in ipairs(tempConfig.rewardID) do
  500. totalWeight = totalWeight + v[3]
  501. end
  502. local random = math.random(1, totalWeight)
  503. for k, v in ipairs(tempConfig.rewardID) do
  504. local weight = v[3]
  505. local itemID = v[1]
  506. local itemCnt = v[2]
  507. if random <= weight then
  508. return itemID, itemCnt
  509. end
  510. random = random - weight
  511. end
  512. end
  513. function zhuanpanByGm(human,mainType,val)
  514. if not mainType and not val then
  515. return
  516. end
  517. if mainType == 0 or mainType > 2 or val < 5 then
  518. return
  519. end
  520. if human.db.zhuanpan[mainType] then
  521. local now = os.time()
  522. human.db.zhuanpan[mainType].ts1 = now - 24 * 60 * 60 + val
  523. end
  524. end
  525. function getCntByType(human, mainType)
  526. local num = 0
  527. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  528. human.db.zhuanpan[mainType].cnt = human.db.zhuanpan[mainType].cnt or 0
  529. num = human.db.zhuanpan[mainType].cnt
  530. end
  531. return num
  532. end
  533. function getLuck(human, msg)
  534. local mainType = msg.type
  535. local config = getLuckConfig(mainType)
  536. if not config then
  537. return
  538. end
  539. local zhuanpan = human.db.zhuanpan[mainType]
  540. if zhuanpan == nil then
  541. return
  542. end
  543. if zhuanpan.luck <= 0 then
  544. return
  545. end
  546. local luckStatus = zhuanpan.luckStatus
  547. if luckStatus == nil then
  548. luckStatus = {}
  549. zhuanpan.luckStatus = luckStatus
  550. end
  551. -- 收集所有可领取的奖励(status == 1)
  552. local itemList = {}
  553. local itemListLen = 0
  554. -- 遍历所有配置的奖励,找到可领取的(status == 1)
  555. for k, v in pairs(config) do
  556. if luckStatus[k] == 1 then
  557. -- 标记为已领取
  558. luckStatus[k] = 2
  559. -- 添加奖励到背包
  560. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  561. -- 收集到奖励列表
  562. itemListLen = itemListLen + 1
  563. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  564. end
  565. end
  566. -- 如果没有可领取的奖励,直接返回
  567. if itemListLen == 0 then
  568. return
  569. end
  570. -- 循环处理幸运值扣除和重新激活奖励(可能领取多轮)
  571. while true do
  572. -- 计算是否需要重新扣除幸运值 重新激活奖励
  573. local luck = zhuanpan.luck
  574. local calc = true
  575. for k, v in pairs(config) do
  576. if luckStatus[k] == nil or luckStatus[k] ~= 2 then
  577. calc = false
  578. end
  579. end
  580. -- 如果所有奖励都已领取,扣除幸运值并重新激活
  581. if calc == true then
  582. zhuanpan.luckStatus = {}
  583. luck = luck - DEFAULT_MAX_LUCK
  584. luck = luck < 0 and 0 or luck
  585. zhuanpan.luck = luck
  586. -- 重新激活幸运值奖励
  587. luckStatus = zhuanpan.luckStatus
  588. for k, v in pairs(config) do
  589. if zhuanpan.luck >= k then
  590. if luckStatus[k] == nil then
  591. luckStatus[k] = 1
  592. end
  593. end
  594. end
  595. -- 如果重新激活后还有可领取的奖励,继续循环领取
  596. local hasMore = false
  597. for k, v in pairs(config) do
  598. if luckStatus[k] == 1 then
  599. hasMore = true
  600. -- 领取这个奖励
  601. luckStatus[k] = 2
  602. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  603. itemListLen = itemListLen + 1
  604. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  605. end
  606. end
  607. -- 如果没有更多可领取的奖励,退出循环
  608. if not hasMore then
  609. break
  610. end
  611. else
  612. -- 不是所有奖励都已领取,退出循环
  613. break
  614. end
  615. end
  616. -- 发送所有奖励列表
  617. if itemListLen > 0 then
  618. BagLogic.sendItemGetList(human, itemList, "zhuanpan_back")
  619. end
  620. query(human, msg.type)
  621. end
  622. function isDot(human)
  623. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  624. if red1 then return true end
  625. --local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  626. --if red2 then return true end
  627. return false
  628. end
  629. function getFree(human)
  630. local oldLuck
  631. local tOldStatus = nil
  632. if human.db.zhuanpan and human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL] then
  633. oldLuck = human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luck
  634. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus then
  635. tOldStatus = Util.copyTable(human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus)
  636. end
  637. end
  638. initDB(human,DEFAULT_ZHUANPAN_TYPE_NORMAL,nil, oldLuck,tOldStatus)
  639. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free ~= 1 then
  640. return
  641. end
  642. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 0
  643. local zhuanpanConfig = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL]
  644. local len = #zhuanpanConfig.freeCnt
  645. local totalWeight = 0
  646. for i = 1,len do
  647. totalWeight = totalWeight + zhuanpanConfig.freeCnt[i][2]
  648. end
  649. local itemCnt = nil
  650. local randomWeight = math.random(1,totalWeight)
  651. for i = 1,len do
  652. if randomWeight < zhuanpanConfig.freeCnt[i][2] then
  653. itemCnt = zhuanpanConfig.freeCnt[i][1]
  654. break
  655. else
  656. randomWeight = randomWeight - zhuanpanConfig.freeCnt[i][2]
  657. end
  658. end
  659. if itemCnt then
  660. local item = {}
  661. item[1] = {zhuanpanConfig.useItemID,itemCnt}
  662. item[2] = zhuanpanConfig.freeitem[1]
  663. BagLogic.addItemList(human, item, "zhuanpan_get")
  664. else
  665. assert()
  666. end
  667. end