NPCProxy.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local NPCProxy = class("NPCProxy", RemoteProxy)
  3. NPCProxy.NAME = global.ProxyTable.NPC
  4. -- NPC打开出售修理
  5. NPCProxy.EVENT_TYPE = {
  6. SELL = 1,
  7. REPAIRE = 2,
  8. DOSOMETHING = 3,
  9. NEWTYPE = 4,
  10. }
  11. function NPCProxy:ctor()
  12. NPCProxy.super.ctor(self)
  13. self._currClickNPCID = nil --当前点击的npc id, 跨服之后服务端发送的Useid是本地的, 导致找不到npc, 只好本地记录一下
  14. self._currentBackground = nil
  15. self._currentNPCTalkData = nil
  16. self._timerWidgets = {}
  17. self._isClickCD = false
  18. end
  19. function NPCProxy:CheckNpcTalkLayerClose(npcTypeIndex)
  20. if self:GetCurrentNPCIndex() == npcTypeIndex then
  21. self:Cleanup()
  22. end
  23. end
  24. function NPCProxy:GetCurrentNPCTalkData()
  25. return self._currentNPCTalkData
  26. end
  27. function NPCProxy:GetCurrentNPCID()
  28. if not self._currentNPCTalkData then
  29. return nil
  30. end
  31. return self._currentNPCTalkData.npcID
  32. end
  33. function NPCProxy:GetCurrentNPCIndex()
  34. if not self._currentNPCTalkData then
  35. return nil
  36. end
  37. return self._currentNPCTalkData.index
  38. end
  39. function NPCProxy:GetCurrentClickNPCID()
  40. return self._currClickNPCID
  41. end
  42. function NPCProxy:SetCurrentNPCTalkData(data)
  43. self._currentNPCTalkData = data
  44. end
  45. function NPCProxy:GetCurrentBackground()
  46. return self._currentBackground
  47. end
  48. function NPCProxy:SetCurrentBackground(data)
  49. self._currentBackground = data
  50. end
  51. function NPCProxy:ResetCurrentBackground()
  52. self._currentBackground = nil
  53. end
  54. function NPCProxy:Cleanup()
  55. self._currClickNPCID = nil
  56. self:ResetCurrentBackground()
  57. global.Facade:sendNotification(global.NoticeTable.SUIITEMBOXPutoutAll)
  58. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Talk_Close)
  59. end
  60. function NPCProxy:ExecuteWithCommonInput(jsonData)
  61. local sendData = {
  62. UserID = jsonData.UserID,
  63. Act = jsonData.Act,
  64. commonInput = jsonData.commonInput,
  65. inputID = jsonData.inputID,
  66. }
  67. self:ExecuteWithJsonData(sendData)
  68. end
  69. function NPCProxy:ExecuteWithJsonData(jsonData)
  70. local currNpcData = self:GetCurrentNPCTalkData()
  71. if currNpcData and currNpcData.index then
  72. jsonData.index = currNpcData.index
  73. end
  74. if jsonData.newIndex then
  75. jsonData.index = jsonData.newIndex
  76. end
  77. dump(jsonData)
  78. NPCEXECUTE_CLOCK = os.clock()
  79. SendTableToServer(global.MsgType.MSG_CS_NPC_TASK_CLICK, jsonData)
  80. end
  81. function NPCProxy:RequestNpcStoreItemList(data)
  82. local sendData = {
  83. UserID = self._currentNPCTalkData.npcID,
  84. Name = data.Name,
  85. }
  86. SendTableToServer(global.MsgType.MSG_CS_NPC_STORE_ITEM_LIST, sendData)
  87. end
  88. function NPCProxy:RequestNpcStoreBuy(data)
  89. local sendData = {
  90. UserID = self._currentNPCTalkData.npcID,
  91. Name = data.Name,
  92. MakeIndex = data.MakeIndex or 0
  93. }
  94. SendTableToServer(global.MsgType.MSG_CS_NPC_STORE_BUY, sendData)
  95. end
  96. function NPCProxy:RequestNpcMakeDrug(data)
  97. local sendData = {
  98. userid = self._currentNPCTalkData.npcID,
  99. name = data.name,
  100. }
  101. SendTableToServer(global.MsgType.MSG_CS_NPC_MAKE_DRUG, sendData)
  102. end
  103. local function handle_MSG_SC_NPC_TALK(msg)
  104. if NPCEXECUTE_CLOCK then
  105. print("NPC TALK RESP cost", os.clock() - NPCEXECUTE_CLOCK)
  106. end
  107. local jsonData = ParseRawMsgToJson(msg)
  108. if not jsonData then
  109. return -1
  110. end
  111. if not jsonData or not jsonData.sMsg then
  112. return
  113. end
  114. -- dump(jsonData)
  115. local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  116. local nData = {}
  117. nData.content = jsonData.sMsg
  118. nData.npcName = jsonData.Name
  119. nData.npcID = jsonData.UserID
  120. nData.index = jsonData.index
  121. -- Timer:id:remaining:content:x:y:anchor:colorID
  122. -- Timer:1:7200:秘境倒计时:%s:10:10:0:255
  123. -- Timer:1:0:秘境倒计时:%s
  124. if nData.content == "CloseNpcLayer" then
  125. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Talk_Close)
  126. return
  127. end
  128. if string.find(nData.content, "<JumpTo:.->") then
  129. local find_info = { string.find(nData.content, "<JumpTo:(.-)>") }
  130. if find_info[1] and find_info[2] then
  131. local slices = string.split(find_info[3], ":")
  132. local jumpID = tonumber(slices[1])
  133. local close = (tonumber(slices[2]) == nil or tonumber(slices[2]) == 0)
  134. -- close npc talk
  135. if close then
  136. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Talk_Close)
  137. end
  138. JUMPTO(jumpID)
  139. end
  140. else
  141. npcProxy:SetCurrentNPCTalkData(nData)
  142. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Talk_Open)
  143. -- 同步至 SL框架
  144. SLBridge:onLUAEvent(LUA_EVENT_NPC_TALK, {id = jsonData.UserID, index = jsonData.index, name = jsonData.Name})
  145. end
  146. end
  147. local function handle_MSG_SC_NPC_TALK_BACKGROUND(msg)
  148. local jsonData = ParseRawMsgToJson(msg)
  149. if not jsonData then
  150. return -1
  151. end
  152. dump(jsonData)
  153. local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  154. npcProxy:SetCurrentBackground(jsonData)
  155. end
  156. local function handle_MSG_SC_NPC_TALK_CLOSE(msg)
  157. print("local function handle_MSG_SC_NPC_TALK_CLOSE(msg) 644")
  158. local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  159. npcProxy:Cleanup()
  160. end
  161. local function handle_MSG_SC_NPC_STORE_OPEN(msg)
  162. local jsonData = ParseRawMsgToJson(msg)
  163. if not jsonData then
  164. return -1
  165. end
  166. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Store_Open, jsonData)
  167. end
  168. local function handle_MSG_SC_NPC_STORE_ITEM_LIST(msg)
  169. local jsonData = ParseRawMsgToJson(msg)
  170. if not jsonData then
  171. return -1
  172. end
  173. local itemList = {}
  174. for _, v in ipairs(jsonData) do
  175. local data = ChangeItemServersSendDatas(v)
  176. table.insert(itemList, data)
  177. end
  178. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Store_Open, {Items = itemList})
  179. end
  180. local function handle_MSG_SC_NPC_STORE_BUY_RESULT_FAIL(msg)
  181. local head = msg:GetHeader()
  182. if not head then
  183. return
  184. end
  185. if head.recog then
  186. local str = GET_STRING(90160009)
  187. if head.recog == 2 then
  188. str = GET_STRING(90160007)
  189. elseif head.recog == 3 then
  190. str = GET_STRING(90160008)
  191. elseif head.recog == 1 then
  192. str = GET_STRING(90160006)
  193. end
  194. local data = {}
  195. data.str = str
  196. data.btnDesc = { GET_STRING(1002) }
  197. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  198. end
  199. end
  200. local function handle_MSG_SC_NPC_STORE_BUY_RESULT_SUCCESS(msg)
  201. local jsonData = ParseRawMsgToJson(msg)
  202. if not jsonData then
  203. return -1
  204. end
  205. dump(jsonData)
  206. local MakeIndex = jsonData.MakeIndex
  207. if MakeIndex and MakeIndex ~= 0 then
  208. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Store_Item_Remove, MakeIndex)
  209. end
  210. end
  211. local function handle_MSG_SC_NPC_MAKE_DRUG_LIST(msg)
  212. local jsonData = ParseRawMsgToJson(msg)
  213. if not jsonData then
  214. return -1
  215. end
  216. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Make_Drug_Open, jsonData)
  217. end
  218. local function handle_MSG_SC_NPC_MAKE_DRUG_SUCCESS(msg)
  219. local head = msg:GetHeader()
  220. if not head then
  221. return
  222. end
  223. local data = {}
  224. data.str = GET_STRING(90160001)
  225. data.btnDesc = { GET_STRING(1002) }
  226. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  227. end
  228. local function handle_MSG_SC_NPC_MAKE_DRUG_FAILED(msg)
  229. local head = msg:GetHeader()
  230. if not head then
  231. return
  232. end
  233. if head.recog then
  234. local tips = true
  235. local data = {}
  236. data.btnDesc = { GET_STRING(1002) }
  237. if head.recog == 1 then
  238. data.str = GET_STRING(90160002)
  239. elseif head.recog == 2 then
  240. data.str = GET_STRING(90160003)
  241. elseif head.recog == 3 then
  242. data.str = GET_STRING(90160004)
  243. elseif head.recog == 4 then
  244. data.str = GET_STRING(90160005)
  245. else
  246. tips = false
  247. end
  248. if tips then
  249. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  250. end
  251. end
  252. end
  253. function NPCProxy:handle_MSG_SC_PROGRESS_BAR(msg)
  254. global.Facade:sendNotification(global.NoticeTable.Layer_ProgressBar_Close)
  255. local jsonData = ParseRawMsgToJson(msg)
  256. if not jsonData then
  257. return nil
  258. end
  259. global.Facade:sendNotification(global.NoticeTable.Layer_ProgressBar_Open, jsonData)
  260. end
  261. function NPCProxy:RequestTalk(npcID)
  262. print("talk to NPC", npcID)
  263. NPCEXECUTE_CLOCK = os.clock()
  264. self:ResetCurrentBackground()
  265. local npc = global.npcManager:FindOneNpcInCurrViewFieldById(npcID)
  266. if npc then
  267. self._currClickNPCID = npcID
  268. local typeIndex = npc:GetTypeIndex()
  269. local sendData = {
  270. UserID = npcID,
  271. index = typeIndex,
  272. }
  273. SendTableToServer(global.MsgType.MSG_CS_NPC_CLICK, sendData)
  274. dump(sendData)
  275. global.Facade:sendNotification(global.NoticeTable.Audio_Play, { type = global.MMO.SND_TYPE_NPC_TALK, index = typeIndex })
  276. -- SL
  277. local talkData =
  278. {
  279. UserID = npcID,
  280. index = typeIndex,
  281. name = npc:GetName()
  282. }
  283. SLBridge:onLUAEvent(LUA_EVENT_TALKTONPC, talkData)
  284. end
  285. end
  286. function NPCProxy:CheckTalk(npcID, isAuto)
  287. -- 摆摊
  288. if false == CheckCanDoSomething() then
  289. return nil
  290. end
  291. -- NPC点击间隔,只有手动生效
  292. if not isAuto then
  293. if self._isClickCD then
  294. return nil
  295. end
  296. local mapProxy = global.Facade:retrieveProxy(global.ProxyTable.Map)
  297. local clickInterval = mapProxy:GetClickNpcSpeed() * 0.001
  298. if clickInterval and clickInterval > 0 then
  299. self._isClickCD = true
  300. PerformWithDelayGlobal(function()
  301. self._isClickCD = false
  302. end, clickInterval)
  303. end
  304. end
  305. -- 现在点击即可打开NPC
  306. local npc = global.npcManager:FindOneNpcInCurrViewFieldById(npcID)
  307. if npc then
  308. self:RequestTalk(npcID)
  309. end
  310. end
  311. function NPCProxy:RequestNotifyTalkOpen(talkLayerID)
  312. if not talkLayerID or "" == talkLayerID then
  313. return nil
  314. end
  315. print("---------------RequestNotifyTalkOpen", talkLayerID)
  316. SendTableToServer(global.MsgType.MSG_CS_NPC_TALK_OPEN_NOTIFY, {layerid = talkLayerID})
  317. end
  318. function NPCProxy:RequestNotifyTalkClose(talkLayerID)
  319. if not talkLayerID or "" == talkLayerID then
  320. return nil
  321. end
  322. print("---------------RequestNotifyTalkClose", talkLayerID)
  323. SendTableToServer(global.MsgType.MSG_CS_NPC_TALK_CLOSE_NOTIFY, {layerid = talkLayerID})
  324. end
  325. function NPCProxy:RegisterMsgHandler()
  326. NPCProxy.super.RegisterMsgHandler(self)
  327. local msgType = global.MsgType
  328. -- NPC 对话数据(以及任务)
  329. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_TALK, handle_MSG_SC_NPC_TALK)
  330. -- NPC 对话框背景数据
  331. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_TALK_BACKGROUND, handle_MSG_SC_NPC_TALK_BACKGROUND)
  332. -- NPC 关闭NPC面板
  333. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_TALK_CLOSE, handle_MSG_SC_NPC_TALK_CLOSE)
  334. -- 打开NPC商店
  335. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_OPEN, handle_MSG_SC_NPC_STORE_OPEN)
  336. -- NPC商店道具列表返回
  337. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_ITEM_LIST, handle_MSG_SC_NPC_STORE_ITEM_LIST)
  338. -- NPC商店购买失败返回
  339. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_BUY_RESULT_FAIL, handle_MSG_SC_NPC_STORE_BUY_RESULT_FAIL)
  340. -- NPC商店购买成功返回
  341. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_STORE_BUY_RESULT_SUCCESS, handle_MSG_SC_NPC_STORE_BUY_RESULT_SUCCESS)
  342. -- 打开NPC 炼药
  343. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_MAKE_DRUG_LIST, handle_MSG_SC_NPC_MAKE_DRUG_LIST)
  344. -- NPC 炼药成功
  345. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_MAKE_DRUG_SUCCESS, handle_MSG_SC_NPC_MAKE_DRUG_SUCCESS)
  346. -- NPC 炼药失败
  347. LuaRegisterMsgHandler(msgType.MSG_SC_NPC_MAKE_DRUG_FAILED, handle_MSG_SC_NPC_MAKE_DRUG_FAILED)
  348. -- NPC 进度条
  349. LuaRegisterMsgHandler(msgType.MSG_SC_PROGRESS_BAR, handler(self, self.handle_MSG_SC_PROGRESS_BAR))
  350. end
  351. return NPCProxy