def execute(userId, message, messageHandle): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): roomNo = redis.get(str(userId) + "_room") redis.lock("lockroom_" + str(roomNo)) try: room = redis.getobj("room_" + str(roomNo)) if room.gameStatus != GameStatus.PLAYING: gl.get_v("serverlogger").logger.info("看牌失败状态不对") redis.unlock("lockroom_" + str(roomNo)) return seat = room.getSeatByUserId(userId) if seat is not None and not seat.lookCard and not seat.end: seat.lookCard = True recExecuteAction = RecExecuteAction() recExecuteAction.playerId = seat.userId for s in room.seats: lookCardAction = JinhuaLookCardAction() if s.userId == userId: lookCardAction.cards.extend(s.initialCards) room.historyActions.append( recExecuteAction.SerializeToString()) recExecuteAction.data = lookCardAction.SerializeToString() messageHandle.send_to_gateway(EXECUTE_ACTION, recExecuteAction, s.userId) room.save(redis) except: print traceback.print_exc() redis.unlock("lockroom_" + str(roomNo))
def sendBetScore(self, messageHandle): if 0 < len(self.betScores): action = BaiRenBetScoreAction() for b in self.betScores: a = action.betScore.add() a.ParseFromString(b) self.betScores = [] executeAction = RecExecuteAction() executeAction.actionType = 2 executeAction.data = action.SerializeToString() messageHandle.broadcast_watch_to_gateway(EXECUTE_ACTION, executeAction, self)
def execute(room, messageHandle): try: if room.gameStatus == GameStatus.WAITING: shuffleData = ShuffleData() conn = grpc.insecure_channel('127.0.0.1:50002') client = zhipai_pb2_grpc.ZhipaiStub(channel=conn) shuffleResult = client.shuffle(shuffleData) update_currency = [] for seat in room.seats: seat.initialCards = [] seat.initialCards.extend( shuffleResult.cardlist[5 * seat.seatNo - 5:5 * seat.seatNo]) seat.score -= int(0.5 * room.score) update_currency.append( UpdateCurrency(-int(0.5 * room.score), seat.userId, room.roomNo)) if 0 != len(update_currency): gl.get_v("update_currency").putall(update_currency) gl.get_v("serverlogger").logger.info("发牌完成") executeAction = RecExecuteAction() if room.gameStatus == GameStatus.PLAYING: for s in room.seats: dealCardAction = DouniuCardAction() if 0 != len(s.initialCards): dealCardAction.cards.append(s.initialCards[4]) else: dealCardAction.cards.append(0) executeAction.data = dealCardAction.SerializeToString() messageHandle.send_to_gateway(EXECUTE_ACTION, executeAction, s.userId) room.gameStatus = GameStatus.OPENING room.executeAsk(messageHandle, 0, 3) else: for s in room.seats: dealCardAction = DouniuCardAction() if 0 != len(s.initialCards): dealCardAction.cards.extend(s.initialCards[0:4]) else: dealCardAction.cards.extend([0, 0, 0, 0]) executeAction.data = dealCardAction.SerializeToString() messageHandle.send_to_gateway(EXECUTE_ACTION, executeAction, s.userId) room.gameStatus = GameStatus.GRABING room.executeAsk(messageHandle, 0, 1) room.historyActions.append(executeAction.SerializeToString()) except: print traceback.print_exc()
def execute(room, messageHandle): try: if room.gameStatus == GameStatus.WAITING: shuffleData = ShuffleData() conn = grpc.insecure_channel('127.0.0.1:50011') client = zhipai_pb2_grpc.ZhipaiStub(channel=conn) shuffleResult = client.shuffle(shuffleData) cardlist = shuffleResult.cardlist room.positions[0].cards.append(cardlist[0]) room.positions[1].cards.append(cardlist[1]) room.startDate = int(time.time()) if room.gameStatus == GameStatus.PLAYING: gameover_cmd.execute(room, messageHandle) else: executeAction = RecExecuteAction() executeAction.actionType = 0 messageHandle.broadcast_watch_to_gateway(EXECUTE_ACTION, executeAction, room) room.gameStatus = GameStatus.PLAYING room.historyActions.append(executeAction.SerializeToString()) room.executeAsk(messageHandle, 0, 2) threading.Thread(target=play_timeout.execute, args=( room.roomNo, messageHandle, room.gameCount, ), name='play_timeout').start() # 线程对象. gl.get_v("serverlogger").logger.info("开始下注") playHandle = PlayScoreHandle(str(room.roomNo), TestQueue()) gl.get_v("play-handle")[str(room.roomNo)] = playHandle threading.Thread(target=playHandle.execute, name='playthread').start() # 线程对象. if gl.get_v(str(room.roomNo) + "sendthread") is None: e = SendScores(room.roomNo, messageHandle) threading.Thread(target=e.execute, name='sendthread').start() # 线程对象. gl.set_v(str(room.roomNo) + "sendthread", e) except: print traceback.print_exc()
def executeAction(self, userId, actionType, data, messageHandle): recExecuteAction = RecExecuteAction() recExecuteAction.actionType = actionType recExecuteAction.playerId = userId if data is not None: recExecuteAction.data = data.SerializeToString() messageHandle.broadcast_seat_to_gateway(EXECUTE_ACTION, recExecuteAction, self) self.historyActions.append(recExecuteAction.SerializeToString())
def execute(room, messageHandle): try: if room.gameStatus == GameStatus.WAITING: shuffleData = ShuffleData() try: with open('./conf/niuniucheat') as infile: cheatDatastrs = infile.read() cheatDatas = cheatDatastrs.split(',') cheatData = shuffleData.cheatData.add() cheatData.level = int(cheatDatas[0]) cheatData = shuffleData.cheatData.add() cheatData.level = int(cheatDatas[1]) cheatData = shuffleData.cheatData.add() cheatData.level = int(cheatDatas[2]) cheatData = shuffleData.cheatData.add() cheatData.level = int(cheatDatas[3]) cheatData = shuffleData.cheatData.add() cheatData.level = int(cheatDatas[4]) except: print traceback.print_exc() conn = grpc.insecure_channel('127.0.0.1:50014') client = zhipai_pb2_grpc.ZhipaiStub(channel=conn) shuffleResult = client.shuffle(shuffleData) room.reDealCard = False room.dealedCards = [] room.surplusCards = [] room.surplusCards.extend(shuffleResult.cardlist) room.positions[0].cards.extend(room.surplusCards[0:5]) room.positions[1].cards.extend(room.surplusCards[5:10]) room.positions[2].cards.extend(room.surplusCards[10:15]) room.positions[3].cards.extend(room.surplusCards[15:20]) room.positions[4].cards.extend(room.surplusCards[20:25]) room.startDate = int(time.time()) if room.gameStatus == GameStatus.PLAYING: if not room.openCard: room.executeAction(0, 5, None, messageHandle) room.opencard = True gameover_cmd.execute(room, messageHandle) else: executeAction = RecExecuteAction() executeAction.actionType = 0 executeAction = RecExecuteAction() dealCardAction = BaiRenDealCardAction() executeAction.data = dealCardAction.SerializeToString() messageHandle.broadcast_watch_to_gateway(EXECUTE_ACTION, executeAction, room) room.gameStatus = GameStatus.PLAYING room.historyActions.append(executeAction.SerializeToString()) room.executeAsk(messageHandle, 0, 2) threading.Thread(target=play_timeout.execute, args=( room.roomNo, messageHandle, room.gameCount, ), name='play_timeout').start() # 线程对象. threading.Thread(target=open_timeout.execute, args=( room.roomNo, room.gameCount, messageHandle, ), name='open_timeout').start() # 线程对象. gl.get_v("serverlogger").logger.info("开始下注") playHandle = PlayScoreHandle(str(room.roomNo), TestQueue(), messageHandle) gl.get_v("play-handle")[str(room.roomNo)] = playHandle threading.Thread(target=playHandle.execute, name='playthread').start() # 线程对象. if gl.get_v(str(room.roomNo) + "sendthread") is None: e = SendScores(room.roomNo, messageHandle) threading.Thread(target=e.execute, name='sendthread').start() # 线程对象. gl.set_v(str(room.roomNo) + "sendthread", e) except: print traceback.print_exc()
def execute(room, messageHandle): rate = float(config.get("tuitongzi", "rate")) if room.gameStatus == GameStatus.PLAYING: room.sendBetScore(messageHandle) settleData = SettleData() i = 0 for p in room.positions: userSettleData = settleData.userSettleData.add() userSettleData.userId = i userSettleData.cardlist.extend(p.cards) userSettleData.score = 1 userSettleData.grab = 1 i += 1 conn = grpc.insecure_channel('127.0.0.1:50013') client = zhipai_pb2_grpc.ZhipaiStub(channel=conn) settleResult = client.settle(settleData) executeAction = RecExecuteAction() dealCardAction = BaiRenDealCardAction() dealCardAction.cards.append(room.positions[0].cards[1]) dealCardAction.cards.append(room.positions[1].cards[1]) dealCardAction.cards.append(room.positions[2].cards[1]) dealCardAction.cards.append(room.positions[3].cards[1]) executeAction.data = dealCardAction.SerializeToString() messageHandle.broadcast_watch_to_gateway(EXECUTE_ACTION, executeAction, room) userScore = {} bankerWin = 0 positionWin = [0, 0, 0, 0] for u in settleResult.userSettleResule: win = 0 if u.win > 0: win = 1 * rate positionWin[u.userId] = 1 elif u.win < 0: win = -1 positionWin[u.userId] = 2 if win != 0: position = room.positions[u.userId] for k in position.playScores: bankerWin -= win * position.playScores[k] if k in userScore: userScore[k] += win * position.playScores[k] else: userScore[k] = win * position.playScores[k] scores = str(bankerWin if bankerWin <= 0 else int(bankerWin * rate)) users = str(room.banker) rebates = [] dayingjia = 0 dayingjiaScore = 0 dashujia = 0 dashujiaScore = 0 update_currency = [] game_details = [] for k in userScore: if dayingjiaScore < userScore[k]: dayingjia = k dayingjiaScore = userScore[k] if dashujiaScore > userScore[k]: dashujia = k dashujiaScore = userScore[k] seat = room.getWatchSeatByUserId(k) if seat is not None: gl.get_v("serverlogger").logger.info('''%d下注前%d''' % (k, seat.score)) gl.get_v("serverlogger").logger.info('''%d下注%d''' % (k, seat.playScore)) gl.get_v("serverlogger").logger.info('''%d输赢%d''' % (k, userScore[k])) userwin = int(userScore[k]) seat.score += userwin scores += "," + str(userwin) users += "," + str(k) if 0 != userwin: update_currency.append( UpdateCurrency(userwin, k, room.roomNo)) game_details.append( CreateGameDetails(k, 7, str(room.roomNo), userwin, userScore[k] - userwin, int(time.time()))) rebate = Rebate() rebate.userId = k rebate.card = abs(userwin) rebates.append(rebate) room.trend.append(positionWin) if len(room.trend) > 10: room.trend.remove(room.trend[0]) room.updateTrend(messageHandle, 0) tuitongziPlayerOneSetResult = BaiRenPlayerOneSetResult() dayingjiaSeat = room.getWatchSeatByUserId(dayingjia) if dayingjiaSeat is not None: room.dayingjia = dayingjia userInfo = tuitongziPlayerOneSetResult.dayingjia userInfo.account = dayingjiaSeat.account userInfo.playerId = dayingjiaSeat.userId userInfo.headUrl = dayingjiaSeat.head userInfo.createTime = dayingjiaSeat.createDate userInfo.ip = dayingjiaSeat.ip userInfo.online = dayingjiaSeat.online userInfo.nick = dayingjiaSeat.nickname userInfo.ready = dayingjiaSeat.ready userInfo.score = dayingjiaSeat.score - dayingjiaSeat.playScore userInfo.sex = dayingjiaSeat.sex userInfo.totalCount = dayingjiaSeat.total_count userInfo.loc = i userInfo.consumeVip = dayingjiaSeat.level tuitongziPlayerOneSetResult.dayingjiaScore = dayingjiaScore dashujiaSeat = room.getWatchSeatByUserId(dashujia) if dashujiaSeat is not None: userInfo = tuitongziPlayerOneSetResult.dashujia userInfo.account = dashujiaSeat.account userInfo.playerId = dashujiaSeat.userId userInfo.headUrl = dashujiaSeat.head userInfo.createTime = dashujiaSeat.createDate userInfo.ip = dashujiaSeat.ip userInfo.online = dashujiaSeat.online userInfo.nick = dashujiaSeat.nickname userInfo.ready = dashujiaSeat.ready userInfo.score = dashujiaSeat.score - dashujiaSeat.playScore userInfo.sex = dashujiaSeat.sex userInfo.totalCount = dashujiaSeat.total_count userInfo.loc = i userInfo.consumeVip = dashujiaSeat.level tuitongziPlayerOneSetResult.dashujiaScore = dashujiaScore for seat in room.seats: if seat.userId != room.banker: daerSettlePlayerInfo = tuitongziPlayerOneSetResult.players.add( ) s = room.getWatchSeatByUserId(seat.userId) daerSettlePlayerInfo.playerId = s.userId daerSettlePlayerInfo.score = 0 if s.userId not in userScore else userScore[ s.userId] daerSettlePlayerInfo.totalScore = s.score gl.get_v("serverlogger").logger.info('''%d结算后总分%d''' % (s.userId, s.score)) daerSettlePlayerInfo = tuitongziPlayerOneSetResult.players.add() banker = None if 1 != room.banker: bankerFinalWin = bankerWin if bankerWin <= 0 else int(bankerWin * rate) if 0 != bankerFinalWin: update_currency.append( UpdateCurrency(bankerFinalWin, room.banker, room.roomNo)) game_details.append( CreateGameDetails(room.banker, 7, str(room.roomNo), bankerFinalWin, bankerWin - bankerFinalWin, int(time.time()))) rebate = Rebate() rebate.userId = room.banker rebate.card = abs(bankerFinalWin) rebates.append(rebate) banker = room.getWatchSeatByUserId(room.banker) room.bankerScore += bankerFinalWin banker.shangzhuangScore = room.bankerScore if banker is not None: banker.score += bankerFinalWin daerSettlePlayerInfo.totalScore = banker.score if 0 < len(rebates): gl.get_v("rebate-handle-queue").put(rebates) if 0 != len(update_currency): gl.get_v("update_currency").putall(update_currency) if 0 != len(game_details): gl.get_v("game_details").putall(game_details) daerSettlePlayerInfo.playerId = room.banker daerSettlePlayerInfo.score = bankerFinalWin recSettleSingle = RecSettleSingle() recSettleSingle.allocId = 7 recSettleSingle.curPlayCount = room.gameCount + 1 recSettleSingle.time = int(time.time()) for s in room.watchSeats: daerSettlePlayerInfo = None if room.getSeatByUserId( s.userId) is None and s.userId != room.banker: daerSettlePlayerInfo = tuitongziPlayerOneSetResult.players.add( ) daerSettlePlayerInfo.playerId = s.userId daerSettlePlayerInfo.score = 0 if s.userId not in userScore else userScore[ s.userId] daerSettlePlayerInfo.totalScore = s.score gl.get_v("serverlogger").logger.info('''%d结算后总分%d''' % (s.userId, s.score)) recSettleSingle.content = tuitongziPlayerOneSetResult.SerializeToString( ) messageHandle.send_to_gateway(SETTLE_GAME, recSettleSingle, s.userId) if daerSettlePlayerInfo is not None: tuitongziPlayerOneSetResult.players.remove( daerSettlePlayerInfo) if banker is not None: banker.lianzhuang += 1 if banker.lianzhuang >= int( config.get("tuitongzi", "maxBankerTimes")): room.xiazhuang = True elif room.bankerScore >= int( config.get("tuitongzi", "getBankerScore")): room.xiazhuang = True messageHandle.send_to_gateway(ASK_XIAZHUANG, None, room.banker) if len(userScore) > 0: record_cmd.execute(room, users, scores) e = gl.get_v(str(room.roomNo) + "sendthread") e.close() gl.get_v("play-handle")[str(room.roomNo)].close() del gl.get_v("play-handle")[str(room.roomNo)] gl.del_v(str(room.roomNo) + "sendthread") if 0 != len(room.watchSeats): room.clear() room.gameCount += 1 levelSeat = [] for s in room.seats: if s.score < room.leaveScore or not s.online: levelSeat.append(s.userId) for l in levelSeat: room.exit(l, messageHandle) threading.Thread(target=start_timeout.execute, args=( room.roomNo, messageHandle, ), name='start_timeout').start() # 线程对象. else: roomover_cmd.execute(room, messageHandle)