コード例 #1
0
ファイル: bot.py プロジェクト: stratfob/emailPoker
def playerInfoLog(gameId, playerId):
    _, board, _, _, _, pot, betToMatch, handLog, _, _, _ = db.getGame(gameId)
    string = "\r\n\r\nPlayers:"
    for i in range(db.numberOfPlayersInGame(gameId)):
        _, _, address, name, stack, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, i)
        string += "\r\n" + name + " - " + str(stack) + " chips"

    _, _, _, name, stack, cards, _, _, _, amountPutInPot, _, amountPutInPotThisRound = db.getPlayer(
        gameId, playerId)
    string += "\r\n\r\nIt is your turn " + name + ".\r\nYour cards are: " + game.cardToUnicode(
        cards.split(":")[0]) + " and " + game.cardToUnicode(
            cards.split(":")[1])
    if board != "":
        string += "\r\nThe board is : "
        for card in board.split(":"):
            string += game.cardToUnicode(str(card)) + ", "
        string = string[:-2]  # get rid of trailing comma
    string += "\r\nThe pot is " + str(pot) + " chips."
    string += "\r\nYour stack is " + str(stack) + " chips."
    if not amountPutInPotThisRound == 0:
        string += "You have already put in " + str(
            amountPutInPotThisRound) + " chips into the pot."
    if betToMatch - amountPutInPotThisRound != 0:
        string += "\r\nTo call you'll need to put in " + str(
            betToMatch - amountPutInPotThisRound) + " chips."
    return string
コード例 #2
0
ファイル: player.py プロジェクト: stratfob/emailPoker
def raiseTo(gameId, playerTuple, raiseToChips):
    _, _, _, _, _, pot, betToMatch, handLog, _, _, _ = db.getGame(gameId)
    _, playerId, address, name, stack, _, _, _, _, amountPutInPot, _, amountPutInPotThisRound = playerTuple

    if betToMatch > raiseToChips:
        return False
    elif betToMatch == raiseToChips:
        call(gameId, playerTuple)
        return True
    else:

        chipsToPutIn = raiseToChips - amountPutInPotThisRound

        logStatement = handLog + "\r\n" + addToPot(gameId, playerId,
                                                   chipsToPutIn, "raise")

        numberOfPlayers = db.numberOfPlayersInGame(gameId)
        for i in range(numberOfPlayers):
            _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
                gameId, i)
            if not bool(folded) and not bool(eliminated) and not bool(isAllIn):
                db.updatePlayer(gameId, i, "isChecked = 0")

        # Raiser is checked
        db.updatePlayer(gameId, playerId, "isChecked = 1")

        db.updateGame(gameId, "handLog = \"" + logStatement + "\"")
        return True
コード例 #3
0
def getAllCardsInPlay(gameId):
    numberOfPlayers = db.numberOfPlayersInGame(gameId)
    allCards = []
    board = db.getGame(gameId)[1]
    if not board == "":
        allCards = board.split(":")
    for i in range(numberOfPlayers):
        allCards += db.getPlayer(gameId, i)[5].split(":")
    return allCards
コード例 #4
0
def startHand(gameId):
    numberOfPlayers = db.numberOfPlayersInGame(gameId)
    _, _, _, dealer, _, _, _, _, handNo, smallBlind, blindIncrement = db.getGame(
        gameId)
    blindsIncreased = False
    if blindIncrement != 0:
        if (handNo + 1) % blindIncrement == 0:
            smallBlind *= 2
            blindsIncreased = True

    handNo += 1
    db.updateGame(
        gameId,
        "handNo = " + str(handNo) + ", smallBlind = " + str(smallBlind))

    smallBlind = smallBlind
    bigBlind = smallBlind * 2

    deck = pd.Deck()
    deck.shuffle()
    # Deal cards and set playes to not be folded
    for i in range(numberOfPlayers):
        _, _, _, _, _, _, _, _, eliminated, _, _, _ = db.getPlayer(gameId, i)

        if eliminated == 0:  # not eliminated
            hand = deck.deal(2)
            db.updatePlayer(
                gameId, i, "folded = 0, isChecked = 0, cards = \"" +
                str(hand[0]) + ":" + str(hand[1]) +
                "\", amountPutInPot = 0, amountPutInPotThisRound = 0")

    #dealer moves to left
    dealerPos, smallBlindPos, bigBlindPos, UTG = getNextStartingPlayers(gameId)

    handLog = "<b>Hand number " + str(
        handNo) + "</b>\r\n--------------------------------------\r\n"
    if blindsIncreased:
        handLog += "Blinds have increased to " + str(
            smallBlind) + " and " + str(bigBlind) + " chips.\r\n"
    handLog += "Dealer: " + db.getPlayer(gameId, dealerPos)[3] + "\r\n"

    #UpdateGame
    db.updateGame(
        gameId,
        "currentPlayer = " + str(UTG) + ", dealer = " + str(dealerPos) +
        ", board = '', phase = 0, pot = 0, betToMatch = " + str(bigBlind))

    handLog += (player.addToPot(gameId, smallBlindPos, smallBlind, "sb") +
                "\r\n")
    handLog += (player.addToPot(gameId, bigBlindPos, bigBlind, "bb"))

    db.updateGame(gameId, "handLog = \"" + handLog + "\"")

    return UTG
