BagLogic.lua 25 KB

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