This repository was archived by the owner on Mar 31, 2022. It is now read-only.
forked from gw2scratch/evtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogAnalyzer.cs
More file actions
516 lines (447 loc) · 15.7 KB
/
Copy pathLogAnalyzer.cs
File metadata and controls
516 lines (447 loc) · 15.7 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
using System;
using System.Collections.Generic;
using System.Linq;
using GW2Scratch.EVTCAnalytics.Events;
using GW2Scratch.EVTCAnalytics.GameData;
using GW2Scratch.EVTCAnalytics.GameData.Encounters;
using GW2Scratch.EVTCAnalytics.Model;
using GW2Scratch.EVTCAnalytics.Model.Agents;
using GW2Scratch.EVTCAnalytics.Model.Skills;
using GW2Scratch.EVTCAnalytics.Processing.Encounters.Modes;
using GW2Scratch.EVTCAnalytics.Processing.Encounters.Results;
using GW2Scratch.EVTCAnalytics.Statistics;
using GW2Scratch.EVTCAnalytics.Statistics.Buffs;
using GW2Scratch.EVTCAnalytics.Statistics.PlayerDataParts;
namespace GW2Scratch.EVTCAnalytics
{
/// <summary>
/// The Log Analyzer calculates potentially performance-heavy statistics.
/// All calculations are cached within the object and not repeated.
/// </summary>
public class LogAnalyzer
{
/// <summary>
/// The simulator used to produce time series data of applied buff stacks.
/// If replaced after buff statistics were calculated, the resulting data will not be updated.
/// </summary>
public BuffSimulator BuffSimulator { get; set; } = new BuffSimulator();
/// <summary>
/// Weapon skill data used for determining which skills a player is using.
/// If replaced after used, the resulting statistics will not be updated.
/// </summary>
public WeaponSkillData WeaponSkillData { get; set; } = new WeaponSkillData();
/// <summary>
/// Definitions of skill detections used to determine which skills a player was using.
/// If replaced after skills were detected, they will not be updated.
/// </summary>
public SkillDetections SkillDetections { get; set; } = new SkillDetections();
/// <summary>
/// The rotation calculator that will be used for determining player skill rotations.
/// If replaced after rotations were cached, they will not be updated.
/// </summary>
public IRotationCalculator RotationCalculator { get; set; } = new RotationCalculator();
/// <summary>
/// Data from the GW2 API, may be null, some statistics won't be calculated.
/// </summary>
public GW2ApiData ApiData { get; set; } = null;
private readonly Log log;
private IReadOnlyList<Player> logPlayers = null;
private IReadOnlyList<PlayerData> logPlayerData = null;
private ResultDeterminerResult logResult = null;
private EncounterMode? encounterMode = null;
private long? logEncounterStart = null;
private long? logEncounterEnd = null;
// Since the health fraction result is float?, we cannot rely on the value being null
// meaning that we haven't calculated the health fraction yet
private bool healthFractionCalculated = false;
private float? healthFraction = null;
/// <summary>
/// Creates a new instance of a log analyzer for a provided log.
/// </summary>
/// <param name="log">The processed log that will be analyzed.</param>
/// <param name="apiData">Data from the GW2 API, may be null, some statistics won't be calculated.</param>
public LogAnalyzer(Log log, GW2ApiData apiData = null)
{
this.log = log;
ApiData = apiData;
}
public TimeSpan GetEncounterDuration()
{
return new TimeSpan(0, 0, 0, 0, (int)(GetEncounterEnd() - GetEncounterStart()));
}
public long GetEncounterStart()
{
logEncounterStart ??= log.StartTime?.TimeMilliseconds ?? log.Events[0].Time;
return logEncounterStart.Value;
}
public long GetEncounterEnd()
{
if (logEncounterEnd == null)
{
logResult ??= log.EncounterData.ResultDeterminer.GetResult(log.Events);
logEncounterEnd = logResult.Time ?? log.EndTime?.TimeMilliseconds ?? log.Events[^1].Time;
}
return logEncounterEnd.Value;
}
public IReadOnlyList<Player> GetPlayers()
{
logPlayers ??= log.Agents.OfType<Player>().ToList();
return logPlayers;
}
public EncounterResult GetResult()
{
logResult ??= log.EncounterData.ResultDeterminer.GetResult(log.Events);
return logResult.EncounterResult;
}
public EncounterMode GetMode()
{
encounterMode ??= log.EncounterData.ModeDeterminer.GetMode(log);
return encounterMode.Value;
}
public float? GetMainEnemyHealthFraction()
{
if (healthFractionCalculated)
{
return healthFraction;
}
healthFraction = log.EncounterData.HealthDeterminer.GetMainEnemyHealthFraction(log);
healthFractionCalculated = true;
return healthFraction;
}
public Encounter GetEncounter()
{
return log.EncounterData.Encounter;
}
public IReadOnlyList<PlayerData> GetPlayerData()
{
if (logPlayerData == null)
{
CalculatePlayerData();
}
return logPlayerData;
}
private void CalculatePlayerData()
{
var players = GetPlayers();
var deathCounts = players.ToDictionary(x => x, x => 0);
var downCounts = players.ToDictionary(x => x, x => 0);
var usedSkills = players.ToDictionary(x => x, x => new HashSet<Skill>());
foreach (var deadEvent in log.Events.OfType<AgentDeadEvent>().Where(x => x.Agent is Player))
{
var player = (Player) deadEvent.Agent;
deathCounts[player]++;
}
foreach (var downEvent in log.Events.OfType<AgentDownedEvent>().Where(x => x.Agent is Player))
{
var player = (Player) downEvent.Agent;
downCounts[player]++;
}
// Buff damage events only tell us which conditions/buffs, not what skill actually applied them
foreach (var damageEvent in log.Events.OfType<PhysicalDamageEvent>().Where(x => x.Attacker is Player))
{
var player = (Player) damageEvent.Attacker;
usedSkills[player].Add(damageEvent.Skill);
}
foreach (var activationEvent in log.Events.OfType<SkillCastEvent>().Where(x => x.Agent is Player))
{
var player = (Player) activationEvent.Agent;
usedSkills[player].Add(activationEvent.Skill);
}
var playerData = new List<PlayerData>();
foreach (var player in players)
{
HashSet<SkillData> utilitySkills = null;
HashSet<SkillData> healingSkills = null;
HashSet<SkillData> eliteSkills = null;
if (ApiData != null)
{
utilitySkills = new HashSet<SkillData>();
healingSkills = new HashSet<SkillData>();
eliteSkills = new HashSet<SkillData>();
foreach (var usedSkill in usedSkills[player])
{
var skillData = ApiData.GetSkillData(usedSkill);
// Skills may be also registered as used if they affect other players and do damage through them
if (skillData != null && skillData.Professions.Contains(player.Profession))
{
if (skillData.Slot == SkillSlot.Elite)
{
eliteSkills.Add(skillData);
}
else if (skillData.Slot == SkillSlot.Utility)
{
utilitySkills.Add(skillData);
}
else if (skillData.Slot == SkillSlot.Heal)
{
healingSkills.Add(skillData);
}
}
}
}
WeaponType land1Weapon1 = WeaponType.Other;
WeaponType land1Weapon2 = WeaponType.Other;
WeaponType land2Weapon1 = WeaponType.Other;
WeaponType land2Weapon2 = WeaponType.Other;
IEnumerable<SkillData> land1WeaponSkills = null;
IEnumerable<SkillData> land2WeaponSkills = null;
// TODO: Dual wield skill handling for Thieves
if (ApiData != null)
{
WeaponSet currentWeaponSet = WeaponSet.Unknown;
// We are only interested in land weapons. This may be imperfect if started on an underwater set.
var firstWeaponSwap = log.Events.OfType<AgentWeaponSwapEvent>().FirstOrDefault(x =>
x.NewWeaponSet == WeaponSet.Land1 || x.NewWeaponSet == WeaponSet.Land2);
if (firstWeaponSwap == null)
{
currentWeaponSet = WeaponSet.Land1;
}
else
{
// First weapon set is the other one than the first swap swaps to (unless it was an underwater one)
currentWeaponSet = firstWeaponSwap.NewWeaponSet == WeaponSet.Land1
? WeaponSet.Land2
: WeaponSet.Land1;
}
foreach (var logEvent in log.Events)
{
if (logEvent is AgentWeaponSwapEvent weaponSwapEvent && weaponSwapEvent.Agent == player)
{
currentWeaponSet = weaponSwapEvent.NewWeaponSet;
continue;
}
SkillData skillData = null;
if (logEvent is StartSkillCastEvent castEvent && castEvent.Agent == player)
{
skillData = ApiData.GetSkillData(castEvent.Skill);
}
if (skillData != null)
{
if (skillData.Professions.Contains(player.Profession) && skillData.Type == SkillType.Weapon)
{
if (skillData.WeaponType.IsTwoHanded() || skillData.Slot == SkillSlot.Weapon1 ||
skillData.Slot == SkillSlot.Weapon2 || skillData.Slot == SkillSlot.Weapon3)
{
if (currentWeaponSet == WeaponSet.Land1)
{
land1Weapon1 = skillData.WeaponType;
}
else if (currentWeaponSet == WeaponSet.Land2)
{
land2Weapon1 = skillData.WeaponType;
}
}
if (skillData.WeaponType.IsTwoHanded() || skillData.Slot == SkillSlot.Weapon4 ||
skillData.Slot == SkillSlot.Weapon5)
{
if (currentWeaponSet == WeaponSet.Land1)
{
land1Weapon2 = skillData.WeaponType;
}
else if (currentWeaponSet == WeaponSet.Land2)
{
land2Weapon2 = skillData.WeaponType;
}
}
}
}
}
land1WeaponSkills = WeaponSkillData
.GetWeaponSkillIds(player.Profession, land1Weapon1, land1Weapon2)
.Select(x => x == -1 ? null : ApiData.GetSkillData(x));
land2WeaponSkills = WeaponSkillData
.GetWeaponSkillIds(player.Profession, land2Weapon1, land2Weapon2)
.Select(x => x == -1 ? null : ApiData.GetSkillData(x));
}
if (ApiData != null)
{
var skillDetections = SkillDetections.GetSkillDetections(player.Profession).ToArray();
foreach (var e in log.Events)
{
foreach (var detection in skillDetections)
{
if (detection.Detection.IsDetected(player, e))
{
var skill = ApiData.GetSkillData(detection.SkillId);
if (detection.Slot == SkillSlot.Utility)
{
utilitySkills.Add(skill);
}
else if (detection.Slot == SkillSlot.Heal)
{
healingSkills.Add(skill);
}
else if (detection.Slot == SkillSlot.Elite)
{
eliteSkills.Add(skill);
}
}
}
}
}
var ignoredSkills = SkillDetections.GetIgnoredSkillIds(player.Profession);
healingSkills?.RemoveWhere(x => ignoredSkills.Contains(x.Id));
utilitySkills?.RemoveWhere(x => ignoredSkills.Contains(x.Id));
eliteSkills?.RemoveWhere(x => ignoredSkills.Contains(x.Id));
var rotation = RotationCalculator.GetRotation(log, player);
var data = new PlayerData(player, downCounts[player], deathCounts[player], rotation, usedSkills[player],
healingSkills, utilitySkills, eliteSkills, land1Weapon1, land1Weapon2, land2Weapon1, land2Weapon2,
land1WeaponSkills, land2WeaponSkills);
playerData.Add(data);
}
logPlayerData = playerData;
}
private static Dictionary<Agent, DamageData> GetDamageData(IEnumerable<Player> players,
IEnumerable<DamageEvent> damageEvents, IEnumerable<SkillCastEvent> skillCastEvents,
BuffData buffData, IReadOnlyList<Skill> skills, long phaseDuration)
{
// Multiple enumeration is needed
var damageEventArray = damageEvents as DamageEvent[] ?? damageEvents.ToArray();
// Could not be present at all
var might = skills.FirstOrDefault(x => x.Id == SkillIds.Might);
var vulnerability = skills.FirstOrDefault(x => x.Id == SkillIds.Vulnerability);
var physicalBossDamages = damageEventArray.OfType<PhysicalDamageEvent>();
var conditionBossDamages = damageEventArray.OfType<BuffDamageEvent>();
var skillCastStarts = skillCastEvents.OfType<StartSkillCastEvent>();
var damageDataByAttacker = new Dictionary<Agent, DamageData>();
void EnsureDamageDataExists(Agent agent)
{
if (!damageDataByAttacker.ContainsKey(agent))
{
damageDataByAttacker[agent] = new DamageData(agent, phaseDuration);
if (buffData.AgentBuffData.ContainsKey(agent))
{
var buffDataBySkill = buffData.AgentBuffData[agent].StackCollectionsBySkills;
if (might != null)
{
bool mightDataAvailable = buffDataBySkill.ContainsKey(might);
damageDataByAttacker[agent].MightDataAvailable = mightDataAvailable;
}
else
{
damageDataByAttacker[agent].MightDataAvailable = false;
}
// Vulnerability has to be checked on targets.
bool vulnerabilityDataAvailable = vulnerability != null;
damageDataByAttacker[agent].VulnerabilityDataAvailable = vulnerabilityDataAvailable;
}
}
}
// Ensure all players are always in the damage data, even if they did no damage.
foreach (var player in players)
{
EnsureDamageDataExists(player);
}
foreach (var skillCastEvent in skillCastStarts)
{
if (skillCastEvent.Agent == null) continue;
EnsureDamageDataExists(skillCastEvent.Agent);
damageDataByAttacker[skillCastEvent.Agent]
.AddSkillCast(skillCastEvent.CastType == StartSkillCastEvent.SkillCastType.WithQuickness);
}
foreach (var damageEvent in physicalBossDamages)
{
var attacker = damageEvent.Attacker;
var defender = damageEvent.Defender;
long damage = damageEvent.Damage;
if (attacker == null)
{
continue; // TODO: Save as unknown damage
}
var mainMaster = attacker;
while (mainMaster.Master != null)
{
mainMaster = mainMaster.Master;
}
EnsureDamageDataExists(mainMaster);
var damageData = damageDataByAttacker[mainMaster];
int mightStacks = 0;
if (damageData.MightDataAvailable)
{
mightStacks = buffData.AgentBuffData[mainMaster].StackCollectionsBySkills[might]
.GetStackCount(damageEvent.Time);
}
int vulnerabilityStacks = 0;
if (damageData.VulnerabilityDataAvailable)
{
if (defender != null)
{
if (buffData.AgentBuffData.TryGetValue(defender, out var defenderBuffs))
{
if (defenderBuffs.StackCollectionsBySkills.TryGetValue(vulnerability,
out var vulnerabilityBuffStacks))
{
vulnerabilityStacks = vulnerabilityBuffStacks.GetStackCount(damageEvent.Time);
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
damageData.AddPhysicalDamage(damage, mightStacks, vulnerabilityStacks);
}
foreach (var damageEvent in conditionBossDamages)
{
var attacker = damageEvent.Attacker;
var defender = damageEvent.Defender;
long damage = damageEvent.Damage;
if (attacker == null)
{
continue; // TODO: Save as unknown damage
}
var mainMaster = attacker;
while (mainMaster.Master != null)
{
mainMaster = mainMaster.Master;
}
EnsureDamageDataExists(mainMaster);
var damageData = damageDataByAttacker[mainMaster];
int mightStacks = 0;
if (damageData.MightDataAvailable)
{
mightStacks = buffData.AgentBuffData[mainMaster].StackCollectionsBySkills[might]
.GetStackCount(damageEvent.Time);
}
int vulnerabilityStacks = 0;
if (damageData.VulnerabilityDataAvailable)
{
if (defender != null)
{
if (buffData.AgentBuffData.TryGetValue(defender, out var defenderBuffs))
{
if (defenderBuffs.StackCollectionsBySkills.TryGetValue(vulnerability,
out var vulnerabilityBuffStacks))
{
vulnerabilityStacks = vulnerabilityBuffStacks.GetStackCount(damageEvent.Time);
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
else
{
damageData.VulnerabilityDataAvailable = false;
}
}
damageData.AddConditionDamage(damage, mightStacks, vulnerabilityStacks);
}
return damageDataByAttacker;
}
}
}