Skip to content

Commit

Permalink
Add score count queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Feb 28, 2024
1 parent 835ee4b commit 253f27e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions database/repositories/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ def fetch_top_scores(
.offset(offset) \
.all()

@session_wrapper
def fetch_top_scores_count(
user_id: int,
mode: int,
session: Session | None = None
) -> int:
return session.query(func.count(DBScore.id)) \
.filter(DBScore.user_id == user_id) \
.filter(DBScore.mode == mode) \
.filter(DBScore.status == 3) \
.scalar()

@session_wrapper
def fetch_leader_scores(
user_id: int,
Expand Down Expand Up @@ -159,6 +171,37 @@ def fetch_leader_scores(

return leader_scores

@session_wrapper
def fetch_leader_count(
user_id: int,
mode: int,
session: Session | None = None
) -> int:
# Find the maximum total score for each beatmap
subquery = session.query(
DBScore.beatmap_id,
DBScore.mode,
func.max(DBScore.total_score).label('max_total_score')
) \
.filter(DBScore.mode == mode) \
.filter(DBScore.status == 3) \
.group_by(DBScore.beatmap_id, DBScore.mode) \
.subquery()

# Get scores where the user has the highest total score
leader_count = session.query(func.count(DBScore.id)) \
.join(subquery, and_(
DBScore.beatmap_id == subquery.c.beatmap_id,
DBScore.mode == subquery.c.mode,
DBScore.total_score == subquery.c.max_total_score
)) \
.filter(DBScore.user_id == user_id) \
.filter(DBScore.mode == mode) \
.filter(DBScore.status == 3) \
.scalar()

return leader_count

@session_wrapper
def fetch_best(
user_id: int,
Expand Down Expand Up @@ -197,6 +240,19 @@ def fetch_pinned(
.offset(offset) \
.all()

@session_wrapper
def fetch_pinned_count(
user_id: int,
mode: int,
session: Session | None = None
) -> int:
return session.query(func.count(DBScore.id)) \
.filter(DBScore.user_id == user_id) \
.filter(DBScore.mode == mode) \
.filter(DBScore.status == 3) \
.filter(DBScore.pinned == True) \
.scalar()

@session_wrapper
def fetch_personal_best(
beatmap_id: int,
Expand Down

0 comments on commit 253f27e

Please sign in to comment.