ShopLogic.lua 25 KB

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