ShopLogic.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. local Lang = require("common.Lang")
  2. local Util = require("common.Util")
  3. local Msg = require("core.Msg")
  4. local ObjHuman = require("core.ObjHuman")
  5. local ShopExcel = require("excel.shop")
  6. local Broadcast = require("broadcast.Broadcast")
  7. local ItemDefine = require("bag.ItemDefine")
  8. local BagLogic = require("bag.BagLogic")
  9. local Grid = require("bag.Grid")
  10. local ShopDefine = require("shop.ShopDefine")
  11. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  12. local GuideLogic = require("guide.GuideLogic")
  13. local VipLogic = require("vip.VipLogic")
  14. local MoshouLogic = require("moshou.MoshouLogic")
  15. local UnionLivenessLogic = require("union.UnionLivenessLogic")
  16. local UnionDefine = require("union.UnionDefine")
  17. local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
  18. local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
  19. local EquipLogic = require("equip.EquipLogic")
  20. local FuwenGrid = require("fuwen.FuwenGrid")
  21. local HeroGrowUp = require("absAct.HeroGrowUp")
  22. local YunYingLogic = require("yunying.YunYingLogic")
  23. local Config = require("Config")
  24. local function inServerIndex(svrIndexs)
  25. for i = 1, #svrIndexs do
  26. local index1 = svrIndexs[i][1]
  27. local index2 = svrIndexs[i][2]
  28. if Config.SVR_INDEX >= index1 and Config.SVR_INDEX <= index2 then
  29. return true
  30. end
  31. end
  32. end
  33. local function initGoodsDb(human, shopType, index, refreshTs)
  34. if not human.db.shop[shopType].goods then return end
  35. human.db.shop[shopType].goods[index] = {}
  36. human.db.shop[shopType].goods[index].nowBuy = 0
  37. human.db.shop[shopType].goods[index].index = index
  38. human.db.shop[shopType].goods[index].refreshTs = refreshTs
  39. end
  40. -- 花费刷新总次数
  41. function getCostCntMax(human, shopType)
  42. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
  43. return VipLogic.getPowerArgs(human, VipLogic.VIP_POWER4) or 0
  44. end
  45. local config = ShopExcel.shop[shopType]
  46. if not config then return 0 end
  47. return config.costCnt
  48. end
  49. -- 剩余花费刷新次数
  50. function getLeftCostCnt(human, shopType)
  51. local maxCnt = getCostCntMax(human, shopType)
  52. local shopData = human.db.shop[shopType]
  53. local useCnt = shopData and shopData.costCnt or 0
  54. return math.max(maxCnt - useCnt, 0)
  55. end
  56. -- 清除删掉的商品 增加限制的数据
  57. function cleanShopGoods(human, shopType)
  58. local shopTypeConfig = ShopExcel.shop[shopType]
  59. if not shopTypeConfig then return end
  60. local shopConfig = ShopExcel[shopType]
  61. if not shopConfig then return end
  62. if not human.db.shop then return end
  63. if not human.db.shop[shopType] then return end
  64. if not human.db.shop[shopType].goods then return end
  65. local shopBuy = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)
  66. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET and not shopBuy then
  67. for k,v in pairs(human.db.shop[shopType].goods) do
  68. human.db.shop[shopType].goods[k].index = 0
  69. break
  70. end
  71. end
  72. -- 清除没有的
  73. for k,v in pairs(human.db.shop[shopType].goods) do
  74. local tempConfig = shopConfig[v.index]
  75. if not tempConfig then
  76. human.db.shop[shopType].goods[k] = nil
  77. end
  78. end
  79. -- 加入新增的
  80. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
  81. for index, v in pairs(shopConfig) do
  82. if not human.db.shop[shopType].goods[index] then
  83. if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 then
  84. initGoodsDb(human, shopType, index, 0)
  85. end
  86. end
  87. end
  88. end
  89. end
  90. -- 查询商店物品
  91. function query(human, shopType)
  92. local shopTypeConfig = ShopExcel.shop[shopType]
  93. if not shopTypeConfig then return end
  94. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  95. local msgRet = Msg.gc.GC_SHOP_QUERY
  96. msgRet.shopInfo.shopType = shopType
  97. msgRet.shopInfo.mainType = shopTypeConfig.mainType
  98. msgRet.shopInfo.refreshType = shopTypeConfig.refreshType
  99. msgRet.shopInfo.name = shopTypeConfig.name
  100. msgRet.shopInfo.icon = shopTypeConfig.icon
  101. msgRet.shopList[0] = 0
  102. for stype, sconfig in pairs(ShopExcel.shop) do
  103. if sconfig.mainType == shopTypeConfig.mainType and inServerIndex(sconfig.serverIndex) then
  104. msgRet.shopList[0] = msgRet.shopList[0] + 1
  105. local net = msgRet.shopList[msgRet.shopList[0]]
  106. net.shopType = stype
  107. net.mainType = sconfig.mainType
  108. net.refreshType = sconfig.refreshType
  109. net.name = sconfig.name
  110. net.icon = sconfig.icon
  111. end
  112. end
  113. Grid.makeItem(msgRet.refreshItem,shopTypeConfig.refreshItemID,shopTypeConfig.refreshItemCnt)
  114. local shopConfig = ShopExcel[shopType]
  115. if not shopConfig then return end
  116. local now = os.time()
  117. initHumanShopDB(human,shopType)
  118. checkFreeCnt(human,shopType)
  119. msgRet.nextRefreshTime = 0
  120. msgRet.freeCnt = human.db.shop[shopType].freeCnt
  121. msgRet.freeMax = shopTypeConfig.freeCnt
  122. msgRet.costCnt = getLeftCostCnt(human, shopType)
  123. msgRet.costMax = getCostCntMax(human, shopType)
  124. if human.db.shop[shopType].refreshTs > now then
  125. msgRet.nextRefreshTime = human.db.shop[shopType].refreshTs - now
  126. end
  127. cleanShopGoods(human, shopType)
  128. -- 不刷新的没有记录的 走配置
  129. local cnt = 0
  130. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
  131. for index, v in pairs(shopConfig) do
  132. if not human.db.shop[shopType].goods[index] then
  133. local tempConfig = v
  134. cnt = cnt + 1
  135. Grid.makeItem(msgRet.list[cnt].itemData, tempConfig.itemID, tempConfig.cnt)
  136. msgRet.list[cnt].itemIndex = index
  137. Grid.makeItem(msgRet.list[cnt].needItem, tempConfig.needItemID, tempConfig.price)
  138. msgRet.list[cnt].maxCanBuy = tempConfig.limitBuyCnt
  139. msgRet.list[cnt].nowBuy = 0
  140. msgRet.list[cnt].zhekou = tempConfig.zhekou
  141. msgRet.list[cnt].order = tempConfig.order
  142. msgRet.list[cnt].needVipLv = tempConfig.needVipLv
  143. msgRet.list[cnt].rare = tempConfig.rare
  144. msgRet.list[cnt].limitType = tempConfig.limitType
  145. end
  146. end
  147. end
  148. for _, v in pairs(human.db.shop[shopType].goods) do
  149. local tempConfig = shopConfig[v.index]
  150. cnt = cnt + 1
  151. Grid.makeItem(msgRet.list[cnt].itemData, tempConfig.itemID, tempConfig.cnt)
  152. msgRet.list[cnt].itemIndex = v.index
  153. Grid.makeItem(msgRet.list[cnt].needItem, tempConfig.needItemID, tempConfig.price)
  154. msgRet.list[cnt].maxCanBuy = tempConfig.limitBuyCnt
  155. msgRet.list[cnt].nowBuy = v.nowBuy
  156. msgRet.list[cnt].zhekou = tempConfig.zhekou
  157. msgRet.list[cnt].order = tempConfig.order
  158. msgRet.list[cnt].needVipLv = tempConfig.needVipLv
  159. msgRet.list[cnt].rare = tempConfig.rare
  160. msgRet.list[cnt].limitType = tempConfig.limitType
  161. end
  162. msgRet.list[0] = cnt
  163. Msg.send(msgRet, human.fd)
  164. end
  165. -- 刷新物品
  166. function refresh(human, shopType)
  167. local shopConfig = ShopExcel.shop[shopType]
  168. if not shopConfig then return end
  169. if not inServerIndex(shopConfig.serverIndex) then return end
  170. cleanShopGoods(human, shopType)
  171. -- 是否可刷新
  172. -- 不是主动刷新,主动+日,主动+购完
  173. if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE1
  174. and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2
  175. and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
  176. return
  177. end
  178. -- 主动+日 主动+购完重置 主动刷新不判断时间
  179. if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2
  180. or shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
  181. local now = os.time()
  182. if human.db.shop[shopType].refreshTs > now -- 刷新时间未到
  183. and human.db.shop[shopType].freeCnt <= 0 -- 免费刷新次数小于0
  184. and human.db.shop[shopType].costCnt >= ShopExcel.shop[shopType].costCnt
  185. and ShopExcel.shop[shopType].costCnt ~= -1 then -- 花费刷新次数小于0
  186. return
  187. end
  188. end
  189. local isFree = nil -- 是否免费
  190. -- 不需要刷新物品,则可免费刷新
  191. if shopConfig.refreshItemID == 0 then
  192. isFree = true
  193. else
  194. -- 需要物品,且可免费次数不等于0,且有剩余可免费次数
  195. if shopConfig.freeCnt ~= 0 and human.db.shop[shopType].freeCnt > 0 then
  196. isFree = true
  197. end
  198. end
  199. -- 判断道具
  200. local needItemID = shopConfig.refreshItemID
  201. local needItemCnt = shopConfig.refreshItemCnt
  202. -- 需要道具
  203. if isFree ~= true then
  204. -- 判断剩余刷新次数
  205. if getCostCntMax(human, shopType) ~= 0 and getCostCntMax(human, shopType) ~= -1 and getLeftCostCnt(human, shopType) < 1 then
  206. return Broadcast.sendErr(human, Lang.SHOP_REFRESH_ERR_CNT)
  207. end
  208. -- 判断道具
  209. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  210. if nowItemCnt < needItemCnt then
  211. return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
  212. end
  213. -- 扣除道具
  214. BagLogic.delItem(human, needItemID, needItemCnt, "shop_refresh")
  215. human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt + 1
  216. else
  217. if shopConfig.refreshItemID ~= 0 then
  218. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt - 1
  219. end
  220. end
  221. -- 刷新
  222. initHumanShopDB(human,shopType,true)
  223. if isFree then
  224. -- 如果没有免费刷新了则刷新时间置0
  225. if human.db.shop[shopType].freeCnt >= shopConfig.freeCnt then
  226. human.db.shop[shopType].refreshTs = 0
  227. end
  228. end
  229. -- 通知客户端
  230. query(human, shopType)
  231. end
  232. function buyAllItemsRefresh(human,shopType)
  233. for itemIndex in pairs(human.db.shop[shopType].goods) do
  234. local tempConfig = ShopExcel[shopType][itemIndex]
  235. if tempConfig == nil then
  236. return
  237. end
  238. local maxCanBuy = tempConfig.limitBuyCnt
  239. if maxCanBuy ~= 0 then
  240. local nowBuy = human.db.shop[shopType].goods[itemIndex].nowBuy
  241. if nowBuy < maxCanBuy then
  242. return
  243. end
  244. end
  245. end
  246. initHumanShopDB(human,shopType,true)
  247. return true
  248. end
  249. -- 购买物品
  250. function buy(human, shopType, itemID, itemIndex, buyCnt)
  251. if buyCnt < 1 then return end --购买数量不该小于1呀
  252. if buyCnt > 30000 then
  253. return
  254. end
  255. local tempConfig = ShopExcel[shopType][itemIndex]
  256. if tempConfig == nil then
  257. return
  258. end
  259. -- 商店强制效验
  260. if tempConfig.itemID ~= itemID then
  261. return Broadcast.sendErr(human, Lang.ITEM_BUY_ERROR)
  262. end
  263. if tempConfig.needVipLv ~= 0 and VipLogic.getVipLv(human) < tempConfig.needVipLv then
  264. return Broadcast.sendErr(human, Lang.VIP_LV_TOO_LOW)
  265. end
  266. -- 装备检测数量
  267. if ItemDefine.isEquip(itemID) then
  268. if not EquipLogic.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  269. return
  270. end
  271. end
  272. -- 符文检测数量
  273. if ItemDefine.isFuwen(itemID) then
  274. if not FuwenGrid.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  275. return
  276. end
  277. end
  278. cleanShopGoods(human, shopType)
  279. -- 判断上限
  280. local maxCanBuy = tempConfig.limitBuyCnt
  281. local dbIndex = itemIndex
  282. local nowBuy = 0
  283. for k,v in pairs(human.db.shop[shopType].goods) do
  284. if v.index == itemIndex then
  285. nowBuy = v.nowBuy
  286. dbIndex = k
  287. break
  288. end
  289. end
  290. if maxCanBuy ~= 0 and maxCanBuy - nowBuy < buyCnt then
  291. return
  292. end
  293. if dbIndex == 0 then
  294. return
  295. end
  296. -- 道具判断
  297. local needItemID = tempConfig.needItemID
  298. local needItemCnt = tempConfig.price * buyCnt
  299. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  300. if nowItemCnt < needItemCnt then
  301. return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
  302. end
  303. -- 改db
  304. if human.db.shop[shopType].goods and human.db.shop[shopType].goods[dbIndex] then
  305. human.db.shop[shopType].goods[dbIndex].nowBuy = human.db.shop[shopType].goods[dbIndex].nowBuy + buyCnt
  306. local refreshType = ShopExcel.shop[shopType].refreshType
  307. if refreshType == ShopDefine.SHOP_REFRESH_TYPE6
  308. or refreshType == ShopDefine.SHOP_REFRESH_TYPE7 then
  309. buyAllItemsRefresh(human,shopType)
  310. end
  311. end
  312. -- 先删
  313. BagLogic.delItem(human, needItemID, needItemCnt, "shop_buy", nil, itemID, buyCnt * tempConfig.cnt)
  314. -- 后加
  315. BagLogic.addItem(human, itemID, buyCnt * tempConfig.cnt, "shop_buy")
  316. -- 加道具
  317. --ChatPaoMaLogic.trigger(human, ChatPaoMaLogic.TRIGGER_TYPE_1, itemID, buyCnt * tempConfig.cnt, needItemID, needItemCnt)
  318. --通知客户端
  319. local rewardID = itemID
  320. local rewardCnt = tempConfig.cnt * buyCnt
  321. local msgRet = Msg.gc.GC_SHOP_BUY
  322. msgRet.shopType = shopType
  323. if ItemDefine.isEquip(rewardID) then
  324. EquipLogic.makeEquipItemOne(human, msgRet.item)
  325. else
  326. Grid.makeItem(msgRet.item, rewardID, rewardCnt)
  327. end
  328. Msg.send(msgRet, human.fd)
  329. query(human, shopType)
  330. GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)
  331. -- 商店购买物品回调
  332. if shopType == ShopDefine.SHOP_TYPE_UNION then
  333. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_SHOP,1)
  334. end
  335. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
  336. ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_4,1)
  337. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE10, 1)
  338. YunYingLogic.onCallBack(human, "onShopBuy",1)
  339. end
  340. end
  341. -- 初始化玩家商店数据
  342. function initHumanShopDB(human, shopType,refresh)
  343. local shopTypeConfig = ShopExcel.shop[shopType]
  344. local shopConfig = ShopExcel[shopType]
  345. if not shopConfig then return end
  346. if not shopTypeConfig then return end
  347. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  348. -- 下次刷新时间
  349. local initDb = nil
  350. if not human.db.shop or not human.db.shop[shopType] or not human.db.shop[shopType].goods then
  351. -- 初始化
  352. initDb = true
  353. elseif refresh == nil then
  354. -- 不是初始化也不是刷新
  355. return
  356. end
  357. human.db.shop = human.db.shop or {}
  358. human.db.shop[shopType] = human.db.shop[shopType] or {}
  359. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt or shopTypeConfig.freeCnt -- 免费刷新次数
  360. human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt or 0 -- 钻石刷新次数
  361. human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs or 0
  362. human.db.shop[shopType].goods = human.db.shop[shopType].goods or {}
  363. -- 固定物品,重置类型
  364. local now = os.time()
  365. if shopTypeConfig.refreshGoods == 1 then
  366. -- 随机物品,刷新类型
  367. human.db.shop[shopType].goods = {}
  368. getRandShopGoods(human,shopType)
  369. else
  370. for i, v in pairs(shopConfig) do
  371. -- 刷新时,活动物品不重置
  372. local refreshTs = v.refreshTs * 60 * 60
  373. local goodsRefreshTs = human.db.shop[shopType].goods[i] and human.db.shop[shopType].goods[i].refreshTs or 0
  374. local endRefreshTs = 0
  375. if initDb == true or refresh ~= nil then -- 初始化
  376. endRefreshTs = now + refreshTs
  377. else -- 其他
  378. local resetCnt = 0
  379. if v.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then -- 日限购
  380. local sameDay = Util.isSameDayByTimes(now, goodsRefreshTs)
  381. if sameDay ~= true then
  382. local dayStart = Util.getDayStartTime(now)
  383. endRefreshTs = dayStart + refreshTs
  384. end
  385. elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then -- 周限购
  386. local sameWeek = Util.isSameWeek(now, goodsRefreshTs)
  387. if sameWeek ~= true then
  388. local weekStart = Util.getWeekStartTime(now)
  389. endRefreshTs = weekStart + refreshTs
  390. end
  391. elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then -- 月限购
  392. local sameMonth = Util.isSameMonth(now, goodsRefreshTs)
  393. if sameMonth ~= true then
  394. local monthStart = Util.getMonthStartTime(now)
  395. endRefreshTs = monthStart + refreshTs
  396. end
  397. end
  398. end
  399. if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 and endRefreshTs > 0 then
  400. initGoodsDb(human, shopType, i, endRefreshTs)
  401. end
  402. end
  403. end
  404. -- 设置免费刷新时间
  405. if refresh ~= nil then
  406. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE1
  407. and human.db.shop[shopType].refreshTs == 0 then
  408. human.db.shop[shopType].refreshTs = os.time() + shopTypeConfig.refreshTs * 60 * 60
  409. end
  410. end
  411. -- 设置商店重置时间
  412. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE3
  413. or shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
  414. -- 每日自动刷新 或 主动刷新+日刷新
  415. local dayStart = Util.getDayStartTime(now)
  416. human.db.shop[shopType].refreshTs = dayStart + 24*60*60 -- 下次刷新时间
  417. elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
  418. -- 周刷新
  419. local weekStart = Util.getWeekStartTime(now)
  420. human.db.shop[shopType].refreshTs = weekStart + 7*24*60*60 -- 下次刷新时间
  421. elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
  422. -- 月刷新
  423. local monthStart = Util.getMonthStartTime(now)
  424. human.db.shop[shopType].refreshTs = monthStart + 30*24*60*60 -- 下次刷新时间
  425. end
  426. end
  427. -- 刷新商店物品
  428. local specialTb = {1,5,6,7,8}
  429. function getRandShopGoods(human,shopType)
  430. local net = human.db.shop[shopType].goods
  431. local shopTypeConfig = ShopExcel.shop[shopType]
  432. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  433. if shopTypeConfig.refreshGoods ~= 1 then
  434. return
  435. end
  436. local shopConfig = ShopExcel[shopType]
  437. if not shopConfig then return end
  438. local now = os.time()
  439. local indexTb = {}
  440. -- 需要随机物品次数
  441. local count = 0
  442. local totalWeight = 0
  443. local totalWeight1 = 0
  444. local totalWeight2 = 0
  445. for k, v in pairs(shopConfig) do
  446. totalWeight = totalWeight + v.weight
  447. if v.itemType == 1 then
  448. totalWeight1 = totalWeight1 + v.weight
  449. end
  450. if v.itemType == 2 then
  451. totalWeight2 = totalWeight2 + v.weight
  452. end
  453. end
  454. for i = 1,shopTypeConfig.goodsCnt do
  455. local randomNum = math.random(1,totalWeight)
  456. for j, v in pairs(shopConfig) do
  457. if indexTb[j] == nil then
  458. if randomNum <= v.weight then
  459. count = count + 1
  460. net[count] = {}
  461. net[count].nowBuy = 0
  462. net[count].index = j
  463. net[count].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
  464. indexTb[j] = true
  465. totalWeight = totalWeight - v.weight
  466. break
  467. else
  468. randomNum = randomNum - v.weight
  469. end
  470. end
  471. end
  472. end
  473. -- 特殊商店刷新
  474. if shopType == ShopDefine.SHOP_TYPE_XUYUAN then
  475. local len = #shopConfig
  476. for _,v in pairs(specialTb) do
  477. local randomNum = 0
  478. if v == 1 then
  479. randomNum = math.random(1, totalWeight1)
  480. else
  481. randomNum = math.random(1, totalWeight2)
  482. end
  483. for j, b in pairs(shopConfig) do
  484. if v == b.itemType or
  485. (v ~= 1 and b.itemType == 2) then
  486. if randomNum <= b.weight then
  487. net[v] = {}
  488. net[v].nowBuy = 0
  489. net[v].index = j
  490. net[v].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
  491. break
  492. else
  493. randomNum = randomNum - b.weight
  494. end
  495. end
  496. end
  497. end
  498. end
  499. end
  500. function updateDaily(human)
  501. local shopTypeConfig = ShopExcel.shop
  502. local len = #shopTypeConfig
  503. local now = os.time()
  504. for i ,v in pairs(shopTypeConfig) do
  505. if inServerIndex(v.serverIndex) then
  506. -- 日刷新 or 主动+日
  507. cleanShopGoods(human, i)
  508. if i == ShopDefine.SHOP_TYPE_BLACKMARKET and human.db.shop and human.db.shop[i] then
  509. human.db.shop[i].costCnt = 0
  510. end
  511. if v.refreshType == ShopDefine.SHOP_REFRESH_TYPE3
  512. or v.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
  513. initHumanShopDB(human,i,true)
  514. -- 周刷新
  515. elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
  516. if human.db.shop and human.db.shop[i] then
  517. local sameWeek = Util.isSameWeek(now,human.db.shop[i].refreshTs)
  518. if sameWeek ~= true then
  519. initHumanShopDB(human,i,true)
  520. end
  521. else
  522. initHumanShopDB(human,i,true)
  523. end
  524. -- 月刷新
  525. elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
  526. if human.db.shop and human.db.shop[i] then
  527. local sameMonth = Util.isSameMonth(now,human.db.shop[i].refreshTs)
  528. if sameMonth ~= true then
  529. initHumanShopDB(human,i,true)
  530. end
  531. else
  532. initHumanShopDB(human,i,true)
  533. end
  534. -- 没配刷新类型,则根据道具类型来刷新
  535. else
  536. if human.db.shop and human.db.shop[i] then
  537. for k, v in pairs(human.db.shop[i].goods) do
  538. local config = ShopExcel[i][v.index]
  539. -- 日限购
  540. if config.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then
  541. local dayStart = Util.getDayStartTime(now)
  542. v.nowBuy = 0
  543. v.refreshTs = dayStart + config.refreshTs * 60 * 60
  544. -- 周限购
  545. elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then
  546. local sameWeek = Util.isSameWeek(now,v.refreshTs)
  547. if sameWeek ~= true then
  548. local weekStart = Util.getWeekStartTime(now)
  549. v.nowBuy = 0
  550. v.refreshTs = weekStart + config.refreshTs * 60 * 60
  551. end
  552. -- 月限购
  553. elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then
  554. local sameMonth = Util.isSameMonth(now,v.refreshTs)
  555. if sameMonth ~= true then
  556. local monthStart = Util.getMonthStartTime(now)
  557. v.nowBuy = 0
  558. v.refreshTs = monthStart + config.refreshTs * 60 * 60
  559. end
  560. end
  561. end
  562. end
  563. end
  564. end
  565. end
  566. end
  567. function checkFreeCnt(human,shopType)
  568. local shopConfig = ShopExcel.shop[shopType]
  569. if not shopConfig then return end
  570. if not inServerIndex(shopConfig.serverIndex) then return end
  571. local now = os.time()
  572. -- 当前商店免费刷新次数不是满的
  573. local freeCnt = shopConfig.freeCnt - human.db.shop[shopType].freeCnt
  574. if freeCnt > 0 then
  575. local opTs = human.db.shop[shopType].refreshTs - shopConfig.refreshTs*60*60
  576. local overTime = now - opTs
  577. local cnt = math.floor(overTime/(shopConfig.refreshTs*60*60))
  578. if cnt == 0 then
  579. return
  580. end
  581. if cnt >= freeCnt then
  582. human.db.shop[shopType].freeCnt = shopConfig.freeCnt
  583. human.db.shop[shopType].refreshTs = 0
  584. else
  585. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt + cnt
  586. human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs + (shopConfig.refreshTs*60*60*cnt)
  587. end
  588. end
  589. end