-
Notifications
You must be signed in to change notification settings - Fork 4
/
cardcalc_fflogsapi.py
333 lines (278 loc) · 9.32 KB
/
cardcalc_fflogsapi.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
329
330
331
332
333
"""
This contains code for pull requests from v2 of the FFLogs API
as required for damage and card calculations used in cardcalc
and damagecalc
"""
from datetime import timedelta
import os
# Imports related to making API requests
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
from python_graphql_client import GraphqlClient
from urllib.parse import urlparse, parse_qs
# local imports
from cardcalc_data import Player, Pet, FightInfo, CardCalcException, ActorList
FFLOGS_CLIENT_ID = os.environ['FFLOGS_CLIENT_ID']
FFLOGS_CLIENT_SECRET = os.environ['FFLOGS_CLIENT_SECRET']
FFLOGS_OAUTH_URL = 'https://www.fflogs.com/oauth/token'
FFLOGS_URL = 'https://www.fflogs.com/api/v2/client'
client = GraphqlClient(FFLOGS_URL)
# this is used to handle sorting events
def _event_priority(event):
return {
'applybuff': 1,
'applybuffstack': 2,
'applydebuff': 3,
'applydebuffstack': 4,
'refreshbuff': 5,
'refreshdebuff': 6,
'removedebuff': 7,
'removedebuffstack': 8,
'removebuff': 9,
'removebuffstack': 10,
'damage': 11,
'damagesnapshot': 12,
}[event]
# used to obtain a bearer token from the fflogs api
def get_bearer_token():
token_client = BackendApplicationClient(client_id=FFLOGS_CLIENT_ID)
oauth = OAuth2Session(client=token_client)
token = oauth.fetch_token(token_url=FFLOGS_OAUTH_URL,
client_id=FFLOGS_CLIENT_ID, client_secret=FFLOGS_CLIENT_SECRET)
return token
# make a request for the data defined in query given a set of
# variables
def call_fflogs_api(query, variables, token):
headers = {
'Content-TYpe': 'application/json',
'Authorization': 'Bearer {}'.format(token['access_token']),
}
data = client.execute(query=query, variables=variables, headers=headers)
return data
def get_last_fight(report, token):
variables = {
'code': report
}
query = """
query reportData($code: String!) {
reportData {
report(code: $code) {
fights {
id
startTime
endTime
name
kill
}
}
}
}
"""
data = call_fflogs_api(query, variables, token)
return data['data']['reportData']['report']['fights'][-1]['id']
def decompose_url(url, token) -> tuple[str, int]:
parts = urlparse(url)
try:
report_id = [segment for segment in parts.path.split(
'/') if segment][-1]
except IndexError:
raise CardCalcException("Invalid URL: {}".format(url))
try:
fight_id = parse_qs(parts.fragment)['fight'][0]
except KeyError:
raise CardCalcException("Fight ID is required. Select a fight first")
if fight_id == 'last':
fight_id = get_last_fight(report_id, token)
fight_id = int(fight_id)
return report_id, fight_id
def get_fight_info(report, fight, token):
variables = {
'code': report
}
query = """
query reportData($code: String!) {
reportData {
report(code: $code) {
fights {
id
startTime
endTime
name
kill
}
}
}
}
"""
data = call_fflogs_api(query, variables, token)
fights = data['data']['reportData']['report']['fights']
for f in fights:
if f['id'] == fight:
return FightInfo(report_id=report, fight_number=f['id'], start_time=f['startTime'], end_time=f['endTime'], name=f['name'], kill=f['kill'])
raise CardCalcException("Fight ID not found in report")
def get_actor_lists(fight_info: FightInfo, token):
variables = {
'code': fight_info.id,
'startTime': fight_info.start,
'endTime': fight_info.end,
}
query = """
query reportData($code: String!, $startTime: Float!, $endTime: Float) {
reportData {
report(code: $code) {
masterData {
pets: actors(type: "Pet") {
id
name
type
subType
petOwner
}
}
table: table(startTime: $startTime, endTime: $endTime)
}
}
}"""
data = call_fflogs_api(query, variables, token)
master_data = data['data']['reportData']['report']['masterData']
table = data['data']['reportData']['report']['table']
pet_list = master_data['pets']
composition = table['data']['composition']
players = {}
pets = {}
for p in composition:
players[p['id']] = Player(p['id'], p['name'], p['type'])
for p in pet_list:
if p['petOwner'] in players:
pets[p['id']] = Pet(p['id'], p['name'], p['petOwner'])
return ActorList(players, pets)
def get_card_play_events(fight_info: FightInfo, token):
variables = {
'code': fight_info.id,
'startTime': fight_info.start,
'endTime': fight_info.end,
}
query = """
query reportData($code: String!, $startTime: Float!, $endTime: Float!) {
reportData {
report(code: $code) {
cardPlayEvents: events(
startTime: $startTime,
endTime: $endTime
filterExpression: "ability.id in (1001883, 1001886, 1001887, 1001882, 1001884, 1001885, 4401, 4402, 4403, 4404, 4405, 4406)"
) {
data
}
}
}
}
"""
data = call_fflogs_api(query, variables, token)
card_events = data['data']['reportData']['report']['cardPlayEvents']['data']
return card_events
def get_card_draw_events(fight_info: FightInfo, token):
variables = {
'code': fight_info.id,
'startTime': fight_info.start,
'endTime': fight_info.end,
}
query = """
query reportData($code: String!, $startTime: Float!, $endTime: Float!) {
reportData {
report(code: $code) {
draws: events(
startTime: $startTime,
endTime: $endTime,
filterExpression: "ability.id in (3590, 1000915, 1000913, 1000914, 1000917, 1000916, 1000918)"
) {
data
}
}
}
}
"""
data = call_fflogs_api(query, variables, token)
card_events = data['data']['reportData']['report']['draws']['data']
return card_events
"""
Get the collection of damage events from FFLogs for a fight
defined in fight_info
Returns dictionary of damage events with three sections:
- prepDamage: the prepare event snapshots for non-tick damage
- rawDamage: the actual damage events for non-tick damage
- tickDamage: buff/debuff events and damage events for tick damage
"""
def get_damage_events(fight_info: FightInfo, token):
variables = {
'code': fight_info.id,
'startTime': fight_info.start,
'endTime': fight_info.end,
}
query = """
query reportData($code: String!, $startTime: Float!, $endTime: Float!) {
reportData {
report(code: $code) {
damage: events(
startTime: $startTime,
endTime: $endTime,
dataType: DamageDone,
limit: 10000,
filterExpression: "isTick='false' and type!='calculateddamage'"
) {
data
}
damagePrep: events(
startTime: $startTime,
endTime: $endTime,
dataType: DamageDone,
limit: 10000,
filterExpression: "isTick=false and type='calculateddamage' and isUnpairedCalculation=false"
) {
data
}
tickDamage: events(
startTime: $startTime,
endTime: $endTime,
dataType: DamageDone,
limit: 10000,
filterExpression: "isTick='true' and ability.id != 500000"
) {
data
}
tickEvents: events(
startTime: $startTime,
endTime: $endTime,
dataType: Debuffs,
hostilityType: Enemies,
limit: 10000,
filterExpression: "ability.id not in (1000493, 1001203, 1001195, 1001221)"
) {
data
}
groundEvents: events(
startTime: $startTime,
endTime: $endTime,
dataType: Buffs,
limit: 10000,
filterExpression: "ability.id in (1000749, 1000501, 1001205, 1000312, 1001869)"
) {
data
}
}
}
}
"""
data = call_fflogs_api(query, variables, token)
prep_damages = data['data']['reportData']['report']['damagePrep']['data']
base_damages = data['data']['reportData']['report']['damage']['data']
tick_damages = data['data']['reportData']['report']['tickDamage']['data']
tick_events = data['data']['reportData']['report']['tickEvents']['data']
ground_events = data['data']['reportData']['report']['groundEvents']['data']
combined_tick_events = sorted((tick_damages + tick_events + ground_events),
key=lambda tick: (tick['timestamp'], _event_priority(tick['type'])))
damage_events = {
'rawDamage': base_damages,
'prepDamage': prep_damages,
'tickDamage': combined_tick_events,
}
return damage_events