您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1243 行
43 KiB

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