ZhuanpanLogic.lua 26 KB

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