NPCSellProxy.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local NPCSellProxy = class("NPCSellProxy", RemoteProxy)
  3. NPCSellProxy.NAME = global.ProxyTable.NPCSellProxy
  4. NPCSellProxy.STEP = {
  5. Waiting = 1,
  6. AddItem = 2,
  7. GetPrice = 3,
  8. HadPrice = 4,
  9. Selling = 5,
  10. Selled = 6
  11. }
  12. function NPCSellProxy:ctor()
  13. NPCSellProxy.super.ctor(self)
  14. self.onSellingData = nil
  15. self.sellStep = NPCSellProxy.STEP.Waiting
  16. self.sellPrice = nil
  17. self.itemFrom = nil
  18. end
  19. function NPCSellProxy:SetOnSellingData(data)
  20. self.onSellingData = data
  21. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  22. if data then
  23. self:SetSellingStep(NPCSellProxy.STEP.AddItem)
  24. BagProxy:SetOnSellOrRepaire(data.MakeIndex)
  25. else
  26. self:SetSellingStep(NPCSellProxy.STEP.Waiting)
  27. self:SetSellingFrom(nil)
  28. BagProxy:CleanOnSellOrRepaire()
  29. end
  30. end
  31. function NPCSellProxy:SetSellingStep(step)
  32. if step then
  33. self.sellStep = step
  34. end
  35. end
  36. function NPCSellProxy:GetSellingStep()
  37. return self.sellStep
  38. end
  39. function NPCSellProxy:SetSellingFrom(from)
  40. self.itemFrom = from
  41. end
  42. function NPCSellProxy:GetSellingFrom()
  43. return self.itemFrom
  44. end
  45. function NPCSellProxy:SetSellingPrice(price)
  46. self.sellPrice = price
  47. end
  48. function NPCSellProxy:RequestNpcStoreSellPre(data)
  49. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  50. local npcId = NPCProxy:GetCurrentNPCID()
  51. if not data or not npcId then
  52. return
  53. end
  54. local step = self:GetSellingStep()
  55. if step ~= NPCSellProxy.STEP.AddItem then
  56. return
  57. end
  58. local Name = data.Name
  59. local MakeIndex = data.MakeIndex
  60. LuaSendMsg(global.MsgType.MSG_CS_NPC_STORE_SELL_PRE, npcId, MakeIndex, 0, 0, Name, string.len(Name))
  61. self:SetSellingStep(NPCSellProxy.STEP.GetPrice)
  62. end
  63. function NPCSellProxy:RequestNpcStoreSell()
  64. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  65. local npcId = NPCProxy:GetCurrentNPCID()
  66. if not self.onSellingData or not npcId then
  67. return
  68. end
  69. local step = self:GetSellingStep()
  70. if step ~= NPCSellProxy.STEP.HadPrice then
  71. return
  72. end
  73. if not self:CheckItemIsCanSell() then
  74. return
  75. end
  76. local data = self.onSellingData
  77. local Name = data.Name
  78. local MakeIndex = data.MakeIndex
  79. LuaSendMsg(global.MsgType.MSG_CS_NPC_STORE_SELL_ITEM, npcId, MakeIndex, 0, 0, Name, string.len(Name))
  80. self:SetSellingStep(NPCSellProxy.STEP.Selling)
  81. end
  82. function NPCSellProxy:CheckItemIsCanSell()
  83. if self.sellPrice and self.sellPrice > 0 then
  84. return true
  85. else
  86. local data = {}
  87. data.str = GET_STRING(90030003)
  88. data.btnType = 1
  89. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  90. self:ResetSellingState()
  91. return false
  92. end
  93. end
  94. function NPCSellProxy:ResetSellingState()
  95. self:SetOnSellingData(nil)
  96. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  97. local EVENT_TYPE = NPCProxy.EVENT_TYPE
  98. local data = {
  99. way = EVENT_TYPE.SELL,
  100. reset = true
  101. }
  102. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_UpDate, data)
  103. end
  104. -- 出售价格返回
  105. local function handle_MSG_SC_NPC_STORE_SELL_PRICE(msg)
  106. local head = msg:GetHeader()
  107. if not head then
  108. return
  109. end
  110. local price = head.recog
  111. local NPCSellProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCSellProxy)
  112. NPCSellProxy:SetSellingPrice(price)
  113. NPCSellProxy:SetSellingStep(NPCSellProxy.STEP.HadPrice)
  114. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  115. local EVENT_TYPE = NPCProxy.EVENT_TYPE
  116. local data = {
  117. way = EVENT_TYPE.SELL,
  118. price = price
  119. }
  120. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_UpDate, data)
  121. end
  122. local function handle_MSG_SC_NPC_STORE_SELL_RESULT_FAIL(msg)
  123. local NPCSellProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCSellProxy)
  124. NPCSellProxy:ResetSellingState()
  125. end
  126. local function handle_MSG_SC_NPC_STORE_SELL_RESULT_SUCCESS(msg)
  127. local NPCSellProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCSellProxy)
  128. NPCSellProxy:ResetSellingState()
  129. end
  130. local function handle_MSG_SC_NPC_SELL(msg)
  131. local jsonData = ParseRawMsgToJson(msg)
  132. if not jsonData then
  133. return -1
  134. end
  135. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  136. local EVENT_TYPE = NPCProxy.EVENT_TYPE
  137. local data = {
  138. type = EVENT_TYPE.SELL
  139. }
  140. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_Open, data)
  141. end
  142. function NPCSellProxy:RegisterMsgHandler()
  143. NPCSellProxy.super.RegisterMsgHandler(self)
  144. local msgType = global.MsgType
  145. -- NPC出售
  146. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_SELL, handle_MSG_SC_NPC_SELL)
  147. -- NPC商店出售道具预显示价格返回
  148. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_SELL_PRICE, handle_MSG_SC_NPC_STORE_SELL_PRICE)
  149. -- NPC商店出售失败返回
  150. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_SELL_RESULT_FAIL, handle_MSG_SC_NPC_STORE_SELL_RESULT_FAIL)
  151. -- NPC商店出售成功返回
  152. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_SELL_RESULT_SUCCESS, handle_MSG_SC_NPC_STORE_SELL_RESULT_SUCCESS)
  153. end
  154. return NPCSellProxy