ZhuanpanLogic.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. local Log = require("common.Log")
  25. local DB = require("common.DB")
  26. local LuaMongo = _G.lua_mongo
  27. -- 允许的渠道
  28. local ALLOW_CHANNELS = {
  29. [11] = true,
  30. [16] = true,
  31. }
  32. DEFAULT_ZHUANPAN_TYPE_NORMAL = 1 -- 基础转盘
  33. DEFAULT_ZHUANPAN_TYPE_GAOJI = 2 -- 高级转盘
  34. DEFAULT_BUY_ZHUANPAN_CNT = 50 -- 购买许愿珠所需钻石
  35. DEFAULT_FREE_REFRESH_TIME = 10800 -- 免费刷新的时间
  36. ZHUAN_PAN_ITEM_KIND_CNT = 8 -- 转盘种类数量
  37. DEFAULT_MAX_LUCK = 1000
  38. REWARD_RECORD = { index={}, order={} }
  39. REWARD_RECORD_CNT = 15
  40. function getLuckConfig(mainType)
  41. local config = nil
  42. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  43. config = ZhuanpanExcel.luck1
  44. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  45. config = ZhuanpanExcel.luck2
  46. end
  47. return config
  48. end
  49. function red(human, type)
  50. if not human.db.zhuanpan then return end
  51. local zhuanpan = human.db.zhuanpan[type]
  52. local config = getLuckConfig(type)
  53. if zhuanpan then
  54. local luck = zhuanpan.luck or 0
  55. local luckStatus = zhuanpan.luckStatus
  56. for k, v in pairs(config) do
  57. if luck >= k then
  58. if luckStatus[k] == 1 then
  59. return true
  60. end
  61. end
  62. end
  63. end
  64. if zhuanpan.free == 1 then
  65. return true
  66. end
  67. end
  68. -- 转盘查询
  69. function query(human, mainType)
  70. local zhuanpanConfig = ZhuanpanExcel[mainType]
  71. if not zhuanpanConfig then return end
  72. -- 高级转盘判断等级
  73. local roleConfig = nil
  74. local flag, roleConfig = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_501)
  75. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  76. if flag ~= true then
  77. return Broadcast.sendErr(human, Lang.ZHUANPAN_GAIJI_NEED_LV)
  78. end
  79. end
  80. local oldCnt = getCntByType(human, mainType)
  81. local oldLuck
  82. local tOldStatus = nil
  83. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  84. oldLuck = human.db.zhuanpan[mainType].luck
  85. if human.db.zhuanpan[mainType].luckStatus then
  86. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  87. end
  88. end
  89. -- 更新
  90. update(human, mainType)
  91. -- 初始化
  92. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  93. local zuanpan = human.db.zhuanpan[mainType]
  94. local config = ZhuanpanExcel.define[mainType]
  95. local msgRet = Msg.gc.GC_ZHUANPAN_QUERY
  96. msgRet.type = mainType
  97. msgRet.isFirst = zuanpan.free == 1 and 1 or 0
  98. local now = os.time()
  99. local ts1 = now - zuanpan.ts1
  100. msgRet.refreshTime = 24 * 60 * 60 - ts1
  101. msgRet.freeRefreshTime = DEFAULT_FREE_REFRESH_TIME - (now - zuanpan.ts2)
  102. if msgRet.freeRefreshTime < 0 then
  103. msgRet.freeRefreshTime = 0
  104. end
  105. Grid.makeItem(msgRet.itemID, config.useItemID , 1)
  106. msgRet.itemCnt[0] = 2
  107. msgRet.itemCnt[1] = config.useItemCnt1
  108. msgRet.itemCnt[2] = config.useItemCnt2
  109. msgRet.drawCnt = config.drawCnt2
  110. Grid.makeItem(msgRet.zuanshiNeed, config.refreshCost[1][1], config.refreshCost[1][2])
  111. msgRet.needLv = config and config.lv or 0
  112. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  113. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_GAOJI].lv
  114. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  115. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL].lv
  116. end
  117. msgRet.needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  118. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  119. local data = zuanpan[i]
  120. local dataID = data.id
  121. local tempConfig = zhuanpanConfig[dataID]
  122. Grid.makeItem(msgRet.list[i].item, data.itemID, data.itemCnt)
  123. msgRet.list[i].id = dataID
  124. msgRet.list[i].chance = tempConfig.chance
  125. msgRet.list[i].maxCnt = tempConfig.getCnt
  126. msgRet.list[i].getCnt = 0
  127. if data.getCnt then
  128. msgRet.list[i].getCnt = data.getCnt
  129. end
  130. end
  131. local luckConfig = getLuckConfig(mainType)
  132. msgRet.luck = zuanpan.luck or 0
  133. local len = 0
  134. for k, v in pairs(luckConfig) do
  135. len = len + 1
  136. local net = msgRet.luckList[len]
  137. net.id = k
  138. net.status = zuanpan.luckStatus[k] or 0
  139. Grid.makeItem(net.item, v.reward[1] , v.reward[2])
  140. end
  141. msgRet.luckList[0] = len
  142. len = 0
  143. if REWARD_RECORD[mainType] then
  144. for k, v in ipairs(REWARD_RECORD[mainType]) do
  145. len = len + 1
  146. local net = msgRet.record[len]
  147. net.id = k
  148. net.tips = v.tips
  149. net.timer = v.timer
  150. net.order = v.order
  151. end
  152. end
  153. msgRet.record[0] = len
  154. msgRet.list[0] = ZHUAN_PAN_ITEM_KIND_CNT
  155. len = 0
  156. local totalWeight = 0
  157. local chanceList = {}
  158. for k, v in pairs(zhuanpanConfig) do
  159. totalWeight = totalWeight + v.chance
  160. chanceList[k] = chanceList[k] or {}
  161. local rewardWeight = 0
  162. for a, b in ipairs(v.rewardID) do
  163. rewardWeight = rewardWeight + b[3]
  164. end
  165. chanceList[k].totalWeight = rewardWeight
  166. end
  167. -- 大全重占比
  168. local temp = {}
  169. for k, v in pairs(zhuanpanConfig) do
  170. local chance = chanceList[k]
  171. local scale = v.chance / totalWeight
  172. for a, b in ipairs(v.rewardID) do
  173. local itemID = b[1]
  174. if not temp[itemID] then
  175. local weight = b[3] / chance.totalWeight
  176. len = len + 1
  177. local net = msgRet.chanceList[len]
  178. local itemConfig = ItemDefine.getConfig(itemID)
  179. if not itemConfig then
  180. print(itemID)
  181. end
  182. net.tips = itemConfig and itemConfig.name or ""
  183. net.chance = weight * scale * 100
  184. temp[itemID] = 1
  185. end
  186. end
  187. end
  188. msgRet.chanceList[0] = len
  189. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  190. msgRet.red1 = red1 == true and 1 or 0
  191. local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  192. msgRet.red2 = red2 == true and 1 or 0
  193. Msg.send(msgRet, human.fd)
  194. end
  195. function refresh(human, mainType, needTime, bRefreshLuck)
  196. if not ZhuanpanExcel[mainType] then
  197. return
  198. end
  199. local oldCnt = getCntByType(human, mainType)
  200. local oldLuck
  201. local tOldStatus = nil
  202. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  203. oldLuck = human.db.zhuanpan[mainType].luck
  204. if human.db.zhuanpan[mainType].luckStatus and not bRefreshLuck then
  205. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  206. end
  207. end
  208. -- 更新
  209. update(human, mainType)
  210. -- 初始化
  211. initDB(human, mainType, oldCnt,oldLuck,tOldStatus)
  212. -- 判断是否免费
  213. if needTime then
  214. local config = ZhuanpanExcel.define[mainType]
  215. local needCost = 0
  216. local now = os.time()
  217. local ts2 = now - human.db.zhuanpan[mainType].ts2
  218. local leftTime = DEFAULT_FREE_REFRESH_TIME - ts2
  219. if leftTime > 0 then
  220. needCost = config.refreshCost[1][2]
  221. end
  222. -- 判断消耗
  223. local cnt = BagLogic.getItemCnt(human, config.refreshCost[1][1])
  224. if cnt < needCost then
  225. return
  226. end
  227. -- 扣消耗
  228. if needCost > 0 then
  229. BagLogic.delItem(human, config.refreshCost[1][1], needCost, "zhuanpan_refresh")
  230. end
  231. -- 改db
  232. local dayStartTime = Util.getDayStartTime(now)
  233. human.db.zhuanpan[mainType].ts2 = now
  234. human.db.zhuanpan[mainType].ts1 = dayStartTime
  235. end
  236. addRandomReward(human, mainType)
  237. -- 通知客户端
  238. if needTime then
  239. query(human, mainType)
  240. end
  241. end
  242. -- 刷新数据
  243. function updateDaily(human)
  244. refresh(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  245. refresh(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  246. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 1
  247. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].freeCnt = 0
  248. ZhuanpanGift.updateDaily(human)
  249. end
  250. -- 抽奖
  251. function getReward(human, mainType, cnt)
  252. local config = ZhuanpanExcel.define[mainType]
  253. if not ZhuanpanExcel[mainType] then
  254. return
  255. end
  256. local needItemID = config.useItemID
  257. local needItemCnt = nil
  258. if cnt == 1 then
  259. needItemCnt = config.useItemCnt1
  260. elseif cnt == config.drawCnt2 then
  261. needItemCnt = config.useItemCnt2
  262. -- 多次抽奖需要达到指定vip等级
  263. --[[local needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  264. if VipLogic.getVipLv(human) < needVipLv then
  265. return Broadcast.sendErr(human, Util.format(Lang.ROLE_VIP_ERROR2, needVipLv))
  266. end]]
  267. end
  268. if needItemCnt == nil then
  269. return
  270. end
  271. local oldCnt = getCntByType(human, mainType)
  272. local oldLuck
  273. local tOldStatus = nil
  274. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  275. oldLuck = human.db.zhuanpan[mainType].luck
  276. if human.db.zhuanpan[mainType].luckStatus then
  277. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  278. end
  279. end
  280. -- 更新
  281. update(human, mainType)
  282. -- 初始化
  283. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  284. local zhuanpan = human.db.zhuanpan[mainType]
  285. if not BagLogic.checkItemCnt(human, needItemID, needItemCnt) then
  286. return
  287. end
  288. -- 每日可抽取次数检测
  289. -- local dayTimes = ClutterDataLogic.GetZhuanPanTimes(human)
  290. -- if dayTimes < cnt then
  291. -- return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  292. -- end
  293. -- --更新每次抽取次数
  294. -- dayTimes = dayTimes - cnt
  295. -- 改为只更新次数
  296. ClutterDataLogic.UpdateZhuanPanTimes(human, cnt)
  297. -- 扣消耗
  298. BagLogic.delItem(human, needItemID, needItemCnt, "zhuanpan_get")
  299. local msgRet = Msg.gc.GC_ZHUANPAN_GET_REWARD
  300. msgRet.type = mainType
  301. msgRet.list[0] = cnt
  302. local len = 0
  303. for i = 1, cnt do
  304. local result = getLuckDrawSingle(human, mainType)
  305. if result == nil then
  306. assert(nil)
  307. end
  308. -- 改db
  309. local data = zhuanpan[result]
  310. local zhuanpanConfig = ZhuanpanExcel[mainType]
  311. local tempConfig = zhuanpanConfig[data.id]
  312. local nowGetCnt = data.getCnt or 0
  313. local maxGetCnt = tempConfig.getCnt
  314. if maxGetCnt ~= 0 then
  315. data.getCnt = nowGetCnt + 1
  316. end
  317. -- 加道具
  318. local itemID = data.itemID
  319. local itemCnt = data.itemCnt
  320. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_get")
  321. local itemConfig = ItemExcel[itemID]
  322. local heroConfig = HeroExcel[itemID]
  323. -- 存在部分 英雄配表里面的id 和装备配表重合了
  324. if itemConfig and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN and heroConfig and heroConfig.star == 5 then
  325. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  326. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE11, tempConfig.grade, itemID)
  327. else
  328. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE10, tempConfig.grade, itemID)
  329. end
  330. end
  331. if tempConfig.getCnt > 0 then
  332. if not REWARD_RECORD[mainType] then
  333. REWARD_RECORD[mainType] = {}
  334. end
  335. local record = REWARD_RECORD[mainType]
  336. local index = REWARD_RECORD.index[mainType] or 1
  337. local order = REWARD_RECORD.order[mainType] or 1
  338. record[index] = record[index] or {}
  339. record[index].tips = Util.format(Lang.ZHUANPAN_RECORD_TWO, human.db.name, ItemDefine.getValue(itemID,"name"), itemCnt)
  340. record[index].timer = os.time()
  341. record[index].order = order
  342. index = index + 1
  343. if index > REWARD_RECORD_CNT then
  344. index = 1
  345. end
  346. REWARD_RECORD.order[mainType] = order + 1
  347. REWARD_RECORD.index[mainType] = index
  348. end
  349. msgRet.list[i] = data.id
  350. len = len + 1
  351. if not ItemDefine.isEquip(itemID) then
  352. Grid.makeItem(msgRet.item[len], itemID, itemCnt)
  353. else
  354. EquipLogic.makeEquipItemOne(human, msgRet.item[len])
  355. end
  356. end
  357. msgRet.item[0] = len
  358. -- 增加积分
  359. local itemID = ItemDefine.ITEM_LUCK_ID
  360. local itemCnt = cnt * 10
  361. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_back")
  362. msgRet.luckItem[0] = 1
  363. Grid.makeItem(msgRet.luckItem[1], itemID, itemCnt)
  364. zhuanpan.cnt = zhuanpan.cnt or 0
  365. zhuanpan.cnt = zhuanpan.cnt + cnt
  366. zhuanpan.luck = zhuanpan.luck or 0
  367. zhuanpan.luck = zhuanpan.luck + cnt * 10
  368. zhuanpan.luckStatus = zhuanpan.luckStatus or {}
  369. local luckConfig = getLuckConfig(mainType)
  370. -- 集火幸运值 奖励
  371. local luckStatus = zhuanpan.luckStatus
  372. for k, v in pairs(luckConfig) do
  373. if zhuanpan.luck >= k then
  374. if luckStatus[k] == nil then
  375. luckStatus[k] = 1
  376. end
  377. end
  378. end
  379. Msg.send(msgRet, human.fd)
  380. query(human, mainType)
  381. YunYingLogic.onCallBack(human, "onFindStar", cnt)
  382. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_FINDSTAR, human.db._id, cnt)
  383. --周任务,寻星
  384. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_1, cnt)
  385. end
  386. -- 购买许愿珠
  387. function buyCnt(human, mainType, cnt)
  388. if cnt < 1 then return end
  389. if mainType ~= DEFAULT_ZHUANPAN_TYPE_NORMAL then
  390. return
  391. end
  392. local needZuanshi = 50 * cnt
  393. -- 判断消耗
  394. if not ObjHuman.checkRMB(human, needZuanshi) then
  395. return
  396. end
  397. -- 扣消耗
  398. ObjHuman.decZuanshi(human, -needZuanshi, "zhuanpan_buy_cnt", ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt)
  399. -- 增加物品
  400. BagLogic.addItem(human, ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt, "zhuanpan_buy_cnt")
  401. -- 通知客户端
  402. local msgRet = Msg.gc.GC_BUY_ZHUANPAN_CNT
  403. Msg.send(msgRet, human.fd)
  404. end
  405. ------------------------------功能函数--------------------------------------
  406. function addRandomReward(human, mainType)
  407. local zhuanpanConfig = ZhuanpanExcel[mainType]
  408. if not zhuanpanConfig then return end
  409. local nowLv = human.db.lv
  410. local itemID = nil
  411. local itemCnt = nil
  412. local cntIndex = 0
  413. for i = 1, #zhuanpanConfig do
  414. local tempConfig = zhuanpanConfig[i]
  415. if not tempConfig then return end
  416. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  417. if nowLv >= tempConfig.minLv and nowLv <= tempConfig.maxLv then
  418. itemID, itemCnt = getRandomItem(human, tempConfig)
  419. if itemID and itemCnt then
  420. cntIndex = cntIndex + 1
  421. human.db.zhuanpan[mainType][cntIndex] = {}
  422. human.db.zhuanpan[mainType][cntIndex].id = i
  423. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  424. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  425. end
  426. end
  427. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  428. itemID, itemCnt = getRandomItem(human, tempConfig)
  429. if itemID and itemCnt then
  430. cntIndex = cntIndex + 1
  431. human.db.zhuanpan[mainType][cntIndex] = {}
  432. human.db.zhuanpan[mainType][cntIndex].id = i
  433. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  434. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  435. end
  436. end
  437. end
  438. end
  439. function getLuckDrawSingle(human, mainType)
  440. local zhuanpanConfig = ZhuanpanExcel[mainType]
  441. if not zhuanpanConfig then return end
  442. local totalWeight = 0
  443. local recordData = nil
  444. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  445. local data = human.db.zhuanpan[mainType][i]
  446. local drawID = data.id
  447. local tempConfig = zhuanpanConfig[drawID]
  448. local nowGetCnt = data.getCnt or 0
  449. local maxGetCnt = tempConfig.getCnt
  450. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  451. totalWeight = totalWeight + tempConfig.chance
  452. end
  453. end
  454. local randNum = math.random(1,totalWeight)
  455. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  456. local data = human.db.zhuanpan[mainType][i]
  457. local drawID = data.id
  458. local tempConfig = zhuanpanConfig[drawID]
  459. local nowGetCnt = data.getCnt or 0
  460. local maxGetCnt = tempConfig.getCnt
  461. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  462. local tempWeight = tempConfig.chance
  463. if randNum <= tempWeight then
  464. return i
  465. end
  466. randNum = randNum - tempWeight
  467. end
  468. end
  469. end
  470. function initDB(human, mainType, cnt, oldluck, tOldStatus)
  471. if human.db.zhuanpan ~= nil and
  472. human.db.zhuanpan[mainType] ~= nil then
  473. return
  474. end
  475. local now = os.time()
  476. human.db.zhuanpan = human.db.zhuanpan or {}
  477. human.db.zhuanpan[mainType] = {}
  478. local dayStartTime = Util.getDayStartTime(now)
  479. human.db.zhuanpan[mainType].ts1 = dayStartTime
  480. human.db.zhuanpan[mainType].ts2 = now - DEFAULT_FREE_REFRESH_TIME
  481. human.db.zhuanpan[mainType].cnt = cnt
  482. human.db.zhuanpan[mainType].luck = oldluck
  483. human.db.zhuanpan[mainType].free = 1
  484. if tOldStatus then
  485. human.db.zhuanpan[mainType].luckStatus = tOldStatus
  486. else
  487. human.db.zhuanpan[mainType].luckStatus = {}
  488. end
  489. addRandomReward(human, mainType)
  490. end
  491. function update(human, mainType)
  492. if human.db.zhuanpan == nil or human.db.zhuanpan[mainType] == nil then
  493. return
  494. end
  495. if human.db.zhuanpan[mainType].ts1 then
  496. -- 防止前端 在结束倒计时结束 发送查询 , 这边还没有结束 返回一样的数据
  497. local now = os.time() + 1
  498. local ts1 = now - human.db.zhuanpan[mainType].ts1
  499. local leftTime = 24 * 60 * 60 - ts1
  500. if leftTime <= 0 then
  501. human.db.zhuanpan[mainType] = nil
  502. end
  503. end
  504. end
  505. function getRandomItem(human, tempConfig)
  506. local totalWeight = 0
  507. for k, v in ipairs(tempConfig.rewardID) do
  508. totalWeight = totalWeight + v[3]
  509. end
  510. local random = math.random(1, totalWeight)
  511. for k, v in ipairs(tempConfig.rewardID) do
  512. local weight = v[3]
  513. local itemID = v[1]
  514. local itemCnt = v[2]
  515. if random <= weight then
  516. return itemID, itemCnt
  517. end
  518. random = random - weight
  519. end
  520. end
  521. function zhuanpanByGm(human,mainType,val)
  522. if not mainType and not val then
  523. return
  524. end
  525. if mainType == 0 or mainType > 2 or val < 5 then
  526. return
  527. end
  528. if human.db.zhuanpan[mainType] then
  529. local now = os.time()
  530. human.db.zhuanpan[mainType].ts1 = now - 24 * 60 * 60 + val
  531. end
  532. end
  533. function getCntByType(human, mainType)
  534. local num = 0
  535. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  536. human.db.zhuanpan[mainType].cnt = human.db.zhuanpan[mainType].cnt or 0
  537. num = human.db.zhuanpan[mainType].cnt
  538. end
  539. return num
  540. end
  541. function getLuck(human, msg)
  542. local mainType = msg.type
  543. local config = getLuckConfig(mainType)
  544. if not config then
  545. return
  546. end
  547. local zhuanpan = human.db.zhuanpan[mainType]
  548. if zhuanpan == nil then
  549. return
  550. end
  551. if zhuanpan.luck <= 0 then
  552. return
  553. end
  554. local luckStatus = zhuanpan.luckStatus
  555. if luckStatus == nil then
  556. luckStatus = {}
  557. zhuanpan.luckStatus = luckStatus
  558. end
  559. -- 收集所有可领取的奖励(status == 1)
  560. local itemList = {}
  561. local itemListLen = 0
  562. -- 遍历所有配置的奖励,找到可领取的(status == 1)
  563. for k, v in pairs(config) do
  564. if luckStatus[k] == 1 then
  565. -- 标记为已领取
  566. luckStatus[k] = 2
  567. -- 添加奖励到背包
  568. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  569. -- 收集到奖励列表
  570. itemListLen = itemListLen + 1
  571. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  572. end
  573. end
  574. -- 如果没有可领取的奖励,直接返回
  575. if itemListLen == 0 then
  576. return
  577. end
  578. -- 循环处理幸运值扣除和重新激活奖励(可能领取多轮)
  579. while true do
  580. -- 计算是否需要重新扣除幸运值 重新激活奖励
  581. local luck = zhuanpan.luck
  582. local calc = true
  583. for k, v in pairs(config) do
  584. if luckStatus[k] == nil or luckStatus[k] ~= 2 then
  585. calc = false
  586. end
  587. end
  588. -- 如果所有奖励都已领取,扣除幸运值并重新激活
  589. if calc == true then
  590. zhuanpan.luckStatus = {}
  591. luck = luck - DEFAULT_MAX_LUCK
  592. luck = luck < 0 and 0 or luck
  593. zhuanpan.luck = luck
  594. -- 重新激活幸运值奖励
  595. luckStatus = zhuanpan.luckStatus
  596. for k, v in pairs(config) do
  597. if zhuanpan.luck >= k then
  598. if luckStatus[k] == nil then
  599. luckStatus[k] = 1
  600. end
  601. end
  602. end
  603. -- 如果重新激活后还有可领取的奖励,继续循环领取
  604. local hasMore = false
  605. for k, v in pairs(config) do
  606. if luckStatus[k] == 1 then
  607. hasMore = true
  608. -- 领取这个奖励
  609. luckStatus[k] = 2
  610. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  611. itemListLen = itemListLen + 1
  612. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  613. end
  614. end
  615. -- 如果没有更多可领取的奖励,退出循环
  616. if not hasMore then
  617. break
  618. end
  619. else
  620. -- 不是所有奖励都已领取,退出循环
  621. break
  622. end
  623. end
  624. -- 发送所有奖励列表
  625. if itemListLen > 0 then
  626. BagLogic.sendItemGetList(human, itemList, "zhuanpan_back")
  627. end
  628. query(human, msg.type)
  629. end
  630. function isDot(human)
  631. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  632. if red1 then return true end
  633. --local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  634. --if red2 then return true end
  635. return false
  636. end
  637. function getFree(human)
  638. local oldLuck
  639. local tOldStatus = nil
  640. if human.db.zhuanpan and human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL] then
  641. oldLuck = human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luck
  642. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus then
  643. tOldStatus = Util.copyTable(human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus)
  644. end
  645. end
  646. initDB(human,DEFAULT_ZHUANPAN_TYPE_NORMAL,nil, oldLuck,tOldStatus)
  647. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free ~= 1 then
  648. return
  649. end
  650. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 0
  651. local zhuanpanConfig = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL]
  652. local len = #zhuanpanConfig.freeCnt
  653. local totalWeight = 0
  654. for i = 1,len do
  655. totalWeight = totalWeight + zhuanpanConfig.freeCnt[i][2]
  656. end
  657. local itemCnt = nil
  658. local randomWeight = math.random(1,totalWeight)
  659. for i = 1,len do
  660. if randomWeight < zhuanpanConfig.freeCnt[i][2] then
  661. itemCnt = zhuanpanConfig.freeCnt[i][1]
  662. break
  663. else
  664. randomWeight = randomWeight - zhuanpanConfig.freeCnt[i][2]
  665. end
  666. end
  667. if itemCnt then
  668. local item = {}
  669. item[1] = {zhuanpanConfig.useItemID,itemCnt}
  670. item[2] = zhuanpanConfig.freeitem[1]
  671. BagLogic.addItemList(human, item, "zhuanpan_get")
  672. else
  673. assert()
  674. end
  675. end
  676. -- 检查账号下是否有任何角色已领取每日固定奖励(账号级别判断)
  677. local function checkAccountDailyFixedReward(account)
  678. if not account then
  679. return false
  680. end
  681. local QueryByAccount = {account = account}
  682. local fields = {zhuanpan = 1}
  683. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  684. while true do
  685. local data = {}
  686. if not LuaMongo.next(data) then
  687. break
  688. end
  689. if data.zhuanpan and data.zhuanpan.dailyFixedReward then
  690. local getTime = data.zhuanpan.dailyFixedReward.getTime
  691. if getTime and Util.isSameDay(getTime) then
  692. return true -- 账号下已有角色今日领取过
  693. end
  694. end
  695. end
  696. return false -- 账号下没有角色今日领取过
  697. end
  698. -- 检查账号下是否有任何角色已领取一次性奖励(账号级别判断)
  699. local function checkAccountOnceReward(account)
  700. if not account then
  701. return false
  702. end
  703. local QueryByAccount = {account = account}
  704. local fields = {zhuanpan = 1}
  705. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  706. while true do
  707. local data = {}
  708. if not LuaMongo.next(data) then
  709. break
  710. end
  711. if data.zhuanpan and data.zhuanpan.onceReward then
  712. local getTime = data.zhuanpan.onceReward.getTime
  713. if getTime then
  714. return true -- 账号下已有角色领取过
  715. end
  716. end
  717. end
  718. return false -- 账号下没有角色领取过
  719. end
  720. -- 每日固定奖励查询
  721. function dailyFixedRewardQuery(human)
  722. local msgRet = Msg.gc.GC_ZHUANPAN_DAILY_FIXED_QUERY
  723. if not msgRet then
  724. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] 错误: msgRet为nil")
  725. return
  726. end
  727. -- 每日更新检查
  728. ObjHuman.updateDaily(human)
  729. -- 初始化数据库
  730. human.db.zhuanpan = human.db.zhuanpan or {}
  731. human.db.zhuanpan.dailyFixedReward = human.db.zhuanpan.dailyFixedReward or {}
  732. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  733. local account = human.db.account
  734. local isReceived = false
  735. -- 先检查当前角色的内存数据
  736. local getTime = human.db.zhuanpan.dailyFixedReward.getTime
  737. if getTime and Util.isSameDay(getTime) then
  738. isReceived = true
  739. else
  740. -- 再检查账号下其他角色(数据库)
  741. isReceived = checkAccountDailyFixedReward(account)
  742. end
  743. if isReceived then
  744. msgRet.status = 2 -- 已领取(账号级别)
  745. else
  746. msgRet.status = 1 -- 可领取
  747. end
  748. -- 设置奖励物品
  749. Grid.makeItem(msgRet.reward, 102, 50)
  750. if human.fd then
  751. Msg.send(msgRet, human.fd)
  752. else
  753. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] 错误: human.fd为nil,无法发送协议")
  754. end
  755. end
  756. -- 每日固定奖励领取
  757. function dailyFixedRewardGet(human, channelId)
  758. if not ALLOW_CHANNELS[channelId] then
  759. Log.write(
  760. Log.LOGID_DEBUG,
  761. "[onceRewardGet] 渠道不匹配: channelId=" .. (channelId or "nil") .. ", 允许=11,12"
  762. )
  763. return Broadcast.sendErr(human, "渠道不匹配")
  764. end
  765. -- 每日更新检查
  766. ObjHuman.updateDaily(human)
  767. -- 初始化数据库
  768. human.db.zhuanpan = human.db.zhuanpan or {}
  769. human.db.zhuanpan.dailyFixedReward = human.db.zhuanpan.dailyFixedReward or {}
  770. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  771. local account = human.db.account
  772. local isReceived = false
  773. -- 先检查当前角色的内存数据
  774. local getTime = human.db.zhuanpan.dailyFixedReward.getTime
  775. if getTime and Util.isSameDay(getTime) then
  776. isReceived = true
  777. else
  778. -- 再检查账号下其他角色(数据库)
  779. isReceived = checkAccountDailyFixedReward(account)
  780. end
  781. if isReceived then
  782. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 账号今日已领取: account="..(account or "nil"))
  783. return Broadcast.sendErr(human, "今日已领取")
  784. end
  785. BagLogic.cleanMomentItemList()
  786. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 102, 50)
  787. local success, err = pcall(function()
  788. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  789. end)
  790. if not success then
  791. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  792. return Broadcast.sendErr(human, "发放奖励失败")
  793. end
  794. -- 记录领取时间
  795. human.db.zhuanpan.dailyFixedReward.getTime = os.time()
  796. -- 领取成功后下发查询协议
  797. local querySuccess, queryErr = pcall(function()
  798. dailyFixedRewardQuery(human)
  799. end)
  800. if not querySuccess then
  801. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  802. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  803. end
  804. -- Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 领取完成")
  805. end
  806. -- 抖音渠道一次性奖励查询
  807. function onceRewardQuery(human)
  808. local msgRet = Msg.gc.GC_ZHUANPAN_ONCE_REWARD_QUERY
  809. if not msgRet then
  810. Log.write(Log.LOGID_DEBUG, "[onceRewardQuery] 错误: msgRet为nil")
  811. return
  812. end
  813. -- 初始化数据库
  814. human.db.zhuanpan = human.db.zhuanpan or {}
  815. human.db.zhuanpan.onceReward = human.db.zhuanpan.onceReward or {}
  816. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  817. local account = human.db.account
  818. local isReceived = false
  819. -- 先检查当前角色的内存数据
  820. local getTime = human.db.zhuanpan.onceReward.getTime
  821. if getTime then
  822. isReceived = true
  823. else
  824. -- 再检查账号下其他角色(数据库)
  825. isReceived = checkAccountOnceReward(account)
  826. end
  827. if isReceived then
  828. msgRet.status = 2 -- 已领取(账号级别)
  829. else
  830. msgRet.status = 1 -- 可领取
  831. end
  832. -- 设置奖励物品列表:[[102,500],[118,10],[111,100000]]
  833. local success, err = pcall(function()
  834. Grid.makeItem(msgRet.reward[1], 102, 500)
  835. Grid.makeItem(msgRet.reward[2], 118, 10)
  836. Grid.makeItem(msgRet.reward[3], 111, 100000)
  837. msgRet.reward[0] = 3 -- 设置数组长度
  838. end)
  839. if not success then
  840. Log.write(Log.LOGID_DEBUG, "[onceRewardQuery] 错误: 设置奖励物品失败: "..tostring(err))
  841. return
  842. end
  843. if human.fd then
  844. Msg.send(msgRet, human.fd)
  845. end
  846. end
  847. -- 抖音渠道一次性奖励领取
  848. function onceRewardGet(human, channelId)
  849. if not ALLOW_CHANNELS[channelId] then
  850. Log.write(
  851. Log.LOGID_DEBUG,
  852. "[onceRewardGet] 渠道不匹配: channelId=" .. (channelId or "nil") .. ", 允许=11,12"
  853. )
  854. return Broadcast.sendErr(human, "渠道不匹配")
  855. end
  856. -- 初始化数据库
  857. human.db.zhuanpan = human.db.zhuanpan or {}
  858. human.db.zhuanpan.onceReward = human.db.zhuanpan.onceReward or {}
  859. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  860. local account = human.db.account
  861. local isReceived = false
  862. -- 先检查当前角色的内存数据
  863. local getTime = human.db.zhuanpan.onceReward.getTime
  864. if getTime then
  865. isReceived = true
  866. else
  867. -- 再检查账号下其他角色(数据库)
  868. isReceived = checkAccountOnceReward(account)
  869. end
  870. if isReceived then
  871. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 账号已领取过,无法重复领取: account="..(account or "nil"))
  872. return Broadcast.sendErr(human, "已领取过,无法重复领取")
  873. end
  874. BagLogic.cleanMomentItemList()
  875. -- 发放奖励:[[102,500],[118,10],[111,100000]]
  876. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 102, 500)
  877. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 118, 10)
  878. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 111, 100000)
  879. local success, err = pcall(function()
  880. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  881. end)
  882. if not success then
  883. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  884. return Broadcast.sendErr(human, "发放奖励失败")
  885. end
  886. -- 记录领取时间(永久记录)
  887. human.db.zhuanpan.onceReward.getTime = os.time()
  888. local querySuccess, queryErr = pcall(function()
  889. onceRewardQuery(human)
  890. end)
  891. if not querySuccess then
  892. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  893. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  894. end
  895. end