BagLogic.lua 26 KB

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