-
Notifications
You must be signed in to change notification settings - Fork 52
/
stats.py
328 lines (249 loc) · 9.88 KB
/
stats.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
MIT License
Copyright (c) 2019-2021 Terbau
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import datetime
from .user import User
from .enums import Platform
replacers = {
'placetop1': 'wins',
}
skips = (
's11_social_bp_level',
's13_social_bp_level',
)
class _StatsBase:
__slots__ = ('raw', '_user', '_stats', '_start_time', '_end_time')
def __init__(self, user: User, data: dict) -> None:
self.raw = data
self._user = user
self._stats = None
self._start_time = datetime.datetime.utcfromtimestamp(data['startTime']) # noqa
if data['endTime'] == 9223372036854775807:
self._end_time = datetime.datetime.utcnow()
else:
self._end_time = datetime.datetime.utcfromtimestamp(data['endTime']) # noqa
@property
def user(self) -> User:
""":class:`User`: The user these stats belongs to."""
return self._user
@property
def start_time(self) -> datetime.datetime:
""":class:`datetime.datetime`: The UTC start time of
the stats retrieved.
"""
return self._start_time
@property
def end_time(self) -> datetime.datetime:
""":class:`datetime.datetime`: The UTC end time of the
stats retrieved.
"""
return self._end_time
def parse(self) -> None:
raise NotImplementedError
def get_stats(self) -> dict:
if self._stats is None:
self.parse()
return self._stats
class StatsV2(_StatsBase):
"""Represents a users Battle Royale stats on Fortnite."""
__slots__ = _StatsBase.__slots__ + ('_combined_stats',
'_platform_specific_combined_stats')
def __init__(self, user: User, data: dict) -> None:
super().__init__(user, data)
self._platform_specific_combined_stats = None
self._combined_stats = None
def __repr__(self) -> str:
return ('<StatsV2 user={0.user!r} start_time={0.start_time!r} '
'end_time={0.end_time!r}>'.format(self))
@staticmethod
def create_stat(stat: str, platform: Platform, playlist: str) -> str:
if stat in replacers.values():
for k, v in replacers.items():
if v == stat:
stat = k
return 'br_{0}_{1}_m0_playlist_{2}'.format(stat,
platform.value,
playlist)
def get_kd(self, data: dict) -> float:
"""Gets the kd of a gamemode
Usage: ::
# gets ninjas kd in solo on input touch
async def get_ninja_touch_solo_kd():
user = await client.fetch_user('Ninja')
stats = await client.fetch_br_stats(user.id)
return stats.get_kd(stats.get_stats()['touch']['defaultsolo'])
Parameters
----------
data: :class:`dict`
A :class:`dict` which atleast includes the keys: ``kills``,
``matchesplayed`` and ``wins``.
Returns
-------
:class:`float`
Returns the kd with a decimal point accuracy of two.
"""
kills = data.get('kills', 0)
matches = data.get('matchesplayed', 0)
wins = data.get('wins', 0)
try:
kd = kills / (matches - wins)
except ZeroDivisionError:
kd = 0
return float(format(kd, '.2f'))
def get_winpercentage(self, data: dict) -> float:
"""Gets the winpercentage of a gamemode
Usage: ::
# gets ninjas winpercentage in solo on input touch
async def get_ninja_touch_solo_winpercentage():
user = await client.fetch_user('Ninja')
stats = await client.fetch_br_stats(user.id)
return stats.get_winpercentage(stats.get_stats()['touch']['defaultsolo'])
Parameters
----------
data: :class:`dict`
A :class:`dict` which atleast includes the keys: matchesplayed`` and ``wins``.
Returns
-------
:class:`float`
Returns the winpercentage with a decimal point accuracy of two.
""" # noqa
matches = data.get('matchesplayed', 0)
wins = data.get('wins', 0)
try:
winper = (wins * 100) / matches
except ZeroDivisionError:
winper = 0
if winper > 100:
winper = 100
return float(format(winper, '.2f'))
def parse(self) -> None:
result = {}
for fullname, stat in self.raw['stats'].items():
if fullname in skips:
continue
parts = fullname.split('_')
name = parts[1]
inp = parts[2]
playlist = '_'.join(parts[5:])
try:
name = replacers[name]
except KeyError:
pass
if name == 'lastmodified':
stat = datetime.datetime.utcfromtimestamp(stat)
if inp not in result:
result[inp] = {}
if playlist not in result[inp]:
result[inp][playlist] = {}
result[inp][playlist][name] = stat
self._stats = result
def _construct_platform_specific_combined_stats(self) -> None:
result = {}
for platform, values in self.get_stats().items():
if platform not in result:
result[platform] = {}
for stats in values.values():
for stat, value in stats.items():
try:
try:
result[platform][stat] += value
except TypeError:
if value > result[platform][stat]:
result[platform][stat] = value
except KeyError:
result[platform][stat] = value
self._platform_specific_combined_stats = result
def _construct_combined_stats(self) -> None:
result = {}
for values in self.get_stats().values():
for stats in values.values():
for stat, value in stats.items():
try:
try:
result[stat] += value
except TypeError:
if value > result[stat]:
result[stat] = value
except KeyError:
result[stat] = value
self._combined_stats = result
def get_stats(self) -> dict:
"""Gets the stats for this user. This function returns the users stats.
Returns
-------
:class:`dict`
Mapping of the users stats. All stats are mapped to their
respective gamemodes.
"""
return super().get_stats()
def get_combined_stats(self, platforms: bool = True) -> dict:
"""Gets combined stats for this user.
Parameters
----------
platforms: :class:`bool`
| ``True`` if the combined stats should be mapped to their
respective region.
| ``False`` to return all stats combined across platforms.
Returns
-------
:class:`dict`
Mapping of the users stats combined. All stats are added together
and no longer sorted into their respective gamemodes.
"""
if platforms:
if self._platform_specific_combined_stats is None:
self._construct_platform_specific_combined_stats()
return self._platform_specific_combined_stats
else:
if self._combined_stats is None:
self._construct_combined_stats()
return self._combined_stats
class StatsCollection(_StatsBase):
"""Represents a users Battle Royale stats collection on Fortnite."""
__slots__ = _StatsBase.__slots__ + ('_name',)
def __init__(self, user: User, data: dict) -> None:
super().__init__(user, data)
self._name = None
def __repr__(self) -> str:
return ('<StatsCollection user={0.user!r} start_time={0.start_time!r} '
'end_time={0.end_time!r}>'.format(self))
def parse(self) -> None:
result = {}
# stat example: br_collection_fish_flopper_orange_length_s14
for stat, value in self.raw['stats'].items():
split = stat.split('_')
name = '_'.join(split[3:-1])
self._name = '_'.join(split[1:3])
result[name] = value
self._stats = result
@property
def name(self) -> str:
""":class:`str`: The collection name."""
if self._stats is None:
self.parse()
return self._name
def get_stats(self) -> dict:
"""Gets the stats collection for this user. This function returns the
users collection.
Returns
-------
:class:`dict`
Mapping of the users collection.
"""
return super().get_stats()