Skip to content

Commit

Permalink
allow custom leaderboard sizes for premium users
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku committed Sep 8, 2024
1 parent 0bc59e8 commit 8ec14ce
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/api/leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import app.state
import app.usecases
import config
from app.constants.leaderboard_type import LeaderboardType
from app.constants.mode import Mode
from app.constants.mods import Mods
from app.constants.privileges import Privileges
from app.models.user import User
from app.repositories.leaderboards import LeaderboardScore
from app.usecases.user import authenticate_user
Expand Down Expand Up @@ -105,6 +107,11 @@ async def get_leaderboard(
else None
)

if user.privileges & Privileges.USER_PREMIUM and user.leaderboard_size is not None:
leaderboard_size = user.leaderboard_size
else:
leaderboard_size = config.LEADERBOARD_SIZE

leaderboard = await app.usecases.leaderboards.fetch_beatmap_leaderboard(
beatmap,
mode,
Expand All @@ -113,6 +120,7 @@ async def get_leaderboard(
mods_filter=mods_filter,
country_filter=country_filter,
user_ids_filter=user_ids_filter,
leaderboard_size=leaderboard_size,
)

response_lines.append(
Expand Down
1 change: 1 addition & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User:
password_bcrypt: str
country: str
vanilla_pp_leaderboards: bool
leaderboard_size: int | None

def __repr__(self) -> str:
return f"<{self.name} ({self.id})>"
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ async def fetch_beatmap_leaderboard(
play_mode: int,
requestee_user_id: int,
scores_table: Literal["scores", "scores_relax", "scores_ap"],
score_limit: int,
mods_filter: int | None = None,
country_filter: str | None = None,
user_ids_filter: list[int] | None = None,
best_scores_only: bool = True,
score_limit: int = 100,
sort_column: Literal["pp", "score"] = "pp",
) -> list[LeaderboardScore]:
params = {
Expand Down
2 changes: 1 addition & 1 deletion app/usecases/leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ async def fetch_beatmap_leaderboard(
*,
requestee_user_id: int,
vanilla_pp_leaderboards: bool,
leaderboard_size: int,
mods_filter: Mods | None = None,
country_filter: str | None = None,
user_ids_filter: list[int] | None = None,
leaderboard_size: int = 100,
) -> Leaderboard:
# if there is a mods filter we will allow non-bests
# so that a user's best modded score will appear
Expand Down
2 changes: 2 additions & 0 deletions app/usecases/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def fetch_db(username: str) -> User | None:
password_bcrypt=db_user["password_md5"],
country=db_user["country"],
vanilla_pp_leaderboards=db_user["vanilla_pp_leaderboards"],
leaderboard_size=db_user["leaderboard_size"],
)


Expand Down Expand Up @@ -79,6 +80,7 @@ async def fetch_db_id(user_id: int) -> User | None:
password_bcrypt=db_user["password_md5"],
country=db_user["country"],
vanilla_pp_leaderboards=db_user["vanilla_pp_leaderboards"],
leaderboard_size=db_user["leaderboard_size"],
)


Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@
SCORE_SUBMISSION_ROUTING_KEYS: list[str] = list(
config("SCORE_SUBMISSION_ROUTING_KEYS", cast=CommaSeparatedStrings),
)

LEADERBOARD_SIZE = config("LEADERBOARD_SIZE", cast=int, default=100)

0 comments on commit 8ec14ce

Please sign in to comment.