Added achievements to data integrity service #268_achievements

This commit is contained in:
Sven Heidemann 2023-07-18 11:10:02 +02:00
parent d4dd55944a
commit 11a4874bfb
2 changed files with 26 additions and 3 deletions

View File

@ -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.model.user_joined_voice_channel import UserJoinedVoiceChannel
from bot_data.service.seeder_service import SeederService from bot_data.service.seeder_service import SeederService
from bot_data.service.user_repository_service import ServerRepositoryABC 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 from modules.base.configuration.base_server_settings import BaseServerSettings
@ -42,6 +43,7 @@ class DataIntegrityService:
user_joins: UserJoinedServerRepositoryABC, user_joins: UserJoinedServerRepositoryABC,
user_joins_vc: UserJoinedVoiceChannelRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC,
user_joined_gs: UserJoinedGameServerRepositoryABC, user_joined_gs: UserJoinedGameServerRepositoryABC,
achievement_service: AchievementService,
dtp: DateTimeOffsetPipe, dtp: DateTimeOffsetPipe,
): ):
self._config = config self._config = config
@ -57,6 +59,7 @@ class DataIntegrityService:
self._user_joins = user_joins self._user_joins = user_joins
self._user_joins_vc = user_joins_vc self._user_joins_vc = user_joins_vc
self._user_joined_gs = user_joined_gs self._user_joined_gs = user_joined_gs
self._achievements = achievement_service
self._dtp = dtp self._dtp = dtp
self._is_for_shutdown = False self._is_for_shutdown = False
@ -360,6 +363,25 @@ class DataIntegrityService:
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get UserJoinedGameServer", 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): def check_data_integrity(self, is_for_shutdown=False):
if is_for_shutdown != self._is_for_shutdown: if is_for_shutdown != self._is_for_shutdown:
self._is_for_shutdown = is_for_shutdown self._is_for_shutdown = is_for_shutdown
@ -371,3 +393,4 @@ class DataIntegrityService:
self._check_user_joins() self._check_user_joins()
self._check_user_joins_vc() self._check_user_joins_vc()
self._check_user_joined_gs() self._check_user_joined_gs()
self._check_for_user_achievements()

View File

@ -78,7 +78,7 @@ class AchievementService:
return self._attributes return self._attributes
def _match(self, value: any, operator: str, expected_value: str) -> bool: 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: def has_user_achievement_already(self, user: User, achievement: Achievement) -> bool:
user_achievements = self._achievements.get_achievements_by_user_id(user.id) 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._achievements.add_user_got_achievement(UserGotAchievement(user, achievement, user.server))
self._db.save_changes() 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) 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}") settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{user.server.discord_id}")
user.xp += settings.xp_per_achievement user.xp += settings.xp_per_achievement
self._users.update_user(user) self._users.update_user(user)