From 104c0b32a1e30a56900bb6a17ab9a009c54b76bb Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Tue, 7 May 2024 16:16:57 +0200 Subject: Refactoring --- tgbot.py | 72 +++++++++++++++++++++++++++++----------------------------------- 1 file changed, 32 insertions(+), 40 deletions(-) (limited to 'tgbot.py') diff --git a/tgbot.py b/tgbot.py index 1bab35e..360f3b0 100755 --- a/tgbot.py +++ b/tgbot.py @@ -9,55 +9,43 @@ from persistence import UserRepository, StatsRepository, PresenceRepository from TelegramBot import TelegramBot, CommandHandler from Commands import * -class ClientInitObserver(ClientInit): - async def on_event(self) -> None: - # Accept pending friends - for friend_request in fortniteClient.incoming_pending_friends: - await fortniteClient.event_friend_request(friend_request) +class FortniteStatusObserverImpl(FortniteStatusObserver): - # Record user stats - if len(statsRepository.getStats()) == 0: - await recordStatsCommand.handle(None) + telegram_bot: TelegramBot + + def __init__(self, telegram_bot: TelegramBot): + self.__telegram_bot = telegram_bot -class FortniteStatusObserver(Observer): - async def update(self, fortniteStatus) -> None: - await telegramBot.send_message_to_all(formatFortniteStatus(fortniteStatus)) + async def update(self, fortnite_status) -> None: + await self.__telegram_bot.send_message_to_all(formatFortniteStatus(fortnite_status)) -class FortnitePresenceObserver(PresenceObserver): - async def update(self, display_name: str, playing: bool, party_size: int) -> None: - if playing: - last_presence = prensenceRepository.getLastUserPresence(display_name) - diff = time.time() - last_presence - if diff > 60 * 60: # 60 minutes - await self.__notifyFriendPlaying(display_name, party_size) - prensenceRepository.setLastUserPresence(display_name, time.time()) +user_repository = UserRepository() +stats_repository = StatsRepository() +presence_repository = PresenceRepository() +telegram_bot = TelegramBot(user_repository) +fortnite_status_notifier = FortniteStatusNotifier(FortniteStatusObserverImpl(telegram_bot)) - async def __notifyFriendPlaying(self, display_name: str, party_size: int): - await telegramBot.send_message_to_all(formatFriendOnline(display_name, party_size)) +fortnite_client = FortniteClient( + ClientInitObserverImpl(), + FriendPresenceObserverImpl(telegram_bot, presence_repository)) -userRepository = UserRepository() -statsRepository = StatsRepository() -prensenceRepository = PresenceRepository() -telegramBot = TelegramBot(userRepository) -fortniteStatusWrapper = FortniteStatusNotifier(FortniteStatusObserver()) -fortniteClient = FortniteClient(FortnitePresenceObserver(), ClientInitObserver()) -recordStatsCommand = RecordStatsCommand(telegramBot, fortniteClient, statsRepository) +record_stats_command = RecordStatsCommand(fortnite_client, stats_repository) -telegramBot.register_command_handler('start', StartCommand(telegramBot, userRepository)) -telegramBot.register_command_handler('status', GetStatusCommand(telegramBot)) -telegramBot.register_command_handler('friends', GetFriendsCommand(telegramBot, fortniteClient)) -telegramBot.register_command_handler('stats', GetStatsCommand(telegramBot, fortniteClient)) -telegramBot.register_command_handler('todaystats', GetTodayStatsCommand(telegramBot, fortniteClient, statsRepository)) -telegramBot.register_command_handler('recordstats', recordStatsCommand) +telegram_bot.register_command_handler('start', StartCommand(telegram_bot, user_repository)) +telegram_bot.register_command_handler('status', GetStatusCommand(telegram_bot)) +telegram_bot.register_command_handler('friends', GetFriendsCommand(telegram_bot, fortnite_client)) +telegram_bot.register_command_handler('stats', GetStatsCommand(telegram_bot, fortnite_client)) +telegram_bot.register_command_handler('todaystats', GetTodayStatsCommand(telegram_bot, fortnite_client, stats_repository)) +telegram_bot.register_command_handler('recordstats', record_stats_command) async def run_tgbot(): - await telegramBot.run() + await telegram_bot.run() -async def run_fortniteStatusWrapper(): - await fortniteStatusWrapper.run() +async def run_fortnite_status_notifier(): + await fortnite_status_notifier.run() -async def run_fortniteClient(): - fortniteClient.run() +async def run_fortnite_client(): + fortnite_client.run() async def run_record_stats(): while True: @@ -67,7 +55,11 @@ async def run_record_stats(): await asyncio.sleep(60 * 60) # 1 hour async def run_all(): - await asyncio.gather(run_tgbot(), run_fortniteStatusWrapper(), run_fortniteClient(), run_record_stats()) + await asyncio.gather( + run_tgbot(), + run_fortnite_status_notifier(), + run_fortnite_client(), + run_record_stats()) if __name__ == '__main__': nest_asyncio.apply() -- cgit v1.2.3