Esempio n. 1
0
def getMove(request):
    board = request.session['board']
    next_move = game.getMove(board)
    
    if next_move:
        board.makeMove(next_move, game.ID_COMPUTER)
        request.session['board'] = board
        
        status = game.isGameOver(board)
        
    
        response = {
            'success': True,
            'x': next_move[0],
            'y': next_move[1],
            'gameover': status
        }
    else:
        response = {
            'success': False,
            'message': 'No safe place to move. This isn\'t possible!'
        }
    
    # place mark, save in session, and check for win
    return HttpResponse(json.dumps(response), mimetype="application/json")
Esempio n. 2
0
def makeMove(request, x, y):
    board = request.session['board']
    result = game.makeMove(board, int(x), int(y))
    if result:
        request.session['board'] = board
        
        status = game.isGameOver(board)
        
        response = {
            'success': True,
            'gameover': status
        }
    else:
        response = {
            'success': False,
            'message': 'Invalid move: spot already taken.'
        }
        
    return HttpResponse(json.dumps(response), mimetype="application/json")
Esempio n. 3
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())