ShopLogic.lua 25 KB

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