BagLogic.lua 26 KB

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