didier/database/crud/wordle.py

83 lines
2.6 KiB
Python
Raw Normal View History

2022-07-25 22:58:02 +02:00
from typing import Optional
from database.enums import TempStorageKey
2022-07-27 21:10:43 +02:00
from database.mongo_types import MongoDatabase
from database.schemas.mongo.temporary_storage import TemporaryStorage
from database.schemas.mongo.wordle import WordleGame
from database.utils.datetime import today_only_date
2022-07-25 22:58:02 +02:00
2022-07-27 21:10:43 +02:00
__all__ = [
"get_active_wordle_game",
"make_wordle_guess",
"start_new_wordle_game",
"set_daily_word",
"reset_wordle_games",
]
2022-07-25 22:58:02 +02:00
2022-07-27 21:10:43 +02:00
async def get_active_wordle_game(database: MongoDatabase, user_id: int) -> Optional[WordleGame]:
2022-07-25 22:58:02 +02:00
"""Find a player's active game"""
2022-07-27 21:10:43 +02:00
collection = database[WordleGame.collection()]
result = await collection.find_one({"user_id": user_id})
if result is None:
return None
return WordleGame(**result)
2022-07-25 22:58:02 +02:00
2022-07-27 21:10:43 +02:00
async def start_new_wordle_game(database: MongoDatabase, user_id: int) -> WordleGame:
2022-07-25 22:58:02 +02:00
"""Start a new game"""
2022-07-27 21:10:43 +02:00
collection = database[WordleGame.collection()]
game = WordleGame(user_id=user_id)
2022-07-25 22:58:02 +02:00
await collection.insert_one(game.dict(by_alias=True))
return game
2022-07-27 21:10:43 +02:00
async def make_wordle_guess(database: MongoDatabase, user_id: int, guess: str):
2022-07-25 22:58:02 +02:00
"""Make a guess in your current game"""
2022-07-27 21:10:43 +02:00
collection = database[WordleGame.collection()]
2022-07-25 22:58:02 +02:00
await collection.update_one({"user_id": user_id}, {"$push": {"guesses": guess}})
2022-07-27 21:10:43 +02:00
async def get_daily_word(database: MongoDatabase) -> Optional[str]:
"""Get the word of today"""
2022-07-27 21:10:43 +02:00
collection = database[TemporaryStorage.collection()]
result = await collection.find_one({"key": TempStorageKey.WORDLE_WORD, "day": today_only_date()})
if result is None:
return None
return result["word"]
2022-07-27 21:10:43 +02:00
async def set_daily_word(database: MongoDatabase, word: str, *, forced: bool = False) -> str:
"""Set the word of today
This does NOT overwrite the existing word if there is one, so that it can safely run
2022-07-27 21:10:43 +02:00
on startup every time.
In order to always overwrite the current word, set the "forced"-kwarg to True.
Returns the word that was chosen. If one already existed, return that instead.
"""
2022-07-27 21:10:43 +02:00
collection = database[TemporaryStorage.collection()]
2022-07-30 16:14:32 +02:00
current_word = None if forced else await get_daily_word(database)
if current_word is not None:
2022-07-27 21:10:43 +02:00
return current_word
await collection.update_one(
2022-07-27 21:10:43 +02:00
{"key": TempStorageKey.WORDLE_WORD}, {"$set": {"day": today_only_date(), "word": word}}, upsert=True
)
2022-07-27 21:10:43 +02:00
# Remove all active games
await reset_wordle_games(database)
return word
async def reset_wordle_games(database: MongoDatabase):
"""Reset all active games"""
collection = database[WordleGame.collection()]
await collection.drop()