def enter(self, userId): '''玩家进入队列''' if self.users.get(userId) != None: ftlog.error(getMethodName(), "already in queue!", self.baseLogStr(None, userId)) return False onlinedata.addOnlineLoc(userId, self.room.roomId, self.room.roomId * 10000, 1) cardLevel, isWinner, isToBeBanker = \ gamedata.getGameAttrs(userId, self.room.gameId, ["cardLevel", "isWinner", "isToBeBanker"]) if cardLevel == None: # 新玩家, 必须初始化cardLevel cardLevel, isWinner, isToBeBanker = 2, False, False gamedata.setGameAttrs(userId, self.room.gameId, ["cardLevel", "isWinner", "isToBeBanker"], [2, False, False]) # 修改users数据的代码段中不允许有异步操作 self.users[userId] = {"enterTime": time.time()} self.users[userId]["cardLevel"], self.users[userId]["isWinner"], self.users[userId]["isToBeBanker"] = \ cardLevel, isWinner, isToBeBanker self.matchTeamMate(userId) if ftlog.is_debug(): ftlog.debug(">>", self.baseLogStr(None, userId), "|self.users[userId]:", self.users[userId], "|locList:", onlinedata.getOnlineLocList(userId), caller=self) return True
def onSitOk(self, userId, idleSeatId, result): '''坐下条件成功后的处理 ''' if ftlog.is_debug(): ftlog.debug('onSitOk << |userId, tableId, seatId:', userId, self.tableId, idleSeatId, "|observers:", self.observers, caller=self) # 设置玩家坐在座位上 seat = self.table.seats[idleSeatId - 1] seat.userId = userId seat.setWaitingState() if ftlog.is_debug(): ftlog.debug("|seats:", self.table.seats, caller=self) if userId in self.table.observers: del self.table.observers[userId] onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) # 设置玩家的在线状态 if ftlog.is_debug(): ftlog.debug("before addOnlineLoc. |tableId, onlineSeatId:", self.tableId, onlinedata.getOnlineLocSeatId(userId, self.roomId, self.tableId), caller=self) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, idleSeatId) if ftlog.is_debug(): ftlog.debug("after addOnlineLoc. |tableId, onlineSeatId:", self.tableId, onlinedata.getOnlineLocSeatId(userId, self.roomId, self.tableId), caller=self) result["seatId"] = idleSeatId result["reason"] = TYRoom.ENTER_ROOM_REASON_OK ftlog.hinfo('onSitOk >> |userId, tableId, seatId:', userId, self.tableId, idleSeatId, "|observers:", self.observers, caller=self)
def _doMatchQuickStart(self): tableInfo = self._match_table_info userInfos = tableInfo['userInfos'] userIds = [] userSeatList = [] for i, userInfo in enumerate(userInfos): this_seat = self.seats[i] userIds.append(userInfo['userId']) this_seat.userId = userInfo['userId'] this_seat.state = TYSeat.SEAT_STATE_WAIT this_seat.call123 = -1 userSeatList.append((userInfo['userId'], i + 1)) # 初始化用户数据 for x in xrange(len(self.players)): self.players[x].initUser(0, 1) ctrlRoomId = self.room.ctrlRoomId ctrlRoomTableId = ctrlRoomId * 10000 for userId, seatId in userSeatList: onlinedata.removeOnlineLoc(userId, ctrlRoomId, ctrlRoomTableId) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, seatId) if ftlog.is_debug(): ftlog.debug("|locList:", onlinedata.getOnlineLocList(userId), caller=self) # 做一个延迟 delayConf = dizhuconf.getPublic().get('matchAnimationDelay', '') inter = delayConf.get('waitToNextMatch', 3) FTTasklet.getCurrentFTTasklet().sleepNb(inter) for x in xrange(len(self.seats)): this_seat = self.seats[x] if this_seat.userId > 0: mq = MsgPack() mq.setCmd('quick_start') mq.setResult('userId', this_seat.userId) mq.setResult('gameId', self.gameId) mq.setResult('roomId', self.roomId) mq.setResult('tableId', self.tableId) mq.setResult('seatId', x + 1) # 发送用户的quick_start router.sendToUser(mq, this_seat.userId) # 发送table_info self.gamePlay.sender.sendTableInfoResAll() delay = self._playAnimation(userInfos) if delay > 0: FTTasklet.getCurrentFTTasklet().sleepNb(delay) for x in xrange(len(self.players)): self.gamePlay.doReady(self.players[x], False) self._sendRanks(userInfos)
def _doSitDown(self, msg, userId, seatId, clientId): """ 比赛牌桌只有玩家断线重连时才会触发坐下操作,既重新坐回牌桌 """ if seatId != 0: if self.seats[seatId - 1].userId == 0: onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) ftlog.warn("reconnect user is cleaned from table", "seats =", self.seats) return False elif userId != self.seats[seatId - 1].userId: onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) ftlog.warn("reconnect user id is not matched", "seats =", self.seats) return False else: ftlog.info("user reconect, userId:", userId) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, seatId) self.players[seatId - 1].offline = 1 self.players[seatId - 1].clientId = clientId self.players[seatId - 1].lang = util.getLanguage( userId, clientId) self.players[seatId - 1].refreshGunSkin() self._sendTableInfo(userId, seatId) self._updateMatchInfo(userId) self._updateMatchRank(userId) self._updateMatchTask(userId) self.players[seatId - 1].dealEnterTable() self.players[seatId - 1].enterTime = int(time.time()) self.players[seatId - 1].offline = 0 from newfish.game import TGFish event = EnterTableEvent(userId, FISH_GAMEID, self.roomId, self.tableId, seatId, 1) TGFish.getEventBus().publishEvent(event) return True else: for i in range(len(self.seats)): if self.seats[i].userId == userId: ftlog.info("lost user reconect, userId:", userId, "i =", i) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, i + 1) self.players[i].offline = 1 self.players[i].clientId = clientId self.players[i].lang = util.getLanguage(userId, clientId) self.players[i].refreshGunSkin() self._sendTableInfo(userId, i + 1) self._updateMatchInfo(userId) self._updateMatchRank(userId) self._updateMatchTask(userId) self.players[i].dealEnterTable() self.players[i].enterTime = int(time.time()) self.players[i].offline = 0 from newfish.game import TGFish event = EnterTableEvent(userId, FISH_GAMEID, self.roomId, self.tableId, seatId, 1) TGFish.getEventBus().publishEvent(event) return True
def enter(self, userId): '''玩家进入队列''' if self.users.get(userId) != None: ftlog.error(getMethodName(), "already in queue!", self.baseLogStr(None, userId)) return False self.users[userId] = {"enterTime": time.time()} onlinedata.addOnlineLoc(userId, self.room.roomId, self.room.roomId * 10000, 1) if ftlog.is_debug(): ftlog.debug(">>", self.baseLogStr(None, userId), "|locList:", onlinedata.getOnlineLocList(userId), caller=self) return True
def doFTEnter(self, ftId, userId): ftlog.info('DizhuFriendTable.doFTEnter', 'tableId=', self.tableId, 'ftId=', ftId, 'tableFtId=', self.ftId) if ftId != self.ftId: return -1, '该房间已经解散' player = self.getPlayer(userId) if player: return 0, '' idleSeatId = self.findIdleSeat(userId) if idleSeatId == 0: return -1, '该房间人数已满' if userId in self.observers: del self.observers[userId] seat = self.seats[idleSeatId - 1] seat.userId = userId seat.online = True # 设置玩家的在线状态 onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, idleSeatId) self.gamePlay.sender.sendQuickStartRes( userId, sessiondata.getClientId(userId), { 'seatId': idleSeatId, 'reason': TYRoom.ENTER_ROOM_REASON_OK }) player = self.players[idleSeatId - 1] player.initUser(0, 0) # 向桌子上所有发送table_info self.gamePlay.sender.sendTableInfoResAll(0) # 启动ready的计时器 msgPackParams = {'seatId': player.seatId, 'userId': player.userId} interval = self.runConfig.actionReadyTimeOut if interval > 0: self.seatTimers[player.seatIndex].setup(interval, 'CL_READY_TIMEUP', msgPackParams) return 0, ''
def doMatchSitDown(self, userId, seatId, clientId): """比赛入座""" self.seats[seatId - 1].userId = userId self.players[seatId - 1] = self.createPlayer(self, seatId - 1, clientId) # 创建玩家 self.players[seatId - 1].clip = self._match_table_info["bullet"] # 玩家子弹 onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, seatId) self._sendTableInfo(userId, seatId) # 发送table_info self._broadcastPlayerSit(userId, seatId) # 广播玩家坐下 self.players[seatId - 1].enterTime = int(time.time()) self.players[seatId - 1].offline = 0 from newfish.game import TGFish event = EnterTableEvent(userId, FISH_GAMEID, self.roomId, self.tableId, seatId) TGFish.getEventBus().publishEvent(event) bireport.reportGameEvent("BI_NFISH_TABLE_ENTER", userId, FISH_GAMEID, self.roomId, self.tableId, self.players[seatId - 1].level, 0, 0, 0, [], clientId)
def testReconnect(self): '''测试断线重连 ''' gameId = 6 userId = 1234 roomId = 60110001 tableId = 1 seatId = 3 clientId = "tuyoo_4.0_hall6" onlinedata.setOnlineState(userId, onlinedata.ONLINE) onlinedata.addOnlineLoc(userId, roomId, tableId, seatId) msg = MsgPack() msg.setCmd("quick_start") msg.setParam("userId", userId) msg.setParam("roomId", roomId) msg.setParam("tableId", tableId) msg.setParam("clientId", clientId) print '=' * 30 print msg DizhuQuickStartDispatcher.dispatchQuickStart(msg) print '=' * 30
def onSitOk(self, userId, idleSeatId, result): '''坐下条件成功后的处理 Return: player:新空座位上的player ''' ftlog.hinfo('onSitOk << |userId, tableId, seatId:', userId, self.tableId, idleSeatId, "|observers:", self.observers, caller=self) # 设置玩家坐在座位上, 为了支持并发坐下,此设置需要在异步操作前完成!!! seat = self.table.seats[idleSeatId - 1] seat.userId = userId seat.setWaitingState() if ftlog.is_debug(): ftlog.debug("|seats:", self.table.seats, caller=self) if userId in self.table.observers: del self.table.observers[userId] onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) # 设置玩家的在线状态 if ftlog.is_debug(): ftlog.debug("before addOnlineLoc. |tableId, onlineSeatId:", self.tableId, onlinedata.getOnlineLocSeatId(userId, self.roomId, self.tableId), caller=self) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, idleSeatId) if ftlog.is_debug(): ftlog.debug("after addOnlineLoc. |tableId, onlineSeatId:", self.tableId, onlinedata.getOnlineLocSeatId(userId, self.roomId, self.tableId), caller=self) # 记录当前座位的userId, 以便对玩家的金币做恢复处理 self.table.recordSeatUserId(idleSeatId, userId) result["seatId"] = idleSeatId result["reason"] = TYRoom.ENTER_ROOM_REASON_OK ftlog.hinfo('onSitOk >> |userId, tableId, seatId:', userId, self.tableId, idleSeatId, "|observers:", self.observers, caller=self)
def testReconnect(self): '''测试断线重连 ''' gameId = 6 userId = random.randint(10000, 20000) roomId = 60011001 tableId = 1 seatId = 3 clientId = "Android_3.501_tuyoo.YDJD.0-hall6.apphui.happy" playMode = "happy" onlinedata.setOnlineState(userId, onlinedata.ONLINE) onlinedata.addOnlineLoc(userId, roomId, tableId, seatId) msg = MsgPack() msg.setCmd("quick_start") msg.setParam("userId", userId) msg.setParam("roomId", roomId) msg.setParam("tableId", tableId) msg.setParam("clientId", clientId) print '='*30 print msg BaseQuickStartDispatcher.dispatchQuickStart(msg, userId, gameId, roomId, tableId, playMode, clientId) print '='*30
def __doMatchQuickStart(self): tableInfo = self._match_table_info seatInfos = tableInfo['seats'] userIds = [] userSeatList = [] for x in xrange(len(seatInfos)) : this_seat = self.seats[x] userIds.append(seatInfos[x]['userId']) this_seat.userId = seatInfos[x]['userId'] this_seat.state = TYSeat.SEAT_STATE_WAIT this_seat.call123 = -1 userSeatList.append((seatInfos[x]['userId'], x + 1)) # 初始化用户数据 for x in xrange(len(self.players)): self.players[x].initUser(0, 1) ctrlRoomId = self.room.ctrlRoomId ctrlRoomTableId = ctrlRoomId * 10000 for userId, seatId in userSeatList : # ftlog.debug("|userId, ctrlRoomId, ctrlRoomTableId:", userId, ctrlRoomId, ctrlRoomTableId, caller=self) onlinedata.removeOnlineLoc(userId, ctrlRoomId, ctrlRoomTableId) # ftlog.debug("|userId, roomId, tableId, seatId:", userId, self.roomId, self.tableId, seatId, caller=self) # ftlog.debug("|locList:", onlinedata.getOnlineLocList(userId), caller=self) onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, seatId) if ftlog.is_debug() : ftlog.debug("|locList:", onlinedata.getOnlineLocList(userId), caller=self) # 增加从比赛等待界面到下一局开始时的时间间隔 inter = self.__getWaitToNextMatchInter() ftlog.debug("test __getWaitToNextMatchInter inter = ", inter, 'time = ', time.time(), caller=self) if inter > 0: FTTasklet.getCurrentFTTasklet().sleepNb(inter) ftlog.debug("test __getWaitToNextMatchInter inter 2 = ", inter, 'time = ', time.time(), caller=self) for x in xrange(len(self.seats)) : this_seat = self.seats[x] if this_seat.userId > 0: mq = MsgPack() mq.setCmd('quick_start') mq.setResult('userId', this_seat.userId) mq.setResult('gameId', self.gameId) mq.setResult('roomId', self.roomId) mq.setResult('tableId', self.tableId) mq.setResult('seatId', x + 1) # 发送用户的quick_start router.sendToUser(mq, this_seat.userId) # 发送table_info self.gamePlay.sender.sendTableInfoResAll() playAnmi = self.__playAnimationIfNeed(tableInfo) if playAnmi['playAnimation'] and playAnmi['delaySeconds'] > 0: FTTasklet.getCurrentFTTasklet().sleepNb(playAnmi['delaySeconds']) for x in xrange(len(self.players)): self.gamePlay.doReady(self.players[x], False) mnotes = self._match_table_info['mnotes'] mtype = mnotes['type'] isFinalStep = mnotes.get('isFinalStep', False) if isFinalStep: mtype = mtype + u',决胜局!' isStartStep = mnotes.get('isStartStep', False) if isStartStep: for userId in userIds: clientVer = sessiondata.getClientId(userId) if clientVer < 3.37: mn = MsgPack() mn.setCmd('m_note') mn.setResult('note', mtype) mn.setResult('mInfos', self._match_table_info['mInfos']) router.sendToUser(mn, userId) else: mn = MsgPack() mn.setCmd('m_note') mn.setResult('note', self._buildNote(userId, tableInfo)) mn.setResult('mInfos', self._match_table_info['mInfos']) router.sendToUser(mn, userId) bscore = mnotes.get('basescore', '') step = mnotes.get('step', '') note = bscore + u',' + step # clmn = MsgPack() # clmn.setCmd('table_call') # clmn.setParam('action', 'CL_MNOTE_SEND') # clmn.setParam('gameId', self.gameId) # clmn.setParam('roomId', self.roomId) # clmn.setParam('tableId', self.tableId) # clmn.setParam('userIds', userIds) # clmn.setParam('note', note) func = functools.partial(self.sendMNoteMsg, userIds, note) FTTimer(3, func) # self._mnoteTimer.setupTimer(0, 3, clmn, tasklet.gdata) for userId in userIds: self.__sendRank(userId)
def doSitDown(self, userId, seatId, msg, clientId): """ 用户坐到某个桌子上,逻辑处理:如果是非重连用户,将用户坐下的消息广播给 其它已经坐下的用户,然后将当前的桌子信息发送给新来用户 继承自table类 这是的seatId为游戏的座位号 返回值: 1)是否做下 2)是否断线重连 """ ftlog.debug('>>MajiangQuickTable.doSitDown seatId =', seatId, ', userId = ', userId, ' tableId:', self.tableId) if (seatId != -1) and (userId != self.seats[seatId][TYSeat.INDEX_SEATE_USERID]): onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) ftlog.warn('reconnecting user id is not matched', 'seats =', self.seats, ' tableId:', self.tableId) return False frameSeatId = self.findIdleSeat(userId) ftlog.debug('MajiangQuickTable.doSitDown userId:', userId, ' findSeatId:', frameSeatId) sitRe = True if 0 == frameSeatId: ftlog.debug('MajiangQuickTable.doSitDown now seats:', self.seats) sendPopTipMsg(userId, '对不起,该房间已满员') self.logic_table.msgProcessor.quick_start_err(userId) sitRe = False elif 0 > frameSeatId: # 补发tableInfo seatId = self.getSeatIdByUserId(userId) if seatId < 0: onlinedata.removeOnlineLoc(userId, self.roomId, self.tableId) else: self.sendMsgTableInfo(msg, userId, seatId, True) elif frameSeatId > 0: isReady = self.getTableConfig(MTDefine.READY_AFTER_SIT, 0) gameSeatId = self.changeFrameSeatToMJSeatId(frameSeatId) # 设置座位的状态 self.seats[gameSeatId][TYSeat.INDEX_SEATE_USERID] = userId # 快速桌用户坐下就是准备状态 self.seats[gameSeatId][ TYSeat.INDEX_SEATE_STATE] = TYSeat.SEAT_STATE_READY if isReady else TYSeat.SEAT_STATE_WAIT # 添加玩家 tPlayer = TYPlayer(self, gameSeatId) self.players[gameSeatId] = tPlayer ftlog.debug('MajiangQuickTable.doSitDown user:'******' seat in:', gameSeatId , ' now seats:', self.seats , ' now playersNum:', self.playersNum) # 向牌桌添加用户:联网/机器人 if TYPlayer.isHuman(userId): locResult = onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, frameSeatId) ftlog.info('MajiangQuickTable.doSitDown, add online loc userId:', userId , ' roomId:', self.roomId , ' tableId:', self.tableId , ' frameSeatId:', frameSeatId , ' locResult:', locResult) _name, _purl, _sex, _coin = userdata.getAttrs(userId, ['name', 'purl', 'sex', 'coin']) player = MPlayer(_name, _sex, userId, 0, _purl, _coin, clientId) # 快速桌 默认坐下就是准备状态 默认非托管状态 self.logic_table.addPlayer(player, gameSeatId, isReady, False) # 发送location消息 self.logic_table.msgProcessor.send_location_message(gameSeatId, userId) else: from difang.majiang2.resource import resource robot = resource.getRobotByUserId(userId) if robot: player = MPlayer(robot['name'], robot['sex'], userId, 0, robot['purl'], robot['coin']) # 机器人默认准备 默认托管状态 self.logic_table.addPlayer(player, gameSeatId, True, True) # 座位号调整,框架返回时进行了加1的操作,调整还原 self.room.updateTableScore(self.getTableScore(), self.tableId) self.sendMsgTableInfo(msg, userId, self.getSeatIdByUserId(userId), False) if self.playersNum != self.maxSeatN: # 一次召唤一个机器人 self.setTimerOfDispatchRobot() uids = self.logic_table.getBroadCastUIDs() self.logic_table.msgProcessor.sendTableEvent(self.playersNum, userId, gameSeatId, uids) else: # 人满了,启动定时器 self.setTimerHandleAutoDecideAction() return sitRe
def _doMatchQuickStart(self): ''' 牌桌开始 初始化牌桌玩家 如果是机器人,根据需求初始化机器人信息 ''' robots = [1, 2] tableInfo = self._match_table_info userInfos = tableInfo['users'] userIds = [] userSeatList = [] for i, userInfo in enumerate(userInfos): if userInfo['type'] == 'robot': userInfo['userId'] = robots.pop() this_seat = self.seats[i] userIds.append(userInfo['userId']) this_seat.userId = userInfo['userId'] this_seat.state = TYSeat.SEAT_STATE_WAIT this_seat.call123 = -1 userSeatList.append((userInfo['userId'], i + 1)) # 初始化用户数据 from dizhu.wx_resource import wx_resource wx_resource.initRobotSetting() robotCount = wx_resource.getRobotCount() robotRange = range(0, robotCount) robots = random.sample(robotRange, 3) for x in xrange(len(self.players)): self.players[x].initUser(0, 1, robots[x]) for userId, seatId in userSeatList: if TYPlayer.isRobot(userId): continue onlinedata.addOnlineLoc(userId, self.roomId, self.tableId, seatId) if ftlog.is_debug(): ftlog.debug("|locList:", onlinedata.getOnlineLocList(userId), caller=self) # 做一个延迟 delayConf = dizhuconf.getPublic().get('matchAnimationDelay', '') inter = delayConf.get('waitToNextMatch', 3) FTTasklet.getCurrentFTTasklet().sleepNb(inter) for x in xrange(len(self.seats)): this_seat = self.seats[x] if TYPlayer.isHuman(this_seat.userId): mq = MsgPack() mq.setCmd('quick_start') mq.setResult('userId', this_seat.userId) mq.setResult('gameId', self.gameId) mq.setResult('roomId', self.roomId) mq.setResult('tableId', self.tableId) mq.setResult('seatId', x + 1) mq.setResult('isOK', True) # 发送用户的quick_start router.sendToUser(mq, this_seat.userId) # 发送table_info self.gamePlay.sender.sendTableInfoResAll() delay = self._playAnimation(userInfos) if delay > 0: FTTasklet.getCurrentFTTasklet().sleepNb(delay) for x in xrange(len(self.players)): self.gamePlay.doReady(self.players[x], False) self._sendRanks(userInfos)