Skip to content

Commit

Permalink
refactor: make /gacha roll appear faster by moving some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AstreaTSS committed Nov 1, 2024
1 parent 16b95ec commit 2a60dfe
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions exts/gacha/gacha_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ async def gacha_roll(self, ctx: utils.THIASlashContext) -> None:
if not ctx.author.has_role(config.player_role):
raise utils.CustomCheckFailure("You do not have the Player role.")

player = await models.GachaPlayer.get_or_create(
ctx.guild.id, ctx.author.id, include={"items": True}
)
player = await models.GachaPlayer.get_or_create(ctx.guild.id, ctx.author.id)

if player.currency_amount < config.gacha.currency_cost:
raise utils.CustomCheckFailure(
Expand All @@ -72,9 +70,7 @@ async def gacha_roll(self, ctx: utils.THIASlashContext) -> None:
"players": {"none": {"player_id": player.id}},
}

item_count = await models.GachaItem.prisma().count(
where=where,
)
item_count = await models.GachaItem.prisma().count(where=where)
if item_count == 0:
raise utils.CustomCheckFailure("There are no items available to roll.")

Expand All @@ -84,11 +80,16 @@ async def gacha_roll(self, ctx: utils.THIASlashContext) -> None:
order={"id": "asc"},
)

new_count = player.currency_amount - config.gacha.currency_cost
embed = item.embed()
embed.set_footer(f"{new_count} {config.names.currency_name(new_count)} left")

await ctx.send(embed=embed)

async with self.bot.db.batch_() as batch:
if item.amount != -1:
item.amount -= 1
batch.prismagachaitem.update(
data={"amount": item.amount}, where={"id": item.id}
data={"amount": {"decrement": 1}}, where={"id": item.id}
)

batch.prismagachaplayer.update(
Expand All @@ -102,12 +103,6 @@ async def gacha_roll(self, ctx: utils.THIASlashContext) -> None:
}
)

new_count = player.currency_amount - config.gacha.currency_cost
embed = item.embed()
embed.set_footer(f"{new_count} {config.names.currency_name(new_count)} left")

await ctx.send(embed=embed)

@gacha.subcommand(
"pull",
sub_cmd_description="Pulls for an item in the gacha. Alias of /gacha roll.",
Expand Down

0 comments on commit 2a60dfe

Please sign in to comment.