Skip to content

Commit

Permalink
Fix session management for !recent
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Feb 24, 2024
1 parent 13cdc0f commit dcd5fc5
Showing 1 changed file with 56 additions and 57 deletions.
113 changes: 56 additions & 57 deletions app/commands/recent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

from app.common.database.repositories import users, scores
from app.common.database.objects import DBScore
from app.common.helpers import performance
from app.common.constants import Mods
from app.objects import Context

from typing import Optional, Tuple

from discord.ui import View, Button
from discord import ButtonStyle
from discord import Interaction
from discord import Embed
from discord import Color
Expand Down Expand Up @@ -52,68 +50,69 @@ async def view_replay(self, interaction: Interaction, button: Button):
@app.session.commands.register(["recent", "last", "r"])
async def recent(context: Context):
"""(username) - Displays the last play of another person or yourself"""
if not (user := users.fetch_by_discord_id(context.message.author.id)):
await context.message.channel.send("You don't have an account linked!")
return
with app.session.database.managed_session() as session:
if not (user := users.fetch_by_discord_id(context.message.author.id, session=session)):
await context.message.channel.send("You don't have an account linked!")
return

if context.args:
if not (user := users.fetch_by_name_extended(context.args[0], session=session)):
await context.message.channel.send("User not found!")
return

score = scores.fetch_recent_all(user_id=user.id, limit=1, session=session)

if context.args:
if not (user := users.fetch_by_name_extended(context.args[0])):
await context.message.channel.send("User not found!")
if not score:
await context.message.channel.send("No recent scores.")
return

score = scores.fetch_recent_all(user_id=user.id, limit=1)

if not score:
await context.message.channel.send("No recent scores.")
return

score = score[0]

rank = score.grade
max_combo = score.max_combo
accuracy = score.acc
n300 = score.n300
n100 = score.n100
n50 = score.n50
nmiss = score.nMiss
pp = score.pp
nscore = score.total_score
mods = Mods(score.mods).short

if_fc_fmt = ""
fc_pp, stars = get_difficulty_info(score)

if score.nMiss > 0 or (score.beatmap.max_combo - score.max_combo) > 10:
if_fc_fmt = f"({fc_pp:.2f}pp if FC)"

stars_fmt = f"{stars:0.1f}⭐"

mode_fmt = ('osu!', 'Taiko', 'Ctb', 'Mania')[score.mode]

embed = Embed(
title=f"[{stars_fmt}] {score.beatmap.beatmapset.full_name} [{score.beatmap.version}] +{mods} ({mode_fmt})",
url=f"http://osu.{config.DOMAIN_NAME}/b/{score.beatmap_id}",
color=Color.blue(),
)
embed.set_author(name=f"Recent play for {user.name}")
embed.set_thumbnail(url=f"https://osu.{config.DOMAIN_NAME}/a/{user.id}?h=50")
embed.set_image(
url=f"https://assets.ppy.sh/beatmaps/{score.beatmap.set_id}/covers/[email protected]"
)
score = score[0]

if score.status < 2:
rank = f"F ({int((score.failtime/1000)/score.beatmap.total_length*100)}%)"
rank = score.grade
max_combo = score.max_combo
accuracy = score.acc
n300 = score.n300
n100 = score.n100
n50 = score.n50
nmiss = score.nMiss
pp = score.pp
nscore = score.total_score
mods = Mods(score.mods).short

embed.description = f"{rank} {max_combo}/{score.beatmap.max_combo} {accuracy*100:.2f}% [{n300}/{n100}/{n50}/{nmiss}] {pp:.2f}pp {if_fc_fmt} {nscore:,}"
replay = None
if_fc_fmt = ""
fc_pp, stars = get_difficulty_info(score)

if score.mode == 0 and app.session.storage.get_replay(score.id):
replay = ViewReplayButton(score)
if score.nMiss > 0 or (score.beatmap.max_combo - score.max_combo) > 10:
if_fc_fmt = f"({fc_pp:.2f}pp if FC)"

await context.message.channel.send(
embed=embed, reference=context.message, mention_author=True,
view=replay
)
stars_fmt = f"{stars:0.1f}⭐"

mode_fmt = ('osu!', 'Taiko', 'Ctb', 'Mania')[score.mode]

embed = Embed(
title=f"[{stars_fmt}] {score.beatmap.beatmapset.full_name} [{score.beatmap.version}] +{mods} ({mode_fmt})",
url=f"http://osu.{config.DOMAIN_NAME}/b/{score.beatmap_id}",
color=Color.blue(),
)
embed.set_author(name=f"Recent play for {user.name}")
embed.set_thumbnail(url=f"https://osu.{config.DOMAIN_NAME}/a/{user.id}?h=50")
embed.set_image(
url=f"https://assets.ppy.sh/beatmaps/{score.beatmap.set_id}/covers/[email protected]"
)

if score.status < 2:
rank = f"F ({int((score.failtime/1000)/score.beatmap.total_length*100)}%)"

embed.description = f"{rank} {max_combo}/{score.beatmap.max_combo} {accuracy*100:.2f}% [{n300}/{n100}/{n50}/{nmiss}] {pp:.2f}pp {if_fc_fmt} {nscore:,}"
replay = None

if score.mode == 0 and app.session.storage.get_replay(score.id):
replay = ViewReplayButton(score)

await context.message.channel.send(
embed=embed, reference=context.message, mention_author=True,
view=replay
)


def get_difficulty_info(score: DBScore) -> Tuple[float, float, float]:
Expand Down

0 comments on commit dcd5fc5

Please sign in to comment.