Ejemplo n.º 1
0
def execute(room, messageHandle):
    try:
        if room.gameStatus == GameStatus.PLAYING:
            shuffleData = ShuffleData()
            conn = grpc.insecure_channel('127.0.0.1:50001')
            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[3 * seat.seatNo - 3:3 *
                                           seat.seatNo])
                seat.playScore = room.score
                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)
            room.deskScore = room.score * len(room.seats)
            room.minScore = room.score
            gl.get_v("serverlogger").logger.info("发牌完成")
    except:
        print traceback.print_exc()
Ejemplo n.º 2
0
def execute(room, messageHandle, userId):
    if room.gameStatus == GameStatus.PLAYING:

        wuziqiPlayerOneSetResult = BaiRenPlayerOneSetResult()

        amount = room.score
        seat = room.getSeatByUserId(userId)
        if seat is not None:
            if seat.score < amount:
                amount = seat.score

        scores = ""
        users = ""
        update_currency = []
        game_details = []
        for seat in room.seats:
            users += "," + str(seat.userId)
            win = 0
            if seat.userId != userId:
                win = amount
            else:
                win = -amount

            seat.score += win
            scores += "," + str(win)
            update_currency.append(
                UpdateCurrency(win, seat.userId, room.roomNo))
            game_details.append(
                CreateGameDetails(seat.userId, 3, str(room.roomNo), win, 0,
                                  int(time.time())))

            daerSettlePlayerInfo = wuziqiPlayerOneSetResult.players.add()
            daerSettlePlayerInfo.playerId = seat.userId
            daerSettlePlayerInfo.score = seat.score
            daerSettlePlayerInfo.totalScore = win
            gl.get_v("serverlogger").logger.info('''%d结算后总分%d''' %
                                                 (seat.userId, seat.score))

        recSettleSingle = RecSettleSingle()
        recSettleSingle.allocId = 3
        recSettleSingle.curPlayCount = room.gameCount + 1
        recSettleSingle.time = int(time.time())
        recSettleSingle.content = wuziqiPlayerOneSetResult.SerializeToString()
        messageHandle.broadcast_seat_to_gateway(SETTLE_GAME, recSettleSingle,
                                                room)

        if len(users) > 0:
            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)
            record_cmd.execute(room, users[1:], scores[1:])
        if 0 != len(room.seats):
            room.clear()
            room.gameCount += 1
        else:
            roomover_cmd.execute(room, messageHandle)
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
def execute(room, messageHandle):
    rate = float(config.get("hongbao", "rate"))
    if room.gameStatus == GameStatus.PLAYING:

        userScore = {}
        bankerWin = 0
        hongbaoPlayerOneSetResult = BaiRenPlayerOneSetResult()

        for (d, x) in room.userScore.items():
            win = 0
            if x % 10 == room.selectNum:
                win = -room.bankerScore + x
            else:
                win = x * rate
            userScore[int(d)] = win
            bankerWin -= win

        scores = str(bankerWin if bankerWin <= 0 else int(bankerWin * rate))
        users = str(room.banker)
        rebates = []
        update_currency = []
        game_details = []
        for k in userScore:
            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, 11, str(room.roomNo), userwin,
                                          userScore[k] - userwin,
                                          int(time.time())))
                    rebate = Rebate()
                    rebate.userId = k
                    rebate.card = abs(userwin)
                    rebates.append(rebate)

        for (d, x) in userScore.items():
            daerSettlePlayerInfo = hongbaoPlayerOneSetResult.players.add()
            s = room.getWatchSeatByUserId(d)
            daerSettlePlayerInfo.playerId = s.userId
            daerSettlePlayerInfo.score = x
            daerSettlePlayerInfo.totalScore = s.score
            gl.get_v("serverlogger").logger.info('''%d结算后总分%d''' %
                                                 (d, s.score))

        daerSettlePlayerInfo = hongbaoPlayerOneSetResult.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, 11, str(room.roomNo),
                                      bankerFinalWin,
                                      bankerWin - bankerFinalWin,
                                      int(time.time())))
                if 0 < bankerWin - bankerFinalWin:
                    rebate = Rebate()
                    rebate.userId = room.banker
                    rebate.card = bankerWin - 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 = 11
        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 = hongbaoPlayerOneSetResult.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 = hongbaoPlayerOneSetResult.SerializeToString(
            )
            messageHandle.send_to_gateway(SETTLE_GAME, recSettleSingle,
                                          s.userId)
            if daerSettlePlayerInfo is not None:
                hongbaoPlayerOneSetResult.players.remove(daerSettlePlayerInfo)

        if banker is not None:
            if room.bankerScore >= int(config.get("hongbao",
                                                  "getBankerScore")):
                room.xiazhuang = True
                messageHandle.send_to_gateway(ASK_XIAZHUANG, None, room.banker)

        if len(userScore) > 0:
            record_cmd.execute(room, users, scores)
        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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def execute(room, messageHandle):
    if room.gameStatus == GameStatus.OPENING:

        settleResult = settle(room)
        winOrLose = {}
        win = {}
        lose = {}
        total = 0
        totalWin = 0
        totalLose = 0
        for userSettleResult in settleResult.userSettleResule:
            if userSettleResult.userId != room.banker:
                seat = room.getSeatByUserId(userSettleResult.userId)
                if seat.score < userSettleResult.win and userSettleResult.win > 0:
                    winOrLose[seat.userId] = seat.score
                    total -= seat.score
                elif -seat.score > userSettleResult.win and userSettleResult.win < 0:
                    winOrLose[seat.userId] = -seat.score
                    total += seat.score
                else:
                    winOrLose[seat.userId] = userSettleResult.win
                    total -= userSettleResult.win
                if userSettleResult.win > 0:
                    win[seat.userId] = winOrLose[seat.userId]
                    totalWin += win[seat.userId]
                else:
                    lose[seat.userId] = winOrLose[seat.userId]
                    totalLose -= lose[seat.userId]

        banker = room.getSeatByUserId(room.banker)
        if banker.score + total < 0:
            for (d, x) in win.items():
                winOrLose[d] = int(x / totalWin * (banker.score + totalLose))
        elif total > banker.score:
            for (d, x) in lose.items():
                winOrLose[d] = int(x / totalLose * (banker.score + totalWin))

        bankerWin = 0
        for (d, x) in winOrLose.items():
            bankerWin += x

        users = ""
        scores = ""
        rebates = []
        update_currency = []
        game_details = []
        douniuPlayerOneSetResult = DouniuPlayerOneSetResult()
        for userSettleResult in settleResult.userSettleResule:
            seat = room.getSeatByUserId(userSettleResult.userId)
            daerSettlePlayerInfo = douniuPlayerOneSetResult.players.add()
            daerSettlePlayerInfo.playerId = userSettleResult.userId
            daerSettlePlayerInfo.cardType = userSettleResult.cardValue
            daerSettlePlayerInfo.card.extend(seat.initialCards)
            users += "," + str(seat.userId)
            if userSettleResult.userId != room.banker:
                scores += "," + str(winOrLose[userSettleResult.userId])
                seat.score += winOrLose[userSettleResult.userId]
                daerSettlePlayerInfo.score = winOrLose[userSettleResult.userId]

                update_currency.append(
                    UpdateCurrency(winOrLose[userSettleResult.userId],
                                   seat.userId, room.roomNo))
                game_details.append(
                    CreateGameDetails(userSettleResult.userId, 2,
                                      str(room.roomNo),
                                      winOrLose[userSettleResult.userId],
                                      int(0.5 * room.score), int(time.time())))
            else:
                scores += "," + str(-bankerWin)
                seat.score -= bankerWin
                daerSettlePlayerInfo.score = -bankerWin

                update_currency.append(
                    UpdateCurrency(-bankerWin, seat.userId, room.roomNo))
                game_details.append(
                    CreateGameDetails(userSettleResult.userId, 2,
                                      str(room.roomNo), -bankerWin,
                                      int(0.5 * room.score), int(time.time())))
            daerSettlePlayerInfo.totalScore = seat.score

            rebate = Rebate()
            rebate.userId = userSettleResult.userId
            rebate.card = int(0.5 * room.score)
            rebates.append(rebate)

        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)
        recSettleSingle = RecSettleSingle()
        recSettleSingle.allocId = 2
        recSettleSingle.curPlayCount = room.gameCount + 1
        recSettleSingle.time = int(time.time())
        recSettleSingle.content = douniuPlayerOneSetResult.SerializeToString()
        messageHandle.broadcast_seat_to_gateway(SETTLE_GAME, recSettleSingle,
                                                room)

        record_cmd.execute(room, users[1:], scores[1:])
        room.clear()

        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)
        room.gameCount += 1
        for seat in room.seats:
            threading.Thread(target=ready_timeout.execute,
                             args=(room.gameCount, room.roomNo, messageHandle,
                                   seat.userId, seat.intoDate),
                             name='ready_timeout').start()  # 线程对象.