Exemplo n.º 1
0
def get_pgcr(activityId):
    user = User.query.filter_by(
        bungieMembershipId=g.user.bungieMembershipId).first()
    my_api = BungieApi(user)

    activity = my_api.get_pgcr(activityId)

    return jsonify(activity)
Exemplo n.º 2
0
def put_pgcr(activityId):
    """
    Add a PGCR to the PGCR table and a reference to the PGCR in Users.pgcrs.
    The User has an allocation of stored PGCR's: pgcr_count
    """

    user = User.query.filter_by(
        bungieMembershipId=g.user.bungieMembershipId).first()
    my_api = BungieApi(user)

    pgcr_list = []
    status = None
    space = None
    exists = None

    pgcr_entries = PGCRs.query.join(User).filter(User.id == user.id).all()
    for e in pgcr_entries:
        pgcr_list.append(e.activityId)

    pgcr_count = user.pgcr_count
    pgcr_allocation = user.pgcr_allocation

    space = True if pgcr_count < pgcr_allocation else False
    exists = True if activityId in pgcr_list else False

    # print(f"space: {space} exists: {exists} activityId: {activityId}")

    if space and not exists:
        activity = my_api.get_pgcr(str(activityId))

        if activity["ErrorStatus"] == "Success":
            print(f"Adding PGCR: {activityId} for user: {user.unique_name}")
            status = True
            duration = 0
            for e in activity["Response"]["entries"]:
                if int(e["values"]["activityDurationSeconds"]["basic"]
                       ["value"]) > duration:
                    duration = int(e["values"]["activityDurationSeconds"]
                                   ["basic"]["value"])
            membershipType = activity["Response"]["activityDetails"][
                "membershipType"]
            mode = activity["Response"]["activityDetails"]["mode"]
            players = len(activity["Response"]["entries"])
            period = datetime.strptime(activity["Response"]["period"],
                                       "%Y-%m-%dT%H:%M:%SZ")

            pgcr = PGCRs.createPGCR(
                activityId=activityId,
                membershipType=membershipType,
                mode=mode,
                players=players,
                duration=duration,
                period=period,
            )
            user.pgcr_count += 1
            user.pgcrs.append(pgcr)
            db.session.add(pgcr)
            db.session.commit()

            response = {
                "errorStatus": "Success",
                "message": f"Stored: {activityId}",
                "activityId": activityId,
                "membershipType": membershipType,
                "mode": mode,
                "players": players,
                "duration": duration,
                "period": period,
                "user_has_room": space,
                "already_stored": exists,
                "pgcr_req_successful": status,
            }
            return jsonify(response)

        else:
            status = False

    response = {
        "errorStatus": "Fail",
        "message": "There was an error",
        "user_has_room": space,
        "already_stored": exists,
        "pgcr_req_successful": status,
    }

    return jsonify(response)
Exemplo n.º 3
0
def pgcr_list(membershipType, membershipId, characterId):

    mode_arg = request.args.get('game_mode', 'pvp')

    game_mode_switch = {
        'pvp': 5,
        'trials': 84,
        'gambit': 63,
        'raid': 4,
        'nightfall': 46,
        'dungeon': 82,
    }
    game_mode = game_mode_switch[mode_arg]

    game_count = 10

    user = User.query.filter_by(
        bungieMembershipId=g.user.bungieMembershipId).first()
    my_api = BungieApi(user)

    activity = my_api.get_activity_history(membershipType,
                                           membershipId,
                                           characterId,
                                           mode=game_mode,
                                           count=game_count)

    pgcr_list = []
    stat_list = []
    activities_res = activity["Response"].get("activities", None)
    if not activities_res:
        print("\n\n\nNo activities!!!")
        pgcr_list_res = {
            "Response": stat_list,
            "statusCode": 200,
            "ErrorStatus": "Success",
        }

        return jsonify(pgcr_list_res)

    for a in activity["Response"]["activities"]:
        pgcr_list.append(a["activityDetails"]["instanceId"])
    pgcr_res_list = []

    for index, instanceId in enumerate(pgcr_list):
        pgcr_res = my_api.get_pgcr(instanceId)
        pgcr_res_list.append(pgcr_res["Response"])

        for entry in pgcr_res["Response"]["entries"]:
            if entry["player"]["destinyUserInfo"][
                    "membershipId"] == membershipId:
                try:
                    avg_life = int(entry["values"]["activityDurationSeconds"]
                                   ["basic"]["value"] /
                                   entry["values"]["deaths"]["basic"]["value"])
                except ZeroDivisionError:
                    avg_life = int(666)
                stats = {
                    "count":
                    index,
                    "instanceId":
                    pgcr_res["Response"]["activityDetails"]["instanceId"],
                    "PGCR":
                    pgcr_res["Response"]["activityDetails"]["instanceId"],
                    "period":
                    pgcr_res["Response"]["period"],
                    "standing":
                    entry["standing"],
                    "activityDurationSeconds":
                    entry["values"]["activityDurationSeconds"]["basic"]
                    ["displayValue"],
                    "averageLifeTime":
                    avg_life,
                    "precisionKills":
                    entry["extended"]["values"]["precisionKills"]["basic"]
                    ["displayValue"],
                    "data":
                    entry,
                }

                # Add weapon definition to responses:
                try:
                    for index, weapons in enumerate(
                            entry["extended"].get("weapons")):
                        definition = get_definition(
                            "DestinyInventoryItemDefinition",
                            str(weapons["referenceId"]))
                        stats["data"]["extended"]["weapons"][index][
                            "definition"] = definition
                except:
                    continue
                stat_list.append(stats)

    pgcr_list_res = {
        "Response": stat_list,
        "statusCode": 200,
        "ErrorStatus": "Success",
    }

    return jsonify(pgcr_list_res)