ZhuanpanLogic.lua 21 KB

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