Beispiel #1
0
def startGame(args,user_integration):


    object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)
    board = chess.Board()
    object.board_state = INITIAL_BOARD + INITIAL_BOARD_REST
    object.save()
    print("inside start game")
    print(user_integration.id)
    m = MessageClass()
    color = args['Color']
    # if color == "White":
    #     MOVE_FLAG = 0
    # else:
    #     MOVE_FLAG = 1
    #
    # #board = chess.Board()

    m.message_text = "You chose " + color
    attachment = MessageAttachmentsClass()

    print(color + " to move")

    if (color=="Black"):
        m = playComputer(args,user_integration)
        return m

    print(IMAGE_URL + INITIAL_BOARD)
    attachment.image_url = IMAGE_URL + INITIAL_BOARD
    field1 = AttachmentFieldsClass()
    field1.title = "Move"
    field1.value = color + " to move"
    attachment.attach_field(field1)
    button = MessageButtonsClass()
    button.text = "Make move"
    button.value = "Make move"
    button.name = "Make move"
    button.command = {
        "service_application": str(user_integration.yellowant_integration_id),
        "function_name": "makemove",
        "inputs": ["move"],
        "data": {"move": "testing"},
    }
    attachment.attach_button(button)

    button1 = MessageButtonsClass()
    button1.text = "Play Computer"
    button1.value = "Play Computer"
    button1.name = "Play Computer"
    button1.command = {"service_application": str(user_integration.yellowant_integration_id), "function_name": "playcomputer",
                       "data": {"move": "testing"},
                      }
    attachment.attach_button(button1)

    m.attach(attachment)


    #m.image_url = "http://www.fen-to-image.com/image/rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"

    return m
Beispiel #2
0
    def get_account_info(self, args):

        print("In get_company_info")
        endpoint = "https://api.dropboxapi.com/2/users/get_account"

        # API parameteres for getting account information

        headers = {
            'Authorization': 'Bearer ' + self.dropbox_access_token,
            'Content-Type': 'application/json',
        }

        data = {"account_id": str(self.account_id)}

        # Consuming the API
        r = requests.post('https://api.dropboxapi.com/2/users/get_account',
                          headers=headers,
                          json=data)

        # Response check
        if r.status_code == requests.codes.ok:

            # Getting response in JSON
            response = r.content.decode("utf-8")
            response = json.loads(response)

            # Creating message objects to structure the message to be shown
            message = MessageClass()
            message.message_text = "User Account Details :"

            attachment = MessageAttachmentsClass()

            field1 = AttachmentFieldsClass()
            field1.title = "Name :"
            field1.value = response["name"]["display_name"]
            attachment.attach_field(field1)

            field2 = AttachmentFieldsClass()
            field2.title = "E-mail :"
            field2.value = response["email"]
            attachment.attach_field(field2)
            attachment.image_url = response["profile_photo_url"]

            message.attach(attachment)
            return message.to_json()
        else:
            m = MessageClass()
            print(r.content.decode("utf-8"))
            d = r.content.decode("utf-8")
            m.message_text = "{0}: {1}".format(r.status_code, r.text)
            return m.to_json()
Beispiel #3
0
    def get_account_info(self,args):

        print("In get_company_info")
        endpoint = "https://api.dropboxapi.com/2/users/get_account"

        # API parameteres for getting account information

        headers = {
            'Authorization': 'Bearer ' + self.dropbox_access_token,
            'Content-Type': 'application/json',
        }

        data = {"account_id": str(self.account_id)}

        # Consuming the API
        r = requests.post('https://api.dropboxapi.com/2/users/get_account', headers=headers, json=data)

        # Response check
        if r.status_code == requests.codes.ok:

            # Getting response in JSON
            response = r.content.decode("utf-8")
            response = json.loads(response)

            # Creating message objects to structure the message to be shown
            message = MessageClass()
            message.message_text = "User Account Details :"

            attachment = MessageAttachmentsClass()

            field1 = AttachmentFieldsClass()
            field1.title = "Name :"
            field1.value = response["name"]["display_name"]
            attachment.attach_field(field1)


            field2 = AttachmentFieldsClass()
            field2.title = "E-mail :"
            field2.value = response["email"]
            attachment.attach_field(field2)
            attachment.image_url = response["profile_photo_url"]

            message.attach(attachment)
            return message.to_json()
        else:
            m = MessageClass()
            print(r.content.decode("utf-8"))
            d = r.content.decode("utf-8")
            m.message_text = "{0}: {1}".format(r.status_code, r.text)
            return m.to_json()
