BagLogic.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. ------------------------------------------------------------
  2. -- 背包
  3. -- addItem 添加道具
  4. -- delItem 删除道具
  5. -- sendItemGetList 通用道具获得界面
  6. -- checkItemCnt 检查道具数量是否足够,否则提示
  7. -- getItemCnt 返回道具数量
  8. ------------------------------------------------------------
  9. local EquipExcel = require("excel.equip").equip
  10. local ItemExcel = require("excel.item").item
  11. local FuwenExcel = require("excel.fuwen").fuwen
  12. local ItemBuyExcel = require("excel.item").buy
  13. local ItemComonBuyExcel = require("excel.item").commonBuy
  14. local Log = require("common.Log")
  15. local LogDefine = require("common.LogDefine")
  16. local Lang = require("common.Lang")
  17. local Util = require("common.Util")
  18. local ObjHuman = require("core.ObjHuman")
  19. local Msg = require("core.Msg")
  20. local Broadcast = require("broadcast.Broadcast")
  21. local Grid = require("bag.Grid")
  22. local ItemLogic = require("bag.ItemLogic")
  23. local ItemDefine = require("bag.ItemDefine")
  24. local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
  25. local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
  26. local FuwenLogic = require("fuwen.FuwenLogic")
  27. local MiddleOption = require("middle.MiddleOption")
  28. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  29. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  30. local EquipLogic = require("equip.EquipLogic")
  31. -- local YunYingLogic = require("yunying.YunYingLogic")
  32. -- local PanelDefine = require("broadcast.PanelDefine")
  33. local HeroGrowUp = require("absAct.HeroGrowUp")
  34. local CommonDefine = require("common.CommonDefine")
  35. local CommonDB = require("common.CommonDB")
  36. local RoleStorageBox = require("roleSystem.RoleStorageBox")
  37. local TriggerDefine = require("trigger.TriggerDefine")
  38. local TriggerLogic = require("trigger.TriggerLogic")
  39. local ITEM_MOMENT_ADD_LIST = {}
  40. local jinbi_id=101
  41. local jinyan_id=111
  42. ADDITEM_TYPE_1 = 1 --顺序 不整合添加道具 非1 表示整合所有相同的道具数量
  43. ADDITEM_TYPE_2 = 2
  44. function updateMomentItem(type, itemID, itemCnt, quality)
  45. if type == ADDITEM_TYPE_1 then
  46. local len = #ITEM_MOMENT_ADD_LIST + 1
  47. ITEM_MOMENT_ADD_LIST[len] = {}
  48. ITEM_MOMENT_ADD_LIST[len][1] = itemID
  49. ITEM_MOMENT_ADD_LIST[len][2] = itemCnt
  50. ITEM_MOMENT_ADD_LIST[len][3] = quality
  51. else
  52. ITEM_MOMENT_ADD_LIST[itemID] = ITEM_MOMENT_ADD_LIST[itemID] or 0
  53. ITEM_MOMENT_ADD_LIST[itemID] = ITEM_MOMENT_ADD_LIST[itemID] + itemCnt
  54. end
  55. end
  56. -- 防止有报错 导致的 别的玩家 额外获得道具 在使用 通用的 添加之前 清除一次
  57. function cleanMomentItemList()
  58. Util.cleanTable(ITEM_MOMENT_ADD_LIST)
  59. end
  60. function addMomentItemList(human, logType, noSend)
  61. if not noSend then
  62. noSend = true
  63. end
  64. addItemList(human, ITEM_MOMENT_ADD_LIST, logType, noSend)
  65. sendListChange(human, ITEM_MOMENT_ADD_LIST)
  66. cleanMomentItemList()
  67. end
  68. -- 通用道具添加 {[1] = {id, cnt}, [2] = {id,cnt}} or {[itemID] = itemCnt} 配表复杂 各自接口下自己for 添加
  69. function addItemList(human, list, logType, noSend)
  70. if not list or next(list) == nil then return end
  71. list = Util.copyTable(list)
  72. if list[1] and list[1][1] then
  73. local extraItemList = {}
  74. for _, item in ipairs(list) do
  75. addItem(human, item[1], item[2], logType, noSend, item[3])
  76. local extraItemInfo = RoleStorageBox.GetExtraItem(human, item[1], item[2], logType)
  77. if extraItemInfo then
  78. table.insert(extraItemList, extraItemInfo)
  79. end
  80. end
  81. -- 额外道具的处理
  82. for _, extraItem in ipairs(extraItemList) do
  83. table.insert(list, extraItem)
  84. addItem(human, extraItem[1], extraItem[2], logType, noSend, extraItem[3])
  85. end
  86. else
  87. local newList
  88. for itemID, itemCnt in pairs(list) do
  89. -- addItem(human, itemID, itemCnt, logType, noSend)
  90. local extraItemInfo = RoleStorageBox.GetExtraItem(human, itemID, itemCnt, logType)
  91. if extraItemInfo then
  92. itemCnt = itemCnt + extraItemInfo[2]
  93. newList = newList or {}
  94. newList[itemID] = {
  95. [2] = itemCnt, -- 兼容之前代码, 防止有ID为1的道具
  96. [4] = extraItemInfo[4]
  97. }
  98. end
  99. addItem(human, itemID, itemCnt, logType, noSend)
  100. end
  101. if newList then
  102. for itemId, itemInfo in pairs(newList) do
  103. list[itemId] = itemInfo
  104. end
  105. end
  106. end
  107. sendItemGetList(human, list, logType)
  108. end
  109. -- 增加装备,道具
  110. function addItem(human, id, cnt, logType, noSend, otherData)
  111. if cnt < 1 then return end
  112. local itemConfig = ItemDefine.getConfig(id)
  113. if not itemConfig then return end
  114. if not LogDefine.DEFINE[logType] or not LogDefine.TYPE["item"] then
  115. assert()
  116. end
  117. cnt = calculateBonusItemCount(human,id,cnt,logType)
  118. MiddleOption.addItem(human, id, cnt, logType)
  119. if handlerSpObj(human, id, cnt, logType) then
  120. print("[addItem] 不进入背包物品直接返回 id, cnt = ", id, cnt)
  121. return
  122. end
  123. if handleFuwen(human, id, cnt, logType) then
  124. print("[addItem] 符文物品不进入背包物品直接返回 id, cnt = ", id, cnt)
  125. return
  126. end
  127. -- 装备走另外的逻辑
  128. if handleEquipAdd(human, id, cnt, logType, otherData) then
  129. print("[addItem] 装备物品其他方式处理 id, cnt = ", id, cnt)
  130. return
  131. end
  132. local oldCnt = human.db.bag[id] or 0
  133. -- local newCnt = math.min(oldCnt + cnt, ItemDefine.BAG_ITEM_MAX_CNT)
  134. local maxNum = ItemDefine.BAG_ITEM_MAX_CNT
  135. if id == ItemDefine.ITEM_GREEN_EXP_ID then
  136. maxNum = ItemDefine.BAG_ITEM_MAX_JINBI
  137. end
  138. local newCnt = math.min(oldCnt + cnt, maxNum)
  139. human.db.bag[id] = newCnt
  140. if not noSend then
  141. sendChange(human, id, oldCnt < 1)
  142. end
  143. Log.write(Log.LOGID_OSS_ITEM, human.db._id, human.db.account, human.db.name, human.db.lv,
  144. LogDefine.DEFINE[logType] + LogDefine.TYPE["item"] , id, cnt, newCnt)
  145. -- 如果道具为公会贡献,记录到当日累计贡献中
  146. if id == ItemDefine.ITEM_UNION_COIN_ID then
  147. human.db.dailyBanggong = human.db.dailyBanggong or 0
  148. human.db.dailyBanggong = human.db.dailyBanggong + cnt
  149. human.db.totalBanggong = human.db.totalBanggong or 0
  150. human.db.totalBanggong = human.db.totalBanggong + cnt
  151. end
  152. -- 根据道具触发红点
  153. checkDotByID(human,id)
  154. return true
  155. end
  156. function checkDotByID(human,id)
  157. local itemConfig = ItemDefine.getConfig(id)
  158. local dotTb = itemConfig.dot
  159. local len = #dotTb
  160. for i = 1,len do
  161. RoleSystemLogic.onDot(human, dotTb[i])
  162. end
  163. end
  164. -- 根据id删除
  165. function delItem(human, id, cnt, logType, noSend, byId, byCnt)
  166. if cnt < 1 then return end
  167. local itemConfig = ItemExcel[id]
  168. if not itemConfig then return end
  169. if not LogDefine.DEFINE[logType] or not LogDefine.TYPE["item"] then
  170. assert()
  171. end
  172. if id == ItemDefine.ITEM_JINBI_ID then
  173. return ObjHuman.updateJinbi(human, -cnt, logType, byId, byCnt)
  174. elseif id == ItemDefine.ITEM_ZUANSHI_ID then
  175. return ObjHuman.decZuanshi(human, -cnt, logType, byId, byCnt)
  176. elseif id == ItemDefine.ITEM_FRIEND_ID then
  177. return ObjHuman.updateFriendHeart(human, -cnt, logType)
  178. end
  179. local newCnt = (human.db.bag[id] or 0) - cnt
  180. if newCnt < 0 then assert(nil) end
  181. if newCnt < 1 then
  182. newCnt = nil
  183. end
  184. human.db.bag[id] = newCnt
  185. if not noSend then
  186. sendChange(human, id)
  187. end
  188. Log.write(Log.LOGID_OSS_ITEM, human.db._id, human.db.account, human.db.name, human.db.lv,
  189. LogDefine.DEFINE[logType] + LogDefine.TYPE["item"] , id, -cnt, newCnt or 0)
  190. if itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN or
  191. itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN_SKIN then
  192. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_206)
  193. end
  194. if id == ItemDefine.ITEM_GREEN_EXP_ID then
  195. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE18, cnt)
  196. TriggerLogic.PublishEvent(TriggerDefine.JINGYAN_DEL, human.db._id, cnt)
  197. end
  198. return true
  199. end
  200. -- 获得道具数量
  201. function getItemCnt(human, id, isUse)
  202. if id == ItemDefine.ITEM_JINBI_ID and not isUse then
  203. return human.db.jinbi or 0
  204. elseif id == ItemDefine.ITEM_ZUANSHI_ID and not isUse then
  205. return human.db.zuanshi or 0
  206. elseif id == ItemDefine.ITEM_FRIEND_ID and not isUse then
  207. return human.db.friendHeart or 0
  208. end
  209. return human.db.bag[id] or 0
  210. end
  211. -- 判断道具数量
  212. function checkItemCnt(human, id, cnt)
  213. if id == ItemDefine.ITEM_ZUANSHI_ID then
  214. return ObjHuman.checkRMB(human,cnt)
  215. end
  216. if getItemCnt(human, id) < cnt then
  217. local name = ItemDefine.getValue(id,"name")
  218. return Broadcast.sendErr(human,Util.format(Lang.COMMON_NO_ITEM, name))
  219. end
  220. return true
  221. end
  222. -- 特殊不进背包的道具
  223. function handlerSpObj(human, itemID, itemCnt, logType)
  224. local itemConfig = ItemExcel[itemID]
  225. if not itemConfig then return end
  226. if itemConfig.subType ~= ItemDefine.ITEM_SUBTYPE_SPOBJ then
  227. return
  228. end
  229. local cmdstr = itemConfig.cmd and itemConfig.cmd[1]
  230. if cmdstr and ItemLogic.cmd[cmdstr] then
  231. ItemLogic.onlyuse(human, itemID, itemCnt, logType, true)
  232. -- 根据道具触发红点
  233. checkDotByID(human,itemID)
  234. end
  235. if ItemDefine.checkIsWave(LogDefine.DEFINE[logType]) then
  236. sendRoll(human, itemID, itemCnt)
  237. end
  238. return true
  239. end
  240. -- 符文
  241. function handleFuwen(human, itemID, itemCnt, logType)
  242. if not FuwenExcel[itemID] then return end
  243. FuwenLogic.add(human, itemID, itemCnt, logType)
  244. if ItemDefine.checkIsWave(LogDefine.DEFINE[logType]) then
  245. sendRoll(human, itemID, itemCnt)
  246. end
  247. return true
  248. end
  249. -- 装备增加
  250. function handleEquipAdd(human, itemID, itemCnt, logType, otherData)
  251. if not EquipExcel[itemID] then return end
  252. EquipLogic.addEquip(human, itemID, itemCnt, logType, otherData)
  253. if EquipExcel[itemID].mainType == ItemDefine.MAINTYPE_EQUIP then
  254. if EquipExcel[itemID].level == 14 then
  255. ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_24,itemCnt)
  256. end
  257. end
  258. if ItemDefine.checkIsWave(LogDefine.DEFINE[logType]) then
  259. sendRoll(human, itemID, itemCnt)
  260. end
  261. return true
  262. end
  263. ---------------------------- msg --------------------------------
  264. -- 发送背包道具
  265. function sendBagList(human)
  266. local msgRet = Msg.gc.GC_BAG_LIST
  267. msgRet.list[0] = 0
  268. for itemID, itemCnt in pairs(human.db.bag) do
  269. msgRet.list[0] = msgRet.list[0] + 1
  270. local net = msgRet.list[msgRet.list[0]]
  271. Grid.makeItem(net, itemID, itemCnt, nil,nil,nil, Grid.getOpflagAtBag(itemID))
  272. if msgRet.list[0] >= ItemDefine.PAGE_LIST_COUNT then
  273. Msg.send(msgRet, human.fd)
  274. msgRet.list[0] = 0
  275. end
  276. end
  277. if msgRet.list[0] > 0 then
  278. Msg.send(msgRet, human.fd)
  279. end
  280. end
  281. -- 改变
  282. function sendChange(human, itemID, isAdd)
  283. --print("12131231进入下发逻辑")
  284. local msgRet = Msg.gc.GC_ITEM_BAG_CHANGE
  285. local itemCnt = human.db.bag[itemID] or 0
  286. msgRet.itemID = itemID
  287. msgRet.itemCnt = itemCnt
  288. msgRet.itemData[0] = 0
  289. if isAdd == true then
  290. --print("下发为true")
  291. msgRet.itemData[0] = 1
  292. Grid.makeItem(msgRet.itemData[1], itemID, itemCnt, nil,nil,nil, Grid.getOpflagAtBag(itemID))
  293. end
  294. Msg.send(msgRet, human.fd)
  295. end
  296. function sendListChange(human, tItemList)
  297. local tAllList , tFlag = {}, {}
  298. if tItemList[1] and tItemList[1][1] then
  299. for _, v in ipairs(tItemList) do
  300. if not tFlag[v[1]] then
  301. tFlag[v[1]] = true
  302. table.insert(tAllList, v[1])
  303. end
  304. end
  305. else
  306. for itemID, itemNum in pairs(tItemList) do
  307. if not tFlag[itemID] then
  308. tFlag[itemID] = true
  309. table.insert(tAllList, itemID)
  310. end
  311. end
  312. end
  313. if nil == _G.next(tAllList) then
  314. return
  315. end
  316. local msgRet = Msg.gc.GC_ITEM_BAG_LIST_CHANGE
  317. msgRet.tList[0] = 0
  318. msgRet.bStart = 1
  319. msgRet.bEnd = 0
  320. local nLen, nAllLen, nSendLen = 0, 0, 0
  321. nAllLen = #tAllList
  322. for _, v in ipairs(tAllList) do
  323. nSendLen = nSendLen + 1
  324. nLen = nLen + 1
  325. local tData = msgRet.tList[nLen]
  326. local nNowNum = getItemCnt(human, v)
  327. Grid.makeItem(tData, v, nNowNum, nil,nil,nil, Grid.getOpflagAtBag(v))
  328. if nLen >= 30 then
  329. msgRet.tList[0] = nLen
  330. Msg.send(msgRet, human.fd)
  331. nLen = 0
  332. msgRet.bStart = 0
  333. msgRet.bEnd = nSendLen == nAllLen and 1 or 0
  334. end
  335. end
  336. if nLen > 0 then
  337. msgRet.tList[0] = nLen
  338. msgRet.bEnd = 1
  339. Msg.send(msgRet, human.fd)
  340. end
  341. end
  342. -- 滚动
  343. function sendRoll(human, itemID, itemCnt)
  344. --[[local msgRet = Msg.gc.GC_ITEM_BAG_ROLL
  345. msgRet.itemData[0] = 1
  346. Grid.makeItem(msgRet.itemData[1], itemID, itemCnt)
  347. Msg.send(msgRet, human.fd)]]
  348. end
  349. -- 通用道具获得面板 list = {...}
  350. function sendItemGetList(human, list, logStr)
  351. if not list or not next(list) then return end
  352. if list[1] and list[1][1] then
  353. sendItemGetList1(human, list, logStr)
  354. else
  355. sendItemGetList2(human, list, logStr)
  356. end
  357. end
  358. -- 通用道具获得面板 list = {[1]={id,cnt},[2]={id2,cnt2}...}
  359. function sendItemGetList1(human, list, logStr)
  360. if not list or not next(list) then return end
  361. local msgRet = Msg.gc.GC_ITEM_GET_LIST
  362. msgRet.popupType = ItemDefine.checkIsWave(LogDefine.DEFINE[logStr]) and 1 or 0
  363. local len = math.min(#list, #msgRet.list)
  364. local cnt = 0
  365. cnt = EquipLogic.makeEquipItem(human, msgRet.list, cnt)
  366. local sourceId = 0
  367. for i=1,len do
  368. if cnt >= #msgRet.list then
  369. break
  370. end
  371. local itemID = list[i][1]
  372. local itemCnt = list[i][2]
  373. sourceId = list[i][4]
  374. itemCnt=calculateBonusItemCount(human,itemID,itemCnt,logStr)
  375. if not ItemDefine.isEquip(itemID) then
  376. cnt = cnt + 1
  377. --Grid.makeItem(msgRet.list[cnt], itemID, itemCnt)
  378. Grid.makeItem(msgRet.list[cnt], itemID, itemCnt, nil,nil,nil, Grid.getOpflagAtBag(itemID), nil, nil, sourceId)
  379. end
  380. end
  381. if cnt > 0 then
  382. msgRet.list[0] = cnt
  383. Msg.send(msgRet,human.fd)
  384. end
  385. end
  386. -- 通用道具获得面板2 list = {[id]=cnt,[id2]=cnt2}
  387. function sendItemGetList2(human, list, logStr)
  388. if not list or not next(list) then return end
  389. local msgRet = Msg.gc.GC_ITEM_GET_LIST
  390. msgRet.popupType = ItemDefine.checkIsWave(LogDefine.DEFINE[logStr]) and 1 or 0
  391. msgRet.list[0] = 0
  392. local cnt = 0
  393. cnt = EquipLogic.makeEquipItem(human, msgRet.list, cnt)
  394. local sourceId = 0
  395. for itemID, itemCnt in pairs(list) do
  396. if cnt >= #msgRet.list then
  397. break
  398. end
  399. -- itemCnt=calculateBonusItemCount(human,itemID,itemCnt,logStr)
  400. -- if not ItemDefine.isEquip(itemID) then
  401. -- cnt = cnt + 1
  402. -- --Grid.makeItem(msgRet.list[cnt], itemID, itemCnt)
  403. -- Grid.makeItem(msgRet.list[cnt], itemID, itemCnt, nil,nil,nil, Grid.getOpflagAtBag(itemID))
  404. -- end
  405. sourceId = 0
  406. local itemNum = 0
  407. if type(itemCnt) == "table" then
  408. itemNum = itemCnt[2]
  409. sourceId = itemCnt[4]
  410. else
  411. itemNum = itemCnt
  412. end
  413. itemNum=calculateBonusItemCount(human,itemID,itemNum,logStr)
  414. if not ItemDefine.isEquip(itemID) then
  415. cnt = cnt + 1
  416. --Grid.makeItem(msgRet.list[cnt], itemID, itemCnt)
  417. Grid.makeItem(msgRet.list[cnt], itemID, itemNum, nil,nil,nil, Grid.getOpflagAtBag(itemID), nil, nil, sourceId)
  418. end
  419. end
  420. --Msg.trace(msgRet)
  421. if cnt > 0 then
  422. msgRet.list[0] = cnt
  423. Msg.send(msgRet,human.fd)
  424. end
  425. end
  426. -- 通用道具获得面板2 list = {[id]=cnt,[id2]=cnt2}
  427. function sendItemGetList3(human, list, logStr)
  428. if not list or not next(list) then return end
  429. local msgRet = Msg.gc.GC_ITEM_GET_LIST
  430. msgRet.popupType = ItemDefine.checkIsWave(LogDefine.DEFINE[logStr]) and 1 or 0
  431. msgRet.list[0] = 0
  432. local cnt = 0
  433. cnt = EquipLogic.makeEquipItem(human, msgRet.list, cnt)
  434. for i = 1, #list do
  435. local items = list[i]
  436. for itemID, itemCnt in pairs(items) do
  437. if cnt >= #msgRet.list then
  438. break
  439. end
  440. if not ItemDefine.isEquip(itemID) then
  441. cnt = cnt + 1
  442. Grid.makeItem(msgRet.list[cnt], itemID, itemCnt)
  443. end
  444. end
  445. end
  446. if cnt > 0 then
  447. msgRet.list[0] = cnt
  448. Msg.send(msgRet,human.fd)
  449. end
  450. end
  451. -- 相同的道具自动叠加一起
  452. local SAME_ITEMS1 = {}
  453. local SAME_ITEMS2 = {}
  454. function sameItemTogether(list)
  455. if not list or not next(list) then return end
  456. for k in pairs(SAME_ITEMS1) do
  457. SAME_ITEMS1[k] = nil
  458. end
  459. for k in pairs(SAME_ITEMS2) do
  460. SAME_ITEMS2[k] = nil
  461. end
  462. for _, item in ipairs(list) do
  463. local itemID = item[1]
  464. local itemCnt = item[2]
  465. -- 装备符文不能叠加
  466. if not ItemDefine.isEquip(itemID) and not ItemDefine.isFuwen(itemID) then
  467. SAME_ITEMS1[itemID] = (SAME_ITEMS1[itemID] or 0) + itemCnt
  468. end
  469. end
  470. for itemID, itemCnt in pairs(SAME_ITEMS1) do
  471. SAME_ITEMS2[#SAME_ITEMS2 + 1] = {itemID, itemCnt}
  472. end
  473. return SAME_ITEMS2, SAME_ITEMS1
  474. end
  475. -- 背包道具出售
  476. function itemSell(human, itemID, itemCnt)
  477. if itemCnt < 1 then return end
  478. local itemConfig = ItemDefine.getConfig(itemID)
  479. if not itemConfig then return end
  480. if not itemConfig.price then return end
  481. local saleItemID = itemConfig.price[1]
  482. local saleItemCnt = itemConfig.price[2]
  483. if not saleItemCnt or saleItemCnt < 1 then return end -- 不能出售
  484. local bagCnt = getItemCnt(human, itemID)
  485. if bagCnt < itemCnt then return end
  486. local itemCntAdd = math.floor(saleItemCnt * itemCnt)
  487. delItem(human, itemID, itemCnt, "item_sale")
  488. addItem(human, saleItemID, itemCntAdd, "item_sale")
  489. local list = {}
  490. list[1] = { [1] = saleItemID, [2]= itemCntAdd }
  491. sendItemGetList(human, list)
  492. local msgRet = Msg.gc.GC_BAG_ITEM_SELL
  493. msgRet.id = itemID
  494. msgRet.cnt = itemCnt
  495. Msg.send(msgRet, human.fd)
  496. end
  497. -- 装备道具出售
  498. function equipSell(human, bagIndex)
  499. local equipGrid = human.db.equipBag[bagIndex]
  500. if not equipGrid then return end
  501. local itemConfig = ItemDefine.getConfig(equipGrid.id)
  502. if not itemConfig then return end
  503. if not itemConfig.price then return end
  504. local saleItemID = itemConfig.price[1]
  505. local saleItemCnt = itemConfig.price[2]
  506. if not saleItemCnt or saleItemCnt < 1 then return end
  507. EquipLogic.delEquip(human, bagIndex, "equip_sale")
  508. local itemCntAdd = saleItemCnt
  509. addItem(human, saleItemID, itemCntAdd, "equip_sale")
  510. sendItemGetList(human, {[1]=itemConfig.price})
  511. end
  512. -- 装备道具出售
  513. function equipSellByQuality(human, equipStr)
  514. local strTbl = Util.split(equipStr, "|")
  515. local equipTypeTbl = Util.split(strTbl[#strTbl], ",", true)
  516. local equipTypeList = {}
  517. for _, equipType in ipairs(equipTypeTbl) do
  518. if equipType == 1 then
  519. equipTypeList[ItemDefine.EQUIP_SUBTYPE_WEAPON] = 1
  520. equipTypeList[ItemDefine.EQUIP_SUBTYPE_CLOTH] = 1
  521. equipTypeList[ItemDefine.EQUIP_SUBTYPE_SHIPIN] = 1
  522. equipTypeList[ItemDefine.EQUIP_SUBTYPE_SHOES] = 1
  523. elseif equipType == 2 then
  524. equipTypeList[ItemDefine.EQUIP_SUBTYPE_RING] = 1
  525. equipTypeList[ItemDefine.EQUIP_SUBTYPE_AMULET] = 1
  526. else
  527. assert(false, string.format("装备类型错误, equipType = %s", equipType))
  528. end
  529. end
  530. local qualityStr = strTbl[1]
  531. local qualityList = Util.split(qualityStr, ",", true)
  532. local len = #qualityList
  533. if len <= 0 or len > 4 then return end
  534. local outItems = {}
  535. for i = 1, #qualityList do
  536. local quality = qualityList[i]
  537. for k, equipGrid in pairs(human.db.equipBag) do
  538. if equipGrid.quality == quality then
  539. local itemConfig = ItemDefine.getConfig(equipGrid.id)
  540. if not itemConfig then assert() end
  541. if not itemConfig.price then assert() end
  542. if equipTypeList[itemConfig.subType] then
  543. local saleItemID = itemConfig.price[1]
  544. local saleItemCnt = itemConfig.price[2]
  545. if not saleItemCnt or saleItemCnt < 1 then assert() end
  546. EquipLogic.delEquip(human, k, "equip_sale", true)
  547. local itemCntAdd = saleItemCnt
  548. addItem(human, saleItemID, itemCntAdd, "equip_sale")
  549. outItems[saleItemID] = outItems[saleItemID] or 0
  550. outItems[saleItemID] = outItems[saleItemID] + itemCntAdd
  551. end
  552. end
  553. end
  554. end
  555. sendItemGetList(human, outItems)
  556. -- 出售成功
  557. local msgRet = Msg.gc.GC_BAG_EQUIP_SELL_QUALITY
  558. Msg.send(msgRet,human.fd)
  559. end
  560. -- 道具购买查询
  561. function queryItemBuy(human, id)
  562. local config = ItemBuyExcel[id]
  563. if not config then return end
  564. local msgRet = Msg.gc.GC_ITEM_BUY_QUERY
  565. msgRet.id = id
  566. msgRet.item[0] = 1
  567. Grid.makeItem(msgRet.item[1], id , 1)
  568. msgRet.canBuy[0] = 2
  569. msgRet.canBuy[1] = config.cnt1
  570. msgRet.canBuy[2] = config.cnt2
  571. msgRet.need[0] = 0
  572. for _, item in ipairs(config.need1) do
  573. msgRet.need[0] = msgRet.need[0] + 1
  574. Grid.makeItem(msgRet.need[msgRet.need[0]], item[1], item[2])
  575. end
  576. msgRet.needTwo[0] = 0
  577. for _, item in ipairs(config.need2) do
  578. msgRet.needTwo[0] = msgRet.needTwo[0] + 1
  579. Grid.makeItem(msgRet.needTwo[msgRet.needTwo[0]], item[1], item[2])
  580. end
  581. --Msg.trace(msgRet)
  582. Msg.send(msgRet,human.fd)
  583. end
  584. -- 购买道具
  585. function buyItem(human, id, buyCnt, cnt)
  586. local config = ItemBuyExcel[id]
  587. if not config then return end
  588. local needList = nil
  589. if config.cnt1 == buyCnt then
  590. needList = config.need1
  591. elseif config.cnt2 == buyCnt then
  592. needList = config.need2
  593. end
  594. if not needList then return end
  595. if cnt < 1 then return end
  596. -- 判断消耗
  597. for _, item in ipairs(needList) do
  598. if not checkItemCnt(human, item[1], item[2] * cnt) then
  599. return
  600. end
  601. end
  602. -- 某些购买日志类型特殊
  603. local logType = "item_buy"
  604. if id == ItemDefine.ITEM_ABS_DRAW_LONGZHU_ID then
  605. logType = "item_buy_dragong"
  606. end
  607. -- 扣除道具
  608. for _, item in ipairs(needList) do
  609. delItem(human, item[1], item[2] * cnt, logType)
  610. end
  611. addItem(human, id, buyCnt * cnt, logType)
  612. Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
  613. local msgRet = Msg.gc.GC_ITEM_BUY
  614. msgRet.id = id
  615. Msg.send(msgRet, human.fd)
  616. end
  617. function sendCommonBuyQuery(human, itemID)
  618. local config = ItemComonBuyExcel[itemID]
  619. if not config then return end
  620. local msgRet = Msg.gc.GC_ITEM_COMMON_BUY_QUERY
  621. Grid.makeItem(msgRet.item, itemID, 1)
  622. msgRet.price = config.price
  623. Msg.send(msgRet, human.fd)
  624. end
  625. function commonBuy(human, itemID, itemCnt)
  626. if itemCnt < 1 then return end
  627. local config = ItemComonBuyExcel[itemID]
  628. if not config then return end
  629. local cost = config.price * itemCnt
  630. if not ObjHuman.checkRMB(human, cost) then
  631. return
  632. end
  633. ObjHuman.decZuanshi(human, -cost, "item_buy", itemID, itemCnt)
  634. addItem(human, itemID, itemCnt, "item_buy")
  635. local msgRet = Msg.gc.GC_ITEM_COMMON_BUY
  636. msgRet.itemID = itemID
  637. Msg.send(msgRet, human.fd)
  638. end
  639. function isDot(human)
  640. for itemID, itemCnt in pairs(human.db.bag) do
  641. local itemConfig = ItemExcel[itemID]
  642. if itemConfig then
  643. if itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN or
  644. itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN_SKIN then
  645. if itemCnt >= itemConfig.fullCnt then
  646. return true
  647. end
  648. end
  649. end
  650. end
  651. return false
  652. end
  653. -- 处理发送奖励类型为table
  654. function BagLogic_HandlePrizetable(tMsgItemList, list, nLen)
  655. if not list[1] or not list[1][1] then
  656. return false
  657. end
  658. local bRet = true
  659. for i = 1, #list do
  660. local items = list[i]
  661. for itemID, itemCnt in pairs(items) do
  662. if nLen >= #tMsgItemList then
  663. bRet = false
  664. break
  665. end
  666. if not ItemDefine.isEquip(itemID) then
  667. nLen = nLen + 1
  668. Grid.makeItem(tMsgItemList[nLen], itemID, itemCnt)
  669. end
  670. end
  671. end
  672. tMsgItemList[0] = nLen
  673. return bRet
  674. end
  675. -- 处理发送奖励为 Key-Value
  676. function BagLogic_HandlePrizeKeyVal(tMsgItemList, list, nLen)
  677. if list[1] and list[1][1] then
  678. return false
  679. end
  680. local bRet = true
  681. for itemID, itemCnt in pairs(list) do
  682. if cnt >= #tMsgItemList then
  683. bRet = false
  684. break
  685. end
  686. if not ItemDefine.isEquip(itemID) then
  687. nLen = nLen + 1
  688. Grid.makeItem(tMsgItemList[nLen], itemID, itemCnt, nil,nil,nil, Grid.getOpflagAtBag(itemID))
  689. end
  690. end
  691. tMsgItemList[0] = nLen
  692. return bRet
  693. end
  694. -- 外部获取需要下发的所有道具信息
  695. -- 主要是处理不弹窗装备的问题
  696. -- 返回是否发送成功
  697. function BagLogic_GetAllSendInfo(human, tMsgItemList, list, nType)
  698. local nLen = 0
  699. if nType ~= CommonDefine.COMMON_SEND_PRIZE_TYPE_ONLYEQUIP then
  700. nLen = EquipLogic.makeEquipItem(human, tMsgItemList, nLen)
  701. tMsgItemList[0] = nLen
  702. return true
  703. end
  704. local nAllLen = #tMsgItemList
  705. tMsgItemList[0] = 0
  706. nLen = EquipLogic.makeEquipItem(human, tMsgItemList, nLen)
  707. if nLen >= nAllLen then
  708. return false
  709. end
  710. tMsgItemList[0] = nLen
  711. if nil == list or nil == _G.next(list) then
  712. return true
  713. end
  714. -- 不同发送奖励类型的处理函数
  715. if CommonDefine.COMMON_SEND_PRIZE_TYPE_TABLE == nType then
  716. return BagLogic_HandlePrizetable(tMsgItemList, list, nLen)
  717. elseif CommonDefine.COMMON_SEND_PRIZE_TYPE_KEYVAL == nType then
  718. return BagLogic_HandlePrizeKeyVal(tMsgItemList, list, nLen)
  719. else
  720. return false
  721. end
  722. end
  723. -- 计算加成后的物品数量
  724. function calculateBonusItemCount(human, id, cnt,logType)
  725. --回退和重置不加成
  726. if logType then
  727. if logType == "hero_huitui" or logType == "hero_reset" then
  728. return cnt
  729. end
  730. end
  731. if id == ItemDefine.ITEM_JINBI_ID or id == ItemDefine.ITEM_GREEN_EXP_ID then
  732. -- 检查是否处于加成时间
  733. local isInBonusTime = CommonDB.GetInFireWork(human)
  734. if isInBonusTime == true then
  735. -- 应用10%加成,向下取整
  736. local nNewCnt = math.floor(cnt * 1.1)
  737. return nNewCnt
  738. end
  739. end
  740. -- 默认返回原数量
  741. return cnt
  742. end