Untitled

mail@pastecode.io avatar
unknown
python
19 days ago
4.8 kB
2
Indexable
Never
class Pokemon_catch(View):
    def __init__(self, user_id):
        self.user_id = user_id
        self.clicks = self.retries = self.food_given = 0
        self.catched = False
        self.choosen = self.item_name = self.emoji = None
        self.max_attempts = pyrandom.randint(2, 5)
        self.max_food_given = 3

    def embed(self, title, desc, color=0x248046):
        return Message(title=title, description=desc, color=color)

    async def check_user(self, interaction):
        if interaction.user.id != self.user_id:
            await defer(interaction, action="create", ephemeral=True)
            respond_interaction(interaction, "❌ Vous ne pouvez pas cliquer.")
            return False
        return True

    @button(label="Ball", style="success", emoji=emoji('pokeball', True))
    async def catch_button(self, interaction, b):
        if not await self.check_user(interaction): return
        await defer(interaction, action="update")

        if self.clicks >= self.max_attempts:
            embed = self.embed(
                "Oh non...",
                f"**{self.item_name}** s'est échappé après **{self.clicks}** essais... 😔"
            )
            respond_interaction(
                interaction, content=None, embed=embed, components=None
            )
            self.stop()
            return

        self.clicks += 1
        capture_chance = min(
            self.choosen['weight'] * (0.2 * self.clicks) - 0.05 * self.food_given, 1.0
        )
        if pyrandom.random() < capture_chance:
            self.catched = True
            self.emoji = "<a:apokeball:1287191436864917504>"

            await SlashMongo("inventory", "items").update_one(
                {"item_numb": str(self.choosen["item_numb"]), 
                "user_id": str(self.user_id)}, {"$inc": {"count": 1}}, upsert=True
            )
            embed = self.embed(
                "Bravo ! ✨",
                f"Tu as réussi à capturer **{self.item_name}** ! 🎉"
            )
            respond_interaction(
                interaction, content=self.emoji, embed=embed, components=None
            )
            self.stop()
            return

        embed = self.embed(
            "❌ Oh non !",
            f"La capture a échoué. Essai N° **{self.clicks}**. Essaie encore !"
        )
        respond_interaction(interaction, embed=embed)

    @button(label="Appât", style="secondary", emoji="🍖")
    async def feed_button(self, interaction, b):
        if not await self.check_user(interaction): return
        await defer(interaction, action="update")

        if self.food_given < self.max_food_given:
            self.food_given += 1
            if pyrandom.random() < 0.6:
                self.max_attempts += 1
            
            embed = self.embed(
                f"🍖 **{self.item_name}** mange...",
                f"### Appâts restants: **`{self.max_food_given - self.food_given}`**\n"
                "Réduis les chances de fuite,\n mais rend la capture plus difficile."
            )
            respond_interaction(interaction, embed=embed)
        else:
            embed = self.embed(
                "❌ Limite atteinte",
                "Tu as atteint la limite max de 3 appâts\npour ce pokemon."
            )
            respond_interaction(interaction, embed=embed)

    @button(label="", style="primary", emoji="🔄")
    async def retry_button(self, interaction, b):
        if not await self.check_user(interaction): return
        await defer(interaction, action="update")

        if self.retries < 3:
            self.retries += 1
            self.clicks = self.food_given = 0
            await self.start(interaction)
        else:
            embed = self.embed(
                "❌ Limite atteinte",
                "Tu as atteint la limite max de 3 relances."
            )
            respond_interaction(interaction, embed=embed)

    async def start(self, interaction):
        all_emojis = await SlashMongo("inventory", "emojis").find({})
        self.choosen = pyrandom.choices(
            all_emojis, weights=map(lambda item: item['weight'], all_emojis))[0]
        self.item_name = self.choosen['emoji_name']
        self.emoji = f"<a:{self.item_name}:{self.choosen['emoji_id']}>"

        embed = self.embed(
            f"Un `{self.item_name}` sauvage apparaît !",
            f"🔄 Relances restantes : **{3 - self.retries}**"
        )
        respond_interaction(
            interaction, content=self.emoji, embed=embed, components=self
        )

view = Pokemon_catch(user.id)
await view.start(interaction)

exit_code = await view.wait(scope=scope)
if exit_code == "timeout":
    respond_interaction(
        interaction, content=f"⏰ {user.mention}, vous avez été déconnecté.",
        embed=None, components=None
    )
    return
Leave a Comment