Skip to content

Commit

Permalink
Fix free quest re-rolls
Browse files Browse the repository at this point in the history
  • Loading branch information
Iapetus-11 committed Jul 3, 2024
1 parent c9d8abd commit b7f2914
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
38 changes: 18 additions & 20 deletions bot/cogs/core/quests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from discord.ext import commands

from bot.utils.ctx import CustomContext
from bot.utils.misc import emojify_item, make_progress_bar
from bot.utils.misc import emojify_item, get_user_and_lang_from_loc, make_progress_bar
from bot.villager_bot import VillagerBotCluster
from common.models.data import Quest
from common.models.db.quests import UserQuest as DbUserQuest
Expand Down Expand Up @@ -49,12 +49,24 @@ async def interaction_check(self, interaction: discord.Interaction) -> bool:

@discord.ui.button(label="Get New Quest", style=discord.ButtonStyle.gray)
async def btn_get_new_quest(self, interaction: discord.Interaction, button: discord.ui.Button):
# TODO: This shit is cooked broua;odkfja;lkdfja;lkdjf;lskj
_, lang = get_user_and_lang_from_loc(self._bot.l, self._loc)
user = await self._db.fetch_user(self._user_id)

if (arrow.now(datetime.timezone.utc) - user.last_dq_reroll).total_seconds() <= (60 * 30):
await interaction.response.edit_message(
embed=None,
view=None,
content=lang.econ.daily_quests.cant_reroll,
)
return

await self._db.delete_user_daily_quest(self._user_id)
await self._db.update_user(self._user_id, last_dq_reroll=datetime.datetime.now(datetime.timezone.utc))

quest = await self._quests.fetch_user_daily_quest(self._user_id)

embed = self._quests.get_quest_embed(self._loc, quest)

await interaction.response.edit_message(embed=embed, view=None)

async def on_timeout(self) -> None:
Expand Down Expand Up @@ -89,9 +101,7 @@ def db(self) -> "Database":
def get_quest_embed(
self, loc: CustomContext | commands.Context | discord.User, quest: UserQuest
):
lang = self.bot.l["en"]
if isinstance(loc, CustomContext | commands.Context):
lang = loc.l
_, lang = get_user_and_lang_from_loc(self.bot.l, loc)

quest_text = lang.econ.daily_quests.mapping[quest.key]
quest_emoji = (
Expand Down Expand Up @@ -226,11 +236,7 @@ async def get_random_quest_to_store(self, user_id: int) -> typing.TypedDict(
async def quest_completed(
self, loc: CustomContext | commands.Context | discord.User, quest: UserQuest
) -> None:
user_id: int
if isinstance(loc, CustomContext | commands.Context):
user_id = loc.author.id
else:
user_id = loc.id
user_id, _ = get_user_and_lang_from_loc(self.bot.l, loc)

await asyncio.sleep(1.0 + random.randint(0, 2))

Expand All @@ -257,11 +263,7 @@ async def notify_of_quest(
loc: CustomContext | commands.Context | discord.User,
quest: UserQuest,
) -> None:
user_id: int
if isinstance(loc, CustomContext | commands.Context):
user_id = loc.author.id
else:
user_id = loc.id
user_id, _ = get_user_and_lang_from_loc(self.bot.l, loc)

await self.db.mark_daily_quest_as_notified(user_id, quest.key)

Expand Down Expand Up @@ -298,11 +300,7 @@ async def update_user_daily_quest(
value: int | float,
mode: typing.Literal["add", "set"] = "add",
) -> None:
user_id: int
if isinstance(loc, CustomContext | commands.Context):
user_id = loc.author.id
else:
user_id = loc.id
user_id, _ = get_user_and_lang_from_loc(self.bot.l, loc)

db_quest = await self.db.update_user_daily_quest(user_id, key, value, mode)
quest = self._get_user_quest_from_db_quest(db_quest)
Expand Down
1 change: 1 addition & 0 deletions bot/data/text/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@
"progress": "**Progress:** {progress}",
"reward": "**Reward:** {reward_amount}{reward_emoji}",
"expires_at": "**Expires:** {expires_at}",
"cant_reroll": "You can't re-roll your quest yet, if you think this is a bug please report it",
"encouragements": {
"close": [
"You're getting close!",
Expand Down
1 change: 1 addition & 0 deletions bot/data/text/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@
"progress": "**Progreso:** {progress}",
"reward": "**Recompensa:** {reward_amount}{reward_emoji}",
"expires_at": "**Tiempo límite:** {expires_at}",
"cant_reroll": "No puede volver a reiniciar su misión todavía. Si cree que esto es un error, por favor reportarlo",
"encouragements": {
"close": [
"¡Ya casi lo tienes!",
Expand Down
1 change: 1 addition & 0 deletions bot/data/text/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@
"progress": "**Avancement :** {progress}",
"reward": "**Récompense :** {reward_amount}{reward_emoji}",
"expires_at": "**Fin :** {expires_at}",
"cant_reroll": "Vous ne pouvez pas encore réinitialiser votre quête, si vous pensez qu'il s'agit d'un bug, veuillez le signaler",
"encouragements": {
"close": [
"Tu y es presque !",
Expand Down
1 change: 1 addition & 0 deletions bot/data/text/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,7 @@
"progress": "**Progresso:** {progress}",
"reward": "**Recompensa:** {reward_amount}{reward_emoji}",
"expires_at": "**Expira em:** {expires_at}",
"cant_reroll": "Você não pode alterar sua missão ainda, se achar que isso é um bug, por favor reporte",
"encouragements": {
"close": [
"Você está chegando perto!",
Expand Down
1 change: 1 addition & 0 deletions bot/models/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ class Econ_DailyQuests(ImmutableBaseModel):
progress: str
reward: str
expires_at: str
cant_reroll: str
encouragements: Econ_Quests_Encouragements


Expand Down
20 changes: 20 additions & 0 deletions bot/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import aiohttp
import discord
from discord.ext import commands

from bot.models.translation import Translation
from bot.utils.ctx import CustomContext
from common.models.data import Data, Emojis
from common.models.db.item import Item
from common.models.db.user import User
Expand Down Expand Up @@ -429,3 +432,20 @@ def text_to_discord_file(text: str, *, file_name: str | None = None) -> discord.
file_data = io.BytesIO(text.encode(encoding="utf8"))
file_data.seek(0)
return discord.File(file_data, filename=file_name)


def get_user_and_lang_from_loc(
langs: dict[str, Translation],
loc: CustomContext | commands.Context | discord.User,
) -> tuple[int, Translation]:
user_id: int
if isinstance(loc, CustomContext | commands.Context):
user_id = loc.author.id
else:
user_id = loc.id

lang = langs["en"]
if isinstance(loc, CustomContext | commands.Context):
lang = loc.l

return user_id, lang
1 change: 1 addition & 0 deletions common/models/db/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class User(BaseModel):
last_vote: datetime.datetime | None
give_alert: bool = Field(default=True)
shield_pearl: datetime.datetime | None
last_dq_reroll: datetime.datetime
3 changes: 2 additions & 1 deletion setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ CREATE TABLE IF NOT EXISTS users ( -- used for economy data
vote_streak INT NOT NULL DEFAULT 0, -- the current vote streak of the user
last_vote TIMESTAMPTZ, -- the time at which the last user voted
give_alert BOOLEAN NOT NULL DEFAULT true, -- whether users should be alerted if someone gives them items or emeralds or not
shield_pearl TIMESTAMPTZ -- time at which last shield pearl was activated
shield_pearl TIMESTAMPTZ, -- time at which last shield pearl was activated
last_dq_reroll TIMESTAMPTZ NOT NULL DEFAULT NOW() -- time at which the daily quest was last re-rolled
);

CREATE TABLE IF NOT EXISTS items (
Expand Down

0 comments on commit b7f2914

Please sign in to comment.