ShopLogic.lua 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 MonthCard = require("present.MonthCard")
  28. local function inServerIndex(svrIndexs)
  29. for i = 1, #svrIndexs do
  30. local index1 = svrIndexs[i][1]
  31. local index2 = svrIndexs[i][2]
  32. if Config.SVR_INDEX >= index1 and Config.SVR_INDEX <= index2 then
  33. return true
  34. end
  35. end
  36. end
  37. local function initGoodsDb(human, shopType, index, refreshTs)
  38. if not human.db.shop[shopType].goods then return end
  39. -- 存在旧的,用旧的数据
  40. local bChose = 0
  41. if human.db.shop[shopType].goods[index] and human.db.shop[shopType].goods[index].bChose then
  42. bChose = human.db.shop[shopType].goods[index].bChose
  43. end
  44. human.db.shop[shopType].goods[index] = {}
  45. human.db.shop[shopType].goods[index].nowBuy = 0
  46. human.db.shop[shopType].goods[index].bChose = bChose
  47. human.db.shop[shopType].goods[index].index = index
  48. human.db.shop[shopType].goods[index].refreshTs = refreshTs
  49. end
  50. local function ShopLogic_GetChose(human, shopType, index)
  51. if not human.db.shop[shopType].goods then
  52. return 0
  53. end
  54. if not human.db.shop[shopType].goods[index] then
  55. return 0
  56. end
  57. if not human.db.shop[shopType].goods[index].bChose then
  58. human.db.shop[shopType].goods[index].bChose = 0
  59. end
  60. return human.db.shop[shopType].goods[index].bChose
  61. end
  62. local function ShopLogic_SetChose(human, shopType, index, nValue)
  63. if not human.db.shop[shopType].goods or not human.db.shop[shopType].goods[index] then
  64. return false
  65. end
  66. if not human.db.shop[shopType].goods[index].bChose then
  67. human.db.shop[shopType].goods[index].bChose = 0
  68. end
  69. human.db.shop[shopType].goods[index].bChose = nValue
  70. return true
  71. end
  72. -- 花费刷新总次数
  73. function getCostCntMax(human, shopType)
  74. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
  75. return VipLogic.getPowerArgs(human, VipLogic.VIP_POWER4) or 0
  76. end
  77. local config = ShopExcel.shop[shopType]
  78. if not config then return 0 end
  79. return config.costCnt
  80. end
  81. -- 剩余花费刷新次数
  82. function getLeftCostCnt(human, shopType)
  83. local maxCnt = getCostCntMax(human, shopType)
  84. local shopData = human.db.shop[shopType]
  85. local useCnt = shopData and shopData.costCnt or 0
  86. return math.max(maxCnt - useCnt, 0)
  87. end
  88. -- 清除删掉的商品 增加限制的数据
  89. function cleanShopGoods(human, shopType)
  90. local shopTypeConfig = ShopExcel.shop[shopType]
  91. if not shopTypeConfig then return end
  92. local shopConfig = ShopExcel[shopType]
  93. if not shopConfig then return end
  94. if not human.db.shop then return end
  95. if not human.db.shop[shopType] then return end
  96. if not human.db.shop[shopType].goods then return end
  97. local shopBuy = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)
  98. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET and not shopBuy then
  99. for k,v in pairs(human.db.shop[shopType].goods) do
  100. human.db.shop[shopType].goods[k].index = 0
  101. break
  102. end
  103. end
  104. -- 清除没有的
  105. for k,v in pairs(human.db.shop[shopType].goods) do
  106. local tempConfig = shopConfig[v.index]
  107. if not tempConfig then
  108. human.db.shop[shopType].goods[k] = nil
  109. end
  110. end
  111. -- 加入新增的
  112. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
  113. for index, v in pairs(shopConfig) do
  114. if not human.db.shop[shopType].goods[index] then
  115. if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 then
  116. initGoodsDb(human, shopType, index, 0)
  117. end
  118. end
  119. end
  120. end
  121. end
  122. -- 查询商店物品
  123. function query(human, shopType)
  124. local shopTypeConfig = ShopExcel.shop[shopType]
  125. if not shopTypeConfig then return end
  126. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  127. local msgRet = Msg.gc.GC_SHOP_QUERY
  128. msgRet.shopInfo.shopType = shopType
  129. msgRet.shopInfo.mainType = shopTypeConfig.mainType
  130. msgRet.shopInfo.refreshType = shopTypeConfig.refreshType
  131. msgRet.shopInfo.name = shopTypeConfig.name
  132. msgRet.shopInfo.icon = shopTypeConfig.icon
  133. msgRet.shopList[0] = 0
  134. for stype, sconfig in pairs(ShopExcel.shop) do
  135. if sconfig.mainType == shopTypeConfig.mainType and inServerIndex(sconfig.serverIndex) then
  136. msgRet.shopList[0] = msgRet.shopList[0] + 1
  137. local net = msgRet.shopList[msgRet.shopList[0]]
  138. net.shopType = stype
  139. net.mainType = sconfig.mainType
  140. net.refreshType = sconfig.refreshType
  141. net.name = sconfig.name
  142. net.icon = sconfig.icon
  143. end
  144. end
  145. Grid.makeItem(msgRet.refreshItem, shopTypeConfig.refreshItemID, shopTypeConfig.refreshItemCnt)
  146. local shopConfig = ShopExcel[shopType]
  147. if not shopConfig then return end
  148. local now = os.time()
  149. initHumanShopDB(human, shopType)
  150. checkFreeCnt(human, shopType)
  151. msgRet.nextRefreshTime = 0
  152. if human.db.shop[shopType].refreshTs > now then
  153. msgRet.nextRefreshTime = human.db.shop[shopType].refreshTs - now
  154. end
  155. cleanShopGoods(human, shopType)
  156. -- 自动购买
  157. AutoBuyChoseItem(human, shopType)
  158. msgRet.freeCnt = human.db.shop[shopType].freeCnt
  159. msgRet.freeMax = shopTypeConfig.freeCnt
  160. msgRet.costCnt = getLeftCostCnt(human, shopType)
  161. msgRet.costMax = getCostCntMax(human, shopType)
  162. -- 先把所有商品数据收集到临时表
  163. local allGoods = {}
  164. -- 不刷新的没有记录的 走配置
  165. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE0 then
  166. for index, v in pairs(shopConfig) do
  167. if not human.db.shop[shopType].goods[index] then
  168. local tempConfig = v
  169. allGoods[#allGoods + 1] = {
  170. itemID = tempConfig.itemID,
  171. itemCnt = tempConfig.cnt,
  172. itemIndex = index,
  173. needItemID = tempConfig.needItemID,
  174. needItemCnt = tempConfig.price,
  175. maxCanBuy = tempConfig.limitBuyCnt,
  176. nowBuy = 0,
  177. zhekou = tempConfig.zhekou,
  178. order = tempConfig.order,
  179. needVipLv = tempConfig.needVipLv,
  180. rare = tempConfig.rare,
  181. limitType = tempConfig.limitType,
  182. bCanChose = tempConfig.isquickbuy and tempConfig.isquickbuy or 0,
  183. bChose = 0,
  184. }
  185. end
  186. end
  187. end
  188. for _, v in pairs(human.db.shop[shopType].goods) do
  189. local tempConfig = shopConfig[v.index]
  190. allGoods[#allGoods + 1] = {
  191. itemID = tempConfig.itemID,
  192. itemCnt = tempConfig.cnt,
  193. itemIndex = v.index,
  194. needItemID = tempConfig.needItemID,
  195. needItemCnt = tempConfig.price,
  196. maxCanBuy = tempConfig.limitBuyCnt,
  197. nowBuy = v.nowBuy,
  198. zhekou = tempConfig.zhekou,
  199. order = tempConfig.order,
  200. needVipLv = tempConfig.needVipLv,
  201. rare = tempConfig.rare,
  202. limitType = tempConfig.limitType,
  203. bCanChose = tempConfig.isquickbuy and tempConfig.isquickbuy or 0,
  204. bChose = ShopLogic_GetChose(human, shopType, v.index),
  205. }
  206. end
  207. -- 分批发送,每批最多 15 条
  208. local BATCH_SIZE = 15
  209. local total = #allGoods
  210. local totalBatches = math.max(math.ceil(total / BATCH_SIZE), 1)
  211. for batchIdx = 1, totalBatches do
  212. local startIdx = (batchIdx - 1) * BATCH_SIZE + 1
  213. local endIdx = math.min(batchIdx * BATCH_SIZE, total)
  214. msgRet.isStart = (batchIdx == 1) and 1 or 0
  215. msgRet.isEnd = (batchIdx == totalBatches) and 1 or 0
  216. -- 非第一批不重复发送商店列表等头部信息
  217. if batchIdx > 1 then
  218. msgRet.shopList[0] = 0
  219. end
  220. local cnt = 0
  221. for i = startIdx, endIdx do
  222. cnt = cnt + 1
  223. local item = allGoods[i]
  224. Grid.makeItem(msgRet.list[cnt].itemData, item.itemID, item.itemCnt)
  225. msgRet.list[cnt].itemIndex = item.itemIndex
  226. Grid.makeItem(msgRet.list[cnt].needItem, item.needItemID, item.needItemCnt)
  227. msgRet.list[cnt].maxCanBuy = item.maxCanBuy
  228. msgRet.list[cnt].nowBuy = item.nowBuy
  229. msgRet.list[cnt].zhekou = item.zhekou
  230. msgRet.list[cnt].order = item.order
  231. msgRet.list[cnt].needVipLv = item.needVipLv
  232. msgRet.list[cnt].rare = item.rare
  233. msgRet.list[cnt].limitType = item.limitType
  234. msgRet.list[cnt].bCanChose = item.bCanChose
  235. msgRet.list[cnt].bChose = item.bChose
  236. end
  237. msgRet.list[0] = cnt
  238. Msg.send(msgRet, human.fd)
  239. end
  240. end
  241. -- 刷新物品
  242. function refresh(human, shopType)
  243. local shopConfig = ShopExcel.shop[shopType]
  244. if not shopConfig then return end
  245. if not inServerIndex(shopConfig.serverIndex) then return end
  246. cleanShopGoods(human, shopType)
  247. -- 是否可刷新
  248. -- 不是主动刷新,主动+日,主动+购完
  249. if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE1
  250. and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2
  251. and shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
  252. return
  253. end
  254. -- 主动+日 主动+购完重置 主动刷新不判断时间
  255. if shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE2
  256. or shopConfig.refreshType ~= ShopDefine.SHOP_REFRESH_TYPE7 then
  257. local now = os.time()
  258. if human.db.shop[shopType].refreshTs > now -- 刷新时间未到
  259. and human.db.shop[shopType].freeCnt <= 0 -- 免费刷新次数小于0
  260. and human.db.shop[shopType].costCnt >= ShopExcel.shop[shopType].costCnt
  261. and ShopExcel.shop[shopType].costCnt ~= -1 then -- 花费刷新次数小于0
  262. return
  263. end
  264. end
  265. local isFree = nil -- 是否免费
  266. -- 不需要刷新物品,则可免费刷新
  267. if shopConfig.refreshItemID == 0 then
  268. isFree = true
  269. else
  270. -- 需要物品,且可免费次数不等于0,且有剩余可免费次数
  271. if shopConfig.freeCnt ~= 0 and human.db.shop[shopType].freeCnt > 0 then
  272. isFree = true
  273. end
  274. end
  275. -- 判断道具
  276. local needItemID = shopConfig.refreshItemID
  277. local needItemCnt = shopConfig.refreshItemCnt
  278. -- 需要道具
  279. if isFree ~= true then
  280. -- 判断剩余刷新次数
  281. if getCostCntMax(human, shopType) ~= 0 and getCostCntMax(human, shopType) ~= -1 and getLeftCostCnt(human, shopType) < 1 then
  282. return Broadcast.sendErr(human, Lang.SHOP_REFRESH_ERR_CNT)
  283. end
  284. -- 判断道具
  285. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  286. if nowItemCnt < needItemCnt then
  287. return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
  288. end
  289. -- 扣除道具
  290. BagLogic.delItem(human, needItemID, needItemCnt, "shop_refresh")
  291. human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt + 1
  292. else
  293. if shopConfig.refreshItemID ~= 0 then
  294. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt - 1
  295. end
  296. end
  297. -- 刷新
  298. initHumanShopDB(human,shopType,true)
  299. if isFree then
  300. -- 如果没有免费刷新了则刷新时间置0
  301. if human.db.shop[shopType].freeCnt >= shopConfig.freeCnt then
  302. human.db.shop[shopType].refreshTs = 0
  303. end
  304. end
  305. -- 自动购买
  306. if (shopType == ShopDefine.SHOP_MAIN_TYPE_1 or shopType == ShopDefine.SHOP_TYPE_XUYUAN) and human.db.shop[shopType].recordList then
  307. BuyItemBtAuto(human, shopType)
  308. end
  309. -- 通知客户端
  310. query(human, shopType)
  311. end
  312. function buyAllItemsRefresh(human,shopType)
  313. for itemIndex in pairs(human.db.shop[shopType].goods) do
  314. local tempConfig = ShopExcel[shopType][itemIndex]
  315. if tempConfig == nil then
  316. return
  317. end
  318. local maxCanBuy = tempConfig.limitBuyCnt
  319. if maxCanBuy ~= 0 then
  320. local nowBuy = human.db.shop[shopType].goods[itemIndex].nowBuy
  321. if nowBuy < maxCanBuy then
  322. return
  323. end
  324. end
  325. end
  326. initHumanShopDB(human,shopType,true)
  327. return true
  328. end
  329. -- 购买物品
  330. function buy(human, shopType, itemID, itemIndex, buyCnt)
  331. if buyCnt < 1 then return end --购买数量不该小于1呀
  332. if buyCnt > 30000 then
  333. return
  334. end
  335. local tempConfig = ShopExcel[shopType][itemIndex]
  336. if tempConfig == nil then
  337. return
  338. end
  339. -- 商店强制效验
  340. if tempConfig.itemID ~= itemID then
  341. return Broadcast.sendErr(human, Lang.ITEM_BUY_ERROR)
  342. end
  343. if tempConfig.needVipLv ~= 0 and VipLogic.getVipLv(human) < tempConfig.needVipLv then
  344. return Broadcast.sendErr(human, Lang.VIP_LV_TOO_LOW)
  345. end
  346. -- 装备检测数量
  347. if ItemDefine.isEquip(itemID) then
  348. if not EquipLogic.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  349. return
  350. end
  351. end
  352. -- 符文检测数量
  353. if ItemDefine.isFuwen(itemID) then
  354. if not FuwenGrid.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  355. return
  356. end
  357. end
  358. cleanShopGoods(human, shopType)
  359. -- 判断上限
  360. local maxCanBuy = tempConfig.limitBuyCnt
  361. local dbIndex = itemIndex
  362. local nowBuy = 0
  363. for k,v in pairs(human.db.shop[shopType].goods) do
  364. if v.index == itemIndex then
  365. nowBuy = v.nowBuy
  366. dbIndex = k
  367. break
  368. end
  369. end
  370. if maxCanBuy ~= 0 and maxCanBuy - nowBuy < buyCnt then
  371. return
  372. end
  373. if dbIndex == 0 then
  374. return
  375. end
  376. -- 道具判断
  377. local needItemID = tempConfig.needItemID
  378. local needItemCnt = tempConfig.price * buyCnt
  379. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  380. if nowItemCnt < needItemCnt then
  381. return Broadcast.sendErr(human, Util.format(Lang.SHOP_ERR_NOITEM,ItemDefine.getValue(needItemID,"name")))
  382. end
  383. -- 改db
  384. if human.db.shop[shopType].goods and human.db.shop[shopType].goods[dbIndex] then
  385. human.db.shop[shopType].goods[dbIndex].nowBuy = human.db.shop[shopType].goods[dbIndex].nowBuy + buyCnt
  386. local refreshType = ShopExcel.shop[shopType].refreshType
  387. if refreshType == ShopDefine.SHOP_REFRESH_TYPE6
  388. or refreshType == ShopDefine.SHOP_REFRESH_TYPE7 then
  389. buyAllItemsRefresh(human,shopType)
  390. end
  391. end
  392. -- 先删
  393. BagLogic.delItem(human, needItemID, needItemCnt, "shop_buy", nil, itemID, buyCnt * tempConfig.cnt)
  394. -- 后加
  395. BagLogic.addItem(human, itemID, buyCnt * tempConfig.cnt, "shop_buy")
  396. -- 加道具
  397. --ChatPaoMaLogic.trigger(human, ChatPaoMaLogic.TRIGGER_TYPE_1, itemID, buyCnt * tempConfig.cnt, needItemID, needItemCnt)
  398. --通知客户端
  399. local rewardID = itemID
  400. local rewardCnt = tempConfig.cnt * buyCnt
  401. local msgRet = Msg.gc.GC_SHOP_BUY
  402. msgRet.shopType = shopType
  403. if ItemDefine.isEquip(rewardID) then
  404. EquipLogic.makeEquipItemOne(human, msgRet.item)
  405. else
  406. Grid.makeItem(msgRet.item, rewardID, rewardCnt)
  407. end
  408. Msg.send(msgRet, human.fd)
  409. query(human, shopType)
  410. GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_SHOPBUY)
  411. -- 商店购买物品回调
  412. if shopType == ShopDefine.SHOP_TYPE_UNION then
  413. UnionLivenessLogic.touchLiveness(human,UnionDefine.UNION_LIVENESS_SHOP,1)
  414. end
  415. if shopType == ShopDefine.SHOP_TYPE_BLACKMARKET then
  416. ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_4,1)
  417. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE10, 1)
  418. YunYingLogic.onCallBack(human, "onShopBuy",1)
  419. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_SHOPBUY, human.db._id, 1)
  420. end
  421. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_7, 1)
  422. end
  423. -- 初始化玩家商店数据
  424. function initHumanShopDB(human, shopType,refresh)
  425. local shopTypeConfig = ShopExcel.shop[shopType]
  426. local shopConfig = ShopExcel[shopType]
  427. if not shopConfig then return end
  428. if not shopTypeConfig then return end
  429. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  430. -- 下次刷新时间
  431. local initDb = nil
  432. if not human.db.shop or not human.db.shop[shopType] or not human.db.shop[shopType].goods then
  433. -- 初始化
  434. initDb = true
  435. elseif refresh == nil then
  436. -- 不是初始化也不是刷新
  437. return
  438. end
  439. human.db.shop = human.db.shop or {}
  440. human.db.shop[shopType] = human.db.shop[shopType] or {}
  441. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt or shopTypeConfig.freeCnt -- 免费刷新次数
  442. human.db.shop[shopType].costCnt = human.db.shop[shopType].costCnt or 0 -- 钻石刷新次数
  443. human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs or 0
  444. human.db.shop[shopType].goods = human.db.shop[shopType].goods or {}
  445. -- 固定物品,重置类型
  446. local now = os.time()
  447. if shopTypeConfig.refreshGoods == 1 then
  448. -- 随机物品,刷新类型
  449. human.db.shop[shopType].goods = {}
  450. getRandShopGoods(human,shopType)
  451. else
  452. for i, v in pairs(shopConfig) do
  453. -- 刷新时,活动物品不重置
  454. local refreshTs = v.refreshTs * 60 * 60
  455. local goodsRefreshTs = human.db.shop[shopType].goods[i] and human.db.shop[shopType].goods[i].refreshTs or 0
  456. local endRefreshTs = 0
  457. if initDb == true or refresh ~= nil then -- 初始化
  458. endRefreshTs = now + refreshTs
  459. else -- 其他
  460. local resetCnt = 0
  461. if v.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then -- 日限购
  462. local sameDay = Util.isSameDayByTimes(now, goodsRefreshTs)
  463. if sameDay ~= true then
  464. local dayStart = Util.getDayStartTime(now)
  465. endRefreshTs = dayStart + refreshTs
  466. end
  467. elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then -- 周限购
  468. local sameWeek = Util.isSameWeek(now, goodsRefreshTs)
  469. if sameWeek ~= true then
  470. local weekStart = Util.getWeekStartTime(now)
  471. endRefreshTs = weekStart + refreshTs
  472. end
  473. elseif v.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then -- 月限购
  474. local sameMonth = Util.isSameMonth(now, goodsRefreshTs)
  475. if sameMonth ~= true then
  476. local monthStart = Util.getMonthStartTime(now)
  477. endRefreshTs = monthStart + refreshTs
  478. end
  479. end
  480. end
  481. if v.limitType ~= ShopDefine.SHOP_LIMIT_TYPE0 and endRefreshTs > 0 then
  482. initGoodsDb(human, shopType, i, endRefreshTs)
  483. end
  484. end
  485. end
  486. -- 设置免费刷新时间
  487. if refresh ~= nil then
  488. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE1
  489. and human.db.shop[shopType].refreshTs == 0 then
  490. human.db.shop[shopType].refreshTs = os.time() + shopTypeConfig.refreshTs * 60 * 60
  491. end
  492. end
  493. -- 设置商店重置时间
  494. if shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE3
  495. or shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
  496. -- 每日自动刷新 或 主动刷新+日刷新
  497. local dayStart = Util.getDayStartTime(now)
  498. human.db.shop[shopType].refreshTs = dayStart + 24*60*60 -- 下次刷新时间
  499. elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
  500. -- 周刷新
  501. local weekStart = Util.getWeekStartTime(now)
  502. human.db.shop[shopType].refreshTs = weekStart + 7*24*60*60 -- 下次刷新时间
  503. elseif shopTypeConfig.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
  504. -- 月刷新
  505. local monthStart = Util.getMonthStartTime(now)
  506. human.db.shop[shopType].refreshTs = monthStart + 30*24*60*60 -- 下次刷新时间
  507. end
  508. end
  509. -- 刷新商店物品
  510. local specialTb = {1,5,6,7,8}
  511. function getRandShopGoods(human,shopType)
  512. local net = human.db.shop[shopType].goods
  513. local shopTypeConfig = ShopExcel.shop[shopType]
  514. if not inServerIndex(shopTypeConfig.serverIndex) then return end
  515. if shopTypeConfig.refreshGoods ~= 1 then
  516. return
  517. end
  518. local shopConfig = ShopExcel[shopType]
  519. if not shopConfig then return end
  520. local now = os.time()
  521. local indexTb = {}
  522. -- 需要随机物品次数
  523. local count = 0
  524. local totalWeight = 0
  525. local totalWeight1 = 0
  526. local totalWeight2 = 0
  527. for k, v in pairs(shopConfig) do
  528. totalWeight = totalWeight + v.weight
  529. if v.itemType == 1 then
  530. totalWeight1 = totalWeight1 + v.weight
  531. end
  532. if v.itemType == 2 then
  533. totalWeight2 = totalWeight2 + v.weight
  534. end
  535. end
  536. for i = 1,shopTypeConfig.goodsCnt do
  537. local randomNum = math.random(1,totalWeight)
  538. for j, v in pairs(shopConfig) do
  539. if indexTb[j] == nil then
  540. if randomNum <= v.weight then
  541. count = count + 1
  542. net[count] = {}
  543. net[count].nowBuy = 0
  544. net[count].index = j
  545. net[count].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
  546. indexTb[j] = true
  547. totalWeight = totalWeight - v.weight
  548. break
  549. else
  550. randomNum = randomNum - v.weight
  551. end
  552. end
  553. end
  554. end
  555. -- 特殊商店刷新
  556. if shopType == ShopDefine.SHOP_TYPE_XUYUAN then
  557. local len = #shopConfig
  558. for _,v in pairs(specialTb) do
  559. local randomNum = 0
  560. if v == 1 then
  561. randomNum = math.random(1, totalWeight1)
  562. else
  563. randomNum = math.random(1, totalWeight2)
  564. end
  565. for j, b in pairs(shopConfig) do
  566. if v == b.itemType or
  567. (v ~= 1 and b.itemType == 2) then
  568. if randomNum <= b.weight then
  569. net[v] = {}
  570. net[v].nowBuy = 0
  571. net[v].index = j
  572. net[v].refreshTs = now + shopTypeConfig.refreshTs * 60 * 60
  573. break
  574. else
  575. randomNum = randomNum - b.weight
  576. end
  577. end
  578. end
  579. end
  580. end
  581. end
  582. function updateDaily(human)
  583. local shopTypeConfig = ShopExcel.shop
  584. local len = #shopTypeConfig
  585. local now = os.time()
  586. for i ,v in pairs(shopTypeConfig) do
  587. if inServerIndex(v.serverIndex) then
  588. -- 日刷新 or 主动+日
  589. cleanShopGoods(human, i)
  590. if i == ShopDefine.SHOP_TYPE_BLACKMARKET and human.db.shop and human.db.shop[i] then
  591. human.db.shop[i].costCnt = 0
  592. end
  593. if v.refreshType == ShopDefine.SHOP_REFRESH_TYPE3
  594. or v.refreshType == ShopDefine.SHOP_REFRESH_TYPE2 then
  595. initHumanShopDB(human,i,true)
  596. -- 周刷新
  597. elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE4 then
  598. if human.db.shop and human.db.shop[i] then
  599. local sameWeek = Util.isSameWeek(now,human.db.shop[i].refreshTs)
  600. if sameWeek ~= true then
  601. initHumanShopDB(human,i,true)
  602. end
  603. else
  604. initHumanShopDB(human,i,true)
  605. end
  606. -- 月刷新
  607. elseif v.refreshType == ShopDefine.SHOP_REFRESH_TYPE5 then
  608. if human.db.shop and human.db.shop[i] then
  609. local sameMonth = Util.isSameMonth(now,human.db.shop[i].refreshTs)
  610. if sameMonth ~= true then
  611. initHumanShopDB(human,i,true)
  612. end
  613. else
  614. initHumanShopDB(human,i,true)
  615. end
  616. -- 没配刷新类型,则根据道具类型来刷新
  617. else
  618. if human.db.shop and human.db.shop[i] then
  619. for k, v in pairs(human.db.shop[i].goods) do
  620. local config = ShopExcel[i][v.index]
  621. -- 日限购
  622. if config.limitType == ShopDefine.SHOP_LIMIT_TYPE2 then
  623. local dayStart = Util.getDayStartTime(now)
  624. v.nowBuy = 0
  625. v.refreshTs = dayStart + config.refreshTs * 60 * 60
  626. -- 周限购
  627. elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE3 then
  628. local sameWeek = Util.isSameWeek(now,v.refreshTs)
  629. if sameWeek ~= true then
  630. local weekStart = Util.getWeekStartTime(now)
  631. v.nowBuy = 0
  632. v.refreshTs = weekStart + config.refreshTs * 60 * 60
  633. end
  634. -- 月限购
  635. elseif config.limitType == ShopDefine.SHOP_LIMIT_TYPE4 then
  636. local sameMonth = Util.isSameMonth(now,v.refreshTs)
  637. if sameMonth ~= true then
  638. local monthStart = Util.getMonthStartTime(now)
  639. v.nowBuy = 0
  640. v.refreshTs = monthStart + config.refreshTs * 60 * 60
  641. end
  642. end
  643. end
  644. end
  645. end
  646. end
  647. end
  648. end
  649. function checkFreeCnt(human,shopType)
  650. local shopConfig = ShopExcel.shop[shopType]
  651. if not shopConfig then return end
  652. if not inServerIndex(shopConfig.serverIndex) then return end
  653. local now = os.time()
  654. -- 当前商店免费刷新次数不是满的
  655. local freeCnt = shopConfig.freeCnt - human.db.shop[shopType].freeCnt
  656. if freeCnt > 0 then
  657. local opTs = human.db.shop[shopType].refreshTs - shopConfig.refreshTs*60*60
  658. local overTime = now - opTs
  659. local cnt = math.floor(overTime/(shopConfig.refreshTs*60*60))
  660. if cnt == 0 then
  661. return
  662. end
  663. if cnt >= freeCnt then
  664. human.db.shop[shopType].freeCnt = shopConfig.freeCnt
  665. human.db.shop[shopType].refreshTs = 0
  666. else
  667. human.db.shop[shopType].freeCnt = human.db.shop[shopType].freeCnt + cnt
  668. human.db.shop[shopType].refreshTs = human.db.shop[shopType].refreshTs + (shopConfig.refreshTs*60*60*cnt)
  669. end
  670. end
  671. end
  672. local function getItemSubBuyCnt(human, recordList, itemCfg, itemInfo)
  673. if not itemCfg or not table.find(recordList, itemInfo.index) then
  674. return -1
  675. end
  676. local subBuyCnt = itemCfg.limitBuyCnt - itemInfo.nowBuy
  677. if subBuyCnt <= 0 then
  678. return subBuyCnt
  679. end
  680. if itemCfg.needVipLv ~= 0 and VipLogic.getVipLv(human) < itemCfg.needVipLv then
  681. return -1
  682. end
  683. -- 装备检测数量
  684. if ItemDefine.isEquip(itemCfg.itemID) then
  685. if not EquipLogic.checkEmptyCnt(human, subBuyCnt * itemCfg.cnt) then
  686. return -1
  687. end
  688. end
  689. -- 符文检测数量
  690. if ItemDefine.isFuwen(itemCfg.itemID) then
  691. if not FuwenGrid.checkEmptyCnt(human, subBuyCnt * itemCfg.cnt) then
  692. return -1
  693. end
  694. end
  695. -- 消耗道具检测
  696. local needItemID = itemCfg.needItemID
  697. local needItemCnt = itemCfg.price * subBuyCnt
  698. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  699. if nowItemCnt < needItemCnt then
  700. return -1
  701. end
  702. return subBuyCnt
  703. end
  704. -- 自动购买基础商店中随机商店里被记录的商品
  705. function BuyItemBtAuto(human, shopType)
  706. local recordList = human.db.shop[shopType] and human.db.shop[shopType].recordList
  707. if not recordList or not next(recordList) then
  708. print("[BuyItemBtAuto] 不存在对应数据")
  709. return
  710. end
  711. local shopConfig = ShopExcel[shopType]
  712. local goods = human.db.shop[shopType].goods
  713. if not shopConfig or not goods then
  714. print("[BuyItemBtAuto] 没有对应物品数据")
  715. return
  716. end
  717. cleanShopGoods(human, shopType)
  718. local len = 0
  719. local itemVector = {}
  720. for k, itemInfo in pairs(goods) do
  721. local itemCfg = shopConfig[itemInfo.index]
  722. local subBuyCnt = getItemSubBuyCnt(human, recordList, itemCfg, itemInfo)
  723. if subBuyCnt > 0 then
  724. -- 更新次数
  725. goods[k].nowBuy = goods[k].nowBuy + subBuyCnt
  726. -- 先删
  727. local itemID = itemCfg.itemID
  728. local itemCnt = subBuyCnt * itemCfg.cnt
  729. BagLogic.delItem(human, itemCfg.needItemID, itemCfg.price * subBuyCnt, "shop_buy", nil, itemID, itemCnt)
  730. -- 累积奖励
  731. len = len + 1
  732. itemVector[len] = {itemID, itemCnt}
  733. -- 触发事件
  734. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_7, 1)
  735. if ShopDefine.SHOP_MAIN_TYPE_1 == ShopDefine.SHOP_TYPE_BLACKMARKET then
  736. ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_4,1)
  737. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE10, 1)
  738. YunYingLogic.onCallBack(human, "onShopBuy",1)
  739. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_SHOPBUY, human.db._id, 1)
  740. end
  741. end
  742. end
  743. if len == 0 then
  744. Broadcast.sendCenter(human, Lang.ITEM_BUY_DEFAULT)
  745. else
  746. print("[BuyItemBtAuto] 开始给奖励")
  747. BagLogic.addItemList(human, itemVector, "shop_buy")
  748. end
  749. end
  750. -- 查询记录基础商店中随机商店的道具Idx列表
  751. function RecordItems_Query(human, shopType)
  752. local shopData = human.db.shop
  753. local recordList = (shopData and shopData[shopType]) and shopData[shopType].recordList
  754. local msgRet = Msg.gc.GC_SHOP_RECORD_QUERY
  755. local itemIdxList = msgRet.itemIdxList
  756. itemIdxList[0] = 0
  757. if recordList then
  758. for k, idx in ipairs(recordList) do
  759. itemIdxList[0] = k
  760. itemIdxList[k] = idx
  761. end
  762. end
  763. Msg.send(msgRet, human.fd)
  764. end
  765. -- 记录基础商店中随机商店的道具Idx列表
  766. function RecordItems(human, itemIdxStr, shopType)
  767. local isBuyCard = MonthCard.IsBuyEverlastingCard(human)
  768. if not isBuyCard then
  769. return Broadcast.sendErr(human, Lang.EVERLASTING_CARD_UNACTIVE)
  770. end
  771. local itemIdxList = {}
  772. if itemIdxStr == "" or tonumber(itemIdxStr) == -1 then
  773. itemIdxList = nil
  774. else
  775. itemIdxList = Util.split(itemIdxStr, ",", true)
  776. end
  777. human.db.shop[shopType].recordList = itemIdxList
  778. RecordItems_Query(human, shopType)
  779. Broadcast.sendCenter(human, Lang.EQUIP_WASH_SAVE_SUC)
  780. end
  781. -- 查询记录基础商店中随机商店的所有道具
  782. function AllItem_Query(human, shopType)
  783. local shopConfig = ShopExcel[shopType]
  784. if not shopConfig then return end
  785. local cnt = 0
  786. local msgRet = Msg.gc.GC_SHOP_ALL_ITEM_QUERY
  787. msgRet.isCanRecord = 0
  788. if MonthCard.IsBuyEverlastingCard(human) then
  789. msgRet.isCanRecord = 1
  790. end
  791. for idx, itemInfo in ipairs(shopConfig) do
  792. cnt = cnt + 1
  793. Grid.makeItem(msgRet.itemList[cnt].itemData, itemInfo.itemID, itemInfo.cnt)
  794. msgRet.itemList[cnt].itemIndex = idx
  795. Grid.makeItem(msgRet.itemList[cnt].needItem, itemInfo.needItemID, itemInfo.price)
  796. msgRet.itemList[cnt].maxCanBuy = itemInfo.limitBuyCnt
  797. msgRet.itemList[cnt].nowBuy = 0
  798. msgRet.itemList[cnt].zhekou = itemInfo.zhekou
  799. msgRet.itemList[cnt].order = itemInfo.order
  800. msgRet.itemList[cnt].needVipLv = itemInfo.needVipLv
  801. msgRet.itemList[cnt].rare = itemInfo.rare
  802. msgRet.itemList[cnt].limitType = itemInfo.limitType
  803. msgRet.itemList[cnt].bCanChose = itemInfo.isquickbuy and itemInfo.isquickbuy or 0
  804. msgRet.itemList[cnt].bChose = ShopLogic_GetChose(human, shopType, idx)
  805. end
  806. msgRet.itemList[0] = cnt
  807. Msg.send(msgRet, human.fd)
  808. end
  809. -- 选中物品
  810. function ChoseItem(human, shopType, bChose, itemIndex)
  811. local isBuyCard = MonthCard.IsBuyEverlastingCard(human)
  812. if not isBuyCard then
  813. return Broadcast.sendErr(human, Lang.EVERLASTING_CARD_UNACTIVE)
  814. end
  815. local tempConfig = ShopExcel[shopType][itemIndex]
  816. if tempConfig == nil then
  817. print("[ChoseItem] 不存在对应的配置")
  818. return
  819. end
  820. local nNowChose = ShopLogic_GetChose(human, shopType, itemIndex)
  821. if nNowChose == bChose then
  822. return
  823. end
  824. local bRet = ShopLogic_SetChose(human, shopType, itemIndex, bChose)
  825. if false == bRet then
  826. print("[ChoseItem] 设置失败")
  827. return
  828. end
  829. --query(human, shopType)
  830. end
  831. -- 自动购买勾选的物品
  832. function AutoBuyChoseItem(human, shopType, bCanBuy)
  833. local isBuyCard = MonthCard.IsBuyEverlastingCard(human)
  834. if not isBuyCard then
  835. return
  836. end
  837. if shopType == ShopDefine.SHOP_TYPE_XUYUAN then
  838. if nil == bCanBuy or false == bCanBuy then
  839. return
  840. end
  841. end
  842. local tItemList = {}
  843. local tDelItemList = {}
  844. for k,v in pairs(human.db.shop[shopType].goods) do
  845. if v.bChose and 1 == v.bChose then
  846. local nIndex = v.index
  847. local tempConfig = ShopExcel[shopType][nIndex]
  848. local nowBuy = v.nowBuy
  849. if tempConfig and tempConfig.limitBuyCnt - nowBuy > 0 then
  850. local nItemID = tempConfig.itemID
  851. -- 装备检测数量
  852. if ItemDefine.isEquip(itemID) then
  853. if not EquipLogic.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  854. break
  855. end
  856. end
  857. -- 符文检测数量
  858. if ItemDefine.isFuwen(itemID) then
  859. if not FuwenGrid.checkEmptyCnt(human, buyCnt * tempConfig.cnt) then
  860. break
  861. end
  862. end
  863. local nBuyCnt = tempConfig.limitBuyCnt - nowBuy
  864. local needItemID = tempConfig.needItemID
  865. local needItemCnt = tempConfig.price * nBuyCnt
  866. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  867. if nowItemCnt >= needItemCnt then
  868. v.nowBuy = v.nowBuy + nBuyCnt
  869. local refreshType = ShopExcel.shop[shopType].refreshType
  870. if refreshType == ShopDefine.SHOP_REFRESH_TYPE6
  871. or refreshType == ShopDefine.SHOP_REFRESH_TYPE7 then
  872. buyAllItemsRefresh(human,shopType)
  873. end
  874. -- 先删
  875. BagLogic.delItem(human, needItemID, needItemCnt, "shop_buy", nil, itemID, nBuyCnt * tempConfig.cnt)
  876. if not tItemList[tempConfig.itemID] then
  877. tItemList[tempConfig.itemID] = 0
  878. end
  879. tItemList[tempConfig.itemID] = tItemList[tempConfig.itemID] + nBuyCnt * tempConfig.cnt
  880. else
  881. break
  882. end
  883. end
  884. end
  885. end
  886. if nil ~= _G.next(tItemList) then
  887. local tGoods = {}
  888. for k, v in pairs(tItemList) do
  889. table.insert(tGoods, {k,v})
  890. end
  891. BagLogic.addItemList(human, tGoods, "shop_buy")
  892. end
  893. end
  894. function GetBuyLiftTimeCard(human)
  895. local tMsgData = Msg.gc.GC_BUY_LIFTTIME_CARD
  896. tMsgData.bBuy = 0
  897. if MonthCard.IsBuyEverlastingCard(human) then
  898. tMsgData.bBuy = 1
  899. end
  900. Msg.send(tMsgData, human.fd)
  901. end