First tests for game stats

This commit is contained in:
stijndcl 2022-07-30 18:27:58 +02:00
parent bf41acd9f4
commit b74f794639
5 changed files with 64 additions and 7 deletions

View file

@ -16,7 +16,7 @@ async def add_dad_joke(session: AsyncSession, joke: str) -> DadJoke:
return dad_joke
async def get_random_dad_joke(session: AsyncSession) -> DadJoke:
async def get_random_dad_joke(session: AsyncSession) -> DadJoke: # pragma: no cover # randomness is untestable
"""Return a random database entry"""
statement = select(DadJoke).order_by(func.random())
row = (await session.execute(statement)).first()

View file

@ -24,15 +24,15 @@ async def get_game_stats(database: MongoDatabase, user_id: int) -> GameStats:
return stats
async def complete_wordle_game(database: MongoDatabase, user_id: int, win: bool, guesses: int):
async def complete_wordle_game(database: MongoDatabase, user_id: int, win: bool, guesses: int = 0):
"""Update the user's Wordle stats"""
stats = await get_game_stats(database, user_id)
update: dict[str, dict[str, Union[int, datetime.datetime]]] = {"$inc": {"wordle.games": 1}}
update: dict[str, dict[str, Union[int, datetime.datetime]]] = {"$inc": {"wordle.games": 1}, "$set": {}}
if win:
update["$inc"]["wordle.wins"] = 1
update["$inc"][f"wordle.guess_distribution.{guesses}"] = 1
update["$inc"][f"wordle.guess_distribution.{guesses - 1}"] = 1
# Update streak
today = today_only_date()

View file

@ -7,8 +7,9 @@ LOCAL_TIMEZONE = zoneinfo.ZoneInfo("Europe/Brussels")
def today_only_date() -> datetime.datetime:
"""Mongo can't handle datetime.date, so we need datetime
"""Mongo can't handle datetime.date, so we need a datetime instance
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)
today = datetime.date.today()
return datetime.datetime(year=today.year, month=today.month, day=today.day)