summaryrefslogtreecommitdiff
path: root/telegram_bot/commands.py
diff options
context:
space:
mode:
authorDmitrii Morozov <snoopdesigns@gmail.com>2024-06-21 20:17:43 +0200
committerDmitrii Morozov <snoopdesigns@gmail.com>2024-06-21 20:17:43 +0200
commit6d4133a04d79b3313c352e536788333af491d4f8 (patch)
tree6c986a0e335eaea57aa30d5800df5066323736fd /telegram_bot/commands.py
parenta0191d146e821c75036fdc4a2b193e0bbbadcdda (diff)
Reworked statistics
Diffstat (limited to 'telegram_bot/commands.py')
-rw-r--r--telegram_bot/commands.py68
1 files changed, 56 insertions, 12 deletions
diff --git a/telegram_bot/commands.py b/telegram_bot/commands.py
index 1e3295b..a1a2888 100644
--- a/telegram_bot/commands.py
+++ b/telegram_bot/commands.py
@@ -1,10 +1,16 @@
import telebot
+from datetime import date, datetime
from telegram_bot import *
from formatter import *
from persistence import *
from fortnite_client import *
from fortnite_status import *
+__stats_now__ = 'stats_now'
+__stats_day__ = 'stats_day'
+__stats_week__ = 'stats_week'
+__stats_month__ = 'stats_month'
+
class StartCommand(CommandHandler):
__telegram_bot: TelegramBot
@@ -48,39 +54,77 @@ class GetFriendsCommand(CommandHandler):
friends = await self.__fortnite_client.get_friends()
await self.__telegram_bot.reply(message, format_users(friends))
-
-class GetStatsCommand(CommandHandler):
+class GetStatsCallbackQueryHandler(CallbackQueryHandler):
__telegram_bot: TelegramBot
__fortnite_client: FortniteClient
+ __stats_repository: StatsRepository
- def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient):
+ def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository):
self.__telegram_bot = telegram_bot
self.__fortnite_client = fortnite_client
+ self.__stats_repository = stats_repository
- async def handle(self, message: telebot.types.Message):
+ async def handle(self, call: telebot.types.CallbackQuery):
if self.__fortnite_client.is_initialized():
+ 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')
+ elif call.data == __stats_week__:
+ await self.reply_with_stats_days_difference(call.message, 7, 'week')
+ elif call.data == __stats_month__:
+ await self.reply_with_stats_days_difference(call.message, 30, 'month')
+
+ async def reply_with_today_stats(self, message):
+ friends = await self.__fortnite_client.get_friends()
+ 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_date = date.today() - datetime.timedelta(days)
+ persisted_stats = self.__stats_repository.get_stats(stats_date)
+ if len(persisted_stats) > 0:
friends = await self.__fortnite_client.get_friends()
- stats = [await friend.fetch_stats() for friend in friends]
- await self.__telegram_bot.reply(message, format_user_stats_list(stats))
+ 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))
+ else:
+ await self.__telegram_bot.reply(message, 'No stats available yet for selected time period')
-class GetTodayStatsCommand(CommandHandler):
+class GetStatsCommand(CommandHandler):
__telegram_bot: TelegramBot
__fortnite_client: FortniteClient
__stats_repository: StatsRepository
+ __reply_markup: any
def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository):
self.__telegram_bot = telegram_bot
self.__fortnite_client = fortnite_client
self.__stats_repository = stats_repository
+ self.__reply_markup = telebot.types.InlineKeyboardMarkup()
+ self.__reply_markup.add(telebot.types.InlineKeyboardButton('Now', callback_data=__stats_now__))
+ self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 day', callback_data=__stats_day__))
+ self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 week', callback_data=__stats_week__))
+ self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 month', callback_data=__stats_month__))
+
+ self.__telegram_bot.register_callback_query(
+ GetStatsCallbackQueryHandler(self.__telegram_bot, self.__fortnite_client, self.__stats_repository),
+ lambda call: True)
+
async def handle(self, message: telebot.types.Message):
- if self.__fortnite_client.is_initialized():
- persisted_stats = self.__stats_repository.get_stats()
- 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))
+ await self.__telegram_bot.reply(message, 'Which statistics would you like to see?', reply_markup=self.__reply_markup)
+
+class GetTodayStatsCommand(CommandHandler):
+
+ __telegram_bot: TelegramBot
+
+ def __init__(self, telegram_bot: TelegramBot):
+ self.__telegram_bot = telegram_bot
+
+ async def handle(self, message: telebot.types.Message):
+ await self.__telegram_bot.reply(message, 'Use new command /stats, you must, young Padawan')
class RecordStatsCommand(CommandHandler):