BagLogic.lua 26 KB

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