Beispiel #4
0
    def list_users(self, args):
        """
            This function returns all the users in the organization
        """
        org_id = args['organization']
        headers = {
            "Content-Type": "application/json",
            "Authorization":
            "Zoho-oauthtoken" + " " + self.zohoinvoice_access_token,
            "X-com-zoho-invoice-organizationid": org_id
        }
        url = settings.ZOHO_USER_URL
        response = requests.get(url, headers=headers)
        print(response)
        print(response.text)
        i = 0
        message = MessageClass()
        message.message_text = "Users"
        response_json = response.json()
        data = response_json['users']

        for user in data:
            attachment = MessageAttachmentsClass()
            attachment.image_url = user["photo_url"]
            attachment.text = "User" + " " + str(i + 1)

            field1 = AttachmentFieldsClass()
            field1.title = "Name"
            field1.value = user['name']
            attachment.attach_field(field1)

            field2 = AttachmentFieldsClass()
            field2.title = "Email"
            field2.value = user['email']
            attachment.attach_field(field2)

            field3 = AttachmentFieldsClass()
            field3.title = "User ID"
            field3.value = user['user_id']
            attachment.attach_field(field3)

            field4 = AttachmentFieldsClass()
            field4.title = "Role ID"
            field4.value = user['role_id']
            attachment.attach_field(field4)
            i = i + 1
            message.attach(attachment)
        return message.to_json()
Beispiel #5
0
def get_employee(args, user_integration):

    employee_id = args.get('employee_id')
    auth_token_object = UserIntegration.objects.get(
        yellowant_integration_id=user_integration.yellowant_integration_id)
    auth_token = auth_token_object.auth_token
    m = MessageClass()
    m.message_text = "The Employee details are:"
    url = "https://people.zoho.com/people/api/forms/P_EmployeeView/records"
    data = {
        'authtoken': auth_token,
        'searchColumn': 'EMPLOYEEID',
        'searchValue': employee_id
    }

    attachment = MessageAttachmentsClass()

    response = requests.post(url, data=data)
    response = response.json()

    field = AttachmentFieldsClass()
    field.title = "First Name"
    field.value = response[0]['First Name']

    field1 = AttachmentFieldsClass()
    field1.title = "Last Name"
    field1.value = response[0]['Last Name']

    field2 = AttachmentFieldsClass()
    field2.title = "Email"
    field2.value = response[0]['Email ID']

    field3 = AttachmentFieldsClass()
    field3.title = "EmployeeID"
    field3.value = response[0]['EmployeeID']

    attachment.attach_field(field)
    attachment.attach_field(field1)
    attachment.attach_field(field2)
    attachment.attach_field(field3)

    attachment.image_url = response[0]['Photo']

    m.attach(attachment)

    return m
Beispiel #6
0
def showBoard(args,user_integration):

    print(user_integration.yellowant_integration_id)
    object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)
    board = chess.Board(object.board_state)
    print(object.board_state)

    m = MessageClass()
    m.message_text = color(object.board_state[-12])  + " to move"
    attachment = MessageAttachmentsClass()
    attachment.image_url = IMAGE_URL + board.fen()[:-13]
    button = MessageButtonsClass()
    button.text = "Make move"
    button.value = "Make move"
    button.name = "Make move"
    button.command = {"service_application": str(user_integration.yellowant_integration_id), "function_name": "makemove",\
                      "inputs": ["move"],
                      "data" : {"move":"testing"}
                      }
    attachment.attach_button(button)
    m.attach(attachment)

    return m
