summaryrefslogtreecommitdiff
path: root/telegram_bot/commands.py
blob: e3f8efa0774f92faafb5f71aa10d72db0bd2b36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import telebot
import datetime
from telegram_bot import *
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
    __user_repository: UserRepository

    def __init__(self, telegram_bot: TelegramBot, user_repository: UserRepository):
        self.__telegram_bot = telegram_bot
        self.__user_repository = user_repository

    async def handle(self, message: telebot.types.Message):
        if message.chat.type == 'private':
            alias = message.chat.username
        else:
            alias = message.chat.title
        self.__user_repository.put_user(message.chat.id, alias)
        await self.__telegram_bot.reply(message, 'This chat successfully registered to receive Fortnite updates')

class GetStatusCommand(CommandHandler):

    __telegram_bot: TelegramBot
    __fortnite_status: FortniteStatus

    def __init__(self, telegram_bot: TelegramBot):
        self.__telegram_bot = telegram_bot
        self.__fortnite_status = FortniteStatus()

    async def handle(self, message: telebot.types.Message):
        await self.__telegram_bot.reply(message, format_fortnite_status(self.__fortnite_status.get_status()))

class GetFriendsCommand(CommandHandler):

    __telegram_bot: TelegramBot
    __fortnite_client: FortniteClient

    def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient):
        self.__telegram_bot = telegram_bot
        self.__fortnite_client = fortnite_client

    async def handle(self, message: telebot.types.Message):
        if self.__fortnite_client.is_initialized():
            friends = await self.__fortnite_client.get_friends()
            await self.__telegram_bot.reply(message, format_users(friends))

class GetStatsCallbackQueryHandler(CallbackQueryHandler):

    __telegram_bot: TelegramBot
    __fortnite_client: FortniteClient
    __stats_repository: StatsRepository

    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, 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, __duration_day__)
            elif call.data == __stats_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, __duration_month__)
            await self.__telegram_bot.answer_callback_query(callback_query_id=call.id)

    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, 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, duration.description))
        else:
            await self.__telegram_bot.reply(message, 'No stats available yet for selected time period')

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):
        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):

    __fortnite_client: FortniteClient
    __stats_repository: StatsRepository

    def __init__(self, fortnite_client: FortniteClient, stats_repository: StatsRepository):
        self.__fortnite_client = fortnite_client
        self.__stats_repository = stats_repository

    async def handle(self, message: telebot.types.Message):
        if self.__fortnite_client.is_initialized():
            stats_datetime = datetime.datetime.now()
            friends = await self.__fortnite_client.get_friends()
            for friend in friends:
                await self.__stats_repository.put_stats(friend, stats_datetime)