Skip to content

Commit

Permalink
type fixes and added rank value calculation to scores repo
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostiDrinks committed Nov 1, 2024
1 parent d5b599d commit b9cbc72
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import app.usecases.performance
import app.utils
from app.constants import regexes
from app.constants.gamemodes import GAMEMODE_REPR_LIST
from app.constants.gamemodes import GAMEMODE_REPR_LIST, GameMode
from app.constants.mods import SPEED_CHANGING_MODS
from app.constants.mods import Mods
from app.constants.privileges import ClanPrivileges
Expand Down Expand Up @@ -315,7 +315,7 @@ async def maplink(ctx: Context) -> str | None:
async def recent(ctx: Context) -> str | None:
"""Show information about a player's most recent score."""
if ctx.args:
target = await users_repo.fetch_one(name=ctx.args[1])
target = await users_repo.fetch_one(name=" ".join(ctx.args))
if not target:
return "Player not found."
score = await scores_repo.fetch_recent(user_id=target["id"])
Expand All @@ -328,15 +328,19 @@ async def recent(ctx: Context) -> str | None:
if beatmap is None:
return "We don't have a beatmap on file for your recent score."

l = [f"[{score['mode']!r}] {beatmap.embed}", f"{score['acc']:.2f}%"]
l = [f"[{GameMode(score['mode'])!r}] {beatmap.embed}", f"{score['acc']:.2f}%"]

if score["mods"]:
l.insert(1, f"+{score['mods']!r}")
l.insert(1, f"+{Mods(score['mods'])!r}")

l = [" ".join(l)]

if score["grade"] != "F":
rank = score["grade"] if score["status"] == SubmissionStatus.BEST else "NA"
rank = (
await scores_repo.calculate_placement(score)
if score["status"] == SubmissionStatus.BEST
else "NA"
)
l.append(f"PASS {{{score['pp']:.2f}pp #{rank}}}")
else:
# XXX: prior to v3.2.0, bancho.py didn't parse total_length from
Expand Down
28 changes: 28 additions & 0 deletions app/repositories/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sqlalchemy.dialects.mysql import FLOAT
from sqlalchemy.dialects.mysql import TINYINT

from app.constants.gamemodes import GameMode
import app.state.services
from app._typing import UNSET
from app._typing import _UnsetSentinel
Expand Down Expand Up @@ -235,6 +236,33 @@ async def fetch_many(
return cast(list[Score], scores)


async def calculate_placement(score: Score) -> int:
assert score["map_md5"] is not None

if GameMode(score["mode"]) >= GameMode.RELAX_OSU:
scoring_metric = "pp"
scoring = score["pp"]
else:
scoring_metric = "score"
scoring = score["score"]

num_better_scores: int | None = await app.state.services.database.fetch_val(
"SELECT COUNT(*) AS c FROM scores s "
"INNER JOIN users u ON u.id = s.userid "
"WHERE s.map_md5 = :map_md5 AND s.mode = :mode "
"AND s.status = 2 AND u.priv & 1 "
f"AND s.{scoring_metric} > :scoring",
{
"map_md5": score["map_md5"],
"mode": GameMode(score["mode"]),
"scoring": scoring,
},
column=0, # COUNT(*)
)
assert num_better_scores is not None
return num_better_scores + 1


async def partial_update(
id: int,
pp: float | _UnsetSentinel = UNSET,
Expand Down

0 comments on commit b9cbc72

Please sign in to comment.