From fd10614b20da01c2d9c958165d452b358a6ce244 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 15 Jul 2023 13:16:13 +0200 Subject: [PATCH] Improved internal achievement checks #268_achievements --- kdb-bot/src/bot_data/db_context.py | 5 ++++ .../bot_data/model/user_got_achievement.py | 3 -- .../achievements_repository_service.py | 4 +-- .../achievements/achievement_service.py | 29 ++++++++++--------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/kdb-bot/src/bot_data/db_context.py b/kdb-bot/src/bot_data/db_context.py index 46eff3bb..fb5e9f55 100644 --- a/kdb-bot/src/bot_data/db_context.py +++ b/kdb-bot/src/bot_data/db_context.py @@ -11,6 +11,7 @@ class DBContext(DatabaseContext): self._logger = logger DatabaseContext.__init__(self) + self._fails = 0 def connect(self, database_settings: DatabaseSettings): try: @@ -32,7 +33,11 @@ class DBContext(DatabaseContext): try: return super(DBContext, self).select(statement) except Exception as e: + if self._fails >= 3: + self._logger.fatal(__name__, f"Database error caused by {statement}", e) + self._logger.error(__name__, f"Database error caused by {statement}", e) + self._fails += 1 try: time.sleep(0.5) return self.select(statement) diff --git a/kdb-bot/src/bot_data/model/user_got_achievement.py b/kdb-bot/src/bot_data/model/user_got_achievement.py index 3ca33d5d..18bb6b92 100644 --- a/kdb-bot/src/bot_data/model/user_got_achievement.py +++ b/kdb-bot/src/bot_data/model/user_got_achievement.py @@ -4,7 +4,6 @@ from typing import Optional from cpl_core.database import TableABC from bot_data.model.achievement import Achievement -from bot_data.model.server import Server from bot_data.model.user import User @@ -13,7 +12,6 @@ class UserGotAchievement(TableABC): self, user: Optional[User], achievement: Optional[Achievement], - server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, id=0, @@ -21,7 +19,6 @@ class UserGotAchievement(TableABC): self._id = id self._user = user self._achievement = achievement - self._server = server TableABC.__init__(self) self._created_at = created_at if created_at is not None else self._created_at diff --git a/kdb-bot/src/bot_data/service/achievements_repository_service.py b/kdb-bot/src/bot_data/service/achievements_repository_service.py index da09786c..6ee69abc 100644 --- a/kdb-bot/src/bot_data/service/achievements_repository_service.py +++ b/kdb-bot/src/bot_data/service/achievements_repository_service.py @@ -41,8 +41,8 @@ class AchievementRepositoryService(AchievementRepositoryABC): return UserGotAchievement( self._users.get_user_by_id(result[1]), self.get_achievement_by_id(result[2]), - self._servers.get_server_by_id(result[3]), - result[5], + result[3], + result[4], id=result[0], ) diff --git a/kdb-bot/src/modules/achievements/achievement_service.py b/kdb-bot/src/modules/achievements/achievement_service.py index f767ef87..23fb3754 100644 --- a/kdb-bot/src/modules/achievements/achievement_service.py +++ b/kdb-bot/src/modules/achievements/achievement_service.py @@ -33,16 +33,20 @@ class AchievementService: self._message_service = message_service self._t = t + self._attributes = List(AchievementAttribute) + + self._attributes.extend( + [ + AchievementAttribute("xp", lambda user: user.xp, "number"), + AchievementAttribute("message_count", lambda user: user.message_count, "number"), + AchievementAttribute("reaction_count", lambda user: user.reaction_count, "number"), + AchievementAttribute("ontime", lambda user: user.ontime, "number"), + AchievementAttribute("level", lambda user: user.level, "Level"), + ] + ) + def get_attributes(self) -> List[AchievementAttribute]: - attributes = List(AchievementAttribute) - - attributes.add(AchievementAttribute("xp", lambda user: user.xp, "number")) - attributes.add(AchievementAttribute("message_count", lambda user: user.message_count, "number")) - attributes.add(AchievementAttribute("reaction_count", lambda user: user.reaction_count, "number")) - attributes.add(AchievementAttribute("ontime", lambda user: user.ontime, "number")) - attributes.add(AchievementAttribute("level", lambda user: user.level, "Level")) - - return attributes + return self._attributes def _match(self, value: str, operator: str, expected_value: str) -> bool: match operator: @@ -66,14 +70,13 @@ class AchievementService: return user_achievements.where(lambda x: x.name == achievement.name).count() > 0 def has_user_achievement(self, user: User, achievement: Achievement) -> bool: - return self._match(str(getattr(user, achievement.attribute)), achievement.operator, achievement.value) + attribute: AchievementAttribute = self._attributes.where(lambda x: x.name == achievement.attribute).single() + return self._match(str(attribute.resolve(user)), achievement.operator, achievement.value) async def validate_achievements_for_user(self, user: User): achievements = self._achievements.get_achievements_by_server_id(user.server.id) for achievement in achievements: - if self.has_user_achievement_already(user, achievement) and not self.has_user_achievement( - user, achievement - ): + if self.has_user_achievement_already(user, achievement) or not self.has_user_achievement(user, achievement): continue self._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server))