| 
 | 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