Skip to content

Commit

Permalink
Fix insight special action button (#454)
Browse files Browse the repository at this point in the history
* Fix insight special action attempting to acquire a lock that was already acquired
- Don't check the current adventure for a user's in_adventure state when doing interaction checks

* Make insight cooldown time consistent with other class abilities

* Remove some debug logs
  • Loading branch information
TrustyJAID authored Feb 26, 2024
1 parent 7fbe3ac commit 72d7d7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions adventure/class_abilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ async def insight(self, ctx: commands.Context):
cooldown_time = max(300, (900 - max((c.luck + c.total_cha) * 2, 0)))
if "cooldown" not in c.heroclass:
c.heroclass["cooldown"] = cooldown_time + 1
if c.heroclass["cooldown"] + cooldown_time <= time.time():
if c.heroclass["cooldown"] <= time.time():
max_roll = 100 if c.rebirths >= 30 else 50 if c.rebirths >= 15 else 20
roll = random.randint(min(c.rebirths - 25 // 2, (max_roll // 2)), max_roll) / max_roll
if ctx.guild.id in self._sessions and self._sessions[ctx.guild.id].insight[0] < roll:
Expand All @@ -572,7 +572,7 @@ async def insight(self, ctx: commands.Context):
good = False
await smart_embed(ctx, _("Another hero has already done a better job than you."))
c.heroclass["ability"] = True
c.heroclass["cooldown"] = time.time()
c.heroclass["cooldown"] = time.time() + cooldown_time
async with self.get_lock(c.user):
await self.config.user(ctx.author).set(await c.to_json(ctx, self.config))
if good:
Expand Down
41 changes: 20 additions & 21 deletions adventure/game_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,12 @@ async def send_cleric(self, interaction: discord.Interaction, c: Character):
async def send_insight(self, interaction: discord.Interaction, c: Character):
user = interaction.user
if c.heroclass["ability"]:
log.debug("Psychic in use already")
await self.send_in_use(interaction)
return
cooldown_time = max(300, (900 - max((c.luck + c.total_cha) * 2, 0)))
if "cooldown" not in c.heroclass:
c.heroclass["cooldown"] = cooldown_time + 1
if c.heroclass["cooldown"] + cooldown_time <= time.time():
if c.heroclass["cooldown"] <= time.time():
max_roll = 100 if c.rebirths >= 30 else 50 if c.rebirths >= 15 else 20
roll = random.randint(min(c.rebirths - 25 // 2, (max_roll // 2)), max_roll) / max_roll
if self.view.insight[0] < roll:
Expand All @@ -354,15 +353,15 @@ async def send_insight(self, interaction: discord.Interaction, c: Character):
cog=self.view.cog,
)
c.heroclass["ability"] = True
c.heroclass["cooldown"] = time.time()
async with self.view.cog.get_lock(c.user):
await self.view.cog.config.user(user).set(await c.to_json(self.view.ctx, self.view.cog.config))
if good:
msg = _("{skill} **{c}** is focusing on the monster ahead...{skill}").format(
c=escape(user.display_name),
skill=self.view.cog.emojis.skills.psychic,
)
await smart_embed(interaction=interaction, message=msg, cog=self.view.cog)
c.heroclass["cooldown"] = time.time() + cooldown_time

await self.view.cog.config.user(user).set(await c.to_json(self.view.ctx, self.view.cog.config))
if good:
msg = _("{skill} **{c}** is focusing on the monster ahead...{skill}").format(
c=escape(user.display_name),
skill=self.view.cog.emojis.skills.psychic,
)
await smart_embed(interaction=interaction, message=msg, cog=self.view.cog)
if good:
session = self.view
if roll <= 0.4:
Expand Down Expand Up @@ -725,21 +724,21 @@ async def interaction_check(self, interaction: discord.Interaction):
all_users = []
in_adventure = False
for guild_session in self.cog._sessions.values():
if guild_session.ctx.message.id == self.ctx.message.id:
continue
if guild_session.in_adventure(user):
in_adventure = True

if in_adventure:
user_id = f"{user.id}-{user.guild.id}"
# iterating through reactions here and removing them seems to be expensive
# so they can just keep their react on the adventures they can't join
if user_id not in self.cog._react_messaged:
await interaction.response.send_message(
_(
"**{c}**, you are already in an existing adventure. "
"Wait for it to finish before joining another one."
).format(c=escape(user.display_name)),
ephemeral=True,
)
self.cog._react_messaged.append(user_id)
return
await interaction.response.send_message(
_(
"**{c}**, you are already in an existing adventure. "
"Wait for it to finish before joining another one."
).format(c=escape(user.display_name)),
ephemeral=True,
)
return not in_adventure
return True

0 comments on commit 72d7d7a

Please sign in to comment.