From 3089066696ce90eb1a4c0b381e9fc414ec00db85 Mon Sep 17 00:00:00 2001 From: ue86388 Date: Tue, 16 Apr 2024 15:28:02 +0200 Subject: User statistics --- tgbot.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'tgbot.py') diff --git a/tgbot.py b/tgbot.py index 1a0e733..ba87df7 100755 --- a/tgbot.py +++ b/tgbot.py @@ -1,16 +1,26 @@ #!/usr/bin/python3 -import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio, sys, logging +import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio, sys, logging, time from FortniteStatusNotifier import * from Formatter import * from FortniteClient import * from FortniteEvents import * -from persistence import UserRepository +from persistence import UserRepository, StatsRepository # Check token in environment variables if "TELEBOT_BOT_TOKEN" not in os.environ: raise AssertionError("Please configure TELEBOT_BOT_TOKEN as environment variables") +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) + + # Record user stats + if len(statsRepository.getStats()) == 0: + await record_user_stats() + class FortniteStatusObserver(Observer): async def update(self, fortniteStatus) -> None: await send_message_to_all(formatFortniteStatus(fortniteStatus)) @@ -43,9 +53,10 @@ class ExceptionHandler(telebot.ExceptionHandler): bot = telebot.async_telebot.AsyncTeleBot( token=os.environ["TELEBOT_BOT_TOKEN"], exception_handler=ExceptionHandler()) -userRepository = UserRepository('db.sqlite') +userRepository = UserRepository() +statsRepository = StatsRepository() fortniteStatusWrapper = FortniteStatusNotifier(FortniteStatusObserver()) -fortniteClient = FortniteClient(FortnitePresenceObserver()) +fortniteClient = FortniteClient(FortnitePresenceObserver(), ClientInitObserver()) @bot.message_handler(commands = ['start']) async def startCommand(message: telebot.types.Message): @@ -62,17 +73,30 @@ async def getStatus(message): @bot.message_handler(commands = ['friends']) async def getFriends(message): - await reply(message, await formatFriends(fortniteClient.get_friends())) + friends = await fortniteClient.get_friends() + await reply(message, formatUsers(friends)) + +@bot.message_handler(commands = ['stats']) +async def getStats(message): + friends = await fortniteClient.get_friends() + stats = [await friend.fetch_stats() for friend in friends] + await reply(message, formatUserStatsList(stats)) + +@bot.message_handler(commands = ['todaystats']) +async def getTodayStats(message): + persisted_stats = statsRepository.getStats() + friends = await fortniteClient.get_friends() + current_stats = [await friend.fetch_stats() for friend in friends] + await reply(message, formatUserStatsDifference(persisted_stats, current_stats)) @bot.message_handler(commands = ['find']) async def findUser(message): arg = message.text.split() if len(arg) > 1: search_user_display_name = arg[1] - users: typing.List[fortnitepy.User] = await fortniteClient.fetch_users_by_display_name(search_user_display_name) - if (len(users) > 0): - for user in users: - await reply(message, await formatUser(user)) + user: User = await fortniteClient.find_user(search_user_display_name) + if user is not None: + await reply(message, formatUser(user)) else: await reply(message, 'User {} not found'.format(search_user_display_name)) else: @@ -83,7 +107,6 @@ async def addUser(message): arg = message.text.split() if len(arg) > 1: user_id = arg[1] - print('Adding user with ID as friend {}'.format(user_id)) await fortniteClient.add_friend(user_id) await reply(message, 'Send friend request successfully') else: @@ -107,6 +130,12 @@ async def reply(message, message_text): message_text, parse_mode='MarkdownV2') +async def record_user_stats(): + print('Recording user stats') + friends = await fortniteClient.get_friends() + for friend in friends: + await statsRepository.putStats(friend) + async def run_tgbot(): await bot.polling() @@ -116,8 +145,15 @@ async def run_fortniteStatusWrapper(): async def run_fortniteClient(): fortniteClient.run() +async def run_record_stats(): + while True: + t = time.localtime() + if t.tm_hour == 5: # only at 05:00 + await record_user_stats() + await asyncio.sleep(60 * 60 * 60) # 1 hour + async def run_all(): - await asyncio.gather(run_tgbot(), run_fortniteStatusWrapper(), run_fortniteClient()) + await asyncio.gather(run_tgbot(), run_fortniteStatusWrapper(), run_fortniteClient(), run_record_stats()) if __name__ == '__main__': nest_asyncio.apply() -- cgit v1.2.3