Fix broken time formatting, remove word field

This commit is contained in:
stijndcl 2022-07-26 21:48:50 +02:00
parent cbd3030565
commit ea4181eac0
9 changed files with 22085 additions and 12 deletions

View file

@ -1,7 +1,9 @@
from typing import Optional
from database.enums import TempStorageKey
from database.mongo_types import MongoCollection
from database.schemas.mongo import WordleGame
from database.utils.datetime import today_only_date
__all__ = ["get_active_wordle_game", "make_wordle_guess", "start_new_wordle_game"]
@ -11,9 +13,9 @@ async def get_active_wordle_game(collection: MongoCollection, user_id: int) -> O
return await collection.find_one({"user_id": user_id})
async def start_new_wordle_game(collection: MongoCollection, user_id: int, word: str) -> WordleGame:
async def start_new_wordle_game(collection: MongoCollection, user_id: int) -> WordleGame:
"""Start a new game"""
game = WordleGame(user_id=user_id, word=word)
game = WordleGame(user_id=user_id)
await collection.insert_one(game.dict(by_alias=True))
return game
@ -21,3 +23,27 @@ async def start_new_wordle_game(collection: MongoCollection, user_id: int, word:
async def make_wordle_guess(collection: MongoCollection, user_id: int, guess: str):
"""Make a guess in your current game"""
await collection.update_one({"user_id": user_id}, {"$push": {"guesses": guess}})
async def get_daily_word(collection: MongoCollection) -> Optional[str]:
"""Get the word of today"""
result = await collection.find_one({"key": TempStorageKey.WORDLE_WORD, "day": today_only_date()})
if result is None:
return None
return result["word"]
async def set_daily_word(collection: MongoCollection, word: str):
"""Set the word of today
This does NOT overwrite the existing word if there is one, so that it can safely run
on startup every time
"""
current_word = await get_daily_word(collection)
if current_word is not None:
return
await collection.update_one(
{"key": TempStorageKey.WORDLE_WORD}, {"day": today_only_date(), "word": word}, upsert=True
)

View file

@ -1,6 +1,6 @@
import enum
__all__ = ["TaskType"]
__all__ = ["TaskType", "TempStorageKey"]
# There is a bug in typeshed that causes an incorrect PyCharm warning
@ -11,3 +11,10 @@ class TaskType(enum.IntEnum):
BIRTHDAYS = enum.auto()
UFORA_ANNOUNCEMENTS = enum.auto()
@enum.unique
class TempStorageKey(str, enum.Enum):
"""Enum for keys to distinguish the TemporaryStorage rows"""
WORDLE_WORD = "wordle_word"

View file

@ -6,7 +6,9 @@ from bson import ObjectId
from overrides import overrides
from pydantic import BaseModel, Field, conlist
__all__ = ["MongoBase", "WordleGame"]
__all__ = ["MongoBase", "TemporaryStorage", "WordleGame"]
from database.utils.datetime import today_only_date
class PyObjectId(str):
@ -55,6 +57,17 @@ class MongoCollection(MongoBase, ABC):
raise NotImplementedError
class TemporaryStorage(MongoCollection):
"""Collection for lots of random things that don't belong in a full-blown collection"""
key: str
@staticmethod
@overrides
def collection() -> str:
return "temporary"
class WordleStats(BaseModel):
"""Model that holds stats about a player's Wordle performance"""
@ -80,9 +93,9 @@ class GameStats(MongoCollection):
class WordleGame(MongoCollection):
"""Collection that holds people's active Wordle games"""
user_id: int
word: str
day: datetime.date = Field(default_factory=lambda: today_only_date())
guesses: conlist(str, min_items=0, max_items=6) = Field(default_factory=list)
user_id: int
@staticmethod
@overrides

View file

@ -1,5 +1,14 @@
import datetime
import zoneinfo
__all__ = ["LOCAL_TIMEZONE"]
__all__ = ["LOCAL_TIMEZONE", "today_only_date"]
LOCAL_TIMEZONE = zoneinfo.ZoneInfo("Europe/Brussels")
def today_only_date() -> datetime.datetime:
"""Mongo can't handle datetime.date, so we need datetime
We do, however, only care about the date, so remove all the rest
"""
return datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)