You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

952 lines
28 KiB

  1. require("luaScript.Protocol.ProtocolCommon")
  2. local defs = require("pk_erdoudizhu.luaScript.Def_ERDouDiZhu")
  3. local Functions = require("pk_erdoudizhu.luaScript.Functions_ERDouDiZhu")
  4. local Cmds = require("pk_erdoudizhu.luaScript.Protocol.ProtocolCmd_ERDouDiZhu")
  5. local Messages = require("pk_erdoudizhu.luaScript.Protocol.ProtocolMessage_ERDouDiZhu")
  6. local Room = class("Room" , require("luaScript.Protocol.ProtocolRoomBase"))
  7. --队列初始化
  8. local callbackQueue = {}
  9. local callbackRunning = true
  10. function Room:ctor(net)
  11. Room.super.ctor(self , net)
  12. -- GPS信息发生变化的时候主动给服务器发消息
  13. app:addEventListener("onUpdateLoctionSuccessed", handler(self, self.onGpsChangeRequest))
  14. --发送
  15. -- 请求准备
  16. self:defMsgFunc{name = "callReadyRequest" , cmd = Cmds.GAME_COMMAND_USER_READY}
  17. -- 玩家请求解散房间,获取其他玩家回复解散房间
  18. self:defMsgFunc{name = "dismissRoomRequest", cmd = Cmds.GAME_COMMAND_DISBAND_GAME, sender = Messages.DismissRequest}
  19. -- 离开房间
  20. self:defMsgFunc{name = "callLeaveRequest" , cmd = Cmds.GAME_COMMAND_USER_LEAVE}
  21. -- 请求托管(取消or发送)
  22. self:defMsgFunc{name = 'HostingRequest', cmd = Cmds.GAME_COMMAND_USER_HOSTING_REQUEST_0x8134, sender = Messages.HostingRequest}
  23. --接收
  24. -- 玩家进入桌子成功
  25. self:defPushMsg{cmd = Cmds.GAME_COMMAND_LOGIN_GAME_SUCCESS, reader = Messages.SitDownSuccessResponse, func = handler(self , self.onSitDownSuccessResponse)}
  26. -- 玩家进入桌子失败
  27. self:defPushMsg{cmd = Cmds.GAME_COMMAND_LOGIN_GAME_ERR, reader = Messages.SitDownFailedResponse, func = handler(self , self.onSitDownFailedResponse)}
  28. -- 断线重连
  29. self:defPushMsg{cmd = Cmds.GAME_COMMAND_GAME_RECONNECTION, reader = Messages.GameReconnection, func = handler(self, self.onGameReconnection)}
  30. -- 用户退出
  31. self:defPushMsg{cmd = Cmds.GAME_COMMAND_LOGOUT_GAME_SUCCESS, reader = Messages.UserExitResponse, func = handler(self , self.onUserExitResponse)}
  32. -- 其他玩家进入桌子
  33. self:defPushMsg{cmd = Cmds.GAME_COMMAND_BROADCAST_USER_LOGIN, reader = Messages.OtherSitDownResponse, func = handler(self , self.onOtherSitDownResponse)}
  34. -- 其他玩家掉线
  35. self:defPushMsg{cmd = Cmds.GAME_COMMAND_BROADCAST_USER_DROPPED, reader = Messages.OtherDroppedResponse, func = handler(self , self.onOtherDroppedResponse)}
  36. -- 其他玩家退出房间
  37. self:defPushMsg{cmd = Cmds.GAME_COMMAND_BROADCAST_USER_LOGOUT, reader = Messages.OtherLogoutResponse, func = handler(self , self.onOtherLogoutResponse)}
  38. -- 收到服务器下发的玩家准备状态
  39. self:defPushMsg{cmd = Cmds.GAME_COMMAND_BROADCAST_USER_READY, reader = Messages.IntPacket, func = handler(self , self.onUserReadyResponse)}
  40. -- 游戏开始
  41. self:defPushMsg{cmd = Cmds.cmd_0x8104, reader = Messages.cmd_0x8104_RespStructure, func = handler(self , self.cmd_0x8104_Resp)}
  42. -- 更新游戏状态
  43. self:defPushMsg{cmd = Cmds.cmd_0x8105, reader = Messages.cmd_0x8105_RespStructure, func = handler(self , self.cmd_0x8105_Resp)}
  44. -- 发牌
  45. self:defPushMsg{cmd = Cmds.cmd_0x8106, reader = Messages.cmd_0x8106_RespStructure, func = handler(self , self.cmd_0x8106_Resp)}
  46. -- 通知玩家叫分
  47. self:defPushMsg{cmd = Cmds.cmd_0x8108, reader = Messages.cmd_0x8108_RespStructure, func = handler(self , self.cmd_0x8108_Resp)}
  48. -- 广播叫分结果
  49. self:defPushMsg{cmd = Cmds.cmd_0x8109, reader = Messages.cmd_0x8109_RespStructure, func = handler(self , self.cmd_0x8109_Resp)}
  50. -- 叫分结果,将底牌发给地主
  51. self:defPushMsg{cmd = Cmds.cmd_0x810a, reader = Messages.cmd_0x810a_RespStructure, func = handler(self , self.cmd_0x810a_Resp)}
  52. -- 通知加倍
  53. self:defPushMsg{cmd = Cmds.cmd_0x810c, reader = Messages.cmd_0x810c_RespStructure, func = handler(self , self.cmd_0x810c_Resp)}
  54. -- 广播玩家加倍结果
  55. self:defPushMsg{cmd = Cmds.cmd_0x810d, reader = Messages.cmd_0x810d_RespStructure, func = handler(self , self.cmd_0x810d_Resp)}
  56. -- 玩家出牌轮次广播
  57. self:defPushMsg{cmd = Cmds.cmd_0x810e, reader = Messages.cmd_0x810e_RespStructure, func = handler(self , self.cmd_0x810e_Resp)}
  58. -- 玩家出牌错误
  59. self:defPushMsg{cmd = Cmds.cmd_0x810f, reader = Messages.cmd_0x810f_RespStructure, func = handler(self , self.cmd_0x810f_Resp)}
  60. -- 玩家出牌成功
  61. self:defPushMsg{cmd = Cmds.cmd_0x8110, reader = Messages.cmd_0x8110_RespStructure, func = handler(self , self.cmd_0x8110_Resp)}
  62. -- 倍率发生变化
  63. self:defPushMsg{cmd = Cmds.cmd_0x8111, reader = Messages.cmd_0x8111_RespStructure, func = handler(self , self.cmd_0x8111_Resp)}
  64. -- 小局结算
  65. self:defPushMsg{cmd = Cmds.cmd_0x8112, reader = Messages.cmd_0x8112_RespStructure, func = handler(self , self.cmd_0x8112_Resp)}
  66. -- 总结算
  67. self:defPushMsg{cmd = Cmds.cmd_0x8113, reader = Messages.cmd_0x8113_RespStructure, func = handler(self , self.cmd_0x8113_Resp)}
  68. -- 无人叫分重新洗牌
  69. self:defPushMsg{cmd = Cmds.cmd_0x8114, reader = Messages.cmd_0x8114_RespStructure, func = handler(self , self.cmd_0x8114_Resp)}
  70. -- 玩家收到解散房间的请求
  71. self:defPushMsg{cmd = Cmds.GAME_COMMAND_BROADCAST_DISBAND_GAME, reader = Messages.DismissResponse, func = handler(self , self.onDismissResponse)}
  72. -- 收到玩家申请解散房间
  73. self:defPushMsg{cmd = Cmds.GAME_COMMAND_DISBAND_GAME, reader = Messages.DismissResult, func = handler(self , self.onUserDismissResultResponse)}
  74. -- 收到玩家申请立即开局
  75. self:defPushMsg{cmd = Cmds.GAME_COMMAND_RESPONSE_EARLY_START, reader = Messages.EarlyStartResult, func = handler(self , self.onQuickStartResponse)}
  76. -- 收到托管回调
  77. self:defPushMsg{cmd = Cmds.GAME_COMMAND_USER_HOSTING_RESPONSE_0x8133, reader = Messages.HostingResponse, func = handler(self,self.onHostingResponse)}
  78. -- 通知抢地主
  79. self:defPushMsg{cmd = Cmds.cmd_0x8123, reader = Messages.cmd_0x8123_RespStructure, func = handler(self , self.cmd_0x8123_Resp)}
  80. -- 抢地主结果
  81. self:defPushMsg{cmd = Cmds.cmd_0x8124, reader = Messages.cmd_0x8124_RespStructure, func = handler(self , self.cmd_0x8124_Resp)}
  82. -- 通知让牌
  83. self:defPushMsg{cmd = Cmds.cmd_0x8121, reader = Messages.cmd_0x8121_RespStructure, func = handler(self , self.cmd_0x8121_Resp)}
  84. -- 让牌结果
  85. self:defPushMsg{cmd = Cmds.cmd_0x8122, reader = Messages.cmd_0x8122_RespStructure, func = handler(self , self.cmd_0x8122_Resp)}
  86. end
  87. -- 无人叫分重新洗牌
  88. function Room:cmd_0x8114_Resp(status, response)
  89. if not self.roomInfo then
  90. logD("self.roomInfo no exist,容错处理!")
  91. return
  92. end
  93. logD("Room:cmd_0x8114_Resp(), ", table.tostring(response))
  94. -- 发送广播通知
  95. self:dispatchEvent({name = "cmd_0x8114_Resp", response = response})
  96. end
  97. -- 总结算
  98. function Room:cmd_0x8113_Resp(status, response)
  99. if not self.roomInfo then
  100. logD("self.roomInfo no exist,容错处理!")
  101. return
  102. end
  103. logD("Room:cmd_0x8113_Resp(), ", table.tostring(response))
  104. self.roomInfo.isGameOver = 1
  105. self.roomInfo._GameOverInfo = response
  106. self.roomInfo._stopFlag = response.stopFlag
  107. -- 发送广播通知
  108. self:dispatchEvent({name = "cmd_0x8113_Resp", response = response})
  109. end
  110. -- 小局结算
  111. function Room:cmd_0x8112_Resp(status, response)
  112. if not self.roomInfo then
  113. logD("self.roomInfo no exist,容错处理!")
  114. return
  115. end
  116. logD("Room:cmd_0x8112_Resp(), ", table.tostring(response))
  117. self.roomInfo.isGameOver = response.isGameOver
  118. for _,v in pairs(response.memberList) do
  119. self.roomInfo.memberList[v.nUserId].nTotalScore = v.nTotalScore
  120. end
  121. for _, v in pairs(response.memberOperateData or {}) do
  122. response.memberList[v.nUserId].ext = v.ext -- 强制把操作数据添加到memeberlist里面,因为小结算直接用的发来的repsonse数据而不是self.roomInfo.memberList
  123. end
  124. -- 发送广播通知
  125. self:dispatchEvent({name = "cmd_0x8112_Resp", response = response})
  126. end
  127. -- 倍率发生变化
  128. function Room:cmd_0x8111_Resp(status, response)
  129. if not self.roomInfo then
  130. logD("self.roomInfo no exist,容错处理!")
  131. return
  132. end
  133. logD("Room:cmd_0x8111_Resp(), ", table.tostring(response))
  134. self.roomInfo.nBombs = response.nBombs
  135. self.roomInfo.curAskLoard = response.baseScore
  136. self.roomInfo.nMult = response.nMult
  137. self.roomInfo.nRang = response.nRang
  138. self.roomInfo.diType = response.diType
  139. -- 发送广播通知
  140. self:dispatchEvent({name = "cmd_0x8111_Resp", response = response})
  141. end
  142. -- 玩家出牌成功
  143. function Room:cmd_0x8110_Resp(status, response)
  144. if not self.roomInfo then
  145. logD("self.roomInfo no exist,容错处理!")
  146. return
  147. end
  148. logD("Room:cmd_0x8110_Resp(), ", table.tostring(response))
  149. -- 发送广播通知
  150. self:dispatchEvent({name = "cmd_0x8110_Resp", response = response})
  151. end
  152. -- 玩家出牌错误
  153. function Room:cmd_0x810f_Resp(status, response)
  154. if not self.roomInfo then
  155. logD("self.roomInfo no exist,容错处理!")
  156. return
  157. end
  158. logD("Room:cmd_0x810f_Resp(), ", table.tostring(response))
  159. -- 发送广播通知
  160. self:dispatchEvent({name = "cmd_0x810f_Resp", response = response})
  161. end
  162. -- 玩家出牌轮次广播
  163. function Room:cmd_0x810e_Resp(status, response)
  164. if not self.roomInfo then
  165. logD("self.roomInfo no exist,容错处理!")
  166. return
  167. end
  168. logD("Room:cmd_0x810e_Resp(), ", table.tostring(response))
  169. -- 发送广播通知
  170. self:dispatchEvent({name = "cmd_0x810e_Resp", response = response})
  171. end
  172. -- 广播玩家加倍结果
  173. function Room:cmd_0x810d_Resp(status, response)
  174. if not self.roomInfo then
  175. logD("self.roomInfo no exist,容错处理!")
  176. return
  177. end
  178. logD("Room:cmd_0x810d_Resp(), ", table.tostring(response))
  179. self.roomInfo.memberList[response.nUserId].askBei = response.askBei
  180. -- 发送广播通知
  181. self:dispatchEvent({name = "cmd_0x810d_Resp", response = response})
  182. end
  183. -- 通知加倍
  184. function Room:cmd_0x810c_Resp(status, response)
  185. if not self.roomInfo then
  186. logD("self.roomInfo no exist,容错处理!")
  187. return
  188. end
  189. logD("Room:cmd_0x810c_Resp(), ", table.tostring(response))
  190. -- 发送广播通知
  191. self:dispatchEvent({name = "cmd_0x810c_Resp", response = response})
  192. end
  193. -- 叫分结果,将底牌发给地主
  194. function Room:cmd_0x810a_Resp(status, response)
  195. if not self.roomInfo then
  196. logD("self.roomInfo no exist,容错处理!")
  197. return
  198. end
  199. logD("Room:cmd_0x810a_Resp(), ", table.tostring(response))
  200. -- 这里有设置了庄家id
  201. for k,v in pairs(response.Datas) do
  202. self.roomInfo[k] = v
  203. end
  204. -- 发送广播通知
  205. self:dispatchEvent({name = "cmd_0x810a_Resp", response = response})
  206. end
  207. -- 通知玩家叫分
  208. function Room:cmd_0x8108_Resp(status, response)
  209. if not self.roomInfo then
  210. logD("self.roomInfo no exist,容错处理!")
  211. return
  212. end
  213. logD("Room:cmd_0x8108_Resp(), ", table.tostring(response))
  214. self.roomInfo.memberList[response.nUserId].askLoard = response.askLoard
  215. -- 发送广播通知
  216. self:dispatchEvent({name = "cmd_0x8108_Resp", response = response})
  217. end
  218. -- 发牌
  219. function Room:cmd_0x8106_Resp(status, response)
  220. if not self.roomInfo then
  221. logD("self.roomInfo no exist,容错处理!")
  222. return
  223. end
  224. logD("Room:cmd_0x8106_Resp(), ", table.tostring(response))
  225. self.roomInfo.memberList[response.nUserId].handCards = response.handCards
  226. -- 发送广播通知
  227. self:dispatchEvent({name = "cmd_0x8106_Resp", response = response})
  228. end
  229. -- 更新游戏状态
  230. function Room:cmd_0x8105_Resp(status, response)
  231. if not self.roomInfo then
  232. logD("self.roomInfo no exist,容错处理!")
  233. return
  234. end
  235. logD("Room:cmd_0x8105_Resp(), ", table.tostring(response))
  236. self.roomInfo.nStatus = response.nStatus
  237. -- 发送广播通知
  238. self:dispatchEvent({name = "cmd_0x8105_Resp", response = response})
  239. end
  240. -- 游戏开始
  241. function Room:cmd_0x8104_Resp(status, response)
  242. if not self.roomInfo then
  243. logD("self.roomInfo no exist,容错处理!")
  244. return
  245. end
  246. logD("Room:cmd_0x8104_Resp(), ", table.tostring(response))
  247. for k,v in pairs(response.Datas) do
  248. self.roomInfo[k] = v
  249. end
  250. --重置准备状态
  251. for _,v in pairs(self.roomInfo.memberList) do
  252. v.nPlayerFlag = 0
  253. end
  254. --重置叫分
  255. self.roomInfo.curAskLoard = 0
  256. -- 重置倍数
  257. self.roomInfo.nMult = 1
  258. -- 重置让牌
  259. self.roomInfo.nRang = 0
  260. -- 重置底牌类型
  261. self.roomInfo.diType = 0
  262. -- 发送广播通知
  263. self:dispatchEvent({name = "cmd_0x8104_Resp", response = response})
  264. end
  265. -- 广播叫分结果
  266. function Room:cmd_0x8109_Resp(status, response)
  267. if not self.roomInfo then
  268. logD("self.roomInfo no exist,容错处理!")
  269. return
  270. end
  271. logD("Room:cmd_0x8109_Resp(), ", table.tostring(response))
  272. if response.askLoard ~= 0 then
  273. self.roomInfo.curAskLoard = response.askLoard
  274. end
  275. -- 发送广播通知
  276. self:dispatchEvent({name = "cmd_0x8109_Resp", response = response})
  277. end
  278. -- 通知抢地主
  279. function Room:cmd_0x8123_Resp(status, response)
  280. if not self.roomInfo then
  281. logD("self.roomInfo no exist,容错处理!")
  282. return
  283. end
  284. logD("Room:cmd_0x8123_Resp(), ", table.tostring(response))
  285. -- 发送广播通知
  286. self:dispatchEvent({name = "cmd_0x8123_Resp", response = response})
  287. end
  288. -- 抢地主结果
  289. function Room:cmd_0x8124_Resp(status, response)
  290. if not self.roomInfo then
  291. logD("self.roomInfo no exist,容错处理!")
  292. return
  293. end
  294. logD("Room:cmd_0x8124_Resp(), ", table.tostring(response))
  295. self.roomInfo.memberList[response.nUserId].qiang = response.opType
  296. -- 发送广播通知
  297. self:dispatchEvent({name = "cmd_0x8124_Resp", response = response})
  298. end
  299. -- 通知让牌
  300. function Room:cmd_0x8121_Resp(status, response)
  301. if not self.roomInfo then
  302. logD("self.roomInfo no exist,容错处理!")
  303. return
  304. end
  305. logD("Room:cmd_0x8121_Resp(), ", table.tostring(response))
  306. -- 发送广播通知
  307. self:dispatchEvent({name = "cmd_0x8121_Resp", response = response})
  308. end
  309. -- 让牌结果
  310. function Room:cmd_0x8122_Resp(status, response)
  311. if not self.roomInfo then
  312. logD("self.roomInfo no exist,容错处理!")
  313. return
  314. end
  315. logD("Room:cmd_0x8122_Resp(), ", table.tostring(response))
  316. self.roomInfo.memberList[response.nUserId].askRangNum = tonumber(response.opType) -- 选择的让牌数
  317. self.roomInfo.nRangTotalNums = tonumber(response.nRangNums) -- 总让牌数
  318. -- 发送广播通知
  319. self:dispatchEvent({name = "cmd_0x8122_Resp", response = response})
  320. end
  321. --8012
  322. function Room:onDismissResponse(status, response)
  323. --[[-- nUserId
  324. , defVar("nUserId", VT_Int, 0)
  325. -- 1: 表示发起解散 2:同意解散 3:不同意解散
  326. , defVar("operateType", VT_UChar, 0)
  327. -- 剩余解散超时时间
  328. , defVar("timeLeft", VT_Short, 0) --]]
  329. if not self.roomInfo then
  330. logD("self.roomInfo no exist,容错处理!")
  331. return
  332. end
  333. local nUserId = response.nUserId
  334. local operateType = response.operateType
  335. -- 收到玩家发起请求时,数据重置
  336. if response.operateType == 1 then
  337. self:resetDismissData()
  338. end
  339. --数据记录
  340. self.dismissInfo[nUserId] = operateType
  341. self:dispatchEvent({name = "onDismissResponse", response = response})
  342. end
  343. -- 发起请求回复 8011
  344. function Room:onUserDismissResultResponse(status, response)
  345. logD("User:onUserDismissResultResponse, ", table.tostring(response))
  346. if not self.roomInfo then
  347. logD("self.roomInfo no exist,容错处理!")
  348. return
  349. end
  350. --[[
  351. -- 0:成功
  352. -- 1:operateType取值范围不对
  353. -- 2:当前没人发起解散
  354. -- 3:已经有人申请解散
  355. , defVar("errorCode", VT_Short, 0)
  356. -- 玩家操作
  357. , defVar("operateType", VT_Int, 0)
  358. -- 桌子上其他用户数量
  359. , defVar("memberList", VT_VectorToMap(UserDismissResult, "nUserId"))
  360. -- 剩余解散超时时间
  361. , defVar("timeLeft", VT_Short, 0) --]]
  362. logD("30000000--runUserDismissResultResponse")
  363. if response.errorCode == 1 then
  364. showTooltip("operateType取值范围不对")
  365. elseif response.errorCode == 2 then
  366. showTooltip("当前没人发起解散")
  367. elseif response.errorCode == 3 then
  368. showTooltip("已经有人申请解散")
  369. elseif response.errorCode == 4 then
  370. showTooltip("用户已经操作过")
  371. else
  372. -- 其他玩家的解散状态
  373. for uid, value in pairs(response.memberList) do
  374. self.dismissInfo[uid] = value.dismissState
  375. end
  376. self:dispatchEvent({name = "onDismissResponse", response = response})
  377. end
  378. end
  379. -- 服务器下发玩家GPS数据发生变化
  380. function Room:onGpsChangeResponse(status, response)
  381. print("Room:onGpsChangeResponse()", table.tostring(response))
  382. --[[
  383. --server收到0x8060协议后,广播当前桌子上所有用户
  384. ServerChangeUserInfo = defClass("ServerChangeUserInfo"
  385. --更新信息的用户uid
  386. , defVar("uid", VT_Int, -1)
  387. --用户信息
  388. , defVar("userInfo", VT_String, "")
  389. --保留字段,暂不使用
  390. , defVar("reserved", VT_String, "")
  391. )
  392. --]]
  393. print("Room:onGpsChangeResponse()", table.tostring(response))
  394. local nUserId = response.uid
  395. local memberInfo = self.roomInfo.memberList[nUserId]
  396. if memberInfo then
  397. memberInfo.userInfo = response.userInfo
  398. end
  399. self:dispatchEvent({name = "onGpsChangeResponse", nUserId = nUserId})
  400. end
  401. --辅助查找
  402. -- 通过玩家逻辑椅子号找到玩家ID
  403. function Room:getUserIdBySeatId(seatId)
  404. return self.seatList[seatId]
  405. end
  406. -- 通过玩家视图椅子号找到玩家ID
  407. function Room:getUserIdByViewId(seatShowId)
  408. return self.seatShowList[seatShowId]
  409. end
  410. -- 通过玩家逻辑椅子号找到视图椅子号
  411. function Room:getViewIdBySeatId(nSeatId)
  412. local userid = self:getUserIdBySeatId(nSeatId)
  413. return self:getViewIdByUserId(userid)
  414. end
  415. -- 通过玩家视图椅子号找到逻辑椅子号
  416. function Room:getSeatIdByViewId(nViewId)
  417. local userid = self:getUserIdByViewId(nViewId)
  418. for seatId,nUserId in pairs(self.seatList) do
  419. if userid == nUserId then
  420. return seatId
  421. end
  422. end
  423. end
  424. -- return table of userInfo, not string
  425. function Room:getUserInfo(nUserId)
  426. if self.roomInfo.memberList[nUserId] then
  427. return json.decode(self.roomInfo.memberList[nUserId].userInfo)
  428. else
  429. return nil
  430. end
  431. end
  432. -- 发起请求立即开局
  433. function Room:requestQuickStart(opType)
  434. --0初始状态 1申请开始游戏 2同意开始游戏 3不同意开始游戏
  435. local request = Messages.EarlyStartRequest:new()
  436. request.optType = opType
  437. self:sendResponse{cmd = Cmds.GAME_COMMAND_REQUEST_EARLY_START , sender = request}
  438. end
  439. -- 立即开局结果
  440. function Room:onQuickStartResponse(status, response)
  441. if not self.roomInfo then
  442. logD("self.roomInfo no exist,容错处理!")
  443. return
  444. end
  445. self.nleftStartGameTimeout = response.nleftStartGameTimeout
  446. self.quickStartInfo = {}
  447. for i,v in pairs(response.earlyStartPlayerInfo) do
  448. self.quickStartInfo[v.nUserId] = v.optType
  449. end
  450. self:dispatchEvent({name = "onQuickStartResponse", response = response})
  451. end
  452. -- 托管通知(回复)
  453. function Room:onHostingResponse(status, response)
  454. logD("Room:onHostingResponse 0x8133()", table.tostring(response))
  455. local userList = response.userList
  456. for k, v in ipairs(userList) do
  457. -- 保留状态,为了下一局
  458. self.roomInfo.memberList[v.nUserId].AIStatus = v.status or 0
  459. -- 是否托管
  460. -- if v.nUserId == self:getMyUserId() then
  461. -- self.roomInfo.hosting = v.status
  462. -- end
  463. self:dispatchEvent({name = 'onHostingResponse', response = {nUserId = v.nUserId, status = v.status}})
  464. end
  465. end
  466. function Room:resetDismissData()
  467. --玩家解散数据(userId 为Key,解散类型为value)
  468. self.dismissInfo = {}
  469. --解散总时间
  470. self.roomInfo.nDismissToTalTime = nil
  471. -- 解散倒计时
  472. self.roomInfo.nDismissStateTime = nil
  473. -- 解散显示
  474. self.roomInfo.nShowDismiss = false
  475. end
  476. function Room:initRoomInfo()
  477. self.roomInfo = {}
  478. self.seatShowList = {}
  479. self:resetDismissData()
  480. --手牌数据
  481. self.cards = {}
  482. end
  483. -- 通知服务器玩家GPS数据发生变化
  484. function Room:onGpsChangeRequest()
  485. print("Room:onUserInfoChangeRequest()")
  486. local request = StringPacket:new()
  487. request.stringValue = app.user.userInfo or ""
  488. logD("Room:onGpsChangeRequest()", table.tostring(request))
  489. self:sendResponse{cmd = Cmds.GAME_COMMAND_CLIENT_CHANGE_USERINFO , sender = request}
  490. end
  491. -- 玩家进入桌子成功
  492. function Room:onSitDownSuccessResponse(status, response)
  493. logD("--------------------玩家进入桌子成功--------------------")
  494. logD("Room:onSitDownSuccessResponse(), ", table.tostring(response))
  495. app.net:onMsgClear()
  496. app.net:onMsgPause()
  497. -- 记录当前桌子号
  498. self:initRoomInfo()
  499. -- 获取到的房间信息
  500. for k,v in pairs(response.Datas) do
  501. self.roomInfo[k] = v
  502. end
  503. --设置默认数据
  504. self.roomInfo.nGameStartCount = self.roomInfo.nGameStartCount or 0
  505. local gameinfo = json.decode(self.roomInfo.strGameInfo)
  506. self.roomInfo.gameinfo = gameinfo
  507. --俱乐部标志
  508. self.roomInfo.clubId = gameinfo.clubId
  509. -- 更新椅子号
  510. self:updateUserSeateShowId()
  511. logD("Room:onSitDownSuccessResponse() roomInfo = ", table.tostring(self.roomInfo))
  512. -- 发送通知
  513. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = GAME_IDS.ERDouDiZhu, gameType = gameinfo.gamerule})
  514. end
  515. -- 玩家进入桌子失败
  516. function Room:onSitDownFailedResponse(status, response)
  517. logD("--------------------玩家进入桌子失败--------------------")
  518. app.waitDialogManager:closeWaitNetworkDialog()
  519. local errorCode = response.nErrorCode
  520. local errorString = ENTER_ROOM_RET_STR[errorCode] or string.format("坐下失败 errorCode = %s", errorCode)
  521. showTooltip(errorString)
  522. end
  523. function Room:getMyRecordUserId()
  524. local myUserId = app.user.loginInfo.uid
  525. local minID = 0
  526. local isFind = false
  527. for k,v in pairsByKeys(self.roomInfo.memberList) do
  528. if myUserId == v.nUserId then
  529. return myUserId
  530. end
  531. if not isFind then
  532. minID = v.nUserId
  533. isFind = true
  534. end
  535. end
  536. return minID
  537. end
  538. -- 检查此桌是否有此ID玩家
  539. function Room:checkUserIDIsInTable(nUserId)
  540. if self.roomInfo.memberList then
  541. local memberInfo = self.roomInfo.memberList[nUserId]
  542. if memberInfo then
  543. return true
  544. else
  545. return false
  546. end
  547. else
  548. return false
  549. end
  550. end
  551. -- 获取桌子上实际人数
  552. function Room:getActualPlayerNum()
  553. local playernum = 0
  554. for i,v in pairs(self.roomInfo.memberList) do
  555. if self:checkUserIDIsInTable(i) then
  556. playernum = playernum + 1
  557. end
  558. end
  559. return playernum
  560. end
  561. -- 更新玩家的相对椅子号
  562. function Room:updateUserSeateShowId()
  563. local myUserId = self:getMyRecordUserId()
  564. local mySeatId = self.roomInfo.memberList[myUserId].nSeatId
  565. self.roomInfo.nUserId = myUserId
  566. self.roomInfo.nSeatId = mySeatId
  567. local serverSeatToViewSeat = {
  568. [4] = {
  569. [0] = 4,
  570. [1] = 1,
  571. [2] = 2,
  572. [3] = 3,
  573. },
  574. [3] = {
  575. [0] = 4,
  576. [1] = 1,
  577. [2] = 3,
  578. },
  579. [2] = {
  580. [0] = 4,
  581. [1] = 2,
  582. }
  583. }
  584. self.seatList = {}
  585. self.userList = {}
  586. self.seatShowList = {}
  587. for k,v in pairs(self.roomInfo.memberList) do
  588. local relativeSeat = (v.nSeatId - mySeatId + self.roomInfo.nMaxPlayCount) % self.roomInfo.nMaxPlayCount
  589. local nSeatShowId = serverSeatToViewSeat[self.roomInfo.nMaxPlayCount][relativeSeat]
  590. self.seatShowList[nSeatShowId] = v.nUserId
  591. self.userList[v.nUserId] = nSeatShowId
  592. self.seatList[v.nSeatId] = v.nUserId
  593. end
  594. logD("User:updateUserSeateShowId(),seatShowList", table.tostring(self.seatShowList))
  595. logD("User:updateUserSeateShowId(),seatList ", table.tostring(self.seatList))
  596. logD("User:updateUserSeateShowId(),userList ", table.tostring(self.userList))
  597. end
  598. --断线重连
  599. function Room:onGameReconnection(status,response)
  600. logD("===============重连成功,清空之前所有存在的队列==================")
  601. app.net:onMsgClear()
  602. app.net:onMsgPause()
  603. logD(" = ", table.tostring(response))
  604. self:initRoomInfo()
  605. -- 获取到的房间信息
  606. for k,v in pairs(response.Datas) do
  607. self.roomInfo[k] = v
  608. end
  609. local gameinfo = json.decode(self.roomInfo.strGameInfo)
  610. self.roomInfo.gameinfo = gameinfo
  611. --俱乐部标志
  612. self.roomInfo.clubId = gameinfo.clubId
  613. --当前叫分
  614. self.roomInfo.curAskLoard = 0
  615. for k,v in pairs(self.roomInfo.memberList) do
  616. --最大叫分
  617. if v.askLoard > self.roomInfo.curAskLoard then
  618. self.roomInfo.curAskLoard = v.askLoard
  619. end
  620. --加倍状态
  621. v.askBei = v.bAskBei == 1 and v.askBei == 2 and 1 or 0
  622. end
  623. -- 将过数据插入到member数据内
  624. for i, v in pairs(response.Datas.guoInfo) do
  625. if self.roomInfo.memberList[i] then
  626. self.roomInfo.memberList[i].nGuo = v.nGuo
  627. end
  628. end
  629. -- 更新椅子号
  630. self:updateUserSeateShowId()
  631. -- 如果处于叫分阶段,移除地主ID
  632. if self.roomInfo.nStatus == defs.GAME_STATUS.ASK_LOARD then
  633. self.roomInfo.nBankerId = 0
  634. end
  635. --小局结算
  636. if self.roomInfo.nStatus == defs.GAME_STATUS.ROUND_OVER then
  637. local myMemberInfo = self.roomInfo.memberList[self:getMyUserId()]
  638. --已经准备,则重置一些状态
  639. if myMemberInfo.nPlayerFlag == 1 then
  640. --庄家
  641. self.roomInfo.nBankerId = 0
  642. --玩家数据
  643. for k,v in pairs(self.roomInfo.memberList) do
  644. --加倍状态
  645. v.askBei = 0
  646. --手牌
  647. v.handCards.Datas = {}
  648. end
  649. end
  650. end
  651. -- 发送通知
  652. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = GAME_IDS.ERDouDiZhu, gameType = gameinfo.gamerule})
  653. end
  654. -- 通过玩家ID获取玩家展示的座位号
  655. function Room:getViewIdByUserId(nUserId)
  656. return self.userList[nUserId]
  657. end
  658. -- 我发起请求解散房间
  659. function Room:requestDismissRoom(opType)
  660. --1: 表示发起解散 2:同意解散 3:不同意解散
  661. local request = Messages.DismissRequest:new()
  662. request.operateType = opType
  663. self:sendResponse{cmd = Cmds.GAME_COMMAND_DISBAND_GAME , sender = request}
  664. end
  665. function Room:onUserExitResponse(status, response)
  666. logD("Room:onUserExitResponse(), ", table.tostring(response))
  667. if not self.roomInfo then
  668. logD("self.roomInfo no exist, 容错处理!")
  669. return
  670. end
  671. local ttExtInfo = string.split(response.userInfo, ",")
  672. app.user.loginInfo.historyCardNum = ttExtInfo[1]
  673. app.user.loginInfo.curCardNum = ttExtInfo[2]
  674. self:dispatchEvent({name = "onUserExitResponseRoom",response = response})
  675. end
  676. -- 其他玩家进入桌子
  677. function Room:onOtherSitDownResponse(status, response)
  678. logD("Room:onOtherSitDownResponse(), response = ", table.tostring(response))
  679. if not self.roomInfo then
  680. logD("self.roomInfo no exist,容错处理!")
  681. return
  682. end
  683. self.roomInfo.memberList[response.nUserId] = response
  684. -- 更新椅子号
  685. self:updateUserSeateShowId()
  686. -- 发送广播通知,某个位置有人坐下了
  687. self:dispatchEvent({name = "onOtherSitDownResponse", response = response})
  688. end
  689. -- 其他玩家是否掉线
  690. function Room:onOtherDroppedResponse(status, response)
  691. logD("User:onOtherDroppedResponse(), response = ", table.tostring(response))
  692. local memberInfo = self.roomInfo.memberList[response.nUserId]
  693. if memberInfo then
  694. memberInfo.nOnlineStatus = response.nOnlineStatus
  695. end
  696. self:dispatchEvent({name = "onOtherDroppedResponse", response = response})
  697. end
  698. -- 其他玩家退出房间
  699. function Room:onOtherLogoutResponse(status, response)
  700. logD("Room:onOtherLogoutResponse(), response = ", table.tostring(response))
  701. if not self.roomInfo then
  702. logD("self.roomInfo no exist,容错处理!")
  703. return
  704. end
  705. --已开局玩家不可退出(不做处理)
  706. if (self.roomInfo.nGameStartCount or 0) > 0 then
  707. logD("onOtherLogoutResponse 已开局玩家不可退出")
  708. return
  709. end
  710. response.viewId = self:getViewIdByUserId(response.nUserId)
  711. self.roomInfo.memberList[response.nUserId] = nil
  712. self:dispatchEvent({name = "onOtherLogoutResponse", response = response})
  713. end
  714. -- 服务器下发玩家的准备状态
  715. function Room:onUserReadyResponse(status, response)
  716. logD("Room:onUserReadyResponse(), ", table.tostring(response))
  717. if not self.roomInfo then
  718. logD("self.roomInfo no exist,容错处理!")
  719. return
  720. end
  721. if response.nUserId == self:getMyUserId() then
  722. --重置庄家
  723. self.roomInfo.nBankerId = 0
  724. --重置炸弹数量
  725. self.roomInfo.nBombs = 0
  726. --重置加倍
  727. for k,v in pairs(self.roomInfo.memberList) do
  728. v.askBei = 0
  729. end
  730. end
  731. --标记准备了
  732. self.roomInfo.memberList[response.nUserId].nPlayerFlag = 1
  733. self:dispatchEvent({name = "onUserReadyResponse", response = response})
  734. end
  735. function Room:getMyUserId()
  736. return self:getUserIdByViewId(defs.VIEW_ID.MY_VIEW_ID)
  737. end
  738. function Room:sendAskLoard(askLoard)
  739. local request = Messages.AskLoard:new()
  740. request.askLoard = askLoard
  741. self:sendResponse{cmd = Cmds.cmd_0x8109 , sender = request}
  742. end
  743. function Room:sendAskBei(askBei)
  744. local request = Messages.AskBei:new()
  745. request.askBei = askBei
  746. self:sendResponse{cmd = Cmds.cmd_0x810d , sender = request}
  747. end
  748. function Room:sendOperation(data)
  749. local request = Messages.Operation:new()
  750. request.opType = data.opType
  751. for _,v in ipairs(data.cards) do
  752. table.insert(request.cards.Datas, v)
  753. end
  754. self:sendResponse{cmd = Cmds.cmd_0x810f , sender = request}
  755. end
  756. function Room:sendQiang(qiang)
  757. local request = Messages.Qiang:new()
  758. request.qiang = qiang
  759. self:sendResponse{cmd = Cmds.cmd_0x8124 , sender = request}
  760. end
  761. function Room:sendRang(rang)
  762. local request = Messages.Rang:new()
  763. request.rang = rang
  764. self:sendResponse{cmd = Cmds.cmd_0x8122 , sender = request}
  765. end
  766. return Room