FriendProxy.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local FriendProxy = class("FriendProxy", RemoteProxy)
  3. FriendProxy.NAME = global.ProxyTable.FriendProxy
  4. local cjson = require("cjson")
  5. -- 服务端的好友类型
  6. FriendProxy.FriendType = {
  7. Friend = 1, -- 好友
  8. Blacklist = 2, -- 黑名单
  9. Apply = 3, -- 申请的好友
  10. }
  11. function FriendProxy:ctor()
  12. self._maxFriend = 100 -- 最大好友人数
  13. self._friendData = {} -- 好友列表
  14. self._friendDataName = {} -- 好友列表 名称做索引
  15. self._applyData = {} -- 申请的好友列表
  16. self._blacklistData = {} -- 黑名单
  17. FriendProxy.super.ctor(self)
  18. end
  19. -- 登录时要请求的数据
  20. function FriendProxy:loginRequest()
  21. self:RequestFriendList()
  22. self:requestFriendBlockList()
  23. end
  24. -- 获取最大好友数量
  25. function FriendProxy:getMaxFriendNum()
  26. return self._maxFriend
  27. end
  28. -- type 1:好友 2: 黑名单 3:申请列表
  29. function FriendProxy:addFriendData(data, type)
  30. if data == nil then
  31. return false
  32. end
  33. if type == FriendProxy.FriendType.Friend then
  34. self._friendData[data.UserId] = data
  35. self._friendDataName[data.Name] = data
  36. self:removeBlockData(data.UserId)
  37. elseif type == FriendProxy.FriendType.Blacklist then
  38. self._blacklistData[data.UserId] = data
  39. self:removeFriendData(data.UserId)
  40. elseif type == FriendProxy.FriendType.Apply then
  41. self._applyData[data.Name] = data
  42. end
  43. end
  44. -- 获取好友列表
  45. function FriendProxy:getFriendData()
  46. return self._friendData
  47. end
  48. -- 根据名字获取好友列表
  49. function FriendProxy:getFriendDataByUid(uid)
  50. return self._friendData[uid]
  51. end
  52. -- 根据name获取好友
  53. function FriendProxy:getFriendDataByUname(uname)
  54. return self._friendDataName[uname]
  55. end
  56. -- 从好友列表移除
  57. function FriendProxy:removeFriendData(uid)
  58. self._friendData[uid] = nil
  59. end
  60. -- 获取好友申请数据
  61. function FriendProxy:getApplyData()
  62. return self._applyData
  63. end
  64. -- 删除好友申请数据
  65. function FriendProxy:deleteApplyData(uname)
  66. self._applyData[uname] = nil
  67. end
  68. function FriendProxy:clearApplyData()
  69. self._applyData = {}
  70. end
  71. -- 获取黑名单列表
  72. function FriendProxy:getBlacklistData()
  73. return self._blacklistData
  74. end
  75. -- 判断是否是黑名单
  76. function FriendProxy:isInBlacklist(uid)
  77. if self._blacklistData[uid] then
  78. return true
  79. end
  80. return false
  81. end
  82. -- 根据id获取黑名单列表
  83. function FriendProxy:getBlacklistDataByUid(uid)
  84. return self._blacklistData[uid]
  85. end
  86. -- 根据玩家昵称获取黑名单
  87. function FriendProxy:getBlacklistDataByUName(uName)
  88. for _, v in pairs(self._blacklistData) do
  89. if v.Name == uName then
  90. return v
  91. end
  92. end
  93. return nil
  94. end
  95. -- 从黑名单列表移除
  96. function FriendProxy:removeBlockData(uid)
  97. self._blacklistData[uid] = nil
  98. end
  99. -- 请求好友列表
  100. function FriendProxy:RequestFriendList()
  101. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_LIST_REQUEST)
  102. end
  103. -- 请求黑名单列表
  104. function FriendProxy:requestFriendBlockList()
  105. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_BLACK_List_REQUEST)
  106. end
  107. -- 请求添加好友
  108. function FriendProxy:requestAddFriend(uid, uname)
  109. local cjson = require("cjson")
  110. local jsonData = cjson.encode({ UserId = uid, Name = uname })
  111. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_ADD_REQUEST, 0, 0, 0, 0, jsonData, string.len(jsonData))
  112. end
  113. -- 请求删除好友
  114. function FriendProxy:requestDelFriend(uid)
  115. local cjson = require("cjson")
  116. local jsonData = cjson.encode({ UserId = uid })
  117. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_DELETE_REQUEST, 0, 0, 0, 0, jsonData, string.len(jsonData))
  118. end
  119. -- 同意好友申请
  120. function FriendProxy:requestAgreeFriendApply(uname)
  121. local cjson = require("cjson")
  122. local jsonData = cjson.encode({ Name = uname })
  123. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_AGREE_REQUEST, 0, 0, 0, 0, jsonData, string.len(jsonData))
  124. end
  125. -- 请求添加黑名单
  126. function FriendProxy:requestAddBlackList(uid, uname)
  127. local cjson = require("cjson")
  128. local jsonData = cjson.encode({ UserId = uid, Name = uname })
  129. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_ADD_BLACK_List_REQUEST, 0, 0, 0, 0, jsonData, string.len(jsonData))
  130. end
  131. -- 请求移除黑名单玩家
  132. function FriendProxy:requestOutBlackList(uid)
  133. local cjson = require("cjson")
  134. local jsonData = cjson.encode({ UserId = uid })
  135. LuaSendMsg(global.MsgType.MSG_CS_FRIEND_OUT_BLACK_List_REQUEST, 0, 0, 0, 0, jsonData, string.len(jsonData))
  136. end
  137. -- 返回服务好友列表(请求/删除都会发送该消息)
  138. function FriendProxy:handle_MSG_SC_FRIEND_LIST_RESPONSE(msg)
  139. --[[
  140. Name
  141. UserId
  142. Job
  143. Online
  144. Level
  145. Guild
  146. ]]
  147. if msg:GetDataLength() <= 0 then
  148. return
  149. end
  150. local jsonData = ParseRawMsgToJson(msg)
  151. for name,_ in pairs(self._friendDataName) do
  152. if name then
  153. local isQuit = true
  154. for k,v in pairs(jsonData) do
  155. if v and v.Name and name == v.Name then
  156. isQuit = false
  157. break
  158. end
  159. end
  160. if isQuit then
  161. SLBridge:onLUAEvent(LUA_EVENT_REMFIREND, name)
  162. break
  163. end
  164. end
  165. end
  166. self._friendData = {}
  167. self._friendDataName = {}
  168. if not jsonData then
  169. SLBridge:onLUAEvent(LUA_EVENT_FRIEND_LIST_UPDATE)
  170. return
  171. end
  172. for _, v in pairs(jsonData) do
  173. self:addFriendData(v, FriendProxy.FriendType.Friend)
  174. end
  175. SLBridge:onLUAEvent(LUA_EVENT_FRIEND_LIST_UPDATE)
  176. end
  177. -- 添加好友结果
  178. function FriendProxy:handle_MSG_SC_FRIEND_ADD_RESPONSE(msg)
  179. local header = msg:GetHeader()
  180. if header.recog == 0 then
  181. -- 申请发送成功
  182. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(300000017))
  183. global.Facade:sendNotification(global.NoticeTable.Layer_AddFriend_Close)
  184. elseif header.recog == 1 then
  185. -- 收到好友添加申请
  186. if msg:GetDataLength() > 0 then
  187. local jsonData = ParseRawMsgToJson(msg)
  188. self:addFriendData(jsonData, FriendProxy.FriendType.Apply)
  189. local data = {}
  190. data.id = 5
  191. data.status = true
  192. data.callback = function()
  193. SL:OpenFriendApplyUI()
  194. end
  195. global.Facade:sendNotification(global.NoticeTable.BubbleTipsStatusChange, data)
  196. global.Facade:sendNotification(global.NoticeTable.Layer_FriendApply_Refresh)
  197. end
  198. elseif header.recog == -1 then
  199. -- 要申请添加的好友不在线
  200. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(300000016))
  201. end
  202. end
  203. -- 对方同意加好友
  204. function FriendProxy:handle_MSG_SC_FRIEND_OTHER_ADD_RESPONSE(msg)
  205. if msg:GetDataLength() <= 0 then
  206. return
  207. end
  208. local jsonData = ParseRawMsgToJson(msg)
  209. local userName = jsonData.Name
  210. global.Facade:sendNotification(global.NoticeTable.SystemTips, string.format(GET_STRING(300000018), userName))
  211. SLBridge:onLUAEvent(LUA_EVENT_ADDFIREND, userName)
  212. FriendProxy:RequestFriendList()
  213. end
  214. -- 黑名单列表返回
  215. function FriendProxy:handle_MSG_SC_FRIEND_BLACK_List_RESPONSE(msg)
  216. if msg:GetDataLength() <= 0 then
  217. return
  218. end
  219. local jsonData = ParseRawMsgToJson(msg)
  220. self._blacklistData = {}
  221. if not jsonData then
  222. SLBridge:onLUAEvent(LUA_EVENT_FRIEND_LIST_UPDATE)
  223. return
  224. end
  225. for _, v in pairs(jsonData) do
  226. self:addFriendData(v, FriendProxy.FriendType.Blacklist)
  227. end
  228. SLBridge:onLUAEvent(LUA_EVENT_FRIEND_LIST_UPDATE)
  229. end
  230. -- 加入黑名单结果
  231. function FriendProxy:handle_MSG_SC_FRIEND_ADD_BLACK_List_RESPONSE(msg)
  232. local header = msg:GetHeader()
  233. -- 失败:玩家不在线
  234. if header.recog == -1 then
  235. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(300000016))
  236. return
  237. end
  238. if msg:GetDataLength() <= 0 then
  239. return
  240. end
  241. local jsonData = ParseRawMsgToJson(msg)
  242. global.Facade:sendNotification(global.NoticeTable.SystemTips, string.format(GET_STRING(300000022), jsonData.Name))
  243. self:requestFriendBlockList()
  244. end
  245. function FriendProxy:RegisterMsgHandler()
  246. FriendProxy.super.RegisterMsgHandler(self)
  247. local msgType = global.MsgType
  248. LuaRegisterMsgHandler(msgType.MSG_SC_FRIEND_LIST_RESPONSE, handler(self, self.handle_MSG_SC_FRIEND_LIST_RESPONSE)) -- 好友列表
  249. LuaRegisterMsgHandler(msgType.MSG_SC_FRIEND_ADD_RESPONSE, handler(self, self.handle_MSG_SC_FRIEND_ADD_RESPONSE)) -- 添加好友结果
  250. LuaRegisterMsgHandler(msgType.MSG_SC_FRIEND_OTHER_ADD_RESPONSE, handler(self, self.handle_MSG_SC_FRIEND_OTHER_ADD_RESPONSE)) -- 对方同意加好友
  251. LuaRegisterMsgHandler(msgType.MSG_SC_FRIEND_BLACK_List_RESPONSE, handler(self, self.handle_MSG_SC_FRIEND_BLACK_List_RESPONSE)) -- 黑名单列表
  252. LuaRegisterMsgHandler(msgType.MSG_SC_FRIEND_ADD_BLACK_List_RESPONSE, handler(self, self.handle_MSG_SC_FRIEND_ADD_BLACK_List_RESPONSE)) -- 加入黑名单结果
  253. end
  254. return FriendProxy