NPCRepaireProxy.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local NPCRepaireProxy = class("NPCRepaireProxy", RemoteProxy)
  3. NPCRepaireProxy.NAME = global.ProxyTable.NPCRepaireProxy
  4. NPCRepaireProxy.STEP = {
  5. Waiting = 1,
  6. AddItem = 2,
  7. GetPrice = 3,
  8. HadPrice = 4,
  9. Repairing = 5,
  10. Repaired = 6
  11. }
  12. function NPCRepaireProxy:ctor()
  13. NPCRepaireProxy.super.ctor(self)
  14. self.repaireStep = NPCRepaireProxy.STEP.Waiting
  15. self.repairePrice = nil
  16. end
  17. function NPCRepaireProxy:SetOnRepairingData(data)
  18. self.onRepairingData = data
  19. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  20. if data then
  21. self:SetRepaireStep(NPCRepaireProxy.STEP.AddItem)
  22. BagProxy:SetOnSellOrRepaire(data.MakeIndex)
  23. else
  24. self:SetRepaireStep(NPCRepaireProxy.STEP.Waiting)
  25. self:SetRepaireStep(nil)
  26. BagProxy:CleanOnSellOrRepaire()
  27. end
  28. end
  29. function NPCRepaireProxy:SetRepaireStep(step)
  30. if step then
  31. self.repaireStep = step
  32. end
  33. end
  34. function NPCRepaireProxy:GetRepaireStep()
  35. return self.repaireStep
  36. end
  37. function NPCRepaireProxy:SetRepairingPrice(price)
  38. self.repairePrice = price
  39. end
  40. function NPCRepaireProxy:RequestNpcStoreRepairePre(data)
  41. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  42. local npcId = NPCProxy:GetCurrentNPCID()
  43. if not data or not npcId then
  44. return
  45. end
  46. local step = self:GetRepaireStep()
  47. if step ~= NPCRepaireProxy.STEP.AddItem then
  48. return
  49. end
  50. local Name = data.Name
  51. local MakeIndex = data.MakeIndex
  52. LuaSendMsg(global.MsgType.MSG_CS_NPC_STORE_REPAIRE_PRE, npcId, MakeIndex, 0, 0, Name, string.len(Name))
  53. self:SetRepaireStep(NPCRepaireProxy.STEP.GetPrice)
  54. end
  55. function NPCRepaireProxy:RequestNpcStoreRepaire()
  56. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  57. local npcId = NPCProxy:GetCurrentNPCID()
  58. if not self.onRepairingData or not npcId then
  59. return
  60. end
  61. local step = self:GetRepaireStep()
  62. if step ~= NPCRepaireProxy.STEP.HadPrice then
  63. return
  64. end
  65. if not self:CheckItemIsCanRepaire() then
  66. return
  67. end
  68. local data = self.onRepairingData
  69. local Name = data.Name
  70. local MakeIndex = data.MakeIndex
  71. LuaSendMsg(global.MsgType.MSG_CS_NPC_STORE_REPAIRE_ITEM, npcId, MakeIndex, 0, 0, Name, string.len(Name))
  72. self:SetRepaireStep(NPCRepaireProxy.STEP.Repairing)
  73. end
  74. function NPCRepaireProxy:CheckItemIsCanRepaire()
  75. if self.repairePrice and self.repairePrice > 0 then
  76. return true
  77. else
  78. local data = {}
  79. data.str = GET_STRING(90030004)
  80. data.btnType = 1
  81. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  82. self:ResetRepairingState()
  83. return false
  84. end
  85. end
  86. function NPCRepaireProxy:RepaireEndChangeItemData(itemMakeIndex, drua, makeDrua)
  87. if not itemMakeIndex then
  88. return
  89. end
  90. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  91. local data = BagProxy:GetItemDataByMakeIndex(itemMakeIndex)
  92. if data and next(data) then
  93. data.Drua = drua or data.Dura
  94. data.MaxDrua = makeDrua or data.MaxDrua
  95. BagProxy:ChangeItemData(data)
  96. end
  97. end
  98. function NPCRepaireProxy:ResetRepairingState()
  99. self:SetOnRepairingData(nil)
  100. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  101. local data = {
  102. way = NPCProxy.EVENT_TYPE.REPAIRE,
  103. reset = true
  104. }
  105. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_UpDate, data)
  106. end
  107. --修理价格返回
  108. local function handle_MSG_SC_NPC_REPAIRE_ITEM_PRICE(msg)
  109. local head = msg:GetHeader()
  110. if not head then
  111. return
  112. end
  113. local price = head.recog
  114. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  115. local EVENT_TYPE = NPCProxy.EVENT_TYPE
  116. local NPCRepaireProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCRepaireProxy)
  117. NPCRepaireProxy:SetRepairingPrice(price)
  118. NPCRepaireProxy:SetRepaireStep(NPCRepaireProxy.STEP.HadPrice)
  119. local data = {
  120. way = EVENT_TYPE.REPAIRE,
  121. price = price
  122. }
  123. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_UpDate, data)
  124. end
  125. local function handle_MSG_SC_NPC_REPAIRE_ITEM_RESULT_SUCCESS(msg)
  126. local header = msg:GetHeader()
  127. if not header then
  128. return
  129. end
  130. local MakeIndex = header.param1
  131. local drua = header.param2
  132. local maxDrua = header.param3
  133. local NPCRepaireProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCRepaireProxy)
  134. NPCRepaireProxy:RepaireEndChangeItemData(MakeIndex, drua, maxDrua)
  135. NPCRepaireProxy:ResetRepairingState()
  136. end
  137. local function handle_MSG_SC_NPC_REPAIRE_ITEM_RESULT_FAIL(msg)
  138. local header = msg:GetHeader()
  139. local errorCode = header.recog --错误
  140. if errorCode == -1 then
  141. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(90030005))
  142. end
  143. local NPCRepaireProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCRepaireProxy)
  144. NPCRepaireProxy:ResetRepairingState()
  145. end
  146. local function handle_MSG_SC_NPC_REPAIRE_ITEM(msg)
  147. local jsonData = ParseRawMsgToJson(msg)
  148. if not jsonData then
  149. return -1
  150. end
  151. local NPCProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  152. local EVENT_TYPE = NPCProxy.EVENT_TYPE
  153. local data = {
  154. type = EVENT_TYPE.REPAIRE
  155. }
  156. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Sell_Repaire_Open, data)
  157. end
  158. function NPCRepaireProxy:RegisterMsgHandler()
  159. NPCRepaireProxy.super.RegisterMsgHandler(self)
  160. local msgType = global.MsgType
  161. -- NPC修理道具打开返回
  162. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_REPAIRE_ITEM, handle_MSG_SC_NPC_REPAIRE_ITEM)
  163. -- NPC商店修理失败返回
  164. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_REPAIRE_ITEM_RESULT_FAIL, handle_MSG_SC_NPC_REPAIRE_ITEM_RESULT_FAIL)
  165. -- NPC商店修理成功返回
  166. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_REPAIRE_ITEM_RESULT_SUCCESS, handle_MSG_SC_NPC_REPAIRE_ITEM_RESULT_SUCCESS)
  167. -- NPC商店修理道具预显示价格返回
  168. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_REPAIRE_ITEM_PRICE, handle_MSG_SC_NPC_REPAIRE_ITEM_PRICE)
  169. end
  170. return NPCRepaireProxy