From 1116a4c59fc5cbce790c2dafb2bf4a0c651f2904 Mon Sep 17 00:00:00 2001 From: Brian Lee Date: Fri, 19 Jul 2024 08:49:35 -0700 Subject: [PATCH] New function that makes playlists for every star level and sorts by lowest accuracy. --- pyproject.toml | 1 + src/saberlist/scoresaber.py | 48 ++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5c1815e..8cd9097 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,5 +34,6 @@ Homepage = "https://git.satstack.dev/blee/beatsaber-playlist-tool" # https://setuptools.pypa.io/en/latest/userguide/entry_point.html [project.scripts] player_scores_by_stars = "saberlist.scoresaber:replay_ranked" +replay_all_by_acc = "saberlist.scoresaber:replay_all_by_acc" leaderboard_songs_by_stars = "saberlist.scoresaber:leaderboard_songs" #replay_ranked_bl = "saberlist.beatleader:replay_ranked" \ No newline at end of file diff --git a/src/saberlist/scoresaber.py b/src/saberlist/scoresaber.py index 61794cd..be800dd 100644 --- a/src/saberlist/scoresaber.py +++ b/src/saberlist/scoresaber.py @@ -18,6 +18,13 @@ def filter_and_sort_scores_by_stars(scores, min_stars: float, max_stars: float): sorted_scores = sorted(filtered_scores, key=lambda x: x.leaderboard.stars) return sorted_scores +def filter_and_sort_scores_by_acc(scores, min_stars: float, max_stars: float): + # Exclude scores outside the specified star range + filtered_scores = [score for score in scores if min_stars <= score.leaderboard.stars < max_stars] + # Calculate the score percentage for sorting + sorted_scores = sorted(filtered_scores, key=lambda x: (x.score.base_score / x.leaderboard.max_score) * 100) + return sorted_scores + # TODO: combine with scores_to_playlist() def leaderboards_to_playlist(leaderboards: list, playlist_title: str, playlist_author: str = "SaberList Tool") -> str: playlist = { @@ -76,6 +83,44 @@ def scores_to_playlist(scores: list, playlist_title, playlist_author = "SaberLis return playlist_json +async def async_replay_all_by_acc(): + """Generate playlists for each star level and sort by lowest accuracy.""" + + scoresaber = ExtendedScoreSaberAPI() + try: + await scoresaber.start() + score_sort = ScoreSort.TOP + scores = [] + default_title = "Replay SS" + default_player_id = '76561199407393962' + player_id = input(f"Enter the playerid (Default: {default_player_id}): ") or default_player_id + + async for player_scores in scoresaber.player_scores_all(player_id, score_sort): + scores.extend(player_scores) + + if scores: + max_stars = max(score.leaderboard.stars for score in scores) + + for stars in range(1, int(max_stars) + 1): + playlist_title = f"{default_title} {stars}★" + filtered_sorted_scores = filter_and_sort_scores_by_acc(scores, stars, stars + 1) + if filtered_sorted_scores: + scores_to_playlist(filtered_sorted_scores, playlist_title) + else: + print(f"No scores found for {stars}★") + else: + print("No scores found for the given player.") + + finally: + await scoresaber._http_client.close() + +def replay_all_by_acc(): + try: + asyncio.run(async_replay_all_by_acc()) + except Exception as e: + print(f"An error occurred: {e}") + + async def async_replay_ranked(): scoresaber = ExtendedScoreSaberAPI() try: @@ -95,7 +140,8 @@ async def async_replay_ranked(): async for player_scores in scoresaber.player_scores_all(player_id, score_sort): scores.extend(player_scores) - filtered_sorted_scores = filter_and_sort_scores_by_stars(scores, min_stars, max_stars) + # filtered_sorted_scores = filter_and_sort_scores_by_stars(scores, min_stars, max_stars) + filtered_sorted_scores = filter_and_sort_scores_by_acc(scores, min_stars, max_stars) scores_to_playlist(filtered_sorted_scores, playlist_title) finally: await scoresaber._http_client.close()