TradeProxy.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local TradeProxy = class("TradeProxy", RemoteProxy)
  3. TradeProxy.NAME = global.ProxyTable.TradeProxy
  4. local sformat = string.format
  5. local bubbleId = 4
  6. local cjson = require("cjson")
  7. function TradeProxy:ctor()
  8. TradeProxy.super.ctor(self)
  9. self._data = {}
  10. self._data.isTrading = false
  11. self.mySelfLockState = false
  12. self.mySureLockState = false --自己的确认状态
  13. self.otherSureLockState = false --对方的确认状态
  14. self._items_data_myself = {} -- 所有的交易中装备属性 包括对方的
  15. self._items_myself_num = 0
  16. self._items_data_other = {}
  17. self._tradeList = {}
  18. self._gold_myself = 0
  19. self._gold_other = 0
  20. self._move_trade_confirmed = true --移动取消交易确认
  21. end
  22. function TradeProxy:Tips(str)
  23. if true then
  24. return
  25. end
  26. -- 发送到滚屏
  27. global.Facade:sendNotification(global.NoticeTable.SystemTips, str)
  28. local proxyChat = global.Facade:retrieveProxy(global.ProxyTable.Chat)
  29. local CHANNEL = proxyChat.CHANNEL
  30. local data = {}
  31. data.Msg = str
  32. data.ChannelId = CHANNEL.System
  33. global.Facade:sendNotification(global.NoticeTable.AddChatItem, data)
  34. end
  35. function TradeProxy:onRegister()
  36. TradeProxy.super.onRegister(self)
  37. end
  38. function TradeProxy:Cleanup()
  39. self._data.trader = nil -- 名字、等级、好友、公会
  40. self.mySelfLockState = false
  41. self.mySureLockState = false
  42. self.otherSureLockState = false
  43. self._tradeMoney = nil
  44. end
  45. function TradeProxy:GetTrader()
  46. return self._data.trader
  47. end
  48. function TradeProxy:IsTrading()
  49. return self._data.isTrading
  50. end
  51. function TradeProxy:GetSelfItemDataByMadeIndex(itemId)
  52. if not itemId then
  53. return
  54. end
  55. return self._items_data_myself[itemId]
  56. end
  57. function TradeProxy:SetSelfItemDataByItemId(itemId, item)
  58. if not itemId then
  59. return
  60. end
  61. if not item then
  62. self._items_myself_num = math.max(self._items_myself_num - 1, 0)
  63. else
  64. if not self._items_data_myself[itemId] then
  65. self._items_myself_num = self._items_myself_num + 1
  66. end
  67. end
  68. self._items_data_myself[itemId] = item
  69. end
  70. function TradeProxy:GetOtherItemDataByMadeIndex(itemId)
  71. if not itemId then
  72. return
  73. end
  74. return self._items_data_other[itemId]
  75. end
  76. function TradeProxy:SetOtherItemDataByItemId(itemId, item)
  77. if not itemId then
  78. return
  79. end
  80. self._items_data_other[itemId] = item
  81. end
  82. function TradeProxy:SetMyselfGoldNum(num)
  83. self._gold_myself = num or 0
  84. end
  85. function TradeProxy:SetOtherGoldNum(num)
  86. self._gold_other = num or 0
  87. end
  88. function TradeProxy:ClearItemData()
  89. self._items_data_myself = {}
  90. self._items_myself_num = 0
  91. self._items_data_other = {}
  92. self._gold_myself = 0
  93. self._gold_other = 0
  94. self.mySureLockState = false
  95. self.otherSureLockState = false
  96. end
  97. function TradeProxy:GetOtherItemData()
  98. return clone(self._items_data_other)
  99. end
  100. function TradeProxy:GetSelfItemData()
  101. return clone(self._items_data_myself)
  102. end
  103. function TradeProxy:GetSelfItemNum()
  104. return self._items_myself_num
  105. end
  106. function TradeProxy:GetSelfItemDataCount()
  107. local data = self:GetSelfItemData()
  108. local count = 0
  109. if not data or next(data) == nil then
  110. return count
  111. end
  112. for k, v in pairs(data) do
  113. if v and next(v) then
  114. count = count + 1
  115. end
  116. end
  117. return count
  118. end
  119. function TradeProxy:GetSelfGoldNum()
  120. return clone(self._gold_myself)
  121. end
  122. function TradeProxy:GetOtherGoldNum()
  123. return clone(self._gold_other)
  124. end
  125. function TradeProxy:GetTradeMoneyID()
  126. return self._tradeMoney
  127. end
  128. function TradeProxy:BagItemQuickAdd(itemData)
  129. if not itemData then
  130. return
  131. end
  132. local state = self:GetLockState()
  133. if state then
  134. return
  135. end
  136. local myTradeDataCount = self:GetSelfItemDataCount()
  137. if myTradeDataCount >= global.MMO.TRADE_MAX_ITEM_NUMBER then
  138. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(90180001))
  139. return
  140. end
  141. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  142. local MakeIndex = itemData.MakeIndex
  143. local Name = itemData.Name
  144. local isBind, isSelf, isMeetType = CheckItemisBind(itemData, ItemConfigProxy:GetBindArticleType().TYPE_NOTRADE)
  145. if isMeetType then
  146. local data = {}
  147. data.str = string.format(GET_STRING(90020014), Name)
  148. data.btnType = 1
  149. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  150. else
  151. self:RequestPutInItem(MakeIndex, Name)
  152. end
  153. end
  154. function TradeProxy:TradeItemQuickPutOut(itemData)
  155. if not itemData then
  156. return
  157. end
  158. local state = self:GetLockState()
  159. if state then
  160. return
  161. end
  162. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  163. if BagProxy:isToBeFull(true) then
  164. return
  165. end
  166. local MakeIndex = itemData.MakeIndex
  167. local Name = itemData.Name
  168. self:RequestPutOutItem(MakeIndex, Name)
  169. end
  170. function TradeProxy:GetLockState(noTips)
  171. local state = self:GetMySureLockState()
  172. if not noTips and state then
  173. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(90180010))
  174. end
  175. return state
  176. end
  177. function TradeProxy:GetMySureLockState()
  178. return self.mySureLockState
  179. end
  180. function TradeProxy:GetOtherSureLockState()
  181. return self.otherSureLockState
  182. end
  183. function TradeProxy:SetMySureLockState(state)
  184. self.mySureLockState = state
  185. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyStatusChange)
  186. end
  187. function TradeProxy:SetOtherSureLockState(state)
  188. self.otherSureLockState = state
  189. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_StatusChange)
  190. end
  191. function TradeProxy:CheckTradeRequest()
  192. local myLockState = self:GetMySureLockState()
  193. local otherLockState = self:GetOtherSureLockState()
  194. if not otherLockState or not myLockState then
  195. return false
  196. end
  197. return true
  198. end
  199. function TradeProxy:RespTradeResult(msg)
  200. --交易失败即弹窗
  201. --[[
  202. -1 自己正在交易
  203. -2 玩家不在线
  204. -3 对方正在交易
  205. -4 距离
  206. -1 //对方离线不能交易
  207. -2 //对方不满足交易条件
  208. -3 //自己不满足交易条件
  209. -4 //玩家在黑名单不能交易
  210. -5 //自己正在交易­
  211. -6 //对方正在交易
  212. -7 //不能和自己交易
  213. -8 //距离太远不能交易
  214. -9 //自己在战斗状态不允许交易
  215. -10 //对方在战斗状态不允许交易
  216. -11 //自己死亡不能交易
  217. -12 //对方死亡不能交易
  218. -13 //对方没有设置允许交易
  219. ]]
  220. local header = msg:GetHeader()
  221. if header and next(header) then
  222. local errorType = header.recog
  223. local errorStr = GET_STRING(90020003)
  224. if errorType == -1 then
  225. errorStr = GET_STRING(90180004)
  226. elseif errorType == -2 then
  227. errorStr = GET_STRING(90180017)
  228. elseif errorType == -3 then
  229. errorStr = GET_STRING(90180011)
  230. elseif errorType == -4 then
  231. errorStr = GET_STRING(90180016)
  232. elseif errorType == -5 then
  233. errorStr = GET_STRING(90180003)
  234. elseif errorType == -6 then
  235. errorStr = GET_STRING(90180005)
  236. elseif errorType == -7 then
  237. errorStr = GET_STRING(90180018)
  238. elseif errorType == -8 then
  239. errorStr = GET_STRING(90180006)
  240. elseif errorType == -9 then
  241. errorStr = GET_STRING(90180019)
  242. elseif errorType == -10 then
  243. errorStr = GET_STRING(90180020)
  244. elseif errorType == -11 then
  245. errorStr = GET_STRING(90180021)
  246. elseif errorType == -12 then
  247. errorStr = GET_STRING(90180022)
  248. elseif errorType == -13 then
  249. errorStr = GET_STRING(90180024)
  250. end
  251. global.Facade:sendNotification(global.NoticeTable.SystemTips, errorStr)
  252. -- 清除旧数据
  253. self:Cleanup()
  254. self:ClearItemData()
  255. self._data.isTrading = false
  256. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_Close)
  257. end
  258. self._move_trade_confirmed = true
  259. end
  260. function TradeProxy:RespTradingInfo(msg)
  261. local len = msg:GetDataLength()
  262. if len <= 0 then
  263. return
  264. end
  265. local tradeData = ParseRawMsgToJson(msg)
  266. local header = msg:GetHeader()
  267. local whoGetMessage = header.recog
  268. --[[
  269. 负数为错误码
  270. -1; //交易条件不满足
  271. -2; //距离太远不能交易
  272. -3; //战斗状态不允许交易
  273. -4; //离线不能交易
  274. -5; //死亡不能交易
  275. -6; //玩家在黑名单不能交易
  276. ]]
  277. if whoGetMessage ~= 1 then
  278. local tipsStr = nil
  279. if whoGetMessage == -1 then
  280. tipsStr = 90180011
  281. elseif whoGetMessage == -2 then
  282. tipsStr = 90180012
  283. elseif whoGetMessage == -3 then
  284. tipsStr = 90180013
  285. elseif whoGetMessage == -4 then
  286. tipsStr = 90180014
  287. elseif whoGetMessage == -5 then
  288. tipsStr = 90180015
  289. elseif whoGetMessage == -6 then
  290. tipsStr = 90180016
  291. end
  292. if tipsStr then
  293. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(tipsStr))
  294. return
  295. end
  296. end
  297. if not tradeData or not next(tradeData) then
  298. return
  299. end
  300. if whoGetMessage == 1 then
  301. self._tradeList[tradeData.UserID] = tradeData
  302. self._tradeMoney = header.param3
  303. local function callback()
  304. local count = self:getInviteCouont()
  305. if count == 1 then
  306. local function CommonTipsCallBack(bType, custom)
  307. if bType == 1 then
  308. self:RequestTrade(tradeData.UserID, tradeData.Name)
  309. end
  310. end
  311. local data = {}
  312. data.str = string.format(GET_STRING(90020010), tradeData.Name)
  313. data.btnType = 2
  314. data.callback = CommonTipsCallBack
  315. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  316. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, { id = bubbleId, status = false })
  317. elseif count > 1 then
  318. local tipsData = {}
  319. tipsData.list = {}
  320. for _, v in pairs(self:GetInviteItems()) do
  321. local data = {}
  322. data.str = string.format(GET_STRING(90020011), v.Name)
  323. data.agreeCall = function()
  324. self:RequestTrade(v.UserID, v.Name)
  325. end
  326. data.disAgreeCall = function()
  327. self:RequestTrade(v.UserID, v.Name, true)
  328. self:ClearInviteItemsById(v.UserID)
  329. if self:getInviteCouont() == 0 then
  330. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, { id = bubbleId, status = false })
  331. end
  332. end
  333. table.insert(tipsData.list, data)
  334. end
  335. local MainPropertyMediator = global.Facade:retrieveMediator("MainPropertyMediator")
  336. if MainPropertyMediator then
  337. local node = MainPropertyMediator:GetBubbleButtonByID(bubbleId)
  338. if node and not tolua.isnull(node) then
  339. tipsData.pos = node:getWorldPosition()
  340. end
  341. end
  342. global.Facade:sendNotification(global.NoticeTable.Layer_CommonBubbleInfo_Open, tipsData)
  343. end
  344. end
  345. local function cancelCallBack()
  346. for _, v in pairs(self:GetInviteItems()) do
  347. self:RequestTrade(v.UserID, v.Name, true)
  348. end
  349. self:ClearInviteItems()
  350. end
  351. local data = {}
  352. data.id = bubbleId
  353. data.time = 30
  354. data.status = true
  355. data.callback = callback
  356. data.timeOverCB = cancelCallBack
  357. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, data)
  358. end
  359. end
  360. function TradeProxy:GetInviteItems()
  361. return self._tradeList
  362. end
  363. function TradeProxy:getInviteCouont()
  364. local count = 0
  365. for _, v in pairs(self._tradeList) do
  366. count = count + 1
  367. end
  368. return count
  369. end
  370. function TradeProxy:ClearInviteItemsById(UserId)
  371. self._tradeList[UserId] = nil
  372. end
  373. function TradeProxy:ClearInviteItems()
  374. self._tradeList = {}
  375. -- 气泡
  376. local tipsData = {}
  377. tipsData.status = false
  378. tipsData.id = bubbleId
  379. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, tipsData)
  380. end
  381. function TradeProxy:RespAcceptAll(msg)
  382. local data = ParseRawMsgToJson(msg)
  383. local header = msg:GetHeader()
  384. if data then
  385. -- 清除旧数据
  386. self:Cleanup()
  387. -- 字符串:对方名字
  388. local trader = {}
  389. trader.name = data.UserName
  390. trader.userId = data.UserID
  391. self._data.trader = trader
  392. self._tradeMoney = header.param3
  393. self:ClearItemData()
  394. self._data.isTrading = true
  395. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_Open)
  396. end
  397. end
  398. function TradeProxy:RespTradingCancel(msg)
  399. local header = msg:GetHeader()
  400. local data = self:GetSelfItemData()
  401. -- 清除旧数据
  402. self:Cleanup()
  403. self:ClearItemData()
  404. self._data.isTrading = false
  405. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_Close)
  406. self._move_trade_confirmed = true
  407. end
  408. function TradeProxy:RespTraderMoneyChange(msg)
  409. local header = msg:GetHeader()
  410. if not header or not next(header) then
  411. return
  412. end
  413. self:SetOtherGoldNum(header.recog)
  414. local data = {}
  415. data.count = header.recog
  416. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MoneyChange, data)
  417. global.Facade:sendNotification(global.NoticeTable.Audio_Play, { type = global.MMO.SND_TYPE_MONEY })
  418. end
  419. function TradeProxy:RespMyselfMoneyChange(msg)
  420. local header = msg:GetHeader()
  421. local tradingNum = header.recog
  422. local nowGoldNum = header.param1
  423. self:SetMyselfGoldNum(tradingNum)
  424. local data = {
  425. count = tradingNum
  426. }
  427. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyMoneyChange, data)
  428. end
  429. function TradeProxy:RespMyselfMoneyChangeFail(msg)
  430. local header = msg:GetHeader()
  431. if not header or not next(header) then
  432. return
  433. end
  434. local tradingNum = header.recog
  435. local nowGoldNum = header.param1
  436. self:SetMyselfGoldNum(tradingNum)
  437. local data = {
  438. count = tradingNum
  439. }
  440. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyMoneyChange, data)
  441. end
  442. function TradeProxy:RespTraderItemPutIn(msg)
  443. --对方放入物品消息
  444. local header = msg:GetHeader()
  445. local itemData = ParseRawMsgToJson(msg)
  446. itemData = ChangeItemServersSendDatas(itemData)
  447. if itemData then
  448. self:SetOtherItemDataByItemId(itemData.MakeIndex, itemData)
  449. local data = {}
  450. data.way = 1
  451. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_TraderItemChange, data)
  452. end
  453. end
  454. function TradeProxy:RespTraderItemPutOut(msg)
  455. --对方取出物品消息
  456. local header = msg:GetHeader()
  457. local MakeIndex = header.recog
  458. if MakeIndex and MakeIndex ~= 0 then
  459. self:SetOtherItemDataByItemId(MakeIndex, nil)
  460. local data = {}
  461. data.way = 1
  462. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_TraderItemChange, data)
  463. end
  464. end
  465. function TradeProxy:RespMyselfItemPutInSuccess(msg)
  466. --自己放入物品结果
  467. local header = msg:GetHeader()
  468. if not header.recog then
  469. return
  470. end
  471. local MakeIndex = header.recog
  472. local data = {}
  473. data.way = 0
  474. data.MakeIndex = header.recog
  475. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  476. local itemData = BagProxy:GetItemDataByMakeIndex(MakeIndex)
  477. BagProxy:DelItemData(itemData, true)
  478. self:SetSelfItemDataByItemId(MakeIndex, itemData)
  479. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyselfItemChange, data)
  480. end
  481. function TradeProxy:RespMyselfItemPutInFail(msg)
  482. local header = msg:GetHeader()
  483. if not header or not header.recog then
  484. return
  485. end
  486. local data = {
  487. trading = {
  488. MakeIndex = header.recog,
  489. state = 1
  490. }
  491. }
  492. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  493. end
  494. function TradeProxy:RespMyselfItemPutOutSuccess(msg)
  495. --自己取出物品结果
  496. local header = msg:GetHeader()
  497. if not header.recog then
  498. return
  499. end
  500. -- 扣掉交易中的
  501. local MakeIndex = header.recog
  502. local data = {}
  503. data.way = 0
  504. data.MakeIndex = MakeIndex
  505. self:SetSelfItemDataByItemId(MakeIndex, nil)
  506. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyselfItemChange, data)
  507. -- 背包中的重新刷出来
  508. local data = {
  509. trading = {
  510. MakeIndex = MakeIndex,
  511. state = 1
  512. }
  513. }
  514. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  515. end
  516. function TradeProxy:RespMyselfItemPutOutFail(msg)
  517. local header = msg:GetHeader()
  518. local data = {}
  519. data.way = 0
  520. data.MakeIndex = header.recog
  521. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_MyselfItemChange, data)
  522. end
  523. function TradeProxy:RespTraderStatusChange(msg)
  524. -- 收到对方状态改变: 参数1是类型 0取消锁定,1锁定
  525. local len = msg:GetDataLength()
  526. if len <= 0 then
  527. return
  528. end
  529. local msgData = msg:GetData()
  530. local sliceStr = msgData:ReadString(len)
  531. local header = msg:GetHeader()
  532. local data = ParseRawMsgToJson(msg)
  533. if not data then
  534. return
  535. end
  536. local UserId = data.uid
  537. local state = data.status
  538. if not UserId then
  539. return
  540. end
  541. local otherData = self:GetTrader()
  542. local myUid = global.gamePlayerController:GetMainPlayerID()
  543. if otherData and otherData.userId and UserId == otherData.userId then
  544. local status = state == 1 and true or false
  545. if not status then
  546. self.mySelfLockState = false
  547. end
  548. self:SetOtherSureLockState(status)
  549. elseif myUid == UserId then
  550. local status = state == 1 and true or false
  551. if not status then
  552. self.mySelfLockState = false
  553. end
  554. self:SetMySureLockState(status)
  555. end
  556. end
  557. function TradeProxy:RespTradeSuccess(msg)
  558. local myTradeData = self._items_data_myself
  559. if myTradeData and next(myTradeData) then
  560. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  561. for k, v in pairs(myTradeData) do
  562. if v and next(v) then
  563. BagProxy:DelItemData(v, true)
  564. end
  565. end
  566. end
  567. self:ClearItemData()
  568. self._data.isTrading = false
  569. global.Facade:sendNotification(global.NoticeTable.Layer_Trade_Close)
  570. self._move_trade_confirmed = true
  571. end
  572. function TradeProxy:RegisterMsgHandler()
  573. local msgType = global.MsgType
  574. -- 请求交易结果
  575. LuaRegisterMsgHandler(msgType.MSG_SC_TRADE_TRADER_RESULT, handler(self, self.RespTradeResult))
  576. -- xx请求和你交易
  577. LuaRegisterMsgHandler(msgType.MSG_SC_TRADE_INFO_RESPONSE, handler(self, self.RespTradingInfo))
  578. -- 打开交易界面
  579. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_ACCEPT_ALL, handler(self, self.RespAcceptAll))
  580. -- (回复): 取消交易,关闭界面
  581. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_CANCEL, handler(self, self.RespTradingCancel))
  582. -- (回复): 对方货币数量改变
  583. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_TRADER_CHANGE_MONEY, handler(self, self.RespTraderMoneyChange))
  584. -- (回复): 自己货币数量改变
  585. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_CHANGE_MONEY, handler(self, self.RespMyselfMoneyChange))
  586. -- (回复): 自己货币数量改变失败
  587. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_CHANGE_MONEY_FAIL, handler(self, self.RespMyselfMoneyChangeFail))
  588. -- (回复): 对方交易物品放入
  589. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_TRADER_PUTIN_ITEM, handler(self, self.RespTraderItemPutIn))
  590. -- (回复): 对方交易物品取出
  591. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_TRADER_PUTOUT_ITEM, handler(self, self.RespTraderItemPutOut))
  592. -- (回复): 自己放入物品成功
  593. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_PUTIN_ITEM_SUCCESS, handler(self, self.RespMyselfItemPutInSuccess))
  594. -- (回复): 自己放入物品失败
  595. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_PUTIN_ITEM_FAIL, handler(self, self.RespMyselfItemPutInFail))
  596. -- (回复): 自己取回物品成功
  597. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_PUTOUT_ITEM_SUCCESS, handler(self, self.RespMyselfItemPutOutSuccess))
  598. -- (回复): 自己取回物品失败
  599. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_MYSELF_PUTOUT_ITEM_FAIL, handler(self, self.RespMyselfItemPutOutFail))
  600. -- (回复): 双方状态改变
  601. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_LOCK_CHANGE_STATUS, handler(self, self.RespTraderStatusChange))
  602. -- (回复): 交易成功
  603. LuaRegisterMsgHandler(msgType.MSG_SC_TRADING_SUCCESSFUL, handler(self, self.RespTradeSuccess))
  604. end
  605. function TradeProxy:RequestTrade(uid, name, refuse)
  606. if not refuse and not CheckCanDoSomething() then
  607. return
  608. end
  609. local tradeTargetUid = 0
  610. local targetLen = 0
  611. if uid then
  612. tradeTargetUid = uid
  613. targetLen = string.len(uid)
  614. end
  615. if not refuse then
  616. self:ClearInviteItems()
  617. end
  618. local refuseStatus = refuse and 1 or 0
  619. LuaSendMsg(global.MsgType.MSG_CS_TRADING_INFO_REQUEST, refuseStatus, 0, 0, 0, tradeTargetUid, targetLen)
  620. end
  621. function TradeProxy:SendTradeRequest(uid, name)
  622. if not CheckCanDoSomething() then
  623. return
  624. end
  625. if self:IsTrading() then
  626. return
  627. end
  628. local tradeTargetUid = 0
  629. local targetLen = 0
  630. if uid then
  631. tradeTargetUid = uid
  632. targetLen = string.len(uid)
  633. end
  634. if CHECK_SERVER_OPTION(global.MMO.SERVER_OPTION_TRADE_DEAL) == true then
  635. LuaSendMsg(global.MsgType.MSG_CS_TRADE_SEND_REQUEST)
  636. else
  637. LuaSendMsg(global.MsgType.MSG_CS_TRADE_SEND_REQUEST, 0, 0, 0, 0, tradeTargetUid, targetLen)
  638. end
  639. end
  640. function TradeProxy:RequestCancle()
  641. -- 请求取消交易
  642. LuaSendMsg(global.MsgType.MSG_CS_TRADING_CANCEL)
  643. end
  644. function TradeProxy:RequestChangeMoney(count)
  645. -- 请求修改货币 ,参数1:数量
  646. LuaSendMsg(global.MsgType.MSG_CS_TRADING_MYSELF_CHANGE_MONEY, count, 0, 0, 0)
  647. end
  648. function TradeProxy:RequestPutInItem(itemID, itemName)
  649. -- 请求放入物品
  650. LuaSendMsg(global.MsgType.MSG_CS_TRADING_MYSELF_PUTIN_ITEM, itemID, 0, 0, 0, itemName, string.len(itemName))
  651. end
  652. function TradeProxy:RequestPutOutItem(itemID, itemName)
  653. -- 请求取出物品
  654. LuaSendMsg(global.MsgType.MSG_CS_TRADING_MYSELF_PUTOUT_ITEM, itemID, 0, 0, 0, itemName, string.len(itemName))
  655. end
  656. function TradeProxy:RequestTradeEnd()
  657. self.mySelfLockState = true
  658. LuaSendMsg(global.MsgType.MSG_CS_TRADING_TRADE, 0, 0, 0, 0)
  659. end
  660. function TradeProxy:RequestChangeLock()
  661. local nowState = self:GetMySureLockState()
  662. -- 主动请求改变自己锁定状态
  663. local changeStatus = not nowState and 1 or 0
  664. LuaSendMsg(global.MsgType.MSG_CS_CHANGE_MY_TRADE_STATE, changeStatus, 0, 0, 0)
  665. end
  666. function TradeProxy:CheckPlayerOut(userId)
  667. if self:IsTrading() then
  668. local trader = self._data and self:GetTrader()
  669. if userId and trader and trader.userId and userId == trader.userId then
  670. self:RequestCancle()
  671. end
  672. end
  673. end
  674. -- 主玩家移动 如果正在交易 直接取消
  675. function TradeProxy:CheckPlayerMove()
  676. if self._move_trade_confirmed and self:IsTrading() then
  677. self._move_trade_confirmed = false
  678. self:RequestCancle()
  679. end
  680. end
  681. return TradeProxy