ShopLogic.lua 37 KB

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