summaryrefslogtreecommitdiff
path: root/app_types/__init__.py
blob: 63f747e9dc4a22ce56d0d24ce0e477b331d332b2 (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
import fortnitepy
import datetime

__season__ = 31 # 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
    level: int
    matches_played: int
    kills: int
    wins: int
    minutesplayed: int

    def get_kd(self):
        try:
            kd = self.kills / (self.matches_played - self.wins)
        except ZeroDivisionError:
            kd = 0
        return float(format(kd, '.2f'))
    
    def get_winpercentage(self):
        try:
            winper = (self.wins * 100) / self.matches_played
        except ZeroDivisionError:
            winper = 0
        if winper > 100:
            winper = 100
        return float(format(winper, '.2f'))

class User:

    id: str
    display_name: str

    __fortnite_user: fortnitepy.user.UserBase

    def from_fortnite_friend(user: fortnitepy.user.UserBase):

        if user is None:
            return None

        instance = User()

        instance.id = user.id
        instance.display_name = user.display_name
        instance.__fortnite_user = user

        return instance

    async def fetch_stats(self) -> UserStats:
        user_stats = UserStats()
        user_stats.user_id = self.id
        user_stats.user_display_name = self.display_name
        
        try:
            br_stats = await self.__fortnite_user.fetch_br_stats()
            bp_level: float = await self.__fortnite_user.fetch_battlepass_level(season=__season__)
            combined_stats = br_stats.get_combined_stats()
            user_stats.level = int(bp_level//1) if bp_level is not None else 0
            user_stats.matches_played = self.get_stats_value(combined_stats, 'matchesplayed')
            user_stats.kills = self.get_stats_value(combined_stats, 'kills')
            user_stats.wins = self.get_stats_value(combined_stats, 'wins')
            user_stats.minutesplayed = self.get_stats_value(combined_stats, 'minutesplayed')
        except:
            user_stats.level = 0
            user_stats.matches_played = 0
            user_stats.kills = 0
            user_stats.wins = 0
            user_stats.minutesplayed = 0
        return user_stats

    def get_stats_value(self, combined_stats: dict, key: str):
        value = 0
        value += self.get_stats_value_null_safe(combined_stats, 'keyboardmouse', key)
        value += self.get_stats_value_null_safe(combined_stats, 'gamepad', key)
        return value

    def get_stats_value_null_safe(self, combined_stats: dict, device_key: str, key: str):
        if device_key in combined_stats:
            if key in combined_stats[device_key]:
                return combined_stats[device_key][key]
        return 0