コード例 #5
0
def startGame(gameId):
    numberOfPlayers = db.numberOfPlayersInGame(gameId)
    dealer = random.randint(0, numberOfPlayers - 1)

    # for debugging, remove randomness
    if bot.DEBUG:
        dealer = 0

    db.updateGame(
        gameId, "currentPlayer = " + str(dealer) + ", dealer = " + str(dealer))
    return startHand(gameId)
コード例 #6
0
def nextRound(gameId):
    _, board, currentPlayer, dealer, phase, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    # 0 - preflop
    # 1 - postflop
    # 2 - turn
    # 3 - river

    newPhase = phase + 1
    numberOfPlayers = db.numberOfPlayersInGame(gameId)
    db.updateGame(gameId, "currentPlayer = " + str(dealer))

    for i in range(numberOfPlayers):
        db.updatePlayer(gameId, i,
                        "isChecked = 0, amountPutInPotThisRound = 0")

    db.updateGame(gameId, "phase = " + str(newPhase))

    if not allAreAllIn(gameId):
        newCurrentPlayer = nextPlayer(gameId)[1]
        db.updateGame(gameId, "currentPlayer = " + str(newCurrentPlayer))

    else:
        newCurrentPlayer = dealer
        db.updateGame(gameId, "currentPlayer = " + str(newCurrentPlayer))

    deck = pd.Deck()
    deck.get_list(
        getAllCardsInPlay(gameId))  # remove all cards currently in play
    deck.shuffle()

    if newPhase == 1:
        newBoard = deck.deal(3)

        handLog = handLog + "\r\nFlop : " + cardToUnicode(str(newBoard[0])) + ", " \
            + cardToUnicode(str(newBoard[1])) + ", " + cardToUnicode(str(newBoard[2]))
        db.updateGame(
            gameId, "board = \"" + str(newBoard[0]) + ":" + str(newBoard[1]) +
            ":" + str(newBoard[2]) + "\", betToMatch = 0, handLog = \"" +
            handLog + "\"")
    elif newPhase == 2 or newPhase == 3:
        newBoard = deck.deal(1)
        if newPhase == 2:
            handLog = handLog + "\r\nTurn : " + cardToUnicode(str(newBoard[0]))
        else:
            handLog = handLog + "\r\nRiver : " + cardToUnicode(str(
                newBoard[0]))
        db.updateGame(
            gameId, "board = \"" + board + ":" + str(newBoard[0]) +
            "\", betToMatch = 0, handLog = \"" + handLog + "\"")

    return newCurrentPlayer
コード例 #7
0
def checkForRoundCompletion(gameId):
    _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    for i in range(numberOfPlayers):
        _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, i)
        if not bool(folded) and not bool(eliminated) and not bool(
                isAllIn) and not bool(isChecked):
            return False

    return True
コード例 #8
0
def nextPlayer(gameId):
    _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    for i in range(numberOfPlayers - 1):
        playerNumber = (i + currentPlayer + 1) % numberOfPlayers
        _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, playerNumber)
        if not bool(folded) and not bool(eliminated) and not bool(
                isAllIn) and not bool(isChecked):
            return db.getPlayer(gameId, playerNumber)

    return -1  # Error state, should check if round is complete first
コード例 #9
0
def allAreAllIn(gameId):
    _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    if nextPlayer(gameId) == -1:
        return True

    for i in range(numberOfPlayers):
        _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, i)
        if not bool(eliminated) and not bool(folded) and not bool(isAllIn):
            return False

    return True