Beispiel #7
0
def makeAMove(args,user_integration):

    print('hello')
    print(user_integration.yellowant_integration_id)
    object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)
    print(object.id)
    print(object.board_state)
    board = chess.Board(object.board_state)
    print(board)
    print(args)
    move = args.get('move')

    m = MessageClass()
    print(object.board_state)
    print(object.board_state[-12])
    col = color_inv(object.board_state[-12])
    print(col)
    m.message_text = col + " to move"
    move_uci = board.parse_san(move)
    if move_uci in board.legal_moves:
        board.push_san(move)
        if board.is_insufficient_material():
            print("Insufficient material")
            m.message_text = "Insufficient material"

        if board.is_stalemate():
            print("Stalemate")
            m.message_text = "Stalemate"
        if board.is_checkmate():
            print(col + " wins")
            m.message_text = col + " wins"

        object.board_state = board.fen()
        object.save()
    else:
        m.message_text = "Invalid move"
        return m



    attachment = MessageAttachmentsClass()
    button = MessageButtonsClass()
    button.text = "Make move"
    button.value = "Make move"
    button.name = "Make move"
    button.command = {"service_application": str(user_integration.yellowant_integration_id), "function_name": "makemove",\
                      "inputs": ["move"],
                      "data" : {"move":"testing"}
                      }
    attachment.attach_button(button)

    button1 = MessageButtonsClass()
    button1.text = "Play Computer"
    button1.value = "Play Computer"
    button1.name = "Play Computer"
    button1.command = {"service_application": str(user_integration.yellowant_integration_id),
                      "function_name": "playcomputer",
                      "data": {"move": "testing"}
                      }
    attachment.attach_button(button1)

    attachment.image_url = IMAGE_URL + board.fen()[:-13]

    m.attach(attachment)

    return m
Beispiel #8
0
def makeMoveAgainst(args,user_integration):
    player_object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)
    opponent_object = UserIntegration.objects.get(yellowant_integration_id=player_object.opponent_integration_id)

    access_token = opponent_object.yellowant_integration_token
    yellowant_user_integration_object = YellowAnt(access_token=access_token)

    move = args.get("move")
    board = chess.Board(player_object.board_state)
    col = color_inv(player_object.board_state[-12])

    m = MessageClass()

    m.message_text = col + " to move"
    move_uci = board.parse_san(move)
    if move_uci in board.legal_moves:
        board.push_san(move)
        if board.is_insufficient_material():
            print("Insufficient material")
            m.message_text = "Insufficient material"
            endGame(args, user_integration)

            webhook_message = MessageClass()
            webhook_field = AttachmentFieldsClass()
            webhook_field.title = "Result"
            webhook_field.value = "Game drawn due to insufficient material"
            attachmentImage = MessageAttachmentsClass()
            attachmentImage.image_url = IMAGE_URL + board.fen()[:-13]
            attachmentImage.attach_field(webhook_field)
            webhook_message.attach(attachmentImage)





        if board.is_stalemate():
            print("Stalemate")
            m.message_text = "Stalemate"
            endGame(args, user_integration)
        if board.is_checkmate():
            print(col + " wins")
            m.message_text = col + " wins"
            endGame(args, user_integration)

        player_object.board_state = board.fen()
        opponent_object.board_state = board.fen()

        player_object.save()
        opponent_object.save()



    else:
        m.message_text = "Invalid move"
        return m

    attachment = MessageAttachmentsClass()
    attachment.image_url = IMAGE_URL + board.fen()[:-13]

    m.attach(attachment)
    webhook_message = MessageClass()
    webhook_field = AttachmentFieldsClass()
    webhook_field.title = "Move"
    webhook_field.value = col + " to move"
    attachmentImage = MessageAttachmentsClass()
    attachmentImage.attach_field(webhook_field)
    attachmentImage.image_url = IMAGE_URL + board.fen()[:-13]

    button = MessageButtonsClass()
    button.text = "Make Move"
    button.value = "Make Move"
    button.name = "Make Move"
    button.command = {
        "service_application": str(opponent_object.yellowant_integration_id),
        "function_name": "makemoveagainst",
        "inputs": ["move"],
        "data": {"user_int": player_object.yellowant_integration_id},
    }
    attachmentImage.attach_button(button)

    webhook_message.attach(attachmentImage)



    send_message = yellowant_user_integration_object.create_webhook_message(
        requester_application=opponent_object.yellowant_integration_id,
        webhook_name="webhook", **webhook_message.get_dict())

    return m
