ShopLogic.lua 30 KB

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