-- 五人斗地主消息处理 local zgwrDdzCmd = require("pk_zgwrddz.luaScript.Protocol.zgwrDdzCmd") local zgwrDdzMessage = require("pk_zgwrddz.luaScript.Protocol.zgwrDdzMessage") require("luaScript.GameTypeDef") local Room = class("Room" , require("luaScript.Protocol.ProtocolRoomBase")) function Room:ctor(net) --self.net:removeMsgHandler(zgwrDdzCmd.GAME_COMMAND_SERVER_CHANGE_USERINFO) Room.super.ctor(self , net); --是否为断线重连 self.isReconnection = false --上一次检查gps的uid,防止同一个uid检测多次 self.lastCheckUid = 0 -- 兼容俱乐部需要 self.roomInfo = {} self.roomInfo.memberList = {} self:registeRequest() self:registeResponse() self:resetDismissData() end -- 通过玩家逻辑椅子号找到玩家ID function Room:getUserIdBySeatId(nSeatId) local p = self:getPlayerCid(nSeatId) if p then return p.userId end return nil end -- 通过玩家视图椅子号找到玩家ID function Room:getUserIdByViewId(nViewId) local p = self:getPlayerVpos(nViewId) if p then return p.userId end return nil end -- 通过玩家ID获取玩家展示的座位号 function Room:getViewIdByUserId(nUserId) if self:getPlayer(nUserId) then return self:getPlayer(nUserId).viewPos end return nil end -- 通过玩家逻辑椅子号找到视图椅子号 function Room:getViewIdBySeatId(nSeatId) local p = self:getPlayerCid(nSeatId) if p then return p.viewPos end return nil end -- 通过玩家ID找到逻辑椅子号 function Room:getSeatIdByUserId(nViewId) local p = self:getPlayerVpos(nViewId) if p then return p.userId end return nil end -- 通过玩家视图椅子号找到逻辑椅子号 function Room:getSeatIdByViewId(nUserId) local p = self:getPlayerVpos(nUserId) if p then return p.viewPos end return nil end -- 获取房间内的用户信息 function Room:getUserInfo(uid) if uid then logD("Room:getUserInfo uid = "..uid) else logD("Room:getUserInfo uid = nil") end if self:getPlayer(uid) then logD("Room:getUserInfo userinfo = "..self:getPlayer(uid).jsonInfo) return self:getPlayer(uid).jsonInfo end return nil end -- 缓存房间信息 function Room:setRoomInfo( roominfo ) self.roomInfo = self.roomInfo or {} for k,v in pairs(roominfo) do self.roomInfo[k] = v end end -- 获取房间信息 function Room:getRoomInfo() return self.roomInfo end -- 设置当前房间轮次 function Room:setCurTurn( pos ) self.roomInfo.curTurn = pos end -- 获取当前房价轮次 function Room:getNextTurn() local MaxCount = self:getRoomInfo().nMaxPlayCount if MaxCount == 4 then if self.roomInfo.curTurn == 4 then return 1 else return self.roomInfo.curTurn+1 end elseif MaxCount == 3 then if self.roomInfo.curTurn == 3 then return 1 else return self.roomInfo.curTurn+1 end else if self.roomInfo.curTurn == 2 then return 1 else return self.roomInfo.curTurn+1 end end end -- 获取当前玩家的上家 function Room:getPreSeatId( cid ) local MaxCount = self:getRoomInfo().nMaxPlayCount if MaxCount == 4 then if cid == 0 then return 3 else return cid-1 end elseif MaxCount == 3 then if cid == 0 then return 2 else return cid-1 end else if cid == 0 then return 1 else return cid-1 end end end -- 添加玩家,如果玩家存在则更新数据 -- key - value 缓存 function Room:addPlayer( pl )--and update self._players = self._players or {} local player = self._players[tostring(pl.userId)] if player then for k,v in pairs(pl) do player[k] = v end else self._players[tostring(pl.userId)] = pl end end -- 删除玩家 function Room:delPlayer( uid ) local player = self._players[tostring(uid)] if player then self._players[tostring(uid)] = nil end end -- 获取玩家 function Room:getPlayer( uid ) local player = self._players[tostring(uid)] return player end -- 根据座位号获取玩家数据 function Room:getPlayerCid( cid ) for _,v in pairs(self._players) do if v.seatId == cid then return v end end return nil end -- 根据视角获取玩家数据 function Room:getPlayerVpos( viewPos ) for _,v in pairs(self._players) do if v.viewPos == viewPos then return v end end return nil end -- 获取所有玩家 function Room:getPlayers() return self._players or {} end -- 获取玩家数量 function Room:getPlayerNum() local count = 0 for _,_ in pairs(self._players) do count = count + 1 end return count end -- 获取玩家信息用于GPS function Room:getUserInfoList() local userInfoList = {} for k,v in pairs(self._players) do userInfoList[v.userId] = {nSeatId=v.seatId, userInfo=v.jsonInfo} end return userInfoList end -- 根据id判断是否是自己 function Room:isMyself( uid ) return tonumber(app.user.loginInfo.uid) == tonumber(uid) end function Room:getMyUserId() return tonumber(app.user.loginInfo.uid) end -- 获取自己信息 function Room:getMyself() return self._players[tostring(app.user.loginInfo.uid)] end -- 获取模式牌张数 function Room:getRuleCardsNum() local gameInfo = self:getRoomInfo().nGameInfo local data = json.decode(gameInfo) local allCards = 0 if tonumber(data.gamerule) == 1 then --经典 allCards = 16 else--十五张 allCards = 15 end return allCards end -- 视角转换 function Room:transPos( pos ) local myself = self:getMyself() local MaxCount = self:getRoomInfo().nMaxPlayCount or 5 local showPos = 0 local myPos = myself.seatId if myPos <= pos then showPos = pos - myPos elseif myPos > pos then showPos = MaxCount - myPos + pos end return showPos+1 end -- 更新玩家flag function Room:updateFlag( userId, flag ) -- 俱乐部需要 if self.roomInfo.memberList[userId] == nil then self.roomInfo.memberList[userId] = {} end self.roomInfo.memberList[userId].nUserId = userId self.roomInfo.memberList[userId].nPlayerFlag = flag end function Room:resetDismissData() --玩家解散数据(userId 为Key,解散类型为value) self.dismissInfo = {} --解散总时间 self.roomInfo.nDismissToTalTime = nil -- 解散倒计时 self.roomInfo.nDismissStateTime = nil -- 解散显示 self.roomInfo.nShowDismiss = false end -- 清除缓存信息 function Room:cleanCache() self:resetDismissData() self._players = {} self.roomInfo = {} self.roomInfo.memberList = {} end ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- -- 我发起请求解散房间 function Room:requestDismissRoom(opType) --1: 表示发起解散 2:同意解散 3:不同意解散 local request = zgwrDdzMessage.DismissRequest:new() request.operateType = opType self:sendResponse{cmd = zgwrDdzCmd.GAME_COMMAND_DISBAND_GAME , sender = request} end -- 请求出牌 function Room:requestOutCards( cards ,dcards) if not dcards then dcards = cards end local request = zgwrDdzMessage.GameOutCards:new() if cards and #cards > 0 then request.opType = 1--正常出牌 else request.opType = 0--pass end --出的牌 癞子保持0x5D for _,v in ipairs(cards) do table.insert(request.byOutCard.Datas, v) end --出的牌 癞子代替具体的牌(0x6X) for _,v in ipairs(dcards) do table.insert(request.byOutCardD.Datas, v) end logD("Room:requestOutCards = "..table.tostring(request)) self:sendResponse{cmd = zgwrDdzCmd.GAME_CAMMAND_OUT_CARDS , sender = request} end -- 请求获取可出牌的牌组(带癞子的牌组需要请求) function Room:requestGetOutCards( cards ) local request = zgwrDdzMessage.GetOutCards:new() --出的牌 癞子保持0x5D for _,v in ipairs(cards) do table.insert(request.byOutCard.Datas, v) end logD("Room:requestGetOutCards = "..table.tostring(request)) self:sendResponse{cmd = zgwrDdzCmd.GAME_COMMAND_GET_GROUP_CARDS , sender = request} end ------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- -- 注册客户端发送到服务器的消息 function Room:registeRequest() -- 离开房间 self:defMsgFunc{name = "callLeaveRequest" , cmd = zgwrDdzCmd.GAME_COMMAND_USER_LEAVE}; -- 玩家请求准备 self:defMsgFunc{name = "callReadyRequestPdk" , cmd = zgwrDdzCmd.GAME_COMMAND_USER_READY}; -- 客户端发起快速请求 --self:defMsgFunc{name = "requestFastStartGame", cmd = zgwrDdzCmd.GAME_COMMAND_FAST_START_GAME, sender = zgwrDdzMessage.FastStartRequest}; -- 玩家请求抢地主 self:defMsgFunc{name = "requestGrabLord" , cmd = zgwrDdzCmd.REQUEST_GRAB_LORD,sender = zgwrDdzMessage.GrabLordRequest}; -- 玩家请求加倍 self:defMsgFunc{name = "requestAddMult" , cmd = zgwrDdzCmd.REQUEST_DOUBLE,sender = zgwrDdzMessage.AddMultRequest}; -- 玩家请求选牌(暗地主) self:defMsgFunc{name = "requestSelectCard" , cmd = zgwrDdzCmd.PLAYER_REQURST_SELECT_CARD,sender = zgwrDdzMessage.Card}; -- 玩家请求获取手牌 self:defMsgFunc{name = "requestGetUserCards" , cmd = zgwrDdzCmd.GAME_COMMAND_GET_USER_CARDS}; -- 请求托管(取消or发送) self:defMsgFunc{name = 'HostingRequest', cmd = zgwrDdzCmd.GAME_COMMAND_USER_HOSTING_REQUEST, sender = zgwrDdzMessage.HostingRequest} -- GPS信息发生变化的时候主动给服务器发消息 app:addEventListener("onUpdateLoctionSuccessed", handler(self, self.onGpsChangeRequest)) end -- 注册服务器发送到客户端的消息监听 function Room:registeResponse() self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_LOGIN_GAME_SUCCESS, reader = zgwrDdzMessage.GameLoginSuccess, func = handler(self , self.onResponseLoginSuc)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_LOGIN_GAME_ERR, reader = zgwrDdzMessage.GameLoginFailed, func = handler(self , self.onResponseLoginFailed)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_USER_READY, reader = zgwrDdzMessage.GameUserReady, func = handler(self , self.onResponseUserReady)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_LOGOUT_GAME_SUCCESS, reader = zgwrDdzMessage.GameUserLogoutSuc, func = handler(self , self.onResponseLogoutSuc)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_USER_LOGOUT, reader = zgwrDdzMessage.GameBroadcastUserLogout, func = handler(self , self.onResponseBroadcastLogout)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_USER_LOGIN, reader = zgwrDdzMessage.GameUserEnter, func = handler(self , self.onResponseBroadcastLogin)}; -- 玩家收到解散房间的请求 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_DISBAND_GAME, reader = zgwrDdzMessage.DismissResponse, func = handler(self , self.onDismissResponse)}; -- 收到玩家申请解散房间 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_DISBAND_GAME, reader = zgwrDdzMessage.DismissResult, func = handler(self , self.onUserDismissResultResponse)}; -- 收到其他玩家掉线信息 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_USER_LEAVE, reader = zgwrDdzMessage.OtherDroppedResponse, func = handler(self , self.onOtherDroppedResponse)}; -------------------------------------------------------------------------- -- 游戏处理逻辑协议 -------------------------------------------------------------------------- self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_GAME_START, reader = zgwrDdzMessage.GameBroadcastGameStart, func = handler(self , self.onResponseGameStart)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_SEND_HAND_CARD, reader = zgwrDdzMessage.GameSendCards, func = handler(self , self.onResponseSendCards)}; -- self:defPushMsg{cmd = zgwrDdzCmd.GAME_CAMMAND_SEND_TIPS, reader = zgwrDdzMessage.GameReturnTips, func = handler(self , self.onResponseSendTips)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_OUT_CARD_ERR, reader = zgwrDdzMessage.GameOutCardsErr, func = handler(self , self.onResponseOutCardsErr)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_OUT_CARD, reader = zgwrDdzMessage.GameBroadcastOutCards, func = handler(self , self.onResponseOutCards)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_RESULT, reader = zgwrDdzMessage.GameBroadcastResult, func = handler(self , self.onResponseGameResult)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_GAME_OVER, reader = zgwrDdzMessage.GameGameOver, func = handler(self , self.onResponseGameOver)}; self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_RECONNECT_SUCCESS, reader = zgwrDdzMessage.GameGameRecover, func = handler(self , self.onResponseGameRecover)}; -- 更新玩家分数 --self:defPushMsg{cmd = zgwrDdzCmd.GAME_CAMMAND_BROADCAST_SCORES, reader = zgwrDdzMessage.GameUpdateScore, func = handler(self , self.onResponseUpdateScore)}; -- 更新当前轮次 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_TURNS, reader = zgwrDdzMessage.GameBroadcastTurns, func = handler(self , self.onResponseUpdateTurns)}; -- 快速开始请求返回 --self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_FAST_START_RESULT, reader = zgwrDdzMessage.FastStartRequestResult, func = handler(self, self.onUserRequestFast)} -- 快速开始广播请求 --self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROAD_FAST_START_GAME, reader = zgwrDdzMessage.FastStartStatus, func = handler(self, self.onBroadcastFastRequest)} -- 快速开始成功 --self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROAD_QUICKLY_START, reader = zgwrDdzMessage.UpdateSeatIds, func = handler(self, self.onFastStartSucc)} --更新游戏状态 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_GAME_STATUS, reader = zgwrDdzMessage.UpdateGameStatus, func = handler(self , self.onUpdateGameStatus)}; --通知玩家抢地主 self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_NOTICE_GRAB_LORD, reader = zgwrDdzMessage.NoticeGrabLord, func = handler(self , self.onNoticeGrabLord)}; --无人抢地主重新洗牌 self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_SHUFFLE_CARDS, reader = zgwrDdzMessage.EmptyContent, func = handler(self , self.onShuffleCards)}; --广播抢地主结果 self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_GRAB_LORD_RESULT, reader = zgwrDdzMessage.BroadGrabLordResult, func = handler(self , self.onBroadGrabLordResult)}; --将底牌发给地主 self:defPushMsg{cmd = zgwrDdzCmd.SEND_DICARDS_TO_LORD, reader = zgwrDdzMessage.SendDiCardsToLord, func = handler(self , self.onSendDiCardsToLord)}; --通知玩家加倍 self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_NOTICE_DOUBLE, reader = zgwrDdzMessage.BroadNoticeDouble, func = handler(self , self.onBroadNoticeDouble)}; --玩家加倍结果 self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_DOUBLE_RESULT, reader = zgwrDdzMessage.AddMultResult, func = handler(self , self.onAddMultResult)}; --广播地主选牌(暗地主) self:defPushMsg{cmd = zgwrDdzCmd.BROADCAST_LORD_SELECT_CARD, reader = zgwrDdzMessage.BroadLordSelectCard, func = handler(self , self.onBroadLordSelectCard)}; --广播玩家选牌结果 self:defPushMsg{cmd = zgwrDdzCmd.PLAYER_REQURST_SELECT_CARD, reader = zgwrDdzMessage.Card, func = handler(self , self.onLordSelectCardResult)}; --告知暗地主玩家 self:defPushMsg{cmd = zgwrDdzCmd.NOTICE_ANLORD_PLAYER, reader = zgwrDdzMessage.NoticeAnLordPlayer, func = handler(self , self.onNoticeAnLordPlayer)}; --玩家出牌轮次广播 --self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_TURNS, reader = zgwrDdzMessage.BroadPlayerTurn, func = handler(self , self.onBroadPlayerTurn)}; --获取出牌的牌组 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_GET_GROUP_CARDS, reader = zgwrDdzMessage.GetOutCardsResult, func = handler(self , self.onGetOutCardsResult)}; --广播游戏倍数变化 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_BROADCAST_RATE_CHANGE, reader = zgwrDdzMessage.UpdateGameMult, func = handler(self , self.onUpdateGameMult)}; --玩家获取手牌 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_GET_USER_CARDS, reader = zgwrDdzMessage.BroadLordSelectCard, func = handler(self , self.onGetUserCards)}; -- 收到托管回调 self:defPushMsg{cmd = zgwrDdzCmd.GAME_COMMAND_USER_HOSTING_RESPONSE, reader = zgwrDdzMessage.HostingResponse, func = handler(self,self.onHostingResponse)} end --玩家获取手牌 function Room:onGetUserCards( status, response ) logD("Room:onGetUserCards response = "..table.tostring(response)) self:dispatchEvent({name = "onGetUserCards" ,response = response}) end --广播游戏倍数变化 function Room:onUpdateGameMult( status, response ) logD("Room:onUpdateGameMult response = "..table.tostring(response)) self:dispatchEvent({name = "onUpdateGameMult" ,response = response}) end --获取出牌的牌组 function Room:onGetOutCardsResult( status, response ) logD("Room:onGetOutCardsResult response = "..table.tostring(response)) self:dispatchEvent({name = "onGetOutCardsResult" ,response = response}) end --[[--玩家出牌轮次广播 function Room:onBroadPlayerTurn( status, response ) logD("Room:onBroadPlayerTurn response = "..table.tostring(response)) self:dispatchEvent({name = "onBroadPlayerTurn" ,response = response}) end--]] --告知暗地主玩家 function Room:onNoticeAnLordPlayer( status, response ) logD("Room:onNoticeAnLordPlayer response = "..table.tostring(response)) self:dispatchEvent({name = "onNoticeAnLordPlayer" ,response = response}) end --广播玩家选牌结果 function Room:onLordSelectCardResult( status, response ) logD("Room:onLordSelectCardResult response = "..table.tostring(response)) self:dispatchEvent({name = "onLordSelectCardResult" ,response = response}) end --广播地主选牌(暗地主) function Room:onBroadLordSelectCard( status, response ) logD("Room:onBroadLordSelectCard response = "..table.tostring(response)) self:dispatchEvent({name = "onBroadLordSelectCard" ,response = response}) end --玩家加倍结果 function Room:onAddMultResult( status, response ) logD("Room:onAddMultResult response = "..table.tostring(response)) self:dispatchEvent({name = "onAddMultResult" ,response = response}) end --将底牌发给地主 function Room:onSendDiCardsToLord( status, response ) logD("Room:onSendDiCardsToLord response = "..table.tostring(response)) self:dispatchEvent({name = "onSendDiCardsToLord" ,response = response}) end --通知玩家加倍 function Room:onBroadNoticeDouble( status, response ) logD("Room:onBroadNoticeDouble response = "..table.tostring(response)) self:dispatchEvent({name = "onBroadNoticeDouble" ,response = response}) end --广播抢地主结果 function Room:onBroadGrabLordResult( status, response ) logD("Room:onBroadGrabLordResult response = "..table.tostring(response)) self:dispatchEvent({name = "onBroadGrabLordResult" ,response = response}) end --通知玩家抢地主 function Room:onShuffleCards( status, response ) logD("Room:onShuffleCards response = "..table.tostring(response)) self:dispatchEvent({name = "onShuffleCards" ,response = response}) end --通知玩家抢地主 function Room:onNoticeGrabLord( status, response ) logD("Room:onNoticeGrabLord response = "..table.tostring(response)) self:dispatchEvent({name = "onNoticeGrabLord" ,response = response}) end --更新游戏状态 function Room:onUpdateGameStatus( status, response ) logD("Room:onUpdateGameStatus response = "..table.tostring(response)) self:dispatchEvent({name = "onUpdateGameStatus" ,response = response}) end -- 快速开始请求返回 function Room:onUserRequestFast( status, response ) --快速开始请求错误提示错误 local errTxt ={ [0] = "成功", [1] = "人数不够", [2] = "其他人已请求", [3] = "房间人数已满", [4] = "非法操作" } local txt = errTxt[response.result] if txt then showTooltip(txt) end end -- 快速开始操作广播 function Room:onBroadcastFastRequest( status, response ) logD("Room:onBroadcastFastRequest response = "..table.tostring(response)) self:dispatchEvent({name = "onBroadcastFastRequest" ,response = response}) end -- 游戏快速开始,更新玩家座位号 function Room:onFastStartSucc( status, response ) logD("Room:onFastStartSucc response = "..table.tostring(response)) --更新最大人数 self.roomInfo.nMaxPlayCount = #response.pList -- 更新椅子号 for _,v in pairs(response.pList) do self:addPlayer({userId = v.nUserId, seatId = v.nSeatId}) end -- 更新视图ID for _,v in pairs(response.pList) do self:addPlayer({userId = v.nUserId, viewPos = self:transPos(v.nSeatId)}) end self:dispatchEvent({name = "onFastStartSucc" ,response = response}) end -- 更新当前房间轮次 function Room:onResponseUpdateTurns( status, response ) self:dispatchEvent({name = "onResponseUpdateTurns",response = response}) end -- 更新玩家分数 function Room:onResponseUpdateScore( status, response ) self:dispatchEvent({name = "onResponseUpdateScore",response = response}) end -- 其他玩家掉线 function Room:onOtherDroppedResponse( status, response ) -- logD("onOtherDroppedResponse:", table.tostring(response)) self:dispatchEvent({name = "onOtherDroppedResponse",response = response}) end -- 加入房间成功回调处理 function Room:onResponseLoginSuc( status, response ) -- logD("PDK_onResponseLoginSuc:", table.tostring(response)) self:cleanCache() local roomInfo = {} roomInfo.nGameInfo = response.nGameInfo--创建房间参数信息 roomInfo.nGameStartCount = response.nGameStartCount or 0 roomInfo.nTotalGameNum = response.nTotalGameNum roomInfo.nMaxPlayCount = response.nMaxPlayCount roomInfo.nRoomOwnedUid = response.nRoomOwnedUid roomInfo.nShowTableId = response.nShowTableId ------------------------------------和断线重连对比缺失字段,所以设置默认值------------------------------------------------- roomInfo.gameStatus = 0 roomInfo.reBankerId = -1 --房间是否是解散状态 roomInfo.bUserDisbandGame = 0 --解散剩余时间 roomInfo.leftTimeOut = 0 --快速开始状态1:有人申请提前开始 roomInfo.userFastStartStatus = 0 --快速开始剩余时间 roomInfo.fastStartTimeout = 0 --每个玩家对应快速开始状态 roomInfo.fastStartPlayersStatus = {} --当前操作玩家id roomInfo.curOpUid = 0 --当前操作玩家操作类型 0-pass 1-可以出牌也可以pass 2-只能出牌 roomInfo.curOpType = 0 roomInfo.recoverItem = nil --------------------------------------------------------------------------------------- self:setRoomInfo(roomInfo) -- 处理玩家信息 for _,v in ipairs(response.players) do local player = {} player.playFlag = v.playFlag player.userId = v.userId player.seatId = v.seatId player.jsonInfo = v.jsonInfo local oinfo = json.decode(v.jsonInfo) player.unionid = oinfo.unionid player.headimgurl = oinfo.headimgurl player.gpsInfo = oinfo.gpsInfo player.sex = oinfo.sex player.openid = oinfo.openid player.nickname = oinfo.nickname player.areano = oinfo.areano --在线状态 0离线 1在线 player.onlineStatus = v.onlineStatus self:addPlayer(player) -- 俱乐部需要 if self.roomInfo.memberList[v.userId] == nil then self.roomInfo.memberList[v.userId] = {} end self.roomInfo.memberList[v.userId].nUserId = v.userId self.roomInfo.memberList[v.userId].nSeatId = v.seatId self.roomInfo.memberList[v.userId].nPlayerFlag = v.playFlag self.roomInfo.memberList[v.userId].userInfo = v.jsonInfo end for _,v in pairs(self._players) do self:addPlayer({userId=v.userId, viewPos=self:transPos(v.seatId)}) end self:dispatchEvent({name = "onEnterRoomSuccess", gameId = GAME_IDS.zgWuRenDouDiZhu, gameType = 1}) end -- 加入房间失败返回 function Room:onResponseLoginFailed( status, response ) -- logD("onResponseLoginFailed:", table.tostring(response)) if not self.roomInfo then print("self.roomInfo no exist,容错处理!") return end local errorCode = response.nErrNo local errorString = ENTER_ROOM_RET_STR[errorCode] or string.format("Unkown Error errorCode = %s", errorCode) showTooltip(errorString) -- 发送通知 self:dispatchEvent({name = "onSitDownFailedResponse",response = response}) end -- 玩家准备广播 function Room:onResponseUserReady( status, response ) -- logD("onResponseUserReady:", table.tostring(response)) self:addPlayer({userId = response.userId, ready = 1}) self:updateFlag( response.userId, 1 ) self:dispatchEvent({name = "onResponseUserReady",userId = response.userId}) self:dispatchEvent({name = "onUserReadyResponse"})--刷新俱乐部里显示状态 end -- 玩家退出桌子成功 function Room:onResponseLogoutSuc( status, response ) logD("onResponseLogoutSuc:", table.tostring(response)) -- fixed:房卡信息没刷新 -- local ttExtInfo = string.split(response.userInfo, ",") -- app.user.loginInfo.historyCardNum = ttExtInfo[1] or app.user.loginInfo.historyCardNum -- app.user.loginInfo.curCardNum = ttExtInfo[2] or app.user.loginInfo.curCardNum local ttExtInfo = string.split(response.extInfo, ",") app.user.loginInfo.historyCardNum = ttExtInfo[1]; app.user.loginInfo.curCardNum = ttExtInfo[2]; self.checkGpsID = nil self:dispatchEvent({name = "onUserExitResponseRoom",response = response}) end -- 广播桌子玩家离开 function Room:onResponseBroadcastLogout( status, response ) -- logD("onResponseBroadcastLogout:", table.tostring(response)) self:delPlayer(response.userId) self.roomInfo.memberList[response.userId] = nil self:updateGpsUserInfo(nil,false) if self.checkGpsID then for i,v in pairs(self.checkGpsID) do if v == response.userId then table.remove(self.checkGpsID,i) end end end self:dispatchEvent({name = "onResponseBroadcastLogout",userId = response.userId}) end -- 其他玩家加入房间 function Room:onResponseBroadcastLogin( status, response ) -- logD("onResponseBroadcastLogin:", table.tostring(response)) local player = {} local v = response player.playFlag = v.playFlag--//是否正在玩 -1: 用户站起 0: 用户坐下 1: 准备状态 2: 正在玩 player.userId = v.userId player.seatId = v.seatId player.jsonInfo = v.jsonInfo local oinfo = json.decode(v.jsonInfo) player.unionid = oinfo.unionid player.headimgurl = oinfo.headimgurl player.gpsInfo = oinfo.gpsInfo player.sex = oinfo.sex player.openid = oinfo.openid player.nickname = oinfo.nickname player.areano = oinfo.areano player.viewPos = self:transPos(v.seatId) self:addPlayer(player) -- 俱乐部需要 if self.roomInfo.memberList[v.userId] == nil then self.roomInfo.memberList[v.userId] = {} end self.roomInfo.memberList[v.userId].nUserId = v.userId self.roomInfo.memberList[v.userId].nSeatId = v.seatId self.roomInfo.memberList[v.userId].nPlayerFlag = v.playFlag self.roomInfo.memberList[v.userId].userInfo = v.jsonInfo --检测GPS self:updateGpsUserInfo(v.userId,true) self:dispatchEvent({name = "onResponseBroadcastLogin", pInfo = player}) end function Room:onDismissResponse( status, response ) logD("onDismissResponse:", table.tostring(response)) --[[-- nUserId , defVar("nUserId", VT_Int, 0) -- 1: 表示发起解散 2:同意解散 3:不同意解散 , defVar("operateType", VT_UChar, 0) -- 剩余解散超时时间 , defVar("timeLeft", VT_Short, 0) --]] if not self.roomInfo then print("self.roomInfo no exist,容错处理!") return end local nUserId = response.nUserId local operateType = response.operateType -- 收到玩家发起请求时,数据重置 if response.operateType == 1 then self:resetDismissData() end --数据记录 self.dismissInfo[nUserId] = operateType self:dispatchEvent({name = "onDismissResponse", response = response}) end function Room:onUserDismissResultResponse( status, response ) logD("onUserDismissResultResponse:", table.tostring(response)) if not self.roomInfo then print("self.roomInfo no exist,容错处理!") return end if response.errorCode == 1 then showTooltip("operateType取值范围不对") elseif response.errorCode == 2 then showTooltip("当前没人发起解散") elseif response.errorCode == 3 then showTooltip("已经有人申请解散") elseif response.errorCode == 4 then showTooltip("用户已经操作过") else -- 其他玩家的解散状态 for uid, value in pairs(response.memberList) do self.dismissInfo[uid] = value.dismissState end self:dispatchEvent({name = "onDismissResponse", response = response}) end end -------------------------------------------------------------------------- -- 游戏处理逻辑协议 -------------------------------------------------------------------------- -- 广播游戏开始 0x8101 function Room:onResponseGameStart( status, response ) -- logD("onResponseGameStart:", table.tostring(response)) self:setCurTurn(0) self:setRoomInfo({nGameStartCount=response.gameStartCount, nTotalGameNum=totalGamenum}) self:dispatchEvent({name = "onResponseGameStart", response = response}) end -- 给玩家发牌 0x8102 function Room:onResponseSendCards( status, response ) logD("onResponseSendCards:", table.tostring(response)) self:dispatchEvent({name = "onResponseSendCards", response = response}) end -- 服务器返回最佳提示 function Room:onResponseSendTips( status, response ) -- logD("onResponseSendTips:", table.tostring(response)) self:dispatchEvent({name = "onResponseSendTips", response = response}) end -- 服务器返回出牌错误提示 function Room:onResponseOutCardsErr( status, response ) logD("onResponseOutCardsErr:", table.tostring(response)) local msg = { [0] = "出牌成功", [1] = "轮次错误", [2] = "牌型错误", [3] = "出牌错误",--牌与手牌对不上 [4] = "出牌为死牌,请重新出牌!",--牌与手牌对不上 [5] = "出牌错误",--牌与手牌对不上 } if response.errFlag == 0 then return end--出牌成功了不做任何处理 if self:isMyself(response.userId) then local m = msg[response.errFlag] or "出牌错误" showTooltip(""..m) end self:dispatchEvent({name = "onResponseOutCardsErr", response = response}) end --玩家出牌成功,广播 function Room:onResponseOutCards( status, response ) -- print("----------------self userId-------------------------"..self:getMyself().userId) logD("onResponseOutCards:", table.tostring(response)) self:dispatchEvent({name = "onResponseOutCards", response = response}) end --单局结算 function Room:onResponseGameResult( status, response ) logD("onResponseGameResult:", table.tostring(response)) self:dispatchEvent({name = "onResponseGameResult", response = response}) end --总结算 function Room:onResponseGameOver( status, response ) logD("onResponseGameOver:", table.tostring(response)) self:dispatchEvent({name = "onResponseGameOver", response = response}) end --断线重连 function Room:onResponseGameRecover( status, response ) --logD("onResponseGameRecover:", table.tostring(response)) self.isReconnection = true self:cleanCache() local roomInfo = {} roomInfo.nMaxPlayCount = response.maxPlayCount roomInfo.nRoomOwnedUid = response.roomOwnedUid roomInfo.nstartGameUid = roomInfo.startGameUid roomInfo.nGameStartCount = response.gameStartCount roomInfo.nTotalGameNum = response.totalGamenum roomInfo.nGameInfo = response.gameInfo--创建房间参数信息 roomInfo.bUserDisbandGame = response.bUserDisbandGame roomInfo.gameStatus = response.gameStatus roomInfo.leftTimeOut = response.leftTimeOut roomInfo.nShowTableId = response.nShowTableId --总倍数 roomInfo.nHMIShowMult = response.nHMIShowMult --基础倍数 roomInfo.nBaseMult = response.nBaseMult --游戏倍数(涨水) roomInfo.nGameCurMult = response.nGameCurMult --四张炸倍数 roomInfo.fourBomb = response.fourBomb --双王炸倍数 roomInfo.twoKingBomb = response.twoKingBomb --八张炸倍数 roomInfo.eightBomb = response.eightBomb --四王炸倍数 roomInfo.fourKingBomb = response.fourKingBomb --12张炸倍数 roomInfo.twelveBomb = response.twelveBomb --6王炸倍数 roomInfo.sixKingBomb = response.sixKingBomb --是否自吃 roomInfo.isSelfEat = response.isSelfEat --游戏底分 roomInfo.nCurBaseScore = response.nCurBaseScore --地主ID roomInfo.nLordId = response.nLordId --暗地主ID roomInfo.nDarkLordId = response.nDarkLordId --暗地主牌 roomInfo.nDarkLordCard = response.nDarkLordCard --底牌 roomInfo.diCards = response.diCards --当前桌上最后一手牌是否显示地主标签0不显示1显示 roomInfo.lastShowDzFlag = response.lastShowDzFlag --当前桌上最后一手牌出牌者id roomInfo.lastOpUid = response.lastOpUid --当前桌上最后一手牌牌型 roomInfo.lastOpType = response.lastOpType --当前桌上最后一手牌 roomInfo.lastOutCards = response.lastOutCards --当前操作玩家id roomInfo.curOpUid = response.curOpUid --当前操作玩家出牌状态 0-pass 1-可以出牌也可以pass 2-只能出牌 roomInfo.curOpType = response.outCardStates --最先出完牌的玩家id roomInfo.winUserId = response.winUserId --是否春天 1:春天 0:什么都不是 roomInfo.spring = response.spring --暗地主可选的牌 roomInfo.canSelectCards = response.canSelectCards roomInfo.recoverItem = response.recoverItem --[[--快速开始状态1:有人申请提前开始 roomInfo.userFastStartStatus = response.userFastStartStatus --快速开始剩余时间 roomInfo.fastStartTimeout = response.fastStartTimeout --每个玩家对应快速开始状态 roomInfo.fastStartPlayersStatus = {} --记牌器数据 roomInfo.cardPlayerInfo = response.cardPlayerInfo--]] if app.club_php.clubID and app.club_php.clubID ~= 0 and roomInfo.nGameStartCount > 0 then app.club_php:getClubList(); end self:setRoomInfo(roomInfo) -- 处理玩家信息 for _,v in ipairs(response.recoverItem) do local player = {} player.userId = v.userId player.seatId = v.seatId player.playFlag = v.playFlag --玩家解散状态 0:初始状态, 1:发起解散 2: 同意解散 3:不同意解散 player.disbandStatus = v.disbandStatus player.onlineStatus = onlineStatus player.jsonInfo = v.jsonInfo local oinfo = json.decode(v.jsonInfo) player.unionid = oinfo.unionid player.headimgurl = oinfo.headimgurl player.gpsInfo = oinfo.gpsInfo player.sex = oinfo.sex player.openid = oinfo.openid player.nickname = oinfo.nickname player.areano = oinfo.areano self:addPlayer(player) -- 俱乐部需要 if self.roomInfo.memberList[v.userId] == nil then self.roomInfo.memberList[v.userId] = {} end self.roomInfo.memberList[v.userId].nUserId = v.userId self.roomInfo.memberList[v.userId].nSeatId = v.seatId self.roomInfo.memberList[v.userId].nPlayerFlag = v.playFlag self.roomInfo.memberList[v.userId].userInfo = v.jsonInfo self.roomInfo.memberList[v.userId].nDisbandStatus = v.disbandStatus --[[local fastStartPlayer = {} fastStartPlayer.userId = v.userId fastStartPlayer.fastStartStatus = v.fastStartStatus table.insert(roomInfo.fastStartPlayersStatus, fastStartPlayer)--]] end if response.extInfo and type(response.extInfo)=='string' and response.extInfo~="" then local extInfo = json.decode(response.extInfo) if extInfo and type(extInfo)=='table' and extInfo[tostring(self:getMyself())] then self.roomInfo.quickStartInfo = extInfo.faststart--提前开局 end local passShow = extInfo.passuser or {} local tmpShowPass = {} for i,v in ipairs(passShow) do tmpShowPass[tostring(v)] = true end for _,v in pairs(roomInfo.recoverItem) do v.operate = extInfo[tostring(v.userId)].operate v.passShow = tmpShowPass[tostring(v.userId)] end end for _,v in pairs(self._players) do self:addPlayer({userId=v.userId, viewPos=self:transPos(v.seatId)}) end --检测GPS self:updateGpsUserInfo(nil,false) self:dispatchEvent({name = "onEnterRoomSuccess", gameId = GAME_IDS.zgWuRenDouDiZhu, gameType = 1}) -- self:dispatchEvent({name = "onResponseGameRecover"})--此句代码无效 end function Room:test() logD("--------------------------pdk room test") end -- 通知服务器玩家GPS数据发生变化 function Room:onGpsChangeRequest() print("Room:onUserInfoChangeRequest()") local request = StringPacket:new() request.stringValue = app.user.userInfo or "" logD("Room:onGpsChangeRequest()", table.tostring(request)) self:sendResponse{cmd = zgwrDdzCmd.GAME_COMMAND_CLIENT_CHANGE_USERINFO , sender = request}; end -- 服务器下发玩家GPS数据发生变化 --[[function Room:onGpsChangeResponse(status, response) local ttime = os.time() local limit = false if not self.gpsChangeTime then self.gpsChangeTime = ttime else local tt = ttime - self.gpsChangeTime if ttime - self.gpsChangeTime < 3 then limit = true end end local nUserId = response.uid if (self.lastCheckUid and self.lastCheckUid == nUserId) or limit then return end self.gpsChangeTime = ttime local memberInfo = self.roomInfo.memberList[nUserId] if memberInfo then memberInfo.userInfo = response.userInfo end local player = self._players[tostring(nUserId)] player.jsonInfo = response.userInfo self.lastCheckUid = nUserId self:dispatchEvent({name = "onGpsChangeResponse", nUserId = nUserId}); end--]] --更新gps用户数据 --需要检测的ID 没有则检测所有人的距离 --isOpenView 危险距离是否主动弹出提示 function Room:updateGpsUserInfo(userId,isOpenView) self.checkGpsID = self.checkGpsID or {} for i,v in pairs(self.checkGpsID) do if v == userId then table.remove(self.checkGpsID,i) return--同一ID4秒内只检测一次 end end if self.roomInfo and self.roomInfo.memberList then local gameStartCount = self:getRoomInfo().nGameStartCount or 0 local isGameStart = gameStartCount>0 --如果是游戏开始后收到消息则不处理 local userInfoList = self:getUserInfoList() --[[for nUserId, memberInfo in pairs(self.roomInfo.memberList) do local nSeatId = memberInfo.nSeatId local userInfo = memberInfo.userInfo if userInfo then local userInfo2 = json.decode(userInfo) if userInfo2 then userInfoList[nUserId] = {nSeatId = nSeatId, userInfo = userInfo} end end end --]] if isGameStart then isOpenView = false end table.insert(self.checkGpsID,userId) self:dispatchEvent({ name = GAME_EVENT.GPS_UPDATE_USER_INFO, userId=userId, userInfoList = userInfoList, isOpenView = isOpenView or false, isGameStart = isGameStart, -- maxPlayerNum = self.roomInfo.nMaxPlayCount, }) end end -- 托管通知(回复) function Room:onHostingResponse(status, response) logD("Room:onHostingResponse 0x8133()", table.tostring(response)) local userList = response.userList for k, v in ipairs(userList) do if self.roomInfo.memberList[v.nUserId] == nil then self.roomInfo.memberList[v.nUserId] = {} end -- 保留状态,为了下一局 self.roomInfo.memberList[v.nUserId].AIStatus = v.status or 0 -- 是否托管 -- if v.nUserId == self:getMyUserId() then -- self.roomInfo.hosting = v.status -- end self:dispatchEvent({name = 'onHostingResponse', response = {nUserId = v.nUserId, status = v.status}}) end end return Room