BagLogic.lua 26 KB

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