From 0846bbb29b2d3ddc844d7abfbeab2bf3db55f876 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 18 Jul 2023 11:10:02 +0200 Subject: [PATCH] Added achievements to data integrity service #268_achievements --- .../service/data_integrity_service.py | 23 +++++++++++++++++++ .../achievements/achievement_service.py | 6 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot_core/service/data_integrity_service.py b/kdb-bot/src/bot_core/service/data_integrity_service.py index c3f76912..01f55653 100644 --- a/kdb-bot/src/bot_core/service/data_integrity_service.py +++ b/kdb-bot/src/bot_core/service/data_integrity_service.py @@ -24,6 +24,7 @@ from bot_data.model.user_joined_server import UserJoinedServer from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from bot_data.service.seeder_service import SeederService from bot_data.service.user_repository_service import ServerRepositoryABC +from modules.achievements.achievement_service import AchievementService from modules.base.configuration.base_server_settings import BaseServerSettings @@ -42,6 +43,7 @@ class DataIntegrityService: user_joins: UserJoinedServerRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC, user_joined_gs: UserJoinedGameServerRepositoryABC, + achievement_service: AchievementService, dtp: DateTimeOffsetPipe, ): self._config = config @@ -57,6 +59,7 @@ class DataIntegrityService: self._user_joins = user_joins self._user_joins_vc = user_joins_vc self._user_joined_gs = user_joined_gs + self._achievements = achievement_service self._dtp = dtp self._is_for_shutdown = False @@ -360,6 +363,25 @@ class DataIntegrityService: except Exception as e: self._logger.error(__name__, f"Cannot get UserJoinedGameServer", e) + def _check_for_user_achievements(self): + self._logger.debug(__name__, f"Start checking UserGotAchievement table") + + for guild in self._bot.guilds: + server = self._servers.find_server_by_discord_id(guild.id) + if server is None: + self._logger.fatal(__name__, f"Server not found in database: {guild.id}") + + for member in guild.members: + if member.bot: + self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot") + continue + + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id) + if user is None: + self._logger.fatal(__name__, f"User not found in database: {member.id}") + + self._bot.loop.create_task(self._achievements.validate_achievements_for_user(user)) + def check_data_integrity(self, is_for_shutdown=False): if is_for_shutdown != self._is_for_shutdown: self._is_for_shutdown = is_for_shutdown @@ -371,3 +393,4 @@ class DataIntegrityService: self._check_user_joins() self._check_user_joins_vc() self._check_user_joined_gs() + self._check_for_user_achievements() diff --git a/kdb-bot/src/modules/achievements/achievement_service.py b/kdb-bot/src/modules/achievements/achievement_service.py index 9e8a0ed8..fe875965 100644 --- a/kdb-bot/src/modules/achievements/achievement_service.py +++ b/kdb-bot/src/modules/achievements/achievement_service.py @@ -78,7 +78,7 @@ class AchievementService: return self._attributes def _match(self, value: any, operator: str, expected_value: str) -> bool: - return self._operators[operator](value, expected_value) + return self._operators[operator](str(value), expected_value) def has_user_achievement_already(self, user: User, achievement: Achievement) -> bool: user_achievements = self._achievements.get_achievements_by_user_id(user.id) @@ -96,10 +96,10 @@ class AchievementService: self._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server)) self._db.save_changes() - await self._give_user_xp(user) + self._give_user_xp(user) await self._send_achievement_notification(user.server.discord_id, user.discord_id, achievement.name) - async def _give_user_xp(self, user: User): + def _give_user_xp(self, user: User): settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{user.server.discord_id}") user.xp += settings.xp_per_achievement self._users.update_user(user)