Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def fold(gameId, playerTuple):
    _, _, _, _, _, pot, betToMatch, handLog, _, _, _ = db.getGame(gameId)
    _, playerId, address, name, stack, _, _, _, _, amountPutInPot, _, amountPutInPotThisRound = playerTuple

    db.updatePlayer(gameId, playerId, "folded = 1")
    db.updateGame(
        gameId, "handLog = \"" + handLog + "\r\nPlayer " + name + " folds.\"")
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def getNextStartingPlayers(gameId):
    _, _, _, dealer, _, _, _, _, _, _, _ = db.getGame(gameId)

    db.updateGame(gameId, "currentPlayer = " + str(dealer))
    # Dealer moves to left
    dealerPos = nextPlayer(gameId)[1]
    db.updateGame(gameId, "currentPlayer = " + str(dealerPos))
    smallBlindPos = nextPlayer(gameId)[1]
    db.updateGame(gameId, "currentPlayer = " + str(smallBlindPos))
    bigBlindPos = nextPlayer(gameId)[1]
    db.updateGame(gameId, "currentPlayer = " + str(bigBlindPos))
    UTG = nextPlayer(gameId)[1]
    db.updateGame(gameId, "currentPlayer = " + str(UTG))
    return (dealerPos, smallBlindPos, bigBlindPos, UTG)
Ejemplo n.º 5
0
def takeFromPot(gameId, player, chips):
    _, _, _, _, _, pot, _, _, _, _, _ = db.getGame(gameId)
    playerTuple = db.getPlayer(gameId, player)
    _, _, address, name, stack, _, _, _, _, amountPutInPot, _, amountPutInPotThisRound = playerTuple
    logStatement = ""

    logStatement = "Player " + name + " takes " + str(
        chips) + " chips from the pot."

    #update player stack
    db.updatePlayer(gameId, player, "stack = " + str(stack + chips))

    #update pot
    db.updateGame(gameId, "pot = " + str(pot - chips))
    return logStatement
Ejemplo n.º 6
0
def call(gameId, playerTuple):
    _, _, _, _, _, pot, betToMatch, handLog, _, _, _ = db.getGame(gameId)
    _, playerId, address, name, stack, _, _, _, _, amountPutInPot, _, amountPutInPotThisRound = playerTuple

    chipsToPutIn = betToMatch - amountPutInPotThisRound

    logStatement = ""
    if chipsToPutIn == 0:
        #Check
        logStatement = handLog + "\r\n" + addToPot(gameId, playerId,
                                                   chipsToPutIn, "check")
    else:
        logStatement = handLog + "\r\n" + addToPot(gameId, playerId,
                                                   chipsToPutIn, "call")

    #update player stack
    db.updatePlayer(gameId, playerId, "isChecked = 1")
    db.updateGame(gameId, "handLog = \"" + logStatement + "\"")
Ejemplo n.º 7
0
def addToPot(gameId, player, chips, action):
    _, _, _, _, _, pot, betToMatch, _, _, _, _ = db.getGame(gameId)
    playerTuple = db.getPlayer(gameId, player)
    _, _, address, name, stack, _, _, _, _, amountPutInPot, _, amountPutInPotThisRound = playerTuple
    logStatement = ""
    chipsPutIn = chips

    if stack <= chips:
        logStatement += "Player " + name + " is all in! "
        chipsPutIn = stack
        db.updatePlayer(gameId, player, "isAllIn = 1")
    logStatement += "Player " + name + " "
    if action == "sb":  # small blind
        logStatement += "posted the small blind of " + str(
            chipsPutIn) + " chips."
    elif action == "bb":  # big blind
        logStatement += "posted the big blind of " + str(
            chipsPutIn) + " chips."
    elif action == "call":
        logStatement += "calls " + str(chipsPutIn) + " chips."
    elif action == "raise":
        # check if amount put in actually makes it a raise (i.e. with all in less than btm)
        if chipsPutIn > betToMatch:
            db.updateGame(
                gameId,
                "betToMatch = " + str(amountPutInPotThisRound + chipsPutIn))
            logStatement += "raises to " + str(amountPutInPotThisRound +
                                               chipsPutIn) + " chips."
        # otherwise betToMatch is not increased and no raise has been made

    elif action == "check":
        logStatement += "checks."

    #update player stack
    db.updatePlayer(
        gameId, player,
        "stack = " + str(stack - chipsPutIn) + ", amountPutInPot = " +
        str(amountPutInPot + chipsPutIn) + ", amountPutInPotThisRound = " +
        str(amountPutInPotThisRound + chipsPutIn))

    #update pot
    db.updateGame(gameId, "pot = " + str(pot + chipsPutIn))
    return logStatement
Ejemplo n.º 8
0
def newGame(mailBody):

    gameId = db.addGame()

    playerIndex = 0
    firstLine = mailBody.split("\r\n")[0]
    smallBlind = firstLine.split(":")[0]
    blindIncrement = firstLine.split(":")[1]

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

    for line in mailBody.split("\r\n")[1:]:
        if line != '':
            playerParts = line.split(":")
            db.addPlayer(gameId, playerIndex, playerParts[0].lower(),
                         playerParts[1], playerParts[2])
            playerIndex += 1

    return gameId
Ejemplo n.º 9
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
Ejemplo n.º 10
0
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())
Ejemplo n.º 11
0
 def post(self):
     scores = self.get_argument('tablescores', None)
     scores = json.loads(scores)
     self.write(json.dumps(db.updateGame(scores, self.tournamentid)))
Ejemplo n.º 12
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
Ejemplo n.º 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")