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.

1317 lines
46 KiB

  1. local ZPDef = ZPFramework.ZPImport("zp_base.luaScript.ZPDef")
  2. local ZPCmd=ZPFramework.ZPImport("zp_base.luaScript.Protocol.ZPCmd")
  3. local ZPMessage=ZPFramework.ZPImport("zp_base.luaScript.Protocol.ZPMessage")
  4. local ZPFuc = ZPFramework.ZPImport("zp_base.luaScript.ZPFunctions")
  5. local Room = class("Room" , require("luaScript.Protocol.ProtocolRoomBase"))
  6. function Room:initRoomInfo()
  7. -- 房间信息
  8. self.roomInfo = ZPMessage.RoomInfoLocal:new()
  9. self.seatShowList = {}
  10. --记录离线uid
  11. self.offLineUid = {}
  12. --记录离线时间
  13. self.offLineTime = {}
  14. self:resetDismissData()
  15. --手牌数据
  16. self.cards = {}
  17. self.bGameStart = false
  18. end
  19. function Room:resetDismissData()
  20. --玩家解散数据(userId 为Key,解散类型为value)
  21. self.dismissInfo = {}
  22. --解散总时间
  23. self.roomInfo.nDismissToTalTime = nil
  24. -- 解散倒计时
  25. self.roomInfo.nDismissStateTime = nil
  26. -- 解散显示
  27. self.roomInfo.nShowDismiss = false
  28. end
  29. -- 游戏正式开始
  30. function Room:onGameStartResponse(status, response)
  31. logD("-------------------- 新的一局开始了 ------------------------")
  32. if not self.roomInfo then
  33. logD("self.roomInfo no exist,容错处理!")
  34. return
  35. end
  36. logD("Room:onGameStartResponse(), ", table.tostring(response))
  37. self.roomInfo.nGameStartCount = response.nGameStartCount;
  38. self.roomInfo.nTotalGameNum = response.nTotalGameNum;
  39. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid;
  40. self.roomInfo.nBankSeatId = response.nBankSeatId;
  41. self.roomInfo.xiaoSeatId = response.xiaoSeatId;
  42. --清空上一局胡牌信息
  43. self.roomInfo.hupaiInfo = nil
  44. -- 清空上一局的手牌信息
  45. self.cards = {}
  46. -- 发送广播通知,游戏开始了
  47. self:dispatchEvent({name = "onGameStartResponse"});
  48. end
  49. -- 游戏发牌结果
  50. function Room:onGameSendCardResponse(status, response)
  51. logD("-------------------- 游戏发牌 ------------------------")
  52. if not self.roomInfo then
  53. logD("self.roomInfo no exist,容错处理!")
  54. return
  55. end
  56. logD("Room:onGameSendCardResponse(), ", table.tostring(response))
  57. self.roomInfo.nbankFirstGrabCard = response.nbankFirstGrabCard
  58. --目前server只发自己过来,后续战绩回放考虑到会发所有玩家来,故以userid为key于扩展
  59. local cardData = self:serverCardToMyCard(response.cardList)
  60. logD("Room:onGameSendCardResponse : "..table.tostring(cardData))
  61. self.roomInfo.memberList[response.nUserId].cardList = cardData
  62. self.roomInfo.nbankFirstGrabCard = response.nbankFirstGrabCard
  63. self.cards[response.nUserId] = cardData
  64. if self:isLittleFamily() then
  65. self.roomInfo.nBankUid = response.nUserId
  66. self.cards[app.user.loginInfo.uid] = cardData
  67. end
  68. -- 发送广播通知,发牌开始了
  69. self:dispatchEvent({name = "onGameSendCardResponse"});
  70. end
  71. -- 系统发牌后发来的自动提操作
  72. function Room:onSystemOperationStart(status, response)
  73. logD("-------------------- 游戏提结果 ------------------------")
  74. if not self.roomInfo then
  75. logD("self.roomInfo no exist,容错处理!")
  76. return
  77. end
  78. logD("Room:onSystemOperationStart(), ", table.tostring(response))
  79. -- 发送广播通知,游戏开始了
  80. self:dispatchEvent({name = "onSystemOperationStart",response = response});
  81. end
  82. -- 广播桌子上所有玩家庄家起手操作
  83. --[[若庄家可以胡牌, 则客户端显示胡牌, 过两个按钮,若不能胡牌,则判断用户是否要出牌,
  84. 若能出牌,则显示出牌提示,若庄家既不能胡牌,也不能出牌,客户端无需处理,
  85. 此种情况下server会自动跳转到下家抓牌, 强制胡牌的情况下必胡
  86. ]]
  87. function Room:onBankerOutCard(status, response)
  88. logD("-------------------- 游戏庄家起手操作------------------------")
  89. if not self.roomInfo then
  90. logD("self.roomInfo no exist,容错处理!")
  91. return
  92. end
  93. self.roomInfo.nBankUid = response.nUserId
  94. logD("Room:onBankerOutCard(), ", table.tostring(response))
  95. -- 发送广播通知,游戏开始了
  96. self:dispatchEvent({name = "onBankerOutCard",response = response});
  97. end
  98. function Room:onMoPai(status,response)
  99. logD("-------------------- 摸牌 ------------------------")
  100. if not self.roomInfo then
  101. print("容错处理")
  102. return
  103. end
  104. logD("Room:onMoPai(), response = ", table.tostring(response))
  105. self:dispatchEvent({name = "onMoPai" , response = response});
  106. end
  107. function Room:onQiPai(status,response)
  108. logD("-------------------- 弃牌 ------------------------")
  109. if not self.roomInfo then
  110. print("容错处理")
  111. return
  112. end
  113. logD("Room:onQiPai(), response = ", table.tostring(response))
  114. self:dispatchEvent({name = "onQiPai",response = response});
  115. end
  116. function Room:onOutCardError(status,response)
  117. logD("-------------------- 出牌错误 ------------------------")
  118. if not self.roomInfo then
  119. print("容错处理")
  120. return
  121. end
  122. logD("Room:onOutCardError(), response = ", table.tostring(response))
  123. --[[错误码说明:
  124. OUT_CARD_SUCCESS = 0, //正常
  125. OUT_CARD_ON_NEED = 1, //不需要出牌
  126. OUT_CARD_INVALID = 2, //出牌非法--]]
  127. local errorCode = response.result
  128. if errorCode == ZPDef.OutCardErrorTip.OUT_CARD_SUCCESS then
  129. showTooltip("出牌无效")
  130. elseif errorCode == ZPDef.OutCardErrorTip.OUT_CARD_SUCCESS then
  131. showTooltip("不需要出牌")
  132. else
  133. showTooltip("出牌未知")
  134. end
  135. self:dispatchEvent({name = "onOutCardError"});
  136. end
  137. function Room:onOutCardSuccess(status,response)
  138. logD("-------------------- 出牌成功 ------------------------")
  139. if not self.roomInfo then
  140. print("容错处理")
  141. return
  142. end
  143. logD("Room:onOutCardSuccess(), response = ", table.tostring(response))
  144. self:dispatchEvent({name = "onOutCardSuccess" , response = response});
  145. end
  146. --小局结算
  147. function Room:onGameXiaoJuResponse(status, response)
  148. logD("--------------------小局结算 ------------------------")
  149. if not self.roomInfo then
  150. logD("self.roomInfo no exist,容错处理!")
  151. return
  152. end
  153. logD("Room:onGameXiaoJuResponse() :", table.tostring(response))
  154. self:dispatchEvent({name = "onGameXiaoJuResponse",response = response});
  155. end
  156. --大局结算
  157. function Room:onGameDaJuResponse(status, response)
  158. logD("-------------------- 大局结算 ------------------------")
  159. if not self.roomInfo then
  160. logD("self.roomInfo no exist,容错处理!")
  161. return
  162. end
  163. logD("Room:onGameDaJuResponse() :", table.tostring(response))
  164. self:dispatchEvent({name = "onGameDaJuResponse",response = response});
  165. end
  166. -- 玩家进入桌子成功
  167. function Room:onSitDownSuccessResponse(status, response)
  168. logD("--------------------玩家进入桌子成功--------------------")
  169. logD("Room:onSitDownSuccessResponse(), ", table.tostring(response))
  170. app.net:onMsgClear()
  171. app.net:onMsgPause()
  172. -- 记录当前桌子号
  173. self:initRoomInfo()
  174. -- 获取到的房间信息
  175. self.roomInfo.nShowTableId = response.nShowTableId
  176. self.roomInfo.nMaxPlayCount = response.nMaxPlayCount
  177. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid
  178. self.roomInfo.nTotalGameNum = response.nTotalGameNum
  179. self.roomInfo.strGameInfo = response.strGameInfo
  180. -- 桌子上其他玩家的信息
  181. for k,v in pairs(response.memberList) do
  182. self.roomInfo.memberList[v.nUserId] = v
  183. end
  184. -- 桌子上其他玩家的信息Ex
  185. if response.memberListEx then
  186. for k,v in pairs(response.memberListEx) do
  187. self.roomInfo.memberList[v.nUserId].nOnlineStatus = v.nOnlineStatus
  188. end
  189. end
  190. -- 更新椅子号
  191. self:updateUserSeateShowId()
  192. logD("Room:onSitDownSuccessResponse() roomInfo = ", table.tostring(self.roomInfo))
  193. -- 发送通知
  194. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = ZPDef.GameID, gameType = self.roomInfo.strGameInfo.gamerule})
  195. end
  196. --获取手牌结果
  197. function Room:onGetHandcardsResponese(status, response)
  198. print("Room:onGetHandcardsResponese()", table.tostring(response))
  199. local cardData = self:serverCardToMyCard(response.handCard)
  200. self.roomInfo.memberList[response.nUserId].cardList = {}
  201. self.roomInfo.memberList[response.nUserId].cardList = cardData
  202. self.cards[response.nUserId] = cardData
  203. self:dispatchEvent({name = "onGetHandcardsResponese"});
  204. end
  205. -- 玩家进入桌子失败
  206. function Room:onSitDownFailedResponse(status, response)
  207. logD("--------------------玩家进入桌子失败--------------------")
  208. app.waitDialogManager:closeWaitNetworkDialog();
  209. local errorCode = response.nErrorCode
  210. local errorString = ENTER_ROOM_RET_STR[errorCode] or string.format("坐下失败 errorCode = %s", errorCode);
  211. showTooltip(errorString);
  212. end
  213. -- 更新玩家的相对椅子号
  214. function Room:updateUserSeateShowId()
  215. local myUserId = self:getMyRecordUserId()
  216. local mySeatId
  217. self.isOnLooker = false
  218. if self.isOnLooker then
  219. mySeatId = 3
  220. else
  221. mySeatId = self.roomInfo.memberList[myUserId].nSeatId
  222. end
  223. self.roomInfo.nUserId = self:getMyRecordUserId()
  224. self.roomInfo.nSeatId = mySeatId
  225. --[视图椅子号] = userID
  226. --[座位号] = userID
  227. --[userID] = 视图椅子号
  228. self.seatList = {}
  229. self.userList = {}
  230. local playerCount = self.bGameStart and self:getActualPlayerNum() or self.roomInfo.nMaxPlayCount or 4
  231. for k,v in pairs(self.roomInfo.memberList) do
  232. local config=ZPDef.ZPRoomPlayerSitConfig[playerCount]
  233. local num=(v.nSeatId - mySeatId + playerCount) % playerCount
  234. local nSeatShowId = config[num+1]
  235. self.seatShowList[nSeatShowId] = v.nUserId
  236. self.seatList[v.nSeatId] = v.nUserId
  237. self.userList[v.nUserId] = nSeatShowId
  238. end
  239. logD("User:updateUserSeateShowId(), seatShowList", table.tostring(self.seatShowList))
  240. logD("User:updateUserSeateShowId(),seatList ", table.tostring(self.seatList))
  241. logD("User:updateUserSeateShowId(),userList ", table.tostring(self.userList))
  242. end
  243. -- 服务器下发玩家的准备状态
  244. function Room:onUserReadyResponse(status, response)
  245. logD("Room:onUserReadyResponse(), ", table.tostring(response))
  246. if not self.roomInfo then
  247. logD("self.roomInfo no exist,容错处理!")
  248. return
  249. end
  250. local nUserId = response.intValue
  251. local memberInfo = self.roomInfo.memberList[nUserId]
  252. --标记准备了
  253. if memberInfo then
  254. memberInfo.nPlayerFlag = 1
  255. end
  256. self:dispatchEvent({name = "onUserReadyResponse"});
  257. end
  258. -- 我发起请求解散房间
  259. function Room:requestDismissRoom(opType)
  260. --1: 表示发起解散 2:同意解散 3:不同意解散
  261. local request = ZPMessage.DismissRequest:new()
  262. request.operateType = opType
  263. self:sendResponse{cmd = ZPCmd.GAME_COMMAND_DISBAND_GAME , sender = request};
  264. end
  265. --8012
  266. function Room:onDismissResponse(status, response)
  267. if not self.roomInfo then
  268. logD("self.roomInfo no exist,容错处理!")
  269. return
  270. end
  271. local nUserId = response.nUserId
  272. local operateType = response.operateType
  273. -- 收到玩家发起请求时,数据重置
  274. if response.operateType == 1 then
  275. self:resetDismissData()
  276. end
  277. --数据记录
  278. self.dismissInfo[nUserId] = operateType;
  279. self:dispatchEvent({name = "onDismissResponse", response = response});
  280. end
  281. -- 发起请求回复 8011
  282. function Room:onUserDismissResultResponse(status, response)
  283. logD("User:onUserDismissResultResponse, ", table.tostring(response))
  284. if not self.roomInfo then
  285. logD("self.roomInfo no exist,容错处理!")
  286. return
  287. end
  288. logD("30000000--runUserDismissResultResponse")
  289. if response.errorCode == 1 then
  290. showTooltip("operateType取值范围不对")
  291. elseif response.errorCode == 2 then
  292. showTooltip("当前没人发起解散")
  293. elseif response.errorCode == 3 then
  294. showTooltip("已经有人申请解散")
  295. elseif response.errorCode == 4 then
  296. showTooltip("用户已经操作过")
  297. else
  298. -- 其他玩家的解散状态
  299. for uid, value in pairs(response.memberList) do
  300. self.dismissInfo[uid] = value.dismissState
  301. end
  302. self:dispatchEvent({name = "onDismissResponse", response = response});
  303. end
  304. end
  305. -- 其他玩家退出房间
  306. function Room:onOtherLogoutResponse(status, response)
  307. logD("Room:onOtherLogoutResponse(), response = ", table.tostring(response))
  308. if not self.roomInfo then
  309. logD("self.roomInfo no exist,容错处理!")
  310. return
  311. end
  312. local nUserId = response.nUserId
  313. local nSeatShowId = self.userList[nUserId]
  314. if self.roomInfo.nGameStartCount == 0 then
  315. local memberList = self.roomInfo.memberList
  316. memberList[nUserId] = nil
  317. end
  318. self:updateGpsUserInfo(nil,false)
  319. self.roomInfo.nStartGameUid = response.nStartGameUid
  320. logD("30000000--runOtherLogoutResponse")
  321. self:dispatchEvent({name = "onOtherLogoutResponse", nUserId = nUserId, nSeatShowId = nSeatShowId});
  322. end
  323. -- 其他玩家进入桌子
  324. function Room:onOtherSitDownResponse(status, response)
  325. logD("Room:onOtherSitDownResponse(), response = ", table.tostring(response))
  326. if not self.roomInfo then
  327. logD("self.roomInfo no exist,容错处理!")
  328. return
  329. end
  330. local other = ZPMessage.RoomMemberInfo:new()
  331. other.nUserId = response.nUserId
  332. other.nSeatId = response.nSeatId
  333. other.userInfo = response.userInfo
  334. other.nTotalMoney = response.nTotalMoney
  335. other.nPlayerFlag = response.nPlayerFlag
  336. local memberList = self.roomInfo.memberList
  337. -- 添加其他玩家的用户ID
  338. if memberList[other.nUserId] then
  339. memberList[other.nUserId] = other:updateTo(memberList[other.nUserId])
  340. else
  341. memberList[other.nUserId] = other
  342. end
  343. -- 更新椅子号
  344. self:updateUserSeateShowId()
  345. --检测GPS
  346. self:updateGpsUserInfo(response.nUserId,true)
  347. local nSeatShowId = self.userList[other.nUserId]
  348. -- 发送广播通知,某个位置有人坐下了
  349. self:dispatchEvent({name = "onOtherSitDownResponse", nSeatShowId = nSeatShowId});
  350. end
  351. function Room:onUserExitResponse(status, response)
  352. logD("Room:onUserExitResponse(), ", table.tostring(response))
  353. if not self.roomInfo then
  354. logD("self.roomInfo no exist,容错处理!")
  355. return
  356. end
  357. local ttExtInfo = string.split(response.userInfo, ",")
  358. app.user.loginInfo.historyCardNum = ttExtInfo[1];
  359. app.user.loginInfo.curCardNum = ttExtInfo[2];
  360. self:dispatchEvent({name = "onUserExitResponseRoom",response = response});
  361. end
  362. function Room:onTingCardResult(status, response)
  363. logD("Room:onTingCardResult(), ", table.tostring(response))
  364. if not self.roomInfo then
  365. logD("self.roomInfo no exist,容错处理!")
  366. return
  367. end
  368. self:dispatchEvent({name = "onTingCardResult",response = response});
  369. end
  370. function Room:onZhaNiaoResponese(status, response)
  371. logD("Room:onZhaNiaoResponese(), ", table.tostring(response))
  372. if not self.roomInfo then
  373. logD("self.roomInfo no exist,容错处理!")
  374. return
  375. end
  376. self.roomInfo.leftZhaBirdTime = response.leftZhaBirdTime
  377. self:dispatchEvent({name = "onZhaNiaoResponese",response = response});
  378. end
  379. function Room:onZhaNiaoResult(status, response)
  380. logD("Room:onZhaNiaoResult(), ", table.tostring(response))
  381. if not self.roomInfo then
  382. logD("self.roomInfo no exist,容错处理!")
  383. return
  384. end
  385. self:dispatchEvent({name = "onZhaNiaoResult",response = response});
  386. end
  387. function Room:onChoicePlace(status, response)
  388. logD("Room:onChoicePlace(), ", table.tostring(response))
  389. if not self.roomInfo then
  390. logD("self.roomInfo no exist,容错处理!")
  391. return
  392. end
  393. self:dispatchEvent({name = "onChoicePlace",response = response});
  394. end
  395. function Room:onBaoJingResponse(status, response)
  396. logD("Room:onBaoJingResponse(), ", table.tostring(response))
  397. if not self.roomInfo then
  398. logD("self.roomInfo no exist,容错处理!")
  399. return
  400. end
  401. self:dispatchEvent({name = "onBaoJingResponse",response = response});
  402. end
  403. --断线重连
  404. function Room:onGameReconnection(status,response)
  405. logD("===============重连成功,清空之前所有存在的队列==================")
  406. app.net:onMsgClear()
  407. app.net:onMsgPause()
  408. logD("Room:onGameReconnection(), response = ", table.tostring(response))
  409. self:initRoomInfo();
  410. -- 获取到的房间信息
  411. self.roomInfo.nShowTableId = response.nShowTableId
  412. self.roomInfo.nMaxPlayCount = response.nMaxPlayCount
  413. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid
  414. self.roomInfo.nTotalGameNum = response.nTotalGameNum
  415. self.roomInfo.nGameStartCount = response.nGameStartCount
  416. self.roomInfo.strGameInfo = response.strGameInfo
  417. self.roomInfo.nStatus = response.nStatus
  418. self.roomInfo.nDismissToTalTime = response.nDismissToTalTime
  419. self.roomInfo.nBankSeatId = response.nBankSeatId
  420. self.roomInfo.nZhongBankCnt = response.nZhongBankCnt
  421. self.roomInfo.bUserDisbandGame = response.bUserDisbandGame
  422. self.roomInfo.nDismissStateTime = response.nDismissStateTime
  423. self.roomInfo.arrayTableInfo = response.arrayTableInfo
  424. self.roomInfo.dipaiList = response.dipaiList
  425. self.roomInfo.showCardSeatId = response.showCardSeatId
  426. self.roomInfo.showCard = response.showCard
  427. self.roomInfo.mainOpCode = response.mainOpCode
  428. self.roomInfo.isSubmitOp = response.isSubmitOp
  429. self.roomInfo.showCardFlag = response.showCardFlag
  430. self.roomInfo.outCardSeatId = response.outCardSeatId
  431. self.roomInfo.lastOperate = response.lastOperate
  432. self.roomInfo.lastOpCard = response.lastOpCard
  433. self.roomInfo.disCardSeatId = response.disCardSeatId
  434. self.roomInfo.disCard = response.disCard
  435. self.roomInfo.leftZhaBirdTime = response.leftZhaBirdTime
  436. self.roomInfo.stopFlag = response.stopFlag
  437. self.roomInfo.winUserId = response.winUserId
  438. self.roomInfo.dianPaoUserId = response.dianPaoUserId
  439. self.roomInfo.huType = response.huType
  440. self.roomInfo.huCard = response.huCard
  441. self.roomInfo.tingCards = response.tingCards or {}
  442. self.roomInfo.hupaiInfo = response.hupaiInfo
  443. self.roomInfo.xiaoSeatId = response.xiaoSeatId;
  444. self.roomInfo.offLineInfo = response.offLineInfo
  445. self.roomInfo.tuoGuanInfo = response.tuoGuanInfo
  446. self:addReconnectionData(response)
  447. if self.roomInfo.nStatus ~= ZPDef.ReconnectState.GAME_STATUS_INIT then
  448. self.bGameStart = true
  449. end
  450. if self.roomInfo.offLineInfo and next(self.roomInfo.offLineInfo) ~= nil then
  451. for i,v in pairs(self.roomInfo.offLineInfo) do
  452. if v.extInfo then
  453. local extIf = json.decode(v.extInfo)
  454. if extIf.OffTime and v.nUserId then
  455. self.offLineTime[v.nUserId] = extIf.OffTime
  456. end
  457. end
  458. end
  459. end
  460. for i,v in pairs(self.roomInfo.hupaiInfo or {}) do--增加容错
  461. self.roomInfo.dianPaoUserId = v.dianPaoUid
  462. self.roomInfo.huCard = v.huCard
  463. end
  464. if app.club_php.clubID and app.club_php.clubID ~= 0 and self.roomInfo.nGameStartCount > 0 then
  465. app.club_php:getClubList();
  466. end
  467. -- 桌子上其他玩家的信息
  468. for k,v in pairs(response.arrayTableInfo) do
  469. if not self.roomInfo.memberList[v.nUserId] then
  470. self.roomInfo.memberList[v.nUserId] = {}
  471. end
  472. self.roomInfo.memberList[v.nUserId].nUserId = v.nUserId
  473. self.roomInfo.memberList[v.nUserId].nSeatId = v.nSeatId
  474. self.roomInfo.memberList[v.nUserId].nPlayerFlag = v.nPlayerFlag
  475. self.roomInfo.memberList[v.nUserId].userInfo = v.userInfo
  476. self.roomInfo.memberList[v.nUserId].nTotalMoney = v.nTotalMoney
  477. self.roomInfo.memberList[v.nUserId].nOnlineStatus = v.nOnlineStatus
  478. self.roomInfo.memberList[v.nUserId].nDisbandStatus = v.nDisbandStatus
  479. --这里只记录,暂时没用到。可能战绩回放的时候是需要用的
  480. local cardData = self:serverCardToMyCard(v.handCard)
  481. self.roomInfo.memberList[v.nUserId].cardList = {}
  482. self.roomInfo.memberList[v.nUserId].cardList = cardData
  483. if v.nSeatId == self.roomInfo.nBankSeatId then
  484. self.roomInfo.nBankUid = v.nUserId
  485. end
  486. end
  487. local myUserId = self:getMyRecordUserId()
  488. for i,v in pairs(self.roomInfo.memberList) do
  489. if self:isLittleFamily() and self.roomInfo.nBankSeatId == v.nSeatId then
  490. self.roomInfo.memberList[myUserId].cardList = v.cardList
  491. end
  492. end
  493. --更新自己的数据
  494. self.cards[myUserId] = self.roomInfo.memberList[myUserId].cardList
  495. print("self.cards[myUserId]:"..table.tostring(self.cards[myUserId]))
  496. --检测GPS
  497. self:updateGpsUserInfo(nil,false)
  498. -- 更新椅子号
  499. self:updateUserSeateShowId(self.roomInfo.xiaoSeatId)
  500. local jsonInfo = json.decode(self.roomInfo.strGameInfo)
  501. -- 发送通知
  502. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = ZPDef.GameID or app.gameId, gameType = jsonInfo.gamerule})
  503. end
  504. --扩展给子类用
  505. function Room:addReconnectionData(response)
  506. end
  507. --约定俗成解析牌
  508. --[[
  509. 思维:
  510. 服务器排好序后发牌过来,最多有10列,每一列最多有3行;
  511. 排序按一列算,普通2-3个,串特殊4个。
  512. 默认3个满一列,串的时候不做任何处理,开局的时候自动串起,之后不会出现4个的情况
  513. 单张的重建一列,并要求单张列数到达3叠满后才会开启新的一列。
  514. 使用:
  515. 传入服务器的牌,返回解析后的数组保存到self.cards[用户ID]去,正常发牌,只有自己的牌,断线重连也只有自己的牌,
  516. 战绩回放的时候,server会返回所有玩家的牌
  517. ]]
  518. function Room:serverCardToMyCard(serverCardList)
  519. --
  520. --根据定义组合牌
  521. local cardData = {}
  522. --每一个组合数据
  523. local singleList = {}
  524. --单个组合索引
  525. local index = 1
  526. --真实索引
  527. local realIndex = 1
  528. local tt = {}
  529. for i = 1,10 do
  530. for k,v in ipairs(serverCardList) do
  531. local idx = 0
  532. local tCard = nil
  533. if not v.opcard then
  534. tCard = v.card
  535. else
  536. tCard = v.opcard
  537. end
  538. if tCard > 16 then
  539. idx = tCard - 16
  540. else
  541. idx = tCard
  542. end
  543. if i == idx then
  544. table.insert(tt,v)
  545. end
  546. end
  547. end
  548. --1 2 da2 4 5 6 7 8 9 10 3
  549. for k,v in ipairs(tt) do
  550. --不是单张牌,直接给一列
  551. if v.type ~= ZPDef.SendCardType.SINGLE_CARD then
  552. if not cardData[realIndex] then
  553. cardData[realIndex] = {}
  554. end
  555. local list,mType = ZPFuc.getZPCardList(v.card,v.type)
  556. cardData[realIndex] = list
  557. realIndex = realIndex + 1
  558. else
  559. if table.nums(singleList) > 0 then
  560. local isFind = false
  561. for kSin,kCard in pairsByKeys(singleList) do
  562. if math.abs(v.card - kCard) <= 1 then
  563. table.insert(cardData[kSin],v.card)
  564. isFind = true
  565. end
  566. end
  567. if not isFind then
  568. singleList[realIndex] = v.card
  569. cardData[realIndex] = {v.card}
  570. realIndex = realIndex + 1
  571. end
  572. else
  573. singleList[realIndex] = v.card
  574. cardData[realIndex] = {v.card}
  575. realIndex = realIndex + 1
  576. end
  577. end
  578. end
  579. return cardData
  580. end
  581. --牌排序算法:
  582. --[[
  583. @parm:color 1表示小写,2为大写
  584. @parm:digit 1-10,表示值
  585. ]]
  586. function Room:getSendCardLogic(color,digit,mType)
  587. local list = {}
  588. if color == 1 then
  589. list.card = digit
  590. else
  591. list.card = digit + 16
  592. end
  593. list.type = mType
  594. return list
  595. end
  596. --服务器排序逻辑,拿来自己写回放排序
  597. function Room:rankHandCard(tPureHandCards)
  598. --发牌
  599. local cardList = {}
  600. local index = 1
  601. --大小写1-10,11为总张数
  602. for digit = 1,10 do
  603. for color = 1,2 do --纯手牌 color == 1表示小写,2为大写
  604. if tPureHandCards[color][digit] ~= 0 then
  605. local otherColor
  606. if color == 1 then
  607. otherColor = 2
  608. else
  609. otherColor = 1
  610. end
  611. --串小写
  612. if (tPureHandCards[color][digit] == 4) then
  613. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 4
  614. tPureHandCards[color][11] = tPureHandCards[color][11] - 4
  615. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.GUN_ZI_FOUR_SAME)
  616. index = index + 1
  617. end
  618. --串大写
  619. if (tPureHandCards[otherColor][digit] == 4) then
  620. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 4
  621. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 4
  622. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.GUN_ZI_FOUR_SAME)
  623. index = index + 1
  624. end
  625. --哨小写
  626. if (tPureHandCards[color][digit] == 3) then
  627. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 3
  628. tPureHandCards[color][11] = tPureHandCards[color][11] - 3
  629. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.KAN_THREE_SAME)
  630. index = index + 1
  631. end
  632. --哨大写
  633. if (tPureHandCards[otherColor][digit] == 3) then
  634. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 3
  635. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 3
  636. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.KAN_THREE_SAME)
  637. index = index + 1
  638. end
  639. --二七十
  640. if(digit == 2) then
  641. if(tPureHandCards[color][7] <= 1 and tPureHandCards[color][10] <= 1) then
  642. if(tPureHandCards[color][2] == 1 and tPureHandCards[color][7] == 1 and tPureHandCards[color][10] == 1 )then
  643. --cardModel.push_back(GetModelTwoSevenTen(make_card(color,digit)))
  644. tPureHandCards[color][2] = tPureHandCards[color][2] - 1
  645. tPureHandCards[color][7] = tPureHandCards[color][7] - 1
  646. tPureHandCards[color][10] = tPureHandCards[color][10] - 1
  647. tPureHandCards[color][11] = tPureHandCards[color][11] - 3
  648. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.TWO_SEVEN_TEN)
  649. index = index + 1
  650. end
  651. end
  652. if(tPureHandCards[otherColor][7] <= 1 and tPureHandCards[otherColor][10] <= 1) then
  653. if(tPureHandCards[otherColor][2] == 1 and tPureHandCards[otherColor][7] == 1
  654. and tPureHandCards[otherColor][10] == 1 ) then
  655. --cardModel.push_back(GetModelTwoSevenTen(make_card(otherColor,digit)))
  656. tPureHandCards[otherColor][2] = tPureHandCards[otherColor][2] - 1
  657. tPureHandCards[otherColor][7] = tPureHandCards[otherColor][7] - 1
  658. tPureHandCards[otherColor][10] = tPureHandCards[otherColor][10] - 1
  659. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 3
  660. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.TWO_SEVEN_TEN)
  661. index = index + 1
  662. end
  663. end
  664. end
  665. --一句话
  666. if(digit+2 <= 10 and tPureHandCards[color][digit] == 1 and tPureHandCards[color][digit+1] == 1
  667. and tPureHandCards[color][digit+2] == 1) then
  668. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 1
  669. tPureHandCards[color][digit+1] = tPureHandCards[color][digit+1] - 1
  670. tPureHandCards[color][digit+2] = tPureHandCards[color][digit+2] - 1
  671. tPureHandCards[color][11] = tPureHandCards[color][11] - 3
  672. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.SHUN_ZI)
  673. index = index + 1
  674. end
  675. --一句话
  676. if(digit+2 <= 10 and tPureHandCards[otherColor][digit] == 1 and tPureHandCards[otherColor][digit+1] == 1
  677. and tPureHandCards[otherColor][digit+2] == 1) then
  678. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 1
  679. tPureHandCards[otherColor][digit+1] = tPureHandCards[otherColor][digit+1] - 1
  680. tPureHandCards[otherColor][digit+2] = tPureHandCards[otherColor][digit+2] - 1
  681. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 3
  682. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.SHUN_ZI)
  683. index = index + 1
  684. end
  685. if(tPureHandCards[color][digit] >= 2 and tPureHandCards[otherColor][digit] == 1) then --大大小搭子
  686. --cardModel.push_back(GetModelDaZiByMix(make_card(color,digit)))
  687. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 2
  688. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 1
  689. tPureHandCards[color][11] = tPureHandCards[color][11] - 2
  690. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] -1
  691. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.DA_ZI_TWO_AND_ONE)
  692. index = index + 1
  693. elseif(tPureHandCards[color][digit] == 1 and tPureHandCards[otherColor][digit] >= 2) then --小小大搭子
  694. --cardModel.push_back(GetModelDaZiBySame(make_card(color,digit)))
  695. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 1
  696. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 2
  697. tPureHandCards[color][11] = tPureHandCards[color][11] - 1
  698. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 2
  699. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.DA_ZI_TWO_AND_ONE)
  700. index = index + 1
  701. elseif(tPureHandCards[color][digit] >= 2)then
  702. --cardModel.push_back(GetModelZhuangZi(make_card(color,digit))) --对子
  703. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 2
  704. tPureHandCards[color][11] = tPureHandCards[color][11] - 2
  705. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.ZHUANG_ZI)
  706. index = index + 1
  707. end
  708. --混对(一张大一张小)
  709. if(tPureHandCards[color][digit] == 1 and tPureHandCards[otherColor][digit] == 1) then
  710. --cardModel.push_back(GetModelMixZhuangZi(make_card(color,digit)))
  711. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 1
  712. tPureHandCards[otherColor][digit] = tPureHandCards[otherColor][digit] - 1
  713. tPureHandCards[color][11] = tPureHandCards[color][11] - 1
  714. tPureHandCards[otherColor][11] = tPureHandCards[otherColor][11] - 1
  715. cardList[index] = self:getSendCardLogic(otherColor,digit,ZPDef.SendCardType.MIX_ZHUANG_ZI)
  716. index = index + 1
  717. elseif(tPureHandCards[color][digit] == 1) then --单张
  718. --IChessModel model
  719. --model.ichess_type = SINGLE_CARD
  720. --model.ichess_opCard = make_card(color,digit)
  721. --model.ichess_cards.push_back(model.ichess_opCard)
  722. --cardModel.push_back(model)
  723. tPureHandCards[color][digit] = tPureHandCards[color][digit] - 1
  724. tPureHandCards[color][11] = tPureHandCards[color][11] - 1
  725. cardList[index] = self:getSendCardLogic(color,digit,ZPDef.SendCardType.SINGLE_CARD)
  726. index = index + 1
  727. end
  728. end
  729. end
  730. end
  731. return cardList
  732. end
  733. -- 其他玩家是否掉线
  734. function Room:onOtherDroppedResponse(status, response)
  735. logD("User:onOtherDroppedResponse(), response = ", table.tostring(response))
  736. logD("30000000--runOtherDroppedResponse")
  737. self:dispatchEvent({name = "onOtherDroppedResponse", response = response});
  738. end
  739. --如果多家可以操作,先操作的会收到通知要等待其他人
  740. function Room:onOperationFirst(status,response)
  741. logD("-------------------- 如果多家可以操作,先操作的会收到通知要等待其他人 ------------------------")
  742. if not self.roomInfo then
  743. print("容错处理")
  744. return
  745. end
  746. self:dispatchEvent({name = "onOperationFirst",response = response})
  747. end
  748. --操作错误
  749. function Room:onOperationError(status)
  750. logD("-------------------- onOperationError ------------------------")
  751. if not self.roomInfo then
  752. print("容错处理")
  753. return
  754. end
  755. showTooltip("操作发生了错误...")
  756. --错误的时候如何处理
  757. self:dispatchEvent({name = "onOperationError"})
  758. end
  759. --操作成功
  760. --[[
  761. 说明:用户自己收到操作结果时, 要判断操作者他是否要出牌,若要出牌,则需提示出牌
  762. 若不用出牌,客户端无需处理, server会广播下家抓牌
  763. ]]
  764. function Room:onOperationCodeSuccess(status,response)
  765. logD("-------------------- 操作成功 ------------------------")
  766. if not self.roomInfo then
  767. print("容错处理")
  768. return
  769. end
  770. logD("Room:onOperationCodeSuccess(), response = ", table.tostring(response))
  771. self:dispatchEvent({name = "onOperationCodeSuccess",response = response});
  772. end
  773. -- 服务器下发玩家的准备状态
  774. function Room:onUserReadyResponse(status, response)
  775. print("Room:onUserReadyResponse(), ", table.tostring(response))
  776. if not self.roomInfo then
  777. print("self.roomInfo no exist,容错处理!")
  778. return
  779. end
  780. local nUserId = response.nUserId
  781. local memberInfo = self.roomInfo.memberList[nUserId]
  782. --标记准备了
  783. if memberInfo then
  784. memberInfo.nPlayerFlag = 1
  785. end
  786. self:dispatchEvent({name = "onUserReadyResponse"});
  787. end
  788. -- 通知服务器玩家GPS数据发生变化
  789. function Room:onGpsChangeRequest()
  790. print("Room:onUserInfoChangeRequest()")
  791. local request = StringPacket:new()
  792. request.stringValue = app.user.userInfo or ""
  793. logD("Room:onGpsChangeRequest()", table.tostring(request))
  794. self:sendResponse{cmd = ZPCmd.GAME_COMMAND_CLIENT_CHANGE_USERINFO , sender = request};
  795. end
  796. --[[-- 服务器下发玩家GPS数据发生变化
  797. function Room:onGpsChangeResponse(status, response)
  798. print("Room:onGpsChangeResponse()", table.tostring(response))
  799. local nUserId = response.uid
  800. local memberInfo = self.roomInfo.memberList[nUserId]
  801. if memberInfo then
  802. memberInfo.userInfo = response.userInfo
  803. end
  804. self:dispatchEvent({name = "onGpsChangeResponse", nUserId = nUserId});
  805. end--]]
  806. -- 服务器下发爆牌结果
  807. function Room:onBaoPaiResponese(status, response)
  808. print("Room:onBaoPaiResponese()", table.tostring(response))
  809. self:dispatchEvent({name = "onBaoPaiResponese",response = response});
  810. end
  811. -- 广播最后一张牌
  812. function Room:onLastCardResponse(status, response)
  813. print("Room:onLastCardResponse()", table.tostring(response))
  814. self:dispatchEvent({name = "onLastCardResponse",response = response});
  815. end
  816. function Room:ctor(net)
  817. Room.super.ctor(self , net);
  818. -- GPS信息发生变化的时候主动给服务器发消息
  819. app:addEventListener("onUpdateLoctionSuccessed", handler(self, self.onGpsChangeRequest))
  820. --发送
  821. -- 请求准备
  822. self:defMsgFunc{name = "callReadyRequest" , cmd = ZPCmd.GAME_COMMAND_USER_READY};
  823. -- 离开房间
  824. self:defMsgFunc{name = "callLeaveRequest" , cmd = ZPCmd.GAME_COMMAND_USER_LEAVE};
  825. -- 玩家请求解散房间,获取其他玩家回复解散房间
  826. self:defMsgFunc{name = "dismissRoomRequest", cmd = ZPCmd.GAME_COMMAND_DISBAND_GAME, sender = ZPMessage.DismissRequest};
  827. -- 玩家请求立即开局
  828. self:defMsgFunc{name = "quickStartRequest", cmd = ZPCmd.GAME_COMMAND_REQUEST_EARLY_START, sender = ZPMessage.EarlyStartRequest};
  829. --发送爆牌选择
  830. self:defMsgFunc{name = "BaoPai",cmd = ZPCmd.GAME_COMMAND_BAO_PAI,sender = ZPMessage.BaoPai};
  831. --获取手牌
  832. self:defMsgFunc{name = "callGetHandcardsRequest" , cmd = ZPCmd.GAME_COMMAND_USER_GET_HANDCARDS};
  833. --GAME
  834. --发送操作码
  835. self:defMsgFunc{name = "sendOperationCode" , cmd = ZPCmd.GAME_COMMAND_SEND_CODE_RESPONSE, sender = ZPMessage.GameSendOpCodeResponse};
  836. --发送出牌
  837. self:defMsgFunc{name = "sendOutCard",cmd = ZPCmd.GAME_COMMAND_OUT_CARD,sender = ZPMessage.OutCard};
  838. --接收
  839. -- 玩家进入桌子成功
  840. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_LOGIN_GAME_SUCCESS, reader = ZPMessage.SitDownSuccessResponse, func = handler(self , self.onSitDownSuccessResponse)};
  841. -- 玩家进入桌子失败
  842. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_LOGIN_GAME_ERR, reader = ZPMessage.SitDownFailedResponse, func = handler(self , self.onSitDownFailedResponse)};
  843. -- 玩家收到解散房间的请求
  844. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_DISBAND_GAME, reader = ZPMessage.DismissResponse, func = handler(self , self.onDismissResponse)};
  845. -- 收到玩家申请解散房间
  846. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_DISBAND_GAME, reader = ZPMessage.DismissResult, func = handler(self , self.onUserDismissResultResponse)};
  847. -- 其他玩家退出房间
  848. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_USER_LOGOUT, reader = ZPMessage.OtherLogoutResponse, func = handler(self , self.onOtherLogoutResponse)};
  849. -- 收到服务器下发的玩家准备状态
  850. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_USER_READY, reader = ZPMessage.IntPacket, func = handler(self , self.onUserReadyResponse)};
  851. -- 其他玩家进入桌子
  852. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_USER_LOGIN, reader = ZPMessage.OtherSitDownResponse, func = handler(self , self.onOtherSitDownResponse)};
  853. -- 用户退出
  854. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_LOGOUT_GAME_SUCCESS, reader = ZPMessage.UserExitResponse, func = handler(self , self.onUserExitResponse)};
  855. -- 游戏正式开始
  856. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_GAME_START, reader = ZPMessage.GameStartResponse, func = handler(self , self.onGameStartResponse)};
  857. --发牌
  858. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_SENDCARD_RESPONSE, reader = ZPMessage.GameSendCardResponse,func = handler(self , self.onGameSendCardResponse)};
  859. --开局后系统可以提的自动消息
  860. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_SYSTEM_RESPONSE, reader = ZPMessage.SystemOperationStart,func = handler(self , self.onSystemOperationStart)};
  861. --广播桌子上所有玩家庄家起手操作
  862. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BAKER_OUTCARD_RESPONSE, reader = ZPMessage.BankerOutCard,func = handler(self , self.onBankerOutCard)};
  863. --如果多家可以操作,先操作的会收到通知要等待其他人
  864. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_OPERATION_FIRST,reader = ZPMessage.OperationCodeFirst,func = handler(self,self.onOperationFirst)}
  865. --若玩家操作不对,如operateCode值不对等,通过协议0x810d通知客户端操作错误,此协议暂定无参数
  866. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_OPERATION_CODE_ERROR,func = handler(self,self.onOperationError)}
  867. --玩家操作成功, 则广播通知桌子上其他玩家操作结果,协议0x810b
  868. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_OPERATION_CODE_SUCCESS,reader = ZPMessage.OperationCodeSuccess,func = handler(self,self.onOperationCodeSuccess)}
  869. --摸牌
  870. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_MO_PAI,reader = ZPMessage.MoPai,func = handler(self,self.onMoPai)}
  871. --弃牌
  872. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_QI_PAI,reader = ZPMessage.QiPai,func = handler(self,self.onQiPai)}
  873. --出牌错误
  874. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_OUT_CARD_ERROR,reader = ZPMessage.OutCardError,func = handler(self,self.onOutCardError)}
  875. --出牌成功
  876. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_OUT_CARD_SUCCESS,reader = ZPMessage.OutCardSuccess,func = handler(self,self.onOutCardSuccess)}
  877. --单局结算
  878. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_GAME_XIAO_JU, reader = ZPMessage.GameXiaoJuResponse, func = handler(self , self.onGameXiaoJuResponse)};
  879. --总结算
  880. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_GAME_DA_JU, reader = ZPMessage.GameDaJuResponse, func = handler(self , self.onGameDaJuResponse)};
  881. --听牌结果
  882. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_TINGCARD, reader = ZPMessage.TingCardResult, func = handler(self , self.onTingCardResult)};
  883. --爆牌结果
  884. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BAO_PAI, reader = ZPMessage.BaoPaiResponse, func = handler(self , self.onBaoPaiResponese)};
  885. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BAO_PAI_RESPONSE, reader = ZPMessage.BaoPaiResponse, func = handler(self , self.onBaoPaiResponese)};
  886. -- 玩家信息改变
  887. --self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_SERVER_CHANGE_USERINFO, reader = ZPMessage.ServerChangeUserInfo, func = handler(self , self.onGpsChangeResponse)};
  888. --断线重连
  889. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_GAME_RECONNECTION,reader = ZPMessage.GameReconnection,func = handler(self,self.onGameReconnection)}
  890. -- 其他玩家掉线
  891. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_USER_DROPPED, reader = ZPMessage.OtherDroppedResponse, func = handler(self , self.onOtherDroppedResponse)};
  892. -- 收到玩家申请立即开局的广播
  893. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_RESPONSE_EARLY_START, reader = ZPMessage.EarlyStartBroadcast, func = handler(self , self.onQuickStartResponse)};
  894. --收到玩家申请立即开局的结果
  895. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROAD_QUICKLY_START, reader = ZPMessage.EarlyStartResult, func = handler(self , self.onQuickStartResultResponse)};
  896. -- 发完牌后,庄家收到最后一张牌
  897. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROADCAST_LAST_CARD, reader = ZPMessage.LastCard, func = handler(self , self.onLastCardResponse)};
  898. -- 请求获取牌结果
  899. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_USER_GET_HANDCARDS, reader = ZPMessage.GetHandcardsResponse, func = handler(self , self.onGetHandcardsResponese)};
  900. -- 托管广播
  901. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_TUOGUAN_RESPONSE, reader = ZPMessage.TuoGuanResponse, func = handler(self , self.onTuoGuanResponse)};
  902. --[[-- 客户端发起快速请求
  903. self:defMsgFunc{name = ZPDef.ZPEvent.RequestFastsStart, cmd = ZPCmd.GAME_COMMAND_FAST_START_GAME, sender = ZPMessage.FastStartRequest}
  904. -- 快速开始请求返回
  905. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_FAST_START_RESULT, reader = ZPMessage.FastStartRequestResult, func = handler(self, self.onUserRequestFast)}
  906. -- 快速开始广播请求
  907. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROAD_FAST_START_GAME, reader = ZPMessage.FastStartStatus, func = handler(self, self.onBroadcastFastRequest)}
  908. -- 快速开始成功
  909. self:defPushMsg{cmd = ZPCmd.GAME_COMMAND_BROAD_QUICKLY_START, reader = ZPMessage.UpdateSeatIds, func = handler(self, self.onFastStartSucc)}--]]
  910. end
  911. --辅助查找
  912. -- 通过玩家逻辑椅子号找到玩家ID
  913. function Room:getUserIdBySeatId(seatId)
  914. return self.seatList[seatId]
  915. end
  916. -- 通过玩家视图椅子号找到玩家ID
  917. function Room:getUserIdByViewId(seatShowId)
  918. return self.seatShowList[seatShowId]
  919. end
  920. -- 通过玩家ID获取玩家展示的座位号
  921. function Room:getViewIdByUserId(nUserId)
  922. if self.userList and self.userList[nUserId] then
  923. return self.userList[nUserId];
  924. else
  925. return nil
  926. end
  927. end
  928. -- 通过玩家逻辑椅子号找到视图椅子号
  929. function Room:getViewIdBySeatId(nSeatId)
  930. local userid = self:getUserIdBySeatId(nSeatId)
  931. return self:getViewIdByUserId(userid)
  932. end
  933. -- 通过玩家视图椅子号找到逻辑椅子号
  934. function Room:getSeatIdByViewId(nViewId)
  935. local userid = self:getUserIdByViewId(nViewId)
  936. for seatId,nUserId in pairs(self.seatList) do
  937. if userid == nUserId then
  938. return seatId
  939. end
  940. end
  941. end
  942. -- return table of userInfo, not string
  943. function Room:getUserInfo(nUserId)
  944. if self.roomInfo.memberList[nUserId] and self.roomInfo.memberList[nUserId].userInfo then
  945. return json.decode(self.roomInfo.memberList[nUserId].userInfo)
  946. else
  947. return nil
  948. end
  949. end
  950. function Room:getMyUserId()
  951. return self:getUserIdByViewId(ZPDef.MyViewID)
  952. end
  953. --获取自己的ID,API仅限回放使用
  954. --[[
  955. 如果自己在里面玩,那么就返回我自己的ID,为第一视角
  956. 如果是管理员或者其他目的进来查看回放,则以这局玩家最小的ID为第一视角
  957. ]]
  958. function Room:getMyRecordUserId()
  959. local myUserId = app.user.loginInfo.uid
  960. local minID = 0
  961. local isFind = false
  962. for k,v in pairsByKeys(self.roomInfo.memberList) do
  963. if myUserId == v.nUserId then
  964. return myUserId
  965. end
  966. if not isFind then
  967. minID = v.nUserId
  968. isFind = true
  969. end
  970. end
  971. return minID
  972. end
  973. -- 发起请求立即开局
  974. function Room:requestQuickStart(opType)
  975. --0初始状态 1申请开始游戏 2同意开始游戏 3不同意开始游戏
  976. local request = ZPMessage.EarlyStartRequest:new()
  977. request.optType = opType
  978. self:sendResponse{cmd = ZPCmd.GAME_COMMAND_REQUEST_EARLY_START , sender = request};
  979. end
  980. -- 申请立即开局广播
  981. function Room:onQuickStartResponse(status, response)
  982. if not self.roomInfo then
  983. logD("self.roomInfo no exist,容错处理!")
  984. return
  985. end
  986. logD("Room:onQuickStartResponse:",table.tostring(response))
  987. self.nleftStartGameTimeout = response.nleftStartGameTimeout
  988. self.quickStartInfo = {}
  989. for i,v in pairs(response.earlyStartPlayerInfo) do
  990. self.quickStartInfo[v.nUserId] = v.optType
  991. end
  992. local nUserId = response.nUserId
  993. local operateType = response.operateType
  994. self:dispatchEvent({name = "onQuickStartResponse", response = response});
  995. end
  996. -- 立即开局结果
  997. function Room:onQuickStartResultResponse(status, response)
  998. if not self.roomInfo then
  999. logD("self.roomInfo no exist,容错处理!")
  1000. return
  1001. end
  1002. logD("Room:onQuickStartResultResponse:",table.tostring(response))
  1003. --nSeatId
  1004. for i,v in pairs(response.updateSeatId) do
  1005. if self.roomInfo.memberList[v.nUserId] then
  1006. self.roomInfo.memberList[v.nUserId].nSeatId = v.nSeatId
  1007. end
  1008. end
  1009. -- 更新椅子号
  1010. self:updateTableView(true)
  1011. self:dispatchEvent({name = "onQuickStartResultResponse", response = response});
  1012. end
  1013. function Room:updateTableView(bGameStart)
  1014. self.bGameStart = bGameStart
  1015. self.seatShowList = {}
  1016. self:updateUserSeateShowId()
  1017. end
  1018. --[[-- 游戏快速开始,更新玩家座位号
  1019. function Room:onFastStartSucc( status, response )
  1020. self.roomInfo.nMaxPlayCount = #response.pList
  1021. local gameInfo = json.decode(self.roomInfo.strGameInfo)
  1022. gameInfo.playnum = self.roomInfo.nMaxPlayCount
  1023. self.roomInfo.strGameInfo = json.encode(gameInfo)
  1024. for _,v in pairs(response.pList) do
  1025. self.roomInfo.memberList[v.nUserId].nSeatId = v.nSeatId
  1026. end
  1027. -- 更新椅子号
  1028. self.seatShowList = {}
  1029. self:updateUserSeateShowId()
  1030. self:dispatchEvent({name = ZPDef.ZPEvent.FastStartSucc ,response = response})
  1031. end
  1032. -- 快速开始操作广播
  1033. function Room:onBroadcastFastRequest( status, response )--
  1034. self:dispatchEvent({name = ZPDef.ZPEvent.BraodcastFastStart ,response = response})
  1035. end
  1036. -- 快速开始请求返回
  1037. function Room:onUserRequestFast( status, response )--快速开始请求错误提示错误
  1038. local errTxt ={
  1039. [0] = "成功",
  1040. [1] = "人数不够",
  1041. [2] = "其他人已请求",
  1042. [3] = "房间人数已满",
  1043. [4] = "非法操作"
  1044. }
  1045. local txt = errTxt[response.result]
  1046. if txt then
  1047. showTooltip(txt)
  1048. end
  1049. end--]]
  1050. -- 检查此桌是否有此ID玩家
  1051. function Room:checkUserIDIsInTable(nUserId)
  1052. if nUserId > 99999 and self.roomInfo.memberList[nUserId] and self.roomInfo.memberList[nUserId].nSeatId then
  1053. return true
  1054. end
  1055. return false
  1056. end
  1057. -- 获取桌子上实际人数
  1058. function Room:getActualPlayerNum()
  1059. local playernum = 0
  1060. for i,v in pairs(self.roomInfo.memberList) do
  1061. if self:checkUserIDIsInTable(i) then
  1062. playernum = playernum + 1
  1063. end
  1064. end
  1065. return playernum
  1066. end
  1067. -- 获取自己是否是小家(观看的人) 且选了小家可以看牌才返回true
  1068. function Room:isLittleFamily()
  1069. if not self.roomInfo.memberList[app.user.loginInfo.uid] then
  1070. return false
  1071. end
  1072. local meSeatID = self.roomInfo.memberList[app.user.loginInfo.uid].nSeatId
  1073. if self.roomInfo.xiaoSeatId and meSeatID and self.roomInfo.xiaoSeatId == meSeatID then
  1074. return true
  1075. end
  1076. return false
  1077. end
  1078. --更新gps用户数据
  1079. --需要检测的ID 没有则检测所有人的距离
  1080. --isOpenView 危险距离是否主动弹出提示
  1081. function Room:updateGpsUserInfo(userId,isOpenView)
  1082. if self.roomInfo and self.roomInfo.memberList then
  1083. local isGameStart = self.roomInfo.nGameStartCount>0
  1084. --如果是游戏开始后收到消息则不处理
  1085. local userInfoList = {}
  1086. for nUserId, memberInfo in pairs(self.roomInfo.memberList) do
  1087. local nSeatId = memberInfo.nSeatId
  1088. local userInfo = memberInfo.userInfo
  1089. if userInfo then
  1090. local userInfo2 = json.decode(userInfo)
  1091. if userInfo2 then
  1092. userInfoList[nUserId] = {nSeatId = nSeatId, userInfo = userInfo}
  1093. end
  1094. end
  1095. end
  1096. if isGameStart then
  1097. isOpenView = false
  1098. end
  1099. self:dispatchEvent({
  1100. name = GAME_EVENT.GPS_UPDATE_USER_INFO,
  1101. userId=userId,
  1102. userInfoList = userInfoList,
  1103. isOpenView = isOpenView or false,
  1104. isGameStart = isGameStart,
  1105. -- maxPlayerNum = self.roomInfo.nMaxPlayCount,
  1106. })
  1107. end
  1108. end
  1109. -- 申请托管
  1110. function Room:requestTuoGuan(optType)
  1111. local request = ZPMessage.TuoGuan:new()
  1112. request.optType = optType;
  1113. logD("requestTuoGuan",table.tostring(request))
  1114. self:sendResponse{cmd = ZPCmd.GAME_COMMAND_REQUEST_TUOGUAN , sender = request};
  1115. end
  1116. function Room:onTuoGuanResponse(status, response)
  1117. logD("Room:onTuoGuanResponse(), ", table.tostring(response))
  1118. if not self.roomInfo then
  1119. logD("self.roomInfo no exist,容错处理!")
  1120. return
  1121. end
  1122. self:dispatchEvent({name = "onTuoGuanResponse",response = response});
  1123. end
  1124. return Room;