Skip to content

Commit 222f376

Browse files
committed
Add /simulate command
1 parent 438208c commit 222f376

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

app/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async def load_cogs(self):
2020
await self.load_extension("app.extensions.recent")
2121
await self.load_extension("app.extensions.profile")
2222
await self.load_extension("app.extensions.top")
23+
await self.load_extension("app.extensions.simulate")
2324
await self.tree.sync()
2425

2526
def run():

app/extensions/simulate.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
from rosu_pp_py import Performance, Beatmap
3+
from discord import app_commands, Interaction
4+
from discord.ext.commands import Bot
5+
6+
from app.common.database.repositories import beatmaps
7+
from app.common.database.objects import DBBeatmap
8+
from app.common.helpers import performance
9+
from app.common.constants import Mods
10+
from app.extensions.types import *
11+
from app.cog import BaseCog
12+
13+
class SimulateScore(BaseCog):
14+
@app_commands.command(name="simulate", description="Simulate pp for a beatmap")
15+
async def simulate_score(
16+
self,
17+
interaction: Interaction,
18+
beatmap_id: int,
19+
mods: str = "NM",
20+
mode: ModeType | None = None,
21+
combo: int | None = None,
22+
accuracy: float = 100.0,
23+
misses: int = 0
24+
) -> None:
25+
if not (beatmap := await self.resolve_beatmap(beatmap_id)):
26+
return await interaction.response.send_message(
27+
"I could not find that beatmap.",
28+
ephemeral=True
29+
)
30+
31+
if not (beatmap_file := self.storage.get_beatmap(beatmap_id)):
32+
return await interaction.response.send_message(
33+
"I could not find that beatmap.",
34+
ephemeral=True
35+
)
36+
37+
target_mods = Mods.from_string(mods).value
38+
target_mode = Modes.get(mode, beatmap.mode)
39+
40+
converted_mode = performance.convert_mode(target_mode)
41+
beatmap_object = Beatmap(bytes=beatmap_file)
42+
beatmap_object.convert(converted_mode, target_mods)
43+
44+
calculator = Performance(lazer=False)
45+
calculator.set_accuracy(accuracy)
46+
calculator.set_misses(misses)
47+
calculator.set_mods(target_mods)
48+
49+
if combo is not None:
50+
calculator.set_combo(combo)
51+
52+
result = calculator.calculate(beatmap_object)
53+
await interaction.response.send_message(f"PP: {result.pp:.2f}")
54+
55+
async def resolve_beatmap(self, beatmap_id: int) -> DBBeatmap | None:
56+
return await self.run_async(
57+
beatmaps.fetch_by_id,
58+
beatmap_id
59+
)
60+
61+
async def setup(bot: Bot):
62+
await bot.add_cog(SimulateScore())

0 commit comments

Comments
 (0)