Beispiel #9
0
def startGameAgainstPlayer(args,user_integration):
    """

    """


    player_object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)

    opponent_object = UserIntegration.objects.get(yellowant_integration_id=player_object.opponent_integration_id)

    board = chess.Board(player_object.board_state)
    color = args['Color']

    attachment = MessageAttachmentsClass()

    print(color + " to move")

    if (color=="Black"):
        webhook_message = MessageClass()
        webhook_message.message_text = "You are playing White!"
        attachment = MessageAttachmentsClass()
        button = MessageButtonsClass()
        button.text = "Make Move"
        button.value = "Make Move"
        button.name = "Make Move"
        button.command = {
            "service_application": str(opponent_object.yellowant_integration_id),
            "function_name": "makemoveagainst",
            "data": {"user_int": player_object.yellowant_integration_id},
        }
        attachment.attach_button(button)

        webhook_message.attach(attachment)
        access_token = opponent_object.yellowant_integration_token
        yellowant_user_integration_object = YellowAnt(access_token=access_token)

        send_message = yellowant_user_integration_object.create_webhook_message(
            requester_application=opponent_object.yellowant_integration_id,
            webhook_name="webhook", **webhook_message.get_dict())

        return

    else:
        print("Inside else")
        m = MessageClass()
        attachment = MessageAttachmentsClass()
        attachment.image_url = IMAGE_URL + INITIAL_BOARD
        m.message_text = "Make a Move"
        field1 = AttachmentFieldsClass()
        field1.title = "Move"
        field1.value = color + " to move"
        attachment.attach_field(field1)

        attachment.image_url = IMAGE_URL + INITIAL_BOARD


        button = MessageButtonsClass()
        button.text = "Make Move"
        button.value = "Make Move"
        button.name = "Make Move"
        button.command = {
            "service_application": str(player_object.yellowant_integration_id),
            "function_name": "makemoveagainst",
            "inputs": ["move"],
            "data": {"user_int": player_object.yellowant_integration_id},
        }
        attachment.attach_button(button)

        m.attach(attachment)

        return m
Beispiel #10
0
def playComputer(args,user_integration):
    """
    Function to play against the Computer.
    """
    object = UserIntegration.objects.get(yellowant_integration_id=user_integration.yellowant_integration_id)
    board = chess.Board(object.board_state)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(dir_path)
    engine = chess.uci.popen_engine("/app/stockfish/Linux/stockfish-9")
    #engine = chess.uci.popen_engine("/app/Stockfish-master/src/stockfish")
    col = color(object.board_state[-12])
    engine.position(board)
    move = engine.go(movetime=2000)
    board.push(chess.Move.from_uci(str(move[0])))
    m = MessageClass()
    if board.is_insufficient_material():
        print("Insufficient material")
        m.message_text = "Insufficient material"

        return m

    if board.is_stalemate():
        print("Stalemate")
        m.message_text = "Stalemate"


    if board.is_checkmate():
        print("Computer wins")
        m.message_text = "Checkmate !! \n"+col + " wins"

        return m

    object.board_state = board.fen()
    object.save()

    attachment = MessageAttachmentsClass()
    print(IMAGE_URL + board.fen()[:-13])
    attachment.image_url = IMAGE_URL + board.fen()[:-13]

    button = MessageButtonsClass()
    button.text = "Make move"
    button.value = "Make move"
    button.name = "Make move"
    button.command = {"service_application": str(user_integration.yellowant_integration_id), "function_name": "makemove",\
                      "inputs": ["move"],
                      "data" : {"move":"testing"}
                      }
    attachment.attach_button(button)

    button1 = MessageButtonsClass()
    button1.text = "Play Computer"
    button1.value = "Play Computer"
    button1.name = "Play Computer"
    button1.command = {"service_application": str(user_integration.yellowant_integration_id), "function_name": "playcomputer", \
                       "data": {"move": "testing"}
                      }
    attachment.attach_button(button1)

    button2 = MessageButtonsClass()
    button2.text = "Accept Invitation"
    button2.value = "Accept Invitation"
    button2.name = "Accept Invitation"
    button2.command = {
                      "service_application" : str(user_integration.yellowant_integration_id),
                      "function_name" : "accept",
                      "data" : {"user_int": user_integration.yellowant_integration_id }
                     }
    attachment.attach_button(button2)


    m.attach(attachment)

    return m