forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_state_tracker.ts
607 lines (529 loc) · 21.1 KB
/
player_state_tracker.ts
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import logDefinitions from '../../resources/netlog_defs';
import { UnreachableCode } from '../../resources/not_reached';
import PartyTracker from '../../resources/party';
import { Party } from '../../types/event';
import {
DeathReportData,
OopsyDeathReason,
OopsyMistake,
OopsyMistakeType,
} from '../../types/oopsy';
import {
MissableAbility,
MissableEffect,
missedAbilityBuffMap,
missedEffectBuffMap,
} from './buff_map';
import { ProcessedOopsyTriggerSet } from './damage_tracker';
import { DeathReport } from './death_report';
import {
CollectedBuff,
MissedBuffCollector,
RequestTimestampCallback,
} from './missed_buff_collector';
import { MistakeCollector } from './mistake_collector';
import {
GetShareMistakeText,
GetSoloMistakeText,
IsPlayerId,
ShortNamify,
Translate,
} from './oopsy_common';
import { OopsyOptions } from './oopsy_options';
const emptyId = 'E0000000';
const timestampFieldIdx = 1;
// TODO: add this to effect_id.ts?
const raiseEffectId = '94';
const getTimestamp = (splitLine: string[]): number => {
const timestampField = splitLine[timestampFieldIdx];
if (timestampField === undefined)
throw new UnreachableCode();
return new Date(timestampField).getTime();
};
export type TrackedLineEventType =
| 'Ability'
| 'GainsEffect'
| 'LosesEffect'
| 'HoTDoT'
| 'MissedAbility'
| 'MissedEffect';
export type TrackedLineEvent = {
timestamp: number;
type: TrackedLineEventType;
targetId: string;
// Annotate this line with a mistake icon.
mistake?: OopsyMistakeType;
// Override the text from the splitLine with explicit text (e.g. solo/share mistake).
mistakeText?: string;
splitLine: string[];
};
export type TrackedDeathReasonEvent = {
timestamp: number;
type: 'DeathReason';
targetId: string;
text: string;
};
export type TrackedMistakeEvent = {
timestamp: number;
type: 'Mistake';
targetId: string;
mistakeEvent: OopsyMistake;
};
export type TrackedEvent = TrackedLineEvent | TrackedDeathReasonEvent | TrackedMistakeEvent;
export type TrackedEventType = TrackedEvent['type'];
// * Tracks various state about the party (party, pets, buffs, deaths).
// * Generates some internal mistakes that need extra tracking (missed buffs, deaths)
// * Tracks events in `trackedEvents` that can be handed to DeathReports for processing.
export class PlayerStateTracker {
public partyTracker: PartyTracker;
private missedBuffCollector;
private triggerSets: ProcessedOopsyTriggerSet[] = [];
private partyIds: Set<string> = new Set();
private deadIds: Set<string> = new Set();
private idToPartyInfo: { [combatantId: string]: Party } = {};
private petIdToOwnerId: { [petId: string]: string } = {};
private abilityIdToBuff: { [abilityId: string]: MissableAbility } = {};
private effectIdToBuff: { [effectId: string]: MissableEffect } = {};
private trackedEvents: TrackedEvent[] = [];
private trackedEffectMap: { [targetId: string]: { [effectId: string]: TrackedEvent } } = {};
// The minimum amount of time to keep events for.
private readonly eventWindowMs = 20 * 1000;
// The time delta in the future to request cleaning up events from the past, after a cleanup.
// The larger this is, the more it exchanges memory for cpu, to keep more events rather than
// constantly cycling `trackedEvents`. 0 = clean up immediately.
private readonly cleanupWindowMs = this.eventWindowMs * 2;
private nextPruneTimestamp?: number;
private baseTime?: number;
private myPlayerId?: string;
// Cached ability -> mistake icon types for "simple" mistakes.
private mistakeDamageMap: { [id: string]: OopsyMistakeType } = {};
private mistakeShareMap: { [id: string]: OopsyMistakeType } = {};
private mistakeSoloMap: { [id: string]: OopsyMistakeType } = {};
constructor(
private options: OopsyOptions,
private collector: MistakeCollector,
requestTimestampCallback: RequestTimestampCallback,
) {
this.partyTracker = new PartyTracker();
this.missedBuffCollector = new MissedBuffCollector(
requestTimestampCallback,
(timestamp, buff) => this.OnBuffCollected(timestamp, buff),
);
// Build maps of ids to buffs for ease of use.
for (const buff of missedAbilityBuffMap) {
if (typeof buff.abilityId === 'string') {
this.abilityIdToBuff[buff.abilityId] = buff;
} else {
for (const id of buff.abilityId)
this.abilityIdToBuff[id] = buff;
}
}
for (const buff of missedEffectBuffMap) {
if (typeof buff.effectId === 'string') {
this.effectIdToBuff[buff.effectId] = buff;
} else {
for (const id of buff.effectId)
this.effectIdToBuff[id] = buff;
}
}
this.OnPartyChanged();
}
OnStartEncounter(timestamp: number): void {
this.baseTime = timestamp;
this.collector.StartEncounter(timestamp);
}
OnStopEncounter(_timestamp: number): void {
// TODO: forward this along to MistakeObserver
}
PushTriggerSet(set: ProcessedOopsyTriggerSet): void {
this.triggerSets.push(set);
for (const set of this.triggerSets) {
for (const value of Object.values(set.damageWarn ?? {}))
this.mistakeDamageMap[value] = 'warn';
for (const value of Object.values(set.damageFail ?? {}))
this.mistakeDamageMap[value] = 'fail';
for (const value of Object.values(set.shareWarn ?? {}))
this.mistakeShareMap[value] = 'warn';
for (const value of Object.values(set.shareFail ?? {}))
this.mistakeShareMap[value] = 'fail';
for (const value of Object.values(set.soloWarn ?? {}))
this.mistakeSoloMap[value] = 'warn';
for (const value of Object.values(set.soloFail ?? {}))
this.mistakeSoloMap[value] = 'fail';
}
}
ClearTriggerSets(): void {
this.triggerSets = [];
this.mistakeDamageMap = {};
this.mistakeShareMap = {};
this.mistakeSoloMap = {};
}
// Called to update the list of player ids we care about.
OnPartyChanged(): void {
// TODO: do we need to clean anything else up here if this changes?
// Or, do we just assume party doesn't change unless at zone change, so ignore edge cases?
const arr = [...this.partyTracker.partyIds];
// Include the player in the party for mistakes even if there is no party.
if (this.myPlayerId && !arr.includes(this.myPlayerId))
arr.push(this.myPlayerId);
this.partyIds = new Set(arr);
}
private Reset(): void {
// Deliberately do not clear idToPartyInfo here.
this.petIdToOwnerId = {};
this.deadIds.clear();
this.trackedEvents = [];
this.baseTime = undefined;
}
OnChangeZone(timestamp: number, zoneName: string, zoneId: number): void {
this.Reset();
// combatants and party info are re-sent on zone change, so clear here
// to periodically trim this.
this.idToPartyInfo = {};
this.collector.OnChangeZone(timestamp, zoneName, zoneId);
}
OnAddedCombatant(_line: string, splitLine: string[]): void {
const id = splitLine[logDefinitions.AddedCombatant.fields.id];
const name = splitLine[logDefinitions.AddedCombatant.fields.name];
const worldIdStr = splitLine[logDefinitions.AddedCombatant.fields.worldId];
const jobStr = splitLine[logDefinitions.AddedCombatant.fields.job];
if (
id !== undefined && name !== undefined &&
worldIdStr !== undefined && jobStr !== undefined
) {
// Generate the party info we would get from OverlayPlugin via logs.
const worldId = parseInt(worldIdStr);
const job = parseInt(jobStr);
// Consider everybody in the party for now and we'll figure it out later.
const inParty = true;
this.idToPartyInfo[id] = { id, name, worldId, job, inParty };
}
// Track pet owners as well.
const petId = splitLine[logDefinitions.AddedCombatant.fields.id];
const ownerId = splitLine[logDefinitions.AddedCombatant.fields.ownerId];
if (petId === undefined || ownerId === undefined)
return;
if (ownerId === '0' || ownerId === '0000')
return;
// Fix any lowercase ids.
this.petIdToOwnerId[petId.toUpperCase()] = ownerId.toUpperCase();
}
OnPartyList(_line: string, splitLine: string[]): void {
// So that party lists can be used from logs, we will fake `onPartyChanged` events
// using log information. AddedCombatant seems to come before PartyList lines,
// so we accumulate those and then generate the party info from here.
// Start from id0 and drop the hash at the end.
const count = parseInt(splitLine[logDefinitions.PartyList.fields.partyCount] ?? '');
if (isNaN(count))
return;
const ids = splitLine.slice(logDefinitions.PartyList.fields.id0, -1);
const party: Party[] = [];
ids.forEach((id, idx) => {
const p = this.idToPartyInfo[id];
if (!p)
return;
// count is 1-indexed and idx is 0-indexed.
p.inParty = idx < count;
party.push(p);
});
this.partyTracker.onPartyChanged({ party });
this.OnPartyChanged();
}
OnChangedPlayer(_line: string, splitLine: string[]): void {
const id = splitLine[logDefinitions.ChangedPlayer.fields.id];
if (id)
this.SetPlayerId(id);
}
SetPlayerId(id: string): void {
if (this.myPlayerId === id)
return;
this.myPlayerId = id;
this.OnPartyChanged();
}
IsInParty(id?: string): boolean {
if (id === undefined)
return false;
return this.partyIds.has(this.petIdToOwnerId[id] ?? id);
}
IsPlayerInParty(id?: string): boolean {
if (id === undefined)
return false;
return this.partyIds.has(id);
}
OnAbility(_line: string, splitLine: string[]): void {
// Abilities can not miss everybody (e.g. Battle Voice never hitting the source)
// so check both target and source.
const targetId = splitLine[logDefinitions.Ability.fields.targetId];
const sourceId = splitLine[logDefinitions.Ability.fields.sourceId];
const targetInParty = this.IsInParty(targetId);
const sourceInParty = this.IsInParty(sourceId);
if (sourceId === undefined || targetId === undefined)
return;
// Just in case, if a target is performing actions, then they are alive.
if (sourceInParty)
this.deadIds.delete(sourceId);
const abilityId = splitLine[logDefinitions.Ability.fields.id];
if (abilityId === undefined)
return;
// Only track events on players. Ideally, it'd be nice to only include
// party members in tracked events, but this is used for death reports
// on dead non-party members.
// TODO: maybe oopsy should only report party failures?
if (IsPlayerId(targetId)) {
this.trackedEvents.push({
timestamp: getTimestamp(splitLine),
type: 'Ability',
targetId: targetId,
splitLine: splitLine,
});
}
// Report missed buffs on the party.
if (!targetInParty && !sourceInParty)
return;
const buff = this.abilityIdToBuff[abilityId];
if (buff)
this.missedBuffCollector.OnAbilityBuff(splitLine, buff);
}
OnGainsEffect(_line: string, splitLine: string[]): void {
const targetId = splitLine[logDefinitions.GainsEffect.fields.targetId];
// Do not consider pets gaining effects here.
// Summoner pets (e.g. Demi-Phoenix) gain party buffs (e.g. Embolden), with no sourceId/source.
if (!targetId || !this.IsPlayerInParty(targetId))
return;
const effectId = splitLine[logDefinitions.GainsEffect.fields.effectId];
if (effectId === undefined)
return;
const timestamp = getTimestamp(splitLine);
// We need to request a cleanup somewhere. Assume that somebody will gain an effect
// at some point. These happen less often than abilities, so we do it here just
// to reduce per-log overhead.
if (this.nextPruneTimestamp === undefined) {
this.nextPruneTimestamp = timestamp + this.cleanupWindowMs;
} else if (timestamp > this.nextPruneTimestamp) {
this.PruneTrackedEvents(timestamp - this.eventWindowMs);
this.nextPruneTimestamp = timestamp + this.cleanupWindowMs;
}
// Upon coming back to life, players get Transcendent / Weakness / Brink of Death.
// However, they also get a Raise effect prior to coming back to life.
if (effectId !== raiseEffectId)
this.deadIds.delete(targetId);
// Keep track of active buffs in case they have a very long duration and fall outside the
// window of this.trackedEffects.
const event: TrackedEvent = {
timestamp: timestamp,
type: 'GainsEffect',
targetId: targetId,
splitLine: splitLine,
};
(this.trackedEffectMap[targetId] ??= {})[effectId] = event;
this.trackedEvents.push(event);
const buff = this.effectIdToBuff[effectId.toUpperCase()];
if (buff)
this.missedBuffCollector.OnEffectBuff(splitLine, buff);
}
OnLosesEffect(_line: string, splitLine: string[]): void {
const targetId = splitLine[logDefinitions.GainsEffect.fields.targetId];
if (!targetId || !this.IsPlayerInParty(targetId))
return;
const effectId = splitLine[logDefinitions.GainsEffect.fields.effectId];
if (effectId === undefined)
return;
this.trackedEvents.push({
timestamp: getTimestamp(splitLine),
type: 'LosesEffect',
targetId: targetId,
splitLine: splitLine,
});
delete this.trackedEffectMap[targetId]?.[effectId];
}
OnDeathReason(timestamp: number, reason: OopsyDeathReason): void {
const targetId = reason.id;
if (!targetId || !IsPlayerId(targetId))
return;
const text = Translate(this.options.DisplayLanguage, reason.text);
if (!text)
return;
this.trackedEvents.push({
timestamp: timestamp,
type: 'DeathReason',
targetId: targetId,
text: text,
});
}
OnMistakeObj(timestamp: number, mistake: OopsyMistake): void {
this.collector.OnMistakeObj(timestamp, mistake);
const targetId = mistake.reportId;
if (!targetId || !IsPlayerId(targetId))
return;
this.trackedEvents.push({
timestamp: timestamp,
type: 'Mistake',
targetId: targetId,
mistakeEvent: mistake,
});
}
// Returns an event for why this person died.
OnDefeated(_line: string, splitLine: string[]): void {
const targetId = splitLine[logDefinitions.WasDefeated.fields.targetId];
if (!targetId || !IsPlayerId(targetId))
return;
const targetInParty = this.IsInParty(targetId);
if (targetInParty)
this.deadIds.add(targetId);
const timestamp = getTimestamp(splitLine);
const firstTimestamp = timestamp - this.eventWindowMs;
const events = this.trackedEvents.filter((event) => {
return event.timestamp >= firstTimestamp && event.targetId === targetId;
});
// Mark simple mistakes that can be attached to single ability ids.
for (const event of events) {
if (event.type !== 'Ability')
continue;
const id = event.splitLine[logDefinitions.Ability.fields.id];
if (!id)
continue;
const type = event.splitLine[logDefinitions.None.fields.type];
const targetCountStr = event.splitLine[logDefinitions.Ability.fields.targetCount];
const targetCount = parseInt(targetCountStr ?? '1');
// Some abilities (e.g. Kampeos Harma 6826) are AOE Ability types but only hit one person.
// The reverse (Ability.type but targetCount > 1) is not possible.
const isSharedDamage = type === logDefinitions.NetworkAOEAbility.type && targetCount !== 1;
// Combining share/solo mistake lines with ability damage lines is a bit of
// duplication, but unless PlayerStateTracker generated share/solo/damage mistakes
// itself, there's no way to undo the mistake + ability. So, we'll add the
// mistake text into the TrackedEventLine for the ability and hide the mistake.
if (id in this.mistakeDamageMap) {
event.mistake = this.mistakeDamageMap[id];
} else if (isSharedDamage && id in this.mistakeShareMap) {
event.mistake = this.mistakeShareMap[id];
const ability = event.splitLine[logDefinitions.Ability.fields.ability] ?? '???';
event.mistakeText = Translate(
this.options.DisplayLanguage,
GetShareMistakeText(ability, targetCount),
);
} else if (!isSharedDamage && id in this.mistakeSoloMap) {
event.mistake = this.mistakeSoloMap[id];
const ability = event.splitLine[logDefinitions.Ability.fields.ability] ?? '???';
event.mistakeText = Translate(this.options.DisplayLanguage, GetSoloMistakeText(ability));
}
}
const targetName = splitLine[logDefinitions.WasDefeated.fields.target] ?? '???';
const reportData: DeathReportData = {
lang: this.options.DisplayLanguage,
baseTimestamp: this.baseTime,
deathTimestamp: timestamp,
targetId: targetId,
targetName: targetName,
events: events,
};
const mistake = DeathReport.generateMistake(reportData);
this.collector.OnMistakeObj(timestamp, mistake);
}
OnHoTDoT(_line: string, splitLine: string[]): void {
const targetId = splitLine[logDefinitions.NetworkDoT.fields.id];
if (!targetId || !this.IsInParty(targetId))
return;
this.trackedEvents.push({
timestamp: getTimestamp(splitLine),
type: 'HoTDoT',
targetId: targetId,
splitLine: splitLine,
});
}
OnWipe(_line: string, _splitLine: string[]): void {
this.Reset();
}
private OnBuffCollected(timestamp: number, collected: CollectedBuff): void {
// TODO: maybe 'mitigation' should become a separate mistake type?
const type: OopsyMistakeType = collected.buff.type === 'mitigation'
? 'heal'
: collected.buff.type;
const ownerId = this.petIdToOwnerId[collected.sourceId];
const blameId = ownerId ?? collected.sourceId;
const sourceName = this.partyTracker.nameFromId(blameId);
if (sourceName === undefined) {
const line = JSON.stringify(collected.splitLine);
console.error(`Couldn't find name for ${blameId} (owner: ${ownerId ?? 'none'}), ${line}`);
return;
}
const gotBuffMap: { [id: string]: boolean } = {};
if (collected.buff.ignoreSelf)
gotBuffMap[blameId] = true;
for (const id of collected.targetIds)
gotBuffMap[id] = true;
const missedIds = this.partyTracker.partyIds.filter((id) => {
// Filter out any empty ids here.
if (id === emptyId)
return false;
// A player is missed if they didn't get the buff and aren't dead.
return !gotBuffMap[id] && !this.deadIds.has(id);
});
if (missedIds.length === 0)
return;
// Append events for each missed player for death reports.
// Whereas the `OnMistakeObj` call blames the source for missing a number of targets,
// `this.trackedEvents` informs a target in a death report that they were missed by a source.
if (collected.buff.type === 'heal' || collected.buff.type === 'mitigation') {
for (const targetId of missedIds) {
this.trackedEvents.push({
timestamp: getTimestamp(collected.splitLine),
type: 'abilityId' in collected.buff ? 'MissedAbility' : 'MissedEffect',
targetId: targetId,
splitLine: collected.splitLine,
});
}
}
const missedNames = missedIds.map((id) => {
const name = this.partyTracker.nameFromId(id);
if (!name) {
const line = JSON.stringify(collected.splitLine);
console.error(`Couldn't find name for ${id}, ${line}`);
}
return name ?? '???';
});
// TODO: oopsy could really use mouseover popups for details.
if (missedNames.length < 4) {
const nameList = missedNames.map((name) => {
return ShortNamify(name, this.options.PlayerNicks);
}).join(', ');
// As a TrackedLineEvent has been pushed for each person missed already,
// explicitly don't add a `reportId` field on these mistakes.
this.OnMistakeObj(timestamp, {
type: type,
blame: sourceName,
triggerType: 'Buff',
text: {
en: `${collected.buffName} missed ${nameList}`,
de: `${collected.buffName} verfehlt ${nameList}`,
fr: `${collected.buffName} manqué(e) sur ${nameList}`,
ja: `(${nameList}) が${collected.buffName}を受けなかった`,
cn: `${nameList} 没受到 ${collected.buffName}`,
ko: `${collected.buffName} ${nameList}에게 적용안됨`,
},
});
return;
}
// If there's too many people, just list the number of people missed.
// TODO: we could also list everybody on separate lines?
this.OnMistakeObj(timestamp, {
type: type,
blame: sourceName,
triggerType: 'Buff',
text: {
en: `${collected.buffName} missed ${missedNames.length} people`,
de: `${collected.buffName} verfehlte ${missedNames.length} Personen`,
fr: `${collected.buffName} manqué(e) sur ${missedNames.length} personnes`,
ja: `${missedNames.length}人が${collected.buffName}を受けなかった`,
cn: `有${missedNames.length}人没受到 ${collected.buffName}`,
ko: `${collected.buffName} ${missedNames.length}명에게 적용안됨`,
},
});
}
private PruneTrackedEvents(timestamp: number) {
// Remove any tracked events that occurred prior to `timestamp`.
const idx = this.trackedEvents.findIndex((event) => event.timestamp >= timestamp);
if (idx === -1)
return;
this.trackedEvents = this.trackedEvents.slice(idx);
}
}