From 130ba1b83fb3f04b769c6c11c4a064a509160407 Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Mon, 24 Jun 2024 17:50:59 +0200 Subject: Type for duration --- app_types/__init__.py | 8 ++++++++ telegram_bot/__init__.py | 2 +- telegram_bot/commands.py | 17 +++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app_types/__init__.py b/app_types/__init__.py index f44474d..12e9adf 100644 --- a/app_types/__init__.py +++ b/app_types/__init__.py @@ -3,6 +3,14 @@ import datetime __season__ = 30 # must correlate to currently active season number +class Duration(): + duration_days: int + description: str + + def __init__(self, duration_days: int, description: str): + self.duration_days = duration_days + self.description = description + class UserStats: user_id: str user_display_name: str diff --git a/telegram_bot/__init__.py b/telegram_bot/__init__.py index ebc8c47..c25af95 100644 --- a/telegram_bot/__init__.py +++ b/telegram_bot/__init__.py @@ -32,7 +32,7 @@ class TelegramBot: await self.__bot.polling( non_stop=True, timeout=500 - ) + ) def register_command_handler(self, command: str, command_handler: CommandHandler): self.__bot.register_message_handler( diff --git a/telegram_bot/commands.py b/telegram_bot/commands.py index c5e02ed..e3f8efa 100644 --- a/telegram_bot/commands.py +++ b/telegram_bot/commands.py @@ -5,12 +5,17 @@ from formatter import * from persistence import * from fortnite_client import * from fortnite_status import * +from app_types import * __stats_now__ = 'stats_now' __stats_day__ = 'stats_day' __stats_week__ = 'stats_week' __stats_month__ = 'stats_month' +__duration_day__ = Duration(1, 'day') +__duration_week__ = Duration(7, 'week') +__duration_month__ = Duration(30, 'month') + class StartCommand(CommandHandler): __telegram_bot: TelegramBot @@ -70,11 +75,11 @@ class GetStatsCallbackQueryHandler(CallbackQueryHandler): if call.data == __stats_now__: await self.reply_with_today_stats(call.message) elif call.data == __stats_day__: - await self.reply_with_stats_days_difference(call.message, 1, 'day') + await self.reply_with_stats_days_difference(call.message, __duration_day__) elif call.data == __stats_week__: - await self.reply_with_stats_days_difference(call.message, 7, 'week') + await self.reply_with_stats_days_difference(call.message, __duration_week__) elif call.data == __stats_month__: - await self.reply_with_stats_days_difference(call.message, 30, 'month') + await self.reply_with_stats_days_difference(call.message, __duration_month__) await self.__telegram_bot.answer_callback_query(callback_query_id=call.id) async def reply_with_today_stats(self, message): @@ -82,13 +87,13 @@ class GetStatsCallbackQueryHandler(CallbackQueryHandler): stats = [await friend.fetch_stats() for friend in friends] await self.__telegram_bot.reply(message, format_user_stats_list(stats)) - async def reply_with_stats_days_difference(self, message, days: int, timeframe_alias: str): - stats_datetime = datetime.datetime.now() - datetime.timedelta(days = days) + async def reply_with_stats_days_difference(self, message, duration: Duration): + stats_datetime = datetime.datetime.now() - datetime.timedelta(days = duration.duration_days) persisted_stats = self.__stats_repository.get_stats(stats_datetime) if len(persisted_stats) > 0: friends = await self.__fortnite_client.get_friends() current_stats = [await friend.fetch_stats() for friend in friends] - await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats, timeframe_alias)) + await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats, duration.description)) else: await self.__telegram_bot.reply(message, 'No stats available yet for selected time period') -- cgit v1.2.3