您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字

1293 行
36 KiB

  1. local RoomCmd=ZPFramework.ZPImport("zp_base.luaScript.Protocol.ZPCmd")
  2. local ZPMessage=ZPFramework.ZPImport("zp_base.luaScript.Protocol.ZPMessage")
  3. local ZPDef=ZPFramework.ZPImport("zp_base.luaScript.ZPDef")
  4. local ZPFuc=ZPFramework.ZPImport("zp_base.luaScript.ZPFunctions")
  5. local ZPProtocol=ZPFramework.ZPFrameworkClassImprot("zp_base.luaScript.Protocol.ZPProtocol")
  6. local Room = class("changpaiRoom" , ZPProtocol)
  7. local tempCardVlue ={
  8. [17] = 20, --11
  9. [18] = 30, --12
  10. [49] = 40, --31
  11. [34] = 41, --22
  12. [20] = 50, --14
  13. [50] = 51, --32
  14. [21] = 60, --15
  15. [36] = 61, --24
  16. [51] = 62, --33
  17. [22] = 70, --16
  18. [82] = 71, --52
  19. [52] = 72, --34
  20. [38] = 80, --26
  21. [53] = 81, --35
  22. [68] = 82, --44
  23. [54] = 90, --36
  24. [84] = 91, --54
  25. [70] = 100, --46
  26. [85] = 101, --55
  27. [86] = 110, --56
  28. [102] = 120, --66
  29. }
  30. function Room:ctor(net)
  31. Room.super.ctor(self , net);
  32. --请求重连消息
  33. self:defMsgFunc{name = ZPDef.ZPEvent.GetTableInfo ,cmd = RoomCmd.GAME_COMMAND_GET_TABLE_INFO};
  34. --发送刷新手牌请求
  35. self:defMsgFunc{name = "reflashHandCard",cmd = RoomCmd.GAME_COMMAND_REFLASH_HANDCARDS,sender = ZPMessage.ReflashHandCard};
  36. --发送起手操作码
  37. self:defMsgFunc{name = "sendQiShouOperate" , cmd = RoomCmd.GAME_COMMAND_QI_SHOU_SEND_OPCODE, sender = ZPMessage.GameQiShouSendOperateCodeResponse};
  38. --接收起手操作提示
  39. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_OPERATE_TISHI, reader = ZPMessage.UserOperateNotify, func = handler(self, self.onUserOperateResponse)};
  40. --广播起手操作
  41. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_QI_SHOU_BROADCARST, reader = ZPMessage.GameQiShouBroadCastOperate, func = handler(self, self.onBroadCastQiShouOperate)};
  42. --广播玩家进牌
  43. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_USER_GETCARD, reader = ZPMessage.GameUserGetCard, func = handler(self, self.onBroadCastUserGetCard)};
  44. --广播头家出牌
  45. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_BANKER_OUT_CARD, reader = ZPMessage.GameBankerOutCard, func = handler(self, self.onBroadCastBankerOutCard)};
  46. --通知玩家包赔
  47. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_CHECK_USER_BAOPEI, reader = ZPMessage.BaoPei, func = handler(self, self.onOutCardIsBaoPeiResponse)};
  48. --玩家进牌后显示的操作
  49. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_TINGCARD_RESPONSE, reader = ZPMessage.GameUserGetCardOperateNotify, func = handler(self, self.onUserGetCardOperteResponse)};
  50. --刷新手牌
  51. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_REFLASH_HANDCARDS_RESPONSE, reader = ZPMessage.ReflashHandcardsResponse, func = handler(self , self.onReflashHandcardsResponese)};
  52. --抢巴胡
  53. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_QIANG_BA_HU, reader = ZPMessage.QiangBaResponse, func = handler(self , self.onQiangBaResponese)};
  54. --过巴变灰
  55. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_GUOBA_LOAK_CARD, reader = ZPMessage.GuoBaResponse, func = handler(self, self.onGuoBaResponse)};
  56. -- 请求托管
  57. self:defMsgFunc{name = ZPDef.ZPEvent.HostingRequest, cmd = RoomCmd.GAME_COMMAND_USER_HOSTING_REQUEST, sender = ZPMessage.HostingRequest}
  58. -- 收到托管回调
  59. self:defPushMsg{cmd = RoomCmd.GAME_COMMAND_USER_HOSTING_RESPONSE, reader = ZPMessage.HostingResponse, func = handler(self,self.onHostingResponse)}
  60. end
  61. -- 获取房间最大玩家人数
  62. function Room:getMaxPlayerCount()
  63. return self.roomInfo.nMaxPlayCount
  64. end
  65. -- 获取房间人数
  66. function Room:getPlayerCount()
  67. local count = 0
  68. for k,v in pairs(self.roomInfo.memberList) do
  69. count = count + 1
  70. end
  71. return count
  72. end
  73. -- 重置上局信息
  74. function Room:resetRound()
  75. self.roomInfo.quickStartInfo = nil
  76. end
  77. function Room:resetFastInfo()
  78. self.roomInfo.quickStartInfo = nil
  79. end
  80. function Room:isMyself(uid)
  81. return uid==self:getMyUserId()
  82. end
  83. -- 更新玩家的相对椅子号
  84. function Room:updateUserSeateShowId(xiaoSeatId)
  85. local myUserId = self:getMyRecordUserId()
  86. local mySeatId = self.roomInfo.memberList[myUserId].nSeatId
  87. self.roomInfo.nUserId = self:getMyRecordUserId()
  88. self.roomInfo.nSeatId = mySeatId
  89. --除了自己的第一次seatid辅助
  90. local anotherSeatID = -1
  91. --[视图椅子号] = userID
  92. --[座位号] = userID
  93. --[userID] = 视图椅子号
  94. self.seatShowList = {}
  95. self.userList = {}
  96. self.seatList = {}
  97. for k,v in pairs(self.roomInfo.memberList) do
  98. if v.nUserId and v.nSeatId then
  99. local maxNum = 3--ZPFuc.getCreateRoomPlayerNum()
  100. local isChange = false
  101. if app.room:getActualPlayerNum() >= 3 then
  102. maxNum = 4
  103. end
  104. local nSeatShowId --= (v.nSeatId - mySeatId + 4) % 4
  105. if mySeatId <= v.nSeatId then
  106. nSeatShowId = v.nSeatId - mySeatId
  107. elseif mySeatId > v.nSeatId then
  108. nSeatShowId = maxNum - mySeatId + v.nSeatId
  109. end
  110. if app.room:getActualPlayerNum() == 2 then--self.roomInfo.playerNum == 2 then
  111. if (v.nSeatId == mySeatId) then
  112. nSeatShowId = 4
  113. else
  114. nSeatShowId = 2--两人玩时,对家坐哪里
  115. end
  116. elseif app.room:getActualPlayerNum() == 3 and v.nUserId ~= 0 then
  117. if (v.nSeatId == mySeatId) then
  118. nSeatShowId = 4
  119. else
  120. --local nSeatShowId = (v.nSeatId - mySeatId + 4) % 4
  121. if nSeatShowId == 2 then
  122. if mySeatId > v.nSeatId then
  123. nSeatShowId = nSeatShowId + 1
  124. isChange = true
  125. else
  126. isChange = true
  127. nSeatShowId = nSeatShowId - 1
  128. end
  129. end
  130. if nSeatShowId == 0 then
  131. if mySeatId > v.nSeatId then
  132. nSeatShowId = nSeatShowId + 1
  133. else
  134. nSeatShowId = 3
  135. end
  136. elseif (nSeatShowId == 1 or nSeatShowId == 3) and anotherSeatID >= 0 then
  137. isChange = true
  138. end
  139. if anotherSeatID < 0 and not isChange then
  140. anotherSeatID = v.nSeatId
  141. end
  142. end
  143. elseif app.room:getActualPlayerNum() == 4 and v.nUserId ~= 0 then
  144. if (v.nSeatId == mySeatId) and (v.nSeatId ~= self.roomInfo.xiaoSeatId) then
  145. nSeatShowId = 4
  146. else
  147. if xiaoSeatId and xiaoSeatId >=0 and nSeatShowId == 2 then
  148. if mySeatId == xiaoSeatId then
  149. if math.abs(mySeatId - v.nSeatId) == 2 then
  150. nSeatShowId = 4
  151. end
  152. elseif mySeatId > v.nSeatId then
  153. isChange = true
  154. nSeatShowId = nSeatShowId + 1
  155. else
  156. isChange = true
  157. nSeatShowId = nSeatShowId - 1
  158. end
  159. else
  160. end
  161. if xiaoSeatId and v.nSeatId == self.roomInfo.xiaoSeatId then
  162. nSeatShowId = 2
  163. end
  164. end
  165. --强制转换小家视角与庄家一致
  166. if mySeatId == xiaoSeatId then
  167. if nSeatShowId == 1 then
  168. nSeatShowId = 3
  169. elseif nSeatShowId == 3 then
  170. nSeatShowId = 1
  171. end
  172. end
  173. end
  174. if nSeatShowId == 0 then
  175. nSeatShowId = 4
  176. end
  177. if v.nUserId == 0 then
  178. nSeatShowId = 2
  179. if app.room:getActualPlayerNum() == 2 then
  180. nSeatShowId = 1
  181. end
  182. else
  183. if self.seatShowList[nSeatShowId] and isChange == true then
  184. if nSeatShowId == 1 then
  185. nSeatShowId = 3
  186. elseif nSeatShowId == 3 then
  187. nSeatShowId = 1
  188. end
  189. elseif self.seatShowList[nSeatShowId] and isChange == false then
  190. local olduid = self.seatShowList[nSeatShowId]
  191. local oldshowid = self.userList[olduid]
  192. local newshowid = 0
  193. if nSeatShowId == 1 then
  194. newshowid = 3
  195. elseif nSeatShowId == 3 then
  196. newshowid = 1
  197. end
  198. self.seatShowList[newshowid] = olduid
  199. self.userList[olduid] = newshowid
  200. end
  201. end
  202. self.seatShowList[nSeatShowId] = v.nUserId
  203. self.seatList[v.nSeatId] = v.nUserId
  204. self.userList[v.nUserId] = nSeatShowId
  205. end
  206. end
  207. logD("User:updateUserSeateShowId(), seatShowList", table.tostring(self.seatShowList))
  208. logD("User:updateUserSeateShowId(),seatList ", table.tostring(self.seatList))
  209. logD("User:updateUserSeateShowId(),userList ", table.tostring(self.userList))
  210. end
  211. --[[-- 玩家进入桌子成功
  212. function Room:onSitDownSuccessResponse(status, response)
  213. logD("--------------------玩家进入桌子成功--------------------")
  214. logD("Room:onSitDownSuccessResponse(), ", table.tostring(response))
  215. app.net:onMsgClear()
  216. app.net:onMsgPause()
  217. -- 记录当前桌子号
  218. self:initRoomInfo()
  219. -- 获取到的房间信息
  220. self.roomInfo.nShowTableId = response.nShowTableId
  221. self.roomInfo.nMaxPlayCount = response.nMaxPlayCount
  222. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid
  223. self.roomInfo.nTotalGameNum = response.nTotalGameNum
  224. self.roomInfo.strGameInfo = response.strGameInfo
  225. -- 桌子上其他玩家的信息
  226. for k,v in pairs(response.memberList) do
  227. self.roomInfo.memberList[v.nUserId] = v
  228. end
  229. -- 更新椅子号
  230. self:updateUserSeateShowId()
  231. logD("Room:onSitDownSuccessResponse() roomInfo = ", table.tostring(self.roomInfo))
  232. -- 发送通知
  233. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = GAME_IDS.luzhouDaEr, gameType = self.roomInfo.strGameInfo.gamerule})
  234. end--]]
  235. -- 游戏正式开始
  236. function Room:onGameStartResponse(status, response)
  237. logD("-------------------- 新的一局开始了 ------------------------")
  238. if not self.roomInfo then
  239. logD("self.roomInfo no exist,容错处理!")
  240. return
  241. end
  242. logD("Room:onGameStartResponse(), ", table.tostring(response))
  243. self.roomInfo.nGameStartCount = response.nGameStartCount;
  244. self.roomInfo.nTotalGameNum = response.nTotalGameNum;
  245. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid;
  246. self.roomInfo.nBankSeatId = response.nBankSeatId;
  247. self.roomInfo.xiaoSeatId = response.xiaoSeatId;
  248. self.roomInfo.fanpaiSeatId = response.turnSeatId;
  249. self.roomInfo.fanpaiValue = response.turnCard;
  250. --清空上一局胡牌信息
  251. self.roomInfo.hupaiInfo = nil
  252. -- 清空上一局的手牌信息
  253. self.cards = {}
  254. -- 发送广播通知,游戏开始了
  255. self:dispatchEvent({name = "onGameStartResponse"});
  256. end
  257. -- 游戏发牌结果
  258. function Room:onGameSendCardResponse(status, response)
  259. logD("-------------------- 游戏发牌 ------------------------")
  260. if not self.roomInfo then
  261. logD("self.roomInfo no exist,容错处理!")
  262. return
  263. end
  264. logD("Room:onGameSendCardResponse(), ", table.tostring(response))
  265. self.roomInfo.nbankFirstGrabCard = response.nbankFirstGrabCard
  266. --[[
  267. --test
  268. response.cardList = {}
  269. response.cardList = {
  270. {
  271. type = 1,
  272. card = {0x11,0x22,0x14,0x15}
  273. },
  274. {
  275. type = 2,
  276. card = {0x11,0x22,0x14,0x15}
  277. },
  278. {
  279. type = 3,
  280. card = {0x11,0x22,0x14,0x15}
  281. },
  282. {
  283. type = 4,
  284. card = {0x11,0x22,0x14,0x15}
  285. }
  286. }
  287. ]]
  288. --目前server只发自己过来,后续战绩回放考虑到会发所有玩家来,故以userid为key于扩展
  289. local cardData = self:serverCardToMyCard(response.cardList)
  290. logD("Room:onGameSendCardResponse : "..table.tostring(cardData))
  291. self.roomInfo.memberList[response.nUserId].cardList = cardData
  292. self.cards[response.nUserId] = cardData
  293. if self:isLittleFamily() then
  294. self.roomInfo.nBankUid = response.nUserId
  295. self.cards[app.user.loginInfo.uid] = cardData
  296. end
  297. self.roomInfo.totalTuoNum = response.totalTuoNum
  298. -- 发送广播通知,发牌开始了
  299. self:dispatchEvent({name = "onGameSendCardResponse"});
  300. end
  301. function Room:onUserOperateResponse(status, response)
  302. logD("------------提示用户偷巴操作---------------")
  303. if not self.roomInfo then
  304. logD("self.roomInfo no exist,容错处理!")
  305. return
  306. end
  307. logD("Room:onUserOperateResponse(), ", table.tostring(response))
  308. --self.roomInfo.TouoperateCard = response.ToucardList
  309. --self.roomInfo.BaoperateCard = response.BacardList
  310. if self:isMyself(response.nUserId) then
  311. self.roomInfo.totoalTuoNum = response.totalTuoNum
  312. end
  313. self:dispatchEvent({name = "onUserOperateResponse",response = response})
  314. end
  315. function Room:onBroadCastQiShouOperate(status, response)
  316. logD("------------广播用户偷巴操作---------------")
  317. if not self.roomInfo then
  318. logD("self.roomInfo no exist,容错处理!")
  319. return
  320. end
  321. logD("Room:onBroadCastQiShouOperate(), ", table.tostring(response))
  322. if self:isMyself(response.nUserId) then
  323. self.roomInfo.totoalTuoNum = response.totalTuoNum
  324. end
  325. self:dispatchEvent({name = "onBroadCastQiShouOperate",response = response})
  326. end
  327. function Room:onBroadCastUserGetCard(status, response)
  328. logD("------------广播用户进牌操作---------------")
  329. if not self.roomInfo then
  330. logD("self.roomInfo no exist,容错处理!")
  331. return
  332. end
  333. logD("Room:onBroadCastUserGetCard(), ", table.tostring(response))
  334. self:dispatchEvent({name = "onBroadCastUserGetCard",response = response})
  335. end
  336. function Room:onGuoBaResponse(status, response)
  337. logD("------------广播用户过巴操作---------------")
  338. if not self.roomInfo then
  339. logD("self.roomInfo no exist,容错处理!")
  340. return
  341. end
  342. logD("Room:onGuoBaResponse(), ", table.tostring(response))
  343. self:dispatchEvent({name = "onGuoBaResponse",response = response})
  344. end
  345. function Room:onBroadCastBankerOutCard(status, response)
  346. logD("------------通知头家出牌--------------")
  347. if not self.roomInfo then
  348. logD("self.roomInfo no exist,容错处理!")
  349. return
  350. end
  351. logD("Room:onBroadCastBankerOutCard(), ", table.tostring(response))
  352. self:dispatchEvent({name = "onBroadCastBankerOutCard",response = response})
  353. end
  354. function Room:onOutCardIsBaoPeiResponse(status, response)
  355. logD("------------通知打出的牌可能包赔--------------")
  356. if not self.roomInfo then
  357. logD("self.roomInfo no exist,容错处理!")
  358. return
  359. end
  360. logD("Room:onOutCardIsBaoPeiResponse(), ", table.tostring(response))
  361. self:dispatchEvent({name = "onOutCardIsBaoPeiResponse",response = response})
  362. end
  363. function Room:onUserGetCardOperteResponse(status, response)
  364. logD("------------进牌后通知玩家显示操作--------------")
  365. if not self.roomInfo then
  366. logD("self.roomInfo no exist,容错处理!")
  367. return
  368. end
  369. logD("Room:onUserGetCardOperteResponse(), ", table.tostring(response))
  370. self:dispatchEvent({name = "onUserGetCardOperteResponse",response = response})
  371. end
  372. function Room:onQiangBaResponese(status, response)
  373. logD("------------通知玩家抢巴操作--------------")
  374. if not self.roomInfo then
  375. logD("self.roomInfo no exist,容错处理!")
  376. return
  377. end
  378. logD("Room:onQiangBaResponese(), ", table.tostring(response))
  379. self:dispatchEvent({name = "onQiangBaResponese",response = response})
  380. end
  381. -- 获取自己是否是小家(观看的人) 且选了小家可以看牌才返回true 长牌用不到
  382. function Room:isLittleFamily()
  383. return false
  384. end
  385. --小局结算
  386. function Room:onGameXiaoJuResponse(status, response)
  387. logD("--------------------小局结算 ------------------------")
  388. if not self.roomInfo then
  389. logD("self.roomInfo no exist,容错处理!")
  390. return
  391. end
  392. if self:getActualPlayerNum() == 2 then
  393. local handcards = self:rankHandCard(response.thirdHandcards);
  394. -- handcards = self:serverCardToMyCard(handcards);
  395. local userId = 0;
  396. local player = {
  397. nUserId = userId,
  398. nTurnScore = 0,
  399. nTotalMoney = 0,
  400. nBaoPaiStatus = 0,
  401. totalTuoNum = 0,
  402. tableWeaveCard = {},
  403. handCard = handcards,
  404. desertedArea = 0,
  405. };
  406. response.playList.Datas[userId] = player;
  407. end
  408. logD("Room:onGameXiaoJuResponse() :", table.tostring(response))
  409. self:dispatchEvent({name = "onGameXiaoJuResponse",response = response});
  410. end
  411. --断线重连
  412. function Room:onGameReconnection(status,response)
  413. logD("===============重连成功,清空之前所有存在的队列==================")
  414. app.net:onMsgClear()
  415. app.net:onMsgPause()
  416. logD("Room:onGameReconnection(), response = ", table.tostring(response))
  417. self:initRoomInfo();
  418. -- 获取到的房间信息
  419. self.roomInfo.nShowTableId = response.nShowTableId
  420. self.roomInfo.nMaxPlayCount = response.nMaxPlayCount
  421. self.roomInfo.nRoomOwnedUid = response.nRoomOwnedUid
  422. self.roomInfo.nTotalGameNum = response.nTotalGameNum
  423. self.roomInfo.nGameStartCount = response.nGameStartCount
  424. self.roomInfo.strGameInfo = response.strGameInfo
  425. self.roomInfo.nStatus = response.nStatus
  426. self.roomInfo.nDismissToTalTime = response.nDismissToTalTime
  427. self.roomInfo.nBankSeatId = response.nBankSeatId
  428. self.roomInfo.nZhongBankCnt = response.nZhongBankCnt
  429. self.roomInfo.bUserDisbandGame = response.bUserDisbandGame
  430. self.roomInfo.nDismissStateTime = response.nDismissStateTime
  431. self.roomInfo.arrayTableInfo = response.arrayTableInfo or {}
  432. self.roomInfo.dipaiList = response.dipaiList
  433. self.roomInfo.showCardSeatId = response.showCardSeatId
  434. self.roomInfo.showCard = response.showCard
  435. self.roomInfo.mainOpCode = response.mainOpCode
  436. self.roomInfo.isSubmitOp = response.isSubmitOp
  437. self.roomInfo.showCardFlag = response.showCardFlag
  438. self.roomInfo.outCardSeatId = response.outCardSeatId
  439. self.roomInfo.lastOperate = response.lastOperate
  440. self.roomInfo.lastOpCard = response.lastOpCard
  441. self.roomInfo.disCardSeatId = response.disCardSeatId
  442. self.roomInfo.disCard = {}
  443. self.roomInfo.leftZhaBirdTime = response.leftZhaBirdTime
  444. self.roomInfo.stopFlag = response.stopFlag
  445. self.roomInfo.winUserId = response.winUserId
  446. self.roomInfo.dianPaoUserId = response.dianPaoUserId
  447. self.roomInfo.huType = response.huType
  448. self.roomInfo.huCard = response.huCard
  449. self.roomInfo.tingCards = response.tingCards
  450. self.roomInfo.hupaiInfo = response.hupaiInfo or {}
  451. self.roomInfo.xiaoSeatId = response.xiaoSeatId;
  452. self.roomInfo.offLineInfo = response.offLineInfo
  453. self.roomInfo.fanpaiSeatId = response.fanpaiSeatId
  454. self.roomInfo.chiPaiList = response.chiCardList or {}
  455. self.roomInfo.isBaopeiCard = response.isBaopeiCard
  456. self.roomInfo.totalTuoNum = response.totalTuoNum
  457. self.roomInfo.curOpreateID = response.curOpreateID
  458. self.roomInfo.isBaopeiGuo = response.isBaopeiGuo
  459. self.roomInfo.houzhuaCard = response.houzhuaCard or {}
  460. self.roomInfo.curEnterCard = response.curEnterCard
  461. self.roomInfo.isZiMoHu = response.isZiMoHu
  462. self.roomInfo.isQiangBaHu = response.isQiangBaHu
  463. for k,v in ipairs(response.disCard or {}) do
  464. table.insert(self.roomInfo.disCard,v)
  465. end
  466. for k,v in ipairs(response.afterbadisCard or {}) do
  467. table.insert(self.roomInfo.disCard,v)
  468. end
  469. --起手操作
  470. self.roomInfo.startOpList = {}
  471. self.roomInfo.startOpList["ishu"] = response.ishu
  472. self.roomInfo.startOpList["TouoperateCode"] = response.TouoperateCode
  473. self.roomInfo.startOpList["ToucardList"] = response.ToucardList or {}
  474. self.roomInfo.startOpList["BaoperateCode"] = response.BaoperateCode
  475. self.roomInfo.startOpList["BacardList"] = response.BacardList or {}
  476. self.roomInfo.startOpList["totalTuoNum"] = response.totalTuoNum
  477. self:addReconnectionData(response)
  478. if self.roomInfo.offLineInfo and next(self.roomInfo.offLineInfo) ~= nil then
  479. for i,v in pairs(self.roomInfo.offLineInfo) do
  480. if v.extInfo then
  481. local extIf = json.decode(v.extInfo)
  482. if extIf.OffTime and v.nUserId then
  483. self.offLineTime[v.nUserId] = extIf.OffTime
  484. end
  485. end
  486. end
  487. end
  488. for i,v in pairs(self.roomInfo.hupaiInfo) do
  489. self.roomInfo.dianPaoUserId = v.dianPaoUid
  490. self.roomInfo.huCard = v.huCard
  491. end
  492. if app.club_php.clubID and app.club_php.clubID ~= 0 and self.roomInfo.nGameStartCount > 0 then
  493. app.club_php:getClubList();
  494. end
  495. -- 桌子上其他玩家的信息
  496. for k,v in pairs(response.arrayTableInfo) do
  497. if not self.roomInfo.memberList[v.nUserId] then
  498. self.roomInfo.memberList[v.nUserId] = {}
  499. end
  500. self.roomInfo.memberList[v.nUserId].nUserId = v.nUserId
  501. self.roomInfo.memberList[v.nUserId].nSeatId = v.nSeatId
  502. self.roomInfo.memberList[v.nUserId].nPlayerFlag = v.nPlayerFlag
  503. self.roomInfo.memberList[v.nUserId].userInfo = v.userInfo
  504. self.roomInfo.memberList[v.nUserId].nTotalMoney = v.nTotalMoney
  505. self.roomInfo.memberList[v.nUserId].nOnlineStatus = v.nOnlineStatus
  506. --这里只记录,暂时没用到。可能战绩回放的时候是需要用的
  507. local cardData = self:serverCardToMyCard(v.handCard)
  508. self.roomInfo.memberList[v.nUserId].cardList = {}
  509. self.roomInfo.memberList[v.nUserId].cardList = cardData
  510. if v.nSeatId == self.roomInfo.nBankSeatId then
  511. self.roomInfo.nBankUid = v.nUserId --头家UID
  512. end
  513. end
  514. if self:getActualPlayerNum() == 2 and table.nums(response.thirdHandcards) > 0 then
  515. local userId = 0
  516. local thirdHandcards = self:rankHandCard(response.thirdHandcards)
  517. thirdHandcards = self:serverCardToMyCard(thirdHandcards)
  518. if not self.roomInfo.memberList[userId] then
  519. self.roomInfo.memberList[userId] = {}
  520. end
  521. self.roomInfo.memberList[userId].cardList = thirdHandcards
  522. if self.roomInfo.arrayTableInfo[userId] then
  523. self.roomInfo.arrayTableInfo[userId].Datas.handCard.Datas = self:rankHandCard(response.thirdHandcards)
  524. end
  525. end
  526. local myUserId = self:getMyRecordUserId()
  527. for i,v in pairs(self.roomInfo.memberList) do
  528. if self:isLittleFamily() and self.roomInfo.nBankSeatId == v.nSeatId then
  529. self.roomInfo.memberList[myUserId].cardList = v.cardList
  530. end
  531. end
  532. --更新自己的数据
  533. self.cards[myUserId] = self.roomInfo.memberList[myUserId].cardList
  534. print("self.cards[myUserId]:"..table.tostring(self.cards[myUserId]))
  535. --检测GPS
  536. self:updateGpsUserInfo(nil,false)
  537. -- 更新椅子号
  538. self:updateUserSeateShowId()
  539. local jsonInfo = json.decode(self.roomInfo.strGameInfo)
  540. self:resetRound()
  541. --没用到
  542. local extInfo = json.decode(response.extJson or "")
  543. if extInfo and type(extInfo)=='table' and extInfo[tostring(self:getMyUserId())] then
  544. local myinfo = extInfo[tostring(self:getMyUserId())]
  545. self.roomInfo.hosting = myinfo.hosted or 0--托管状态
  546. self.roomInfo.quickStartInfo = extInfo.faststart--提前开局
  547. else
  548. self.roomInfo.hosting = 0
  549. end
  550. -- 发送通知
  551. self:dispatchEvent({name = "onEnterRoomSuccess", gameId = ZPDef.GameID or app.gameId, gameType = jsonInfo.gamerule})
  552. end
  553. --获取手牌结果
  554. function Room:onGetHandcardsResponese(status, response)
  555. end
  556. --获取手牌结果
  557. function Room:onReflashHandcardsResponese(status, response)
  558. print("Room:onReflashHandcardsResponese()", table.tostring(response))
  559. local cardData = self:serverCardToMyCard(response.handCard)
  560. self.roomInfo.memberList[response.nUserId].cardList = {}
  561. self.roomInfo.memberList[response.nUserId].cardList = cardData
  562. self.cards[response.nUserId] = cardData
  563. self:dispatchEvent({name = "onReflashHandcardsResponese"});
  564. end
  565. -- 系统发牌后发来的自动提操作
  566. function Room:onSystemOperationStart(status, response)
  567. logD("-------------------- 等待玩家报叫------------------------")
  568. if not self.roomInfo then
  569. logD("self.roomInfo no exist,容错处理!")
  570. return
  571. end
  572. logD("Room:onSystemOperationStart(), ", table.tostring(response))
  573. end
  574. function Room:onOutCardError(status,response)
  575. logD("-------------------- 出牌错误 ------------------------")
  576. if not self.roomInfo then
  577. print("容错处理")
  578. return
  579. end
  580. logD("Room:onOutCardError(), response = ", table.tostring(response))
  581. --[[错误码说明:
  582. OUT_CARD_SUCCESS = 0, //正常
  583. OUT_CARD_ON_NEED = 1, //不需要出牌
  584. OUT_CARD_INVALID = 2, //出牌非法
  585. OUT_CARD_BAOPEI = 3, //不出包赔牌--]]
  586. local errorCode = response.result
  587. if errorCode == ZPDef.OutCardErrorTip.OUT_CARD_SUCCESS then
  588. showTooltip("出牌无效")
  589. elseif errorCode == ZPDef.OutCardErrorTip.OUT_CARD_SUCCESS then
  590. showTooltip("不需要出牌")
  591. elseif errorCode == ZPDef.OutCardErrorTip.OUT_CARD_BAOPEI then
  592. showTooltip("请重新选择!")
  593. else
  594. showTooltip("出牌未知")
  595. end
  596. self:dispatchEvent({name = "onOutCardError"});
  597. end
  598. --[[
  599. 思维:
  600. 服务器把牌都排序好了,客户端只要拿到数据负责显示
  601. ]]
  602. function Room:serverCardToMyCard(serverCardList)
  603. --
  604. --根据定义组合牌
  605. local cardData = {}
  606. --单个组合索引
  607. local index = 1
  608. --真实索引
  609. local realIndex = 1
  610. local tempserverCardlist = {}
  611. local singleCardList = {}
  612. for k,v in ipairs(serverCardList) do
  613. table.insert(tempserverCardlist,v.card)
  614. end
  615. table.sort(tempserverCardlist,function(a,b)
  616. local index = a[1]
  617. local numA = tempCardVlue[index]
  618. local index1 = b[1]
  619. local numB = tempCardVlue[index1]
  620. if numA ~= nil and numB ~= nil then
  621. return numA < numB
  622. end
  623. end)
  624. logD("Room:sortMycard : "..table.tostring(tempserverCardlist))
  625. for k,v in ipairs(tempserverCardlist) do
  626. local list = v
  627. if #list <= 5 then
  628. --如果是四张以下直接放一列
  629. local cardlist = {}
  630. for k,v in ipairs(list) do
  631. table.insert(cardlist,v)
  632. end
  633. --先按值牌序
  634. table.sort(cardlist,function(a,b)
  635. local numA = tonumber(a)
  636. local numB = tonumber(b)
  637. if numA ~= nil and numB ~= nil then
  638. return numA < numB
  639. end
  640. end)
  641. --按点数排序
  642. table.sort(cardlist,function(a,b)
  643. local numA = tempCardVlue[a]
  644. local numB = tempCardVlue[b]
  645. if (numA ~= nil) and (numB ~= nil) then
  646. return numA < numB
  647. end
  648. return false
  649. end)
  650. cardData[realIndex] = cardlist
  651. realIndex = realIndex + 1
  652. else --大于五张要分开
  653. --首先考虑三张一样的
  654. local cardlist = {}
  655. for k,v in ipairs(list) do
  656. table.insert(cardlist,v)
  657. end
  658. --local tempCardList = self:getSameVlaueList(cardlist)
  659. local tempCardList = self:spiltSameList(cardlist)
  660. local tempResultCardList = {}
  661. for k,v in pairs(tempCardList) do
  662. table.insert(tempResultCardList,v)
  663. end
  664. --按点数排序
  665. table.sort(tempResultCardList,function(a,b)
  666. local index = a[1]
  667. local numA = tempCardVlue[index]
  668. local index1 = b[1]
  669. local numB = tempCardVlue[index1]
  670. if (numA ~= nil) and (numB ~= nil) then
  671. return numA < numB
  672. end
  673. return false
  674. end)
  675. for k,templist in pairs (tempResultCardList) do
  676. local cardlist = templist
  677. cardData[realIndex] = cardlist
  678. realIndex = realIndex + 1
  679. end
  680. --[[ for k,templist in pairs(tempResultCardList) do
  681. if #templist > 5 then
  682. local same,sameValue = self:checkListHaveSame(templist)
  683. local indexValue = {}
  684. if same then
  685. for k,v in ipairs(sameValue) do
  686. local cardlist = {}
  687. for i = 1,#templist do
  688. if templist[i] == tonumber(v) then
  689. table.insert(cardlist,templist[i])
  690. table.insert(indexValue,i) --保存重复的索引值
  691. end
  692. end
  693. cardData[realIndex] = cardlist
  694. realIndex = realIndex + 1
  695. end
  696. local ttlist = {}
  697. for i = 1,#templist do
  698. if not self:isHasInCardlist(i,indexValue) then
  699. table.insert(ttlist,templist[i])
  700. end
  701. end
  702. if #ttlist >= 1 then
  703. local cardlist = {}
  704. for k,v in ipairs(ttlist) do
  705. table.insert(cardlist,v)
  706. end
  707. cardData[realIndex] = cardlist
  708. realIndex = realIndex + 1
  709. end
  710. else
  711. local cardlist = {}
  712. for i = 1,#templist do
  713. table.insert(cardlist,templist[i])
  714. if #cardlist == 5 then
  715. cardData[realIndex] = cardlist
  716. realIndex = realIndex + 1
  717. cardlist = {}
  718. end
  719. end
  720. if #cardlist >= 1 then
  721. cardData[realIndex] = cardlist
  722. realIndex = realIndex + 1
  723. end
  724. end
  725. else
  726. local cardlist = templist
  727. cardData[realIndex] = cardlist
  728. realIndex = realIndex + 1
  729. end
  730. end--]]
  731. end
  732. end
  733. --最后判断单张列表是否还有值
  734. if #singleCardList > 0 then
  735. cardData[realIndex] = singleCardList
  736. realIndex = realIndex + 1
  737. end
  738. logD("Room:resultCardData : "..table.tostring(cardData))
  739. return cardData
  740. end
  741. function Room:checkListHaveSame(list)
  742. local tt = {}
  743. local same = false
  744. local sameValue = {}
  745. for i = 1,#list do
  746. local value = list[i]
  747. if tt[value] then
  748. tt[value] = tt[value] + 1
  749. else
  750. tt[value] = 1
  751. end
  752. end
  753. for k,v in pairs(tt) do
  754. if v >= 3 then
  755. same = true
  756. table.insert(sameValue,k)
  757. end
  758. end
  759. return same,sameValue
  760. end
  761. --获取点数相同的列数
  762. function Room:getSameVlaueList(list)
  763. local tt = {}
  764. table.sort(list,function(a,b)
  765. local numA = tempCardVlue[tonumber(a)]
  766. local numB = tempCardVlue[tonumber(b)]
  767. if numA ~= nil and numB ~= nil then
  768. return numA < numB
  769. end
  770. end)
  771. for i = 1,#list do
  772. local value = math.modf(tempCardVlue[tonumber(list[i])]/10)
  773. if tt[value] then
  774. table.insert(tt[value],list[i])
  775. else
  776. tt[value] = {}
  777. table.insert(tt[value],list[i])
  778. end
  779. end
  780. return tt
  781. end
  782. --拆分数组中对子和三个以上的值
  783. function Room:spiltSameList(list)
  784. local tt = {}
  785. local templist = list
  786. --按点数从小到大排序
  787. table.sort(templist,function(a,b)
  788. local numA = tempCardVlue[tonumber(a)]
  789. local numB = tempCardVlue[tonumber(b)]
  790. if (numA ~= nil) and (numB ~= nil) then
  791. return numA < numB
  792. end
  793. return false
  794. end)
  795. local ttValue = {}
  796. for i = 1,#list do
  797. local value = list[i]
  798. if ttValue[value] then
  799. ttValue[value] = ttValue[value] + 1
  800. else
  801. ttValue[value] = 1
  802. end
  803. end
  804. local ttResult = {}
  805. for k,v in pairs(ttValue) do
  806. local cardlist = {}
  807. local card = tonumber(k)
  808. cardlist.card = {}
  809. cardlist.value = math.modf(tempCardVlue[tonumber(k)]/10)
  810. cardlist.num = v
  811. for i = 1,v do
  812. table.insert(cardlist.card,tonumber(k))
  813. end
  814. table.insert(ttResult,cardlist)
  815. end
  816. --先把加起来等于5的分开出来
  817. local index = 1
  818. for i = 1, #ttResult do
  819. local resultData = ttResult[i]
  820. if resultData.num then
  821. for j = i+1,#ttResult do
  822. local resultData1 = ttResult[j]
  823. if resultData1.num then
  824. --把加起来为4的列数都先放好
  825. if ((resultData1.num + resultData.num == 5) and (resultData.num == 1 or resultData.num == 4) and resultData.value == resultData1.value)
  826. or ((resultData1.num + resultData.num == 5) and (resultData.num == 3 or resultData.num == 2) and resultData.value == resultData1.value) then
  827. local cardlist = {}
  828. for k,v in pairs(resultData1.card) do
  829. table.insert(cardlist,v)
  830. end
  831. for k,v in pairs(resultData.card) do
  832. table.insert(cardlist,v)
  833. end
  834. tt[index] = cardlist
  835. index = index + 1
  836. ttResult[i] = {}
  837. ttResult[j] = {}
  838. break
  839. end
  840. end
  841. end
  842. end
  843. end
  844. --再把对子放一起去 最多放两对
  845. for i = 1, #ttResult do
  846. local resultData = ttResult[i]
  847. if resultData.num then
  848. for j = i+1,#ttResult do
  849. local resultData1 = ttResult[j]
  850. if resultData1.num then
  851. --把加起来为4的列数都先放好
  852. if ((resultData1.num + resultData.num == 4) and (resultData.num == resultData.num) and resultData.value == resultData1.value) then
  853. local cardlist = {}
  854. for k,v in pairs(resultData1.card) do
  855. table.insert(cardlist,v)
  856. end
  857. for k,v in pairs(resultData.card) do
  858. table.insert(cardlist,v)
  859. end
  860. tt[index] = cardlist
  861. index = index + 1
  862. ttResult[i] = {}
  863. ttResult[j] = {}
  864. break
  865. end
  866. end
  867. end
  868. end
  869. end
  870. local leftNum = 0
  871. for i = 1,#ttResult do
  872. local resultData = ttResult[i]
  873. if resultData.num and resultData.num <= 2 then
  874. leftNum = leftNum + resultData.num
  875. elseif resultData.num and resultData.num >= 3 then --大于三张的要单独放一列
  876. local cardlist = {}
  877. if resultData.num then
  878. for k,v in pairs(resultData.card) do
  879. table.insert(cardlist,v)
  880. end
  881. end
  882. tt[index] = cardlist
  883. index = index + 1
  884. ttResult[i] = {}
  885. end
  886. end
  887. for i = 1,#tt do
  888. table.sort(tt,function(a,b)
  889. local index = a[1]
  890. local numA = tempCardVlue[index]
  891. local index1 = b[1]
  892. local numB = tempCardVlue[index1]
  893. if numA ~= nil and numB ~= nil then
  894. return numA < numB
  895. end
  896. return false
  897. end)
  898. end
  899. if leftNum <= 2 then
  900. local hasFind = false
  901. local lens = table.nums(tt)
  902. if lens > 0 then
  903. --优先去找对子
  904. for i=lens,1,-1 do
  905. local cardlist = tt[i]
  906. if table.nums(cardlist) == 4 and leftNum == 1 then
  907. for i = 1,#ttResult do
  908. local resultData = ttResult[i]
  909. if resultData.num then
  910. for k,v in pairs(resultData.card) do
  911. table.insert(cardlist,v)
  912. end
  913. end
  914. end
  915. hasFind = true
  916. end
  917. if hasFind then
  918. break
  919. end
  920. end
  921. --如果没有对子则找三个的
  922. if not hasFind then
  923. for i=lens,1,-1 do
  924. local cardlist = tt[i]
  925. if table.nums(cardlist) == 3 then
  926. for i = 1,#ttResult do
  927. local resultData = ttResult[i]
  928. if resultData.num then
  929. for k,v in pairs(resultData.card) do
  930. table.insert(cardlist,v)
  931. end
  932. end
  933. end
  934. hasFind = true
  935. end
  936. if hasFind then
  937. break
  938. end
  939. end
  940. end
  941. end
  942. --如果三个的都没有则单独放一列
  943. if not hasFind then
  944. local cardlist = {}
  945. for i = 1,#ttResult do
  946. local resultData = ttResult[i]
  947. if resultData.num then
  948. for k,v in pairs(resultData.card) do
  949. table.insert(cardlist,v)
  950. end
  951. end
  952. end
  953. tt[index] = cardlist
  954. index = index + 1
  955. end
  956. elseif leftNum > 2 and leftNum <= 5 then
  957. local cardlist = {}
  958. for i = 1,#ttResult do
  959. local resultData = ttResult[i]
  960. if resultData.num then
  961. for k,v in pairs(resultData.card) do
  962. table.insert(cardlist,v)
  963. end
  964. end
  965. end
  966. tt[index] = cardlist
  967. index = index + 1
  968. else
  969. local mValue = {}
  970. for i = 1,#ttResult do
  971. local resultData = ttResult[i]
  972. if resultData.num then
  973. if not mValue[resultData.value] then
  974. mValue[resultData.value] = {}
  975. end
  976. for k,v in pairs(resultData.card) do
  977. table.insert(mValue[resultData.value],v)
  978. end
  979. end
  980. end
  981. for k,v in pairs(mValue) do
  982. tt[index] = v
  983. index = index + 1
  984. end
  985. end
  986. for i = 1,#tt do
  987. local tempResultCardList = tt[i]
  988. table.sort(tempResultCardList,function(a,b)
  989. local numA = tempCardVlue[tonumber(a)]
  990. local numB = tempCardVlue[tonumber(b)]
  991. if numA ~= nil and numB ~= nil then
  992. return numA < numB
  993. end
  994. return false
  995. end)
  996. end
  997. return tt
  998. end
  999. function Room:isHasInCardlist(value,list)
  1000. local isHas = false
  1001. for k,v in pairs(list) do
  1002. if value == v then
  1003. isHas = true
  1004. end
  1005. end
  1006. return isHas
  1007. end
  1008. --回放排序 --先做最简单的排序
  1009. function Room:rankHandCard(tPureHandCards)
  1010. --发牌
  1011. local index = 1
  1012. local cardData = {}
  1013. local list = {}
  1014. local cardList = {}
  1015. --把手分成点数相同列数
  1016. local tempHandCardList = self:getSameVlaueList(tPureHandCards)
  1017. logD("Room:rankHandCard : "..table.tostring(tempHandCardList))
  1018. --按照点数去寻找牌组 点数只有2-12
  1019. for i = 2,12 do
  1020. list = {}
  1021. cardList = {}
  1022. local hCardList = tempHandCardList[i]
  1023. if hCardList and table.nums(hCardList) > 0 then
  1024. --先把找到的列数放入数组中
  1025. for i = 1,#hCardList do
  1026. table.insert(cardList, hCardList[i])
  1027. end
  1028. tempHandCardList[i] = {}
  1029. --再找点数加起来为14的列数
  1030. local secondDian = 14 - i
  1031. local secondList = tempHandCardList[secondDian]
  1032. if secondList and table.nums(secondList) > 0 then
  1033. --再把找到的加起来为14的列数放入数组中
  1034. for i = 1,#secondList do
  1035. table.insert(cardList, secondList[i])
  1036. end
  1037. tempHandCardList[secondDian] = {}
  1038. end
  1039. end
  1040. if table.nums(cardList) > 0 then
  1041. table.sort(cardList,function(a,b)
  1042. local numA = tempCardVlue[tonumber(a)]
  1043. local numB = tempCardVlue[tonumber(b)]
  1044. if numA ~= nil and numB ~= nil then
  1045. return numA < numB
  1046. end
  1047. end)
  1048. list.card = cardList
  1049. cardData[index] = list
  1050. index = index + 1
  1051. end
  1052. end
  1053. return cardData
  1054. end
  1055. ---
  1056. -- 托管通知
  1057. -- @return
  1058. --
  1059. function Room:onHostingResponse (status, response)
  1060. local userList = response.userList
  1061. for k, v in ipairs(userList) do
  1062. -- 是否托管
  1063. if v.nUserId == self:getMyUserId() then
  1064. self.roomInfo.hosting = v.status
  1065. end
  1066. self:dispatchEvent({name = ZPDef.ZPEvent.HostingRequest, response = {nUserId = v.nUserId, status = v.status}});
  1067. end
  1068. end
  1069. --获取自己的ID,API仅限回放使用
  1070. --[[
  1071. 如果自己在里面玩,那么就返回我自己的ID,为第一视角
  1072. 如果是管理员或者其他目的进来查看回放,则以这局玩家最小的ID为第一视角
  1073. ]]
  1074. function Room:getMyRecordUserId()
  1075. local myUserId = app.user.loginInfo.uid
  1076. local minID = 0
  1077. local isFind = false
  1078. for k,v in pairsByKeys(self.roomInfo.memberList) do
  1079. if myUserId == v.nUserId then
  1080. return myUserId
  1081. end
  1082. if not isFind and v.nUserId ~= 0 then
  1083. minID = v.nUserId
  1084. isFind = true
  1085. end
  1086. end
  1087. return minID
  1088. end
  1089. return Room;