def add_pending_player_id(self, player_id):
        self.pending_player_ids.append(player_id)

        session = get_new_db_session()
        session.query(Account).filter(Account.id == self.account_id).update(
            {'pending_player_ids': json.dumps({'pending_player_ids': self.pending_player_ids})}
        )
        session.commit()
        session.close()
    def __init__(self, account_id):
        session = get_new_db_session()
        account = session.query(Account).filter(Account.id == account_id).first()

        self.account_id = account_id
        self.email = account.email
        self.active_player_ids = json.loads(account.active_player_ids)['active_player_ids']
        self.pending_player_ids = json.loads(account.pending_player_ids)['pending_player_ids']
        self.game_invitations = json.loads(account.game_invitations)['game_invitation_ids']

        session.close()
def process_game_invite():
    """
    take in game reply object, pass along appropriate response to game service
    """
    response_object = request.get_json()
    logger.debug(
        'received game invite response {} from player id {} for game_id {}'.
        format(response_object['accept'], response_object['accountId'],
               response_object['gameId']))
    account_email = retrieve_account_email(response_object['accountId'])

    # TODO cleaner method for doing this
    session = get_new_db_session()
    account = session.query(Account).filter(
        Account.id == response_object['accountId']).first()

    if response_object['accept']:
        packet = send_invite_accept(game_id=response_object['gameId'],
                                    player_email=account_email,
                                    account_id=response_object['accountId'])

        pending_player_ids = json.loads(account.pending_player_ids)
        pending_player_ids['pending_player_ids'].append(packet.playerId)
        session.query(Account).filter(
            Account.id == response_object['accountId']).update(
                {'pending_player_ids': json.dumps(pending_player_ids)})

        game_invites = json.loads(account.game_invitations)
        game_invites['game_invitation_ids'].remove(response_object['gameId'])
        session.query(Account).filter(
            Account.id == response_object['accountId']).update(
                {'game_invitations': json.dumps(game_invites)})

        session.commit()
        session.close()
    else:
        send_invite_decline(game_id=response_object['gameId'],
                            player_email=account_email,
                            account_id=response_object['accountId'])
        game_invites = json.loads(account.game_invitations)
        game_invites['game_invitation_ids'].remove(response_object['gameId'])
        session.query(Account).filter(
            Account.id == response_object['accountId']).update(
                {'game_invitations': json.dumps(game_invites)})

        session.commit()
        session.close()

    return None, status.HTTP_202_ACCEPTED
def retrieve_account_id_from_db(email):
    """
    lookup and return a given account id from the database
    """
    session = get_new_db_session()

    try:
        account = session.query(Account).filter(Account.email == email).first()
        if account:
            return account.id
        else:
            raise NoSuchAccountException
    except SQLAlchemyError:
        raise SQLAlchemyError
    finally:
        session.close()
def retrieve_account_email(account_id):
    """
    retreives and returns account email for given account_id
    """
    session = get_new_db_session()

    try:
        account = session.query(Account).filter(
            Account.id == account_id).first()
        if account:
            return account.email
        else:
            raise NoSuchAccountException
    except SQLAlchemyError:
        raise SQLAlchemyError
    finally:
        session.close()
def check_account_id_exists(account_id):
    """
    checks that given account id exists in db
    """
    session = get_new_db_session()

    try:
        account = session.query(Account).filter(
            Account.id == account_id).first()
        if account:
            return True
        else:
            return False
    except SQLAlchemyError:
        raise SQLAlchemyError
    finally:
        session.close()
def game_invite(account_id=None, game_id=None):
    """
    invites given account to given game
    Returns True if success, False if failure
    """
    session = get_new_db_session()

    try:
        account = session.query(Account).filter(
            Account.id == account_id).first()
        if not account:
            return False

        game_invitations = json.loads(account.game_invitations)
        game_invitations['game_invitation_ids'].append(game_id)

        account.game_invitations = json.dumps(game_invitations)
        session.commit()

        return True
    except SQLAlchemyError:
        raise SQLAlchemyError
    finally:
        session.close()