コード例 #10
0
def isGameOver(gameId):
    _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    numbernotElim = 0
    for i in range(numberOfPlayers):
        _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, i)
        if not bool(eliminated):
            numbernotElim += 1
            if numbernotElim > 1:
                return False

    return True
コード例 #11
0
def isHandOver(gameId):
    _, _, currentPlayer, dealer, phase, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    if phase == 4:
        return True

    numberChecked = 0
    for i in range(numberOfPlayers):
        _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
            gameId, i)
        if not bool(folded) and not bool(eliminated):
            numberChecked += 1
            if numberChecked > 1:
                return False

    return True
コード例 #12
0
ファイル: bot.py プロジェクト: stratfob/emailPoker
def readMail(imapper, mailId=None, fakeMail=None):

    if DEBUG:
        mail_id = mailId
        mail = fakeMail
    else:
        mail_id = imapper.listids()[0]
        mail = imapper.mail(mail_id)

    # If new email
    if not db.isMailRead(mail_id):
        db.addReadMail(mail_id)

        if mail.title.upper() == "NEW":
            #make new game
            gameId = game.newGame(mail.body)
            firstPlayer = game.startGame(gameId)

            print("First player = " + str(firstPlayer))

            #Send first mail
            sendMail(
                db.getPlayer(gameId, firstPlayer)[2], gameId,
                db.getGame(gameId)[7] + playerInfoLog(gameId, firstPlayer) +
                instructions())

            return gameId
        #look for game with id in title
        elif not len(db.getGame(mail.title[4:])) == 0:

            gameId = mail.title[4:]
            playerEmail = mail.from_addr[mail.from_addr.find('<') +
                                         1:mail.from_addr.rfind('>')].lower()
            playerTuple = db.getPlayerByEmail(gameId, playerEmail)
            gameTuple = db.getGame(gameId)

            successfulRaise = True

            #if current player
            if playerTuple[1] == gameTuple[2]:  # current player

                if mail.body.upper().startswith("CALL"):
                    player.call(gameId, playerTuple)
                elif mail.body.upper().startswith("RAISE"):
                    if len(mail.body.split(" ")) < 2:
                        successfulRaise = False
                    else:
                        if mail.body.split(" ")[1].strip().find('\r') == -1:
                            chipsToRaiseTo = int(
                                mail.body.split(" ")[1].strip())
                        else:
                            chipsToRaiseTo = int(
                                mail.body.split(" ")[1].strip()
                                [:mail.body.split(" ")[1].strip().find('\r')])
                        successfulRaise = player.raiseTo(
                            gameId, playerTuple, chipsToRaiseTo)

                elif mail.body.upper().startswith("FOLD"):
                    player.fold(gameId, playerTuple)
                elif mail.body.upper().startswith("ALL IN"):
                    successfulRaise = player.allIn(gameId, playerTuple)
                else:
                    successfulRaise = False

                if successfulRaise:

                    if not game.allAreAllIn(gameId):
                        nextPlayerTuple = game.nextPlayer(gameId)

                    if game.checkForRoundCompletion(gameId):
                        # Starts next round and returns active player
                        nextPlayerTuple = db.getPlayer(gameId,
                                                       game.nextRound(gameId))

                    if game.isHandOver(gameId):
                        game.showdown(gameId)

                        # Send mail to all players
                        for i in range(db.numberOfPlayersInGame(gameId)):
                            sendMail(
                                db.getPlayer(gameId, i)[2], gameId,
                                db.getGame(gameId)[7])

                        if not game.isGameOver(gameId):
                            nextPlayerTuple = db.getPlayer(
                                gameId, game.startHand(gameId))

                    if not game.isGameOver(gameId):
                        if not game.allAreAllIn(gameId):
                            db.updateGame(
                                gameId,
                                "currentPlayer = " + str(nextPlayerTuple[1]))
                            sendMail(
                                nextPlayerTuple[2], gameId,
                                db.getGame(gameId)[7] +
                                playerInfoLog(gameId, nextPlayerTuple[1]) +
                                instructions())
                        else:
                            _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
                                gameId)
                            numberOfPlayers = db.numberOfPlayersInGame(gameId)

                            while not game.isHandOver(gameId):
                                game.nextRound(gameId)

                            game.showdown(gameId)

                            # Send mail to all players
                            for i in range(db.numberOfPlayersInGame(gameId)):
                                sendMail(
                                    db.getPlayer(gameId, i)[2], gameId,
                                    db.getGame(gameId)[7])

                            if not game.isGameOver(gameId):
                                nextPlayerTuple = db.getPlayer(
                                    gameId, game.startHand(gameId))
                                sendMail(
                                    nextPlayerTuple[2], gameId,
                                    db.getGame(gameId)[7] +
                                    playerInfoLog(gameId, nextPlayerTuple[1]) +
                                    instructions())

                    if game.isGameOver(gameId):
                        _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
                            gameId)
                        numberOfPlayers = db.numberOfPlayersInGame(gameId)

                        for i in range(numberOfPlayers):
                            _, _, _, _, _, _, isAllIn, folded, eliminated, _, isChecked, _ = db.getPlayer(
                                gameId, i)
                            if not bool(eliminated):
                                winner = i
                                break

                        for i in range(numberOfPlayers):
                            sendMail(
                                db.getPlayer(gameId,
                                             i)[2], gameId, "The winner was " +
                                db.getPlayer(gameId, winner)[3] +
                                "! Thanks for playing :)")

                        db.deleteGame(gameId)

                # unsuccessful raise
                else:
                    sendMail(
                        playerEmail, gameId,
                        "Invalid input. Please try again.\r\n" +
                        instructions())
コード例 #13
0
def showdown(gameId):
    _, _, currentPlayer, dealer, _, pot, betToMatch, handLog, _, _, _ = db.getGame(
        gameId)
    numberOfPlayers = db.numberOfPlayersInGame(gameId)

    board = db.getGame(gameId)[1]
    string = ""
    if board != "":
        string += "\r\n\r\nThe board was : "
        for card in board.split(":"):
            string += cardToUnicode(str(card)) + ", "
        string = string[:-2]  # get rid of trailing comma
    db.updateGame(gameId,
                  "handLog = \"" + db.getGame(gameId)[7] + string + "\"")

    isStillIn = []

    for i in range(numberOfPlayers):
        _, _, _, _, stack, cards, isAllIn, folded, eliminated, amountPutInPot, isChecked, _ = db.getPlayer(
            gameId, i)
        if not bool(folded) and not bool(eliminated):
            isStillIn.append([i, amountPutInPot, stack, cards])

    thisPot = pot

    while len(isStillIn) > 1:
        minimumAIP = min(i[1] for i in isStillIn)
        thisPot = minimumAIP * len(isStillIn)
        for i in isStillIn:
            i[1] -= minimumAIP

        winners = getWinners(gameId, isStillIn)

        for i in winners:
            handLog = db.getGame(gameId)[7]
            playerTuple = db.getPlayer(gameId, i[0])

            logStatement = playerTuple[3] + " shows " + cardToUnicode(
                playerTuple[5].split(":")[0]) + ", " + cardToUnicode(
                    playerTuple[5].split(":")[1])
            logStatement += " (" + i[1] + ")"
            logStatement += "\r\n" + player.takeFromPot(
                gameId, i[0], thisPot // len(winners))
            db.updateGame(
                gameId,
                "handLog = \"" + handLog + "\r\n" + logStatement + "\"")

        newIsStillIn = []

        for i in isStillIn:
            if i[1] != 0:
                newIsStillIn.append(i)
            else:
                if i[0] not in (j[0] for j in winners):
                    playerTuple = db.getPlayer(gameId, i[0])
                    logStatement = playerTuple[3] + " shows " + \
                        cardToUnicode(playerTuple[5].split(":")[0]) + ", " +  \
                        cardToUnicode(playerTuple[5].split(":")[1])
                    handLog = db.getGame(gameId)[7]
                    db.updateGame(
                        gameId, "handLog = \"" + handLog + "\r\n" +
                        logStatement + "\"")

        isStillIn = newIsStillIn
        pot -= thisPot
        thisPot = 0

    if len(isStillIn) == 1:
        handLog = db.getGame(gameId)[7]
        logStatement = player.takeFromPot(
            gameId, isStillIn[0][0], isStillIn[0][1] + (pot - isStillIn[0][1]))
        db.updateGame(gameId,
                      "handLog = \"" + handLog + "\r\n" + logStatement + "\"")

    for i in range(numberOfPlayers):
        _, _, _, _, stack, cards, isAllIn, folded, eliminated, amountPutInPot, isChecked, _ = db.getPlayer(
            gameId, i)
        if stack <= 0:
            db.updatePlayer(gameId, i, "eliminated = 1")
        db.updatePlayer(gameId, i, "isAllIn = 0")