summaryrefslogtreecommitdiff
path: root/tgbot.py
blob: 6fbd07c7b9ce10975c45231cc7ae410d1a2bda50 (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
#!/usr/bin/python3

import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio, sys
from fortnite_status_notifier import *
from formatter import *
from fortnite_client import *
from fortnite_events import *
from persistence import UserRepository, StatsRepository, PresenceRepository
from telegram_bot import TelegramBot, CommandHandler
from commands import *

class FortniteStatusObserverImpl(FortniteStatusObserver):

    telegram_bot: TelegramBot

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

    async def update(self, fortnite_status) -> None:
        await self.__telegram_bot.send_message_to_all(format_fortnite_status(fortnite_status))

user_repository = UserRepository()
stats_repository = StatsRepository()
presence_repository = PresenceRepository()
telegram_bot = TelegramBot(user_repository)
fortnite_status_notifier = FortniteStatusNotifier(FortniteStatusObserverImpl(telegram_bot))

fortnite_client = FortniteClient(
    ClientInitObserverImpl(), 
    FriendPresenceObserverImpl(telegram_bot, presence_repository))

record_stats_command = RecordStatsCommand(fortnite_client, stats_repository)

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 telegram_bot.run()

async def run_fortnite_status_notifier():
    await fortnite_status_notifier.run()

async def run_fortnite_client():
    fortnite_client.run()

async def run_record_stats():
    while True:
        t = time.localtime()
        if t.tm_hour == 5: # only at 05:00
            await recordStatsCommand.handle(None)
        await asyncio.sleep(60 * 60) # 1 hour

async def run_all():
    await asyncio.gather(
        run_tgbot(), 
        run_fortnite_status_notifier(), 
        run_fortnite_client(), 
        run_record_stats())

if __name__ == '__main__':
    nest_asyncio.apply()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_all())
    loop.close()