/
fflogs.go
352 lines (295 loc) · 8 KB
/
fflogs.go
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package lunar
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const (
graphQLApi = "https://www.fflogs.com/api/v2/client"
authURL = "https://www.fflogs.com/oauth/authorize"
tokenURL = "https://www.fflogs.com/oauth/token"
//Use this query to obtain the ids, start and end times of all the fights in the report.
//It also grabs the name of the fight, which is usually the name of the boss.
//We need the start and end time to retrieve information about damage downs and deaths.
fightsQuery = `
query($report: String!) {
reportData {
report(code: $report) {
fights {
id
name
startTime
endTime
}
}
}
}
`
query = `
query($report: String!, $start: Float!, $end: Float!, $fid: [Int!]!) {
reportData {
report(code: $report) {
...composition
...rdpsRanking
...dmgDowns
}
}
}
fragment dmgDowns on Report {
events(startTime: $start, endTime: $end, dataType: Debuffs, abilityID: 1002092) {
data
}
}
fragment composition on Report {
composition: table(fightIDs: $fid, startTime: $start, endTime: $end, dataType: Summary)
}
fragment rdpsRanking on Report {
rankings(fightIDs: $fid, playerMetric: rdps)
}
`
)
var (
JobTickers = map[string]string{
"Paladin": "PLD",
"Warrior": "WAR",
"DarkKnight": "DRK",
"Gunbreaker": "GNB",
"Monk": "MNK",
"Dragoon": "DRG",
"Ninja": "NIN",
"Samurai": "SAM",
"WhiteMage": "WHM",
"Scholar": "SCH",
"Astrologian": "AST",
"Bard": "BRD",
"Machinist": "MCH",
"Dancer": "DNC",
"BlackMage": "BLM",
"Summoner": "SMN",
"RedMage": "RDM",
"BlueMage": "BLU",
}
)
type CompositionPlayer struct {
Name string `json:"name"`
Job string `json:"type"`
ID int `json:"id"`
}
type DeathEvent struct {
PlayerName string `json:"name"`
PlayerID int `json:"id"`
}
type DamageDowns struct {
DebuffMode string `json:"type"`
TargetID int `json:"targetID"`
}
type Fight struct {
ID int `json:"id"`
BossName string `json:"name"`
StartTime int `json:"startTime"`
EndTime int `json:"endTime"`
}
type RdpsRankingCharacter struct {
PlayerName string `json:"name"`
Rdps float64 `json:"amount"`
FightRank string `json:"rank"`
BestRank string `json:"best"`
RankPercent int `json:"rankPercent"`
//Class2 is to detect if the player is a combined healer or tank ranking
Class2 *string `json:"class_2"`
}
type SpeedRanking struct {
Rank string `json:"rank"`
TotalParses int `json:"totalParses"`
RankPercent int `json:"rankPercent"`
}
type FightsResponse struct {
QueryData struct {
ReportData struct {
Report struct {
Fights []Fight `json:"fights"`
} `json:"report"`
} `json:"reportData"`
} `json:"data"`
}
type QueryResponse struct {
Data struct {
ReportData struct {
Report struct {
Composition struct {
CompositionData struct {
TotalTime float64 `json:"totalTime"`
Party []CompositionPlayer `json:"composition"`
DeathEvents []DeathEvent `json:"deathEvents"`
} `json:"data"`
} `json:"composition"`
Rankings struct {
RankingData []RankingData `json:"data"`
} `json:"rankings"`
DamageDownEvents struct {
DmgDownInformation []DamageDowns `json:"data"`
} `json:"events"`
} `json:"report"`
} `json:"reportData"`
} `json:"data"`
}
type RankingData struct {
Roles struct {
Tanks struct {
TankCharacters []RdpsRankingCharacter `json:"characters"`
} `json:"tanks"`
Healers struct {
HealerCharacters []RdpsRankingCharacter `json:"characters"`
} `json:"healers"`
DPS struct {
DPSCharacters []RdpsRankingCharacter `json:"characters"`
} `json:"dps"`
} `json:"roles"`
SpeedRank SpeedRanking `json:"speed"`
}
type DeconstructedQueryResponse struct {
TotalTime float64
Deaths map[string]int
Players map[string]RdpsRankingCharacter
//RdpsRankings []RdpsRankingCharacter
SpeedRank SpeedRanking
DamageDowns map[string]int
Jobs map[string]string
}
type FFLogsClient struct {
client *http.Client
AccessToken Token
}
type Token struct {
AccessToken string `json:"access_token"`
}
func NewFFLogsClient() FFLogsClient {
hc := http.Client{Timeout: time.Second * 10}
//Retrieve the access token using Oauth2
clientID := os.Getenv("LUNAR_HYDAELYN_ID")
clientSecret := os.Getenv("LUNAR_HYDAELYN_SECRET")
multiForm := url.Values{}
multiForm.Add("grant_type", `client_credentials`)
req, err := http.NewRequest(http.MethodPost, tokenURL, strings.NewReader(multiForm.Encode()))
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(clientID, clientSecret)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := hc.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var token Token
json.Unmarshal(bodyBytes, &token)
return FFLogsClient{
client: &http.Client{Timeout: time.Second * 10},
AccessToken: token,
}
}
func (c FFLogsClient) runRequest(query string, variables map[string]interface{}) []byte {
requestBody := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}{
Query: query,
Variables: variables,
}
payload, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
body := bytes.NewReader(payload)
req, err := http.NewRequest(http.MethodGet, graphQLApi, body)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprint("Bearer ", c.AccessToken.AccessToken))
hc := http.Client{Timeout: time.Second * 10}
resp, err := hc.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return bodyBytes
}
func (c FFLogsClient) GetLastFightInfo(report string) (*Fight, int) {
requestVariables := map[string]interface{}{
"report": report,
}
var response FightsResponse
json.Unmarshal(c.runRequest(fightsQuery, requestVariables), &response)
fights := response.QueryData.ReportData.Report.Fights
numFights := len(fights)
highestTime := 0
var lastFight *Fight
for i, f := range fights {
if f.EndTime > highestTime {
highestTime = f.EndTime
lastFight = &fights[i]
}
}
if numFights > 0 {
return lastFight, numFights
} else {
return nil, 0
}
}
func (c FFLogsClient) GrabReportInfo(report string, fight Fight) DeconstructedQueryResponse {
requestVariables := map[string]interface{}{
"report": report,
"start": fight.StartTime,
"end": fight.EndTime,
"fid": [1]int{fight.ID},
}
var response QueryResponse
json.Unmarshal(c.runRequest(query, requestVariables), &response)
var dr DeconstructedQueryResponse
dr.TotalTime = response.Data.ReportData.Report.Composition.CompositionData.TotalTime
dr.SpeedRank = response.Data.ReportData.Report.Rankings.RankingData[0].SpeedRank
dr.Players = make(map[string]RdpsRankingCharacter)
for _, player := range response.Data.ReportData.Report.Rankings.RankingData[0].Roles.Tanks.TankCharacters {
if player.Class2 == nil {
dr.Players[player.PlayerName] = player
}
}
for _, player := range response.Data.ReportData.Report.Rankings.RankingData[0].Roles.Healers.HealerCharacters {
if player.Class2 == nil {
dr.Players[player.PlayerName] = player
}
}
for _, player := range response.Data.ReportData.Report.Rankings.RankingData[0].Roles.DPS.DPSCharacters {
dr.Players[player.PlayerName] = player
}
dr.Deaths = make(map[string]int)
dr.DamageDowns = make(map[string]int)
for _, s := range dr.Players {
dr.Deaths[s.PlayerName] = 0
dr.DamageDowns[s.PlayerName] = 0
}
for _, s := range response.Data.ReportData.Report.Composition.CompositionData.DeathEvents {
dr.Deaths[s.PlayerName]++
}
dr.Jobs = make(map[string]string)
for _, player := range response.Data.ReportData.Report.Composition.CompositionData.Party {
dr.Jobs[player.Name] = JobTickers[player.Job]
}
return dr
}