From 253f27efb35ac75301856e2ee695af0f0b6abd15 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 28 Feb 2024 21:07:37 +0100 Subject: [PATCH] Add score count queries --- database/repositories/scores.py | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/database/repositories/scores.py b/database/repositories/scores.py index e1e1a52..1c2c900 100644 --- a/database/repositories/scores.py +++ b/database/repositories/scores.py @@ -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, @@ -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, @@ -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,