2023-08-10 14:05:40 +03:00
|
|
|
import logging
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from bson import ObjectId
|
|
|
|
|
|
|
|
from modules.database import col_users
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PyroUser:
|
|
|
|
"""Dataclass of DB entry of a user"""
|
|
|
|
|
|
|
|
__slots__ = (
|
|
|
|
"_id",
|
|
|
|
"id",
|
|
|
|
"group",
|
|
|
|
"failed",
|
|
|
|
"emojis",
|
|
|
|
"selected",
|
|
|
|
"score",
|
|
|
|
"mistakes",
|
|
|
|
)
|
|
|
|
|
|
|
|
_id: ObjectId
|
|
|
|
id: int
|
|
|
|
group: int
|
|
|
|
failed: int
|
|
|
|
emojis: List[str]
|
|
|
|
selected: List[str]
|
|
|
|
score: int
|
|
|
|
mistakes: int
|
|
|
|
|
|
|
|
@classmethod
|
2023-08-11 16:04:21 +03:00
|
|
|
async def create_if_not_exists(
|
2023-08-10 14:05:40 +03:00
|
|
|
cls,
|
|
|
|
id: int,
|
|
|
|
group: int,
|
|
|
|
failed: bool = False,
|
|
|
|
emojis: List[str] = [],
|
|
|
|
selected: List[str] = [],
|
|
|
|
score: int = 0,
|
|
|
|
mistakes: int = 0,
|
|
|
|
):
|
2023-08-11 16:04:21 +03:00
|
|
|
db_entry = await col_users.find_one({"id": id, "group": group})
|
2023-08-14 12:01:28 +03:00
|
|
|
|
2023-08-10 14:05:40 +03:00
|
|
|
if db_entry is None:
|
2023-08-11 16:04:21 +03:00
|
|
|
inserted = await col_users.insert_one(
|
2023-08-10 14:05:40 +03:00
|
|
|
{
|
|
|
|
"id": id,
|
|
|
|
"group": group,
|
|
|
|
"failed": failed,
|
|
|
|
"emojis": emojis,
|
|
|
|
"selected": selected,
|
|
|
|
"score": score,
|
|
|
|
"mistakes": mistakes,
|
|
|
|
}
|
|
|
|
)
|
2023-08-14 12:01:28 +03:00
|
|
|
db_entry = await col_users.find_one({"_id": inserted.inserted_id})
|
|
|
|
|
|
|
|
if db_entry is None:
|
|
|
|
raise RuntimeError("Could not find inserted user entry.")
|
|
|
|
|
2023-08-10 14:05:40 +03:00
|
|
|
return cls(**db_entry)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def set_failed(self, failed: bool = True) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s's failure state has been set to %s", self.id, failed)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"failed": failed}})
|
2023-08-10 14:05:40 +03:00
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def set_emojis(self, emojis: List[str]) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s's emojis have been set to %s", self.id, emojis)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"emojis": emojis}})
|
2023-08-10 14:05:40 +03:00
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def set_score(self, score: int = 0) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s's score has been set to %s", self.id, score)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"score": score}})
|
2023-08-10 14:05:40 +03:00
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def set_mistakes(self, mistakes: int = 0) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s's mistakes count has been set to %s", self.id, mistakes)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"mistakes": mistakes}})
|
2023-08-10 14:05:40 +03:00
|
|
|
|
2023-08-12 23:36:26 +03:00
|
|
|
async def set_selected(self, emojis: List[str] = None) -> None:
|
|
|
|
logger.debug("%s's elected emojis have been set to %s", self.id, emojis)
|
|
|
|
await col_users.update_one({"_id": self._id}, {"$set": {"selected": emojis}})
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def update_score(self, points: int = 1) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s point(s) have been added to %s score", points, self.id)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one(
|
2023-08-10 14:05:40 +03:00
|
|
|
{"_id": self._id}, {"$set": {"score": self.score + points}}
|
|
|
|
)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def update_mistakes(self, points: int = 1) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("%s point(s) have been added to %s mistakes", points, self.id)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one(
|
2023-08-10 14:05:40 +03:00
|
|
|
{"_id": self._id}, {"$set": {"mistakes": self.mistakes + points}}
|
|
|
|
)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
async def update_selected(self, entry: str) -> None:
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.debug("Emoji %s has been added to %s's selection list", entry, self.id)
|
2023-08-11 16:04:21 +03:00
|
|
|
await col_users.update_one(
|
2023-08-10 14:05:40 +03:00
|
|
|
{"_id": self._id}, {"$set": {"selected": self.selected + [entry]}}
|
|
|
|
)
|