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

import fortnitepy
import json
import os
import typing
from DeviceAuth import DeviceAuth
from FortniteEvents import *
from Types import *

__fortnite_account_key__ = 'fornite-account-key'

class FortniteClient(fortnitepy.Client):

    device_auth = DeviceAuth()
    presenceObserver = None
    clientInit = None

    def __init__(self, friendPresenceObserver: PresenceObserver, clientInit: ClientInit):
        self.presenceObserver = friendPresenceObserver
        self.clientInit = clientInit
        if self.device_auth.device_auth_file_exists():
            self.__auth_device_auth()
        else:
            self.__auth_authorization_code()
    
    async def get_friends(self) -> typing.List[User]:
        return [User.from_fortnite_friend(friend) for friend in self.friends]
    
    async def find_user(self, display_name: str):
        user: fortnitepy.User = await self.fetch_user_by_display_name(display_name)
        return User.from_fortnite_friend(user)

    def __auth_authorization_code(self):
        code = input("Enter authorization code (https://www.epicgames.com/id/api/redirect?clientId=3446cd72694c4a4485d81b77adbb2141&responseType=code):") 
        super().__init__(
            auth=fortnitepy.AuthorizationCodeAuth(
                code = code
            )
        )
    
    def __auth_device_auth(self):
        device_auth_details = self.device_auth.get_device_auth_details().get(__fortnite_account_key__, {})
        super().__init__(
            auth=fortnitepy.DeviceAuth(
                **device_auth_details
            )
        )

    async def event_device_auth_generate(self, details, email):
        self.device_auth.store_device_auth_details(email, details)

    # Generate auth details if none were supplied yet
    async def generate_auth_details(self):
        if not self.device_auth.device_auth_file_exists():
            device_auth_data = await self.auth.generate_device_auth()
            details = {
                'device_id': device_auth_data['deviceId'],
                'account_id': device_auth_data['accountId'],
                'secret': device_auth_data['secret'],
            }
            self.auth.__dict__.update(details)
            self.dispatch_event(
                'device_auth_generate',
                details,
                __fortnite_account_key__
            )

    async def event_ready(self):
        print('----------------')
        print('FortniteClient ready as:')
        print(self.user.display_name)
        print(self.user.id)
        print('----------------')

        await self.generate_auth_details()
        
        # Call observers
        await self.clientInit.on_event()

    async def event_friend_request(self, request: typing.Union[fortnitepy.friend.IncomingPendingFriend, fortnitepy.friend.OutgoingPendingFriend]):
        await IncomingFriendRequest.on_event(request)

    async def event_friend_presence(self, before, after: fortnitepy.Presence):
        await FriendPresence.on_event(before, after, self.presenceObserver)