ShopLogic.lua 36 KB

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