Allow force-resetting the game

pull/124/head
stijndcl 2022-07-27 21:25:07 +02:00
parent db499f3742
commit 4a137bcad8
4 changed files with 20 additions and 3 deletions

View File

@ -6,6 +6,8 @@ from bson import ObjectId
from overrides import overrides from overrides import overrides
from pydantic import BaseModel, Field, validator from pydantic import BaseModel, Field, validator
from database.constants import WORDLE_GUESS_COUNT
__all__ = ["MongoBase", "TemporaryStorage", "WordleGame"] __all__ = ["MongoBase", "TemporaryStorage", "WordleGame"]
from database.utils.datetime import today_only_date from database.utils.datetime import today_only_date
@ -117,3 +119,16 @@ class WordleGame(MongoCollection):
raise ValueError(f"guess_distribution must be no longer than 6 elements, found {len(value)}") raise ValueError(f"guess_distribution must be no longer than 6 elements, found {len(value)}")
return value return value
def is_game_over(self, word: str) -> bool:
"""Check if the current game is over"""
# No guesses yet
if not self.guesses:
return False
# Max amount of guesses allowed
if len(self.guesses) == WORDLE_GUESS_COUNT:
return True
# Found the correct word
return self.guesses[-1] == word

View File

@ -47,10 +47,12 @@ class Games(commands.Cog):
# Make a guess # Make a guess
if guess: if guess:
# The guess is not a real word # The guess is not a real word
if guess not in self.client.wordle_words: if guess.lower() not in self.client.wordle_words:
embed = WordleErrorEmbed(message=f"`{guess}` is not a valid word.").to_embed() embed = WordleErrorEmbed(message=f"`{guess}` is not a valid word.").to_embed()
return await interaction.followup.send(embed=embed) return await interaction.followup.send(embed=embed)
guess = guess.lower()
await make_wordle_guess(self.client.mongo_db, interaction.user.id, guess) await make_wordle_guess(self.client.mongo_db, interaction.user.id, guess)
# Don't re-request the game, we already have it # Don't re-request the game, we already have it

View File

@ -130,7 +130,7 @@ class Tasks(commands.Cog):
async def reset_wordle_word(self, forced: bool = False): async def reset_wordle_word(self, forced: bool = False):
"""Reset the daily Wordle word""" """Reset the daily Wordle word"""
db = self.client.mongo_db db = self.client.mongo_db
word = await set_daily_word(db, random.choice(tuple(self.client.wordle_words))) word = await set_daily_word(db, random.choice(tuple(self.client.wordle_words)), forced=forced)
self.client.database_caches.wordle_word.data = [word] self.client.database_caches.wordle_word.data = [word]
@reset_wordle_word.before_loop @reset_wordle_word.before_loop

View File

@ -108,7 +108,7 @@ class WordleEmbed(EmbedBaseModel):
embed.description = "\n\n".join(rows) embed.description = "\n\n".join(rows)
# If the game is over, reveal the word # If the game is over, reveal the word
if len(self.game.guesses) == WORDLE_GUESS_COUNT or (self.game.guesses and self.game.guesses[-1] == self.word): if self.game.is_game_over(self.word):
embed.description += f"\n\nThe word was **{self.word.upper()}**!" embed.description += f"\n\nThe word was **{self.word.upper()}**!"
embed.set_footer(text=footer()) embed.set_footer(text=footer())