forked from OverlayPlugin/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dps_phase_tracker.js
253 lines (225 loc) · 6.86 KB
/
dps_phase_tracker.js
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
import NetRegexes from '../../../resources/netregexes';
import { LocaleNetRegex } from '../../../resources/translations';
const kTestPhaseStart = NetRegexes.gameLog({ line: '.*?cactbot phase start.*?', capture: false });
const kTestPhaseEnd = NetRegexes.gameLog({ line: '.*?cactbot phase end.*?', capture: false });
export default class DpsPhaseTracker {
constructor(options) {
// Ordered list of phases. Each phase is:
// {
// name: display name string
// start: {
// Encounter: {...}, // initial encounter info
// Combatant: {...}, // initial combatant info
// },
// diff : {
// Encounter: {...}, // diff with latest encounter info
// Combatant: {...}, // diff with latest combatant info
// },
// complete: bool
// element: optional externally set html element associated with this phase
// }
//
this.phases = [];
this.title = null;
this.lastData = null;
this.zone = null;
this.inCombat = false;
this.defaultPhase = null;
this.defaultPhaseIdx = 0;
this.lang = options.ParserLanguage;
this.areaSealRegex = LocaleNetRegex.areaSeal[this.lang] || LocaleNetRegex.areaSeal['en'];
this.areaUnsealRegex = LocaleNetRegex.areaUnseal[this.lang] || LocaleNetRegex.areaUnseal['en'];
this.countdownStartRegex = LocaleNetRegex.countdownStart[this.lang] ||
LocaleNetRegex.countdownStart['en'];
}
onNetLog(e) {
const log = e.rawLine;
if (!this.defaultPhase) {
if (this.areaSealRegex.test(log) || kTestPhaseStart.test(log)) {
this.defaultPhaseIdx++;
this.defaultPhase = `B${this.defaultPhaseIdx}`;
this.onFightPhaseStart(this.defaultPhase, this.lastData);
return;
}
} else {
if (this.areaUnsealRegex.test(log) || kTestPhaseEnd.test(log)) {
this.onFightPhaseEnd(this.defaultPhase, this.lastData);
this.defaultPhase = 0;
return;
}
}
}
onChangeZone(zone) {
this.clearPhases();
this.zone = zone;
this.title = null;
}
onOverlayDataUpdate(dps) {
this.lastData = dps;
// Update each open phase with new diffs.
for (let i = 0; i < this.phases.length; ++i) {
const phase = this.phases[i];
if (phase.complete)
continue;
const diff = this.diffUpdateInfo(phase.start, dps);
if (!diff)
continue;
phase.diff = diff;
}
}
onFightStart(boss) {
this.clearPhases();
this.title = boss.id;
}
onFightEnd() {
}
inCombatChanged(inCombat) {
if (this.inCombat === inCombat)
return;
this.inCombat = inCombat;
if (inCombat)
this.clearPhases();
else if (!inCombat)
this.onFightEnd();
}
onFightPhaseStart(name, dps) {
this.onOverlayDataUpdate(dps);
// Make sure there's no phase name collision.
for (let i = 0; i < this.phases.length; ++i) {
if (this.phases[i].name === name && !this.phases[i].complete) {
console.error(`Duplicate phase: ${name}`);
return;
}
}
this.phases.push({
'name': name,
'start': dps,
'diff': null,
'element': null,
'complete': false,
});
}
onFightPhaseEnd(name, dps) {
this.onOverlayDataUpdate(dps);
for (let i = 0; i < this.phases.length; ++i) {
if (this.phases[i].name === name && !this.phases[i].complete) {
this.phases[i].complete = true;
return;
}
}
console.error(`Can't find phase: ${name}`);
}
clearPhases() {
this.defaultPhase = null;
this.defaultPhaseIdx = 0;
for (let i = 0; i < this.phases.length; ++i) {
const element = this.phases[i].element;
if (element)
element.remove();
}
this.phases = [];
}
// Takes two {Encounter: {}, Combatant: {}} objects and returns the
// difference between the two as an {Encounter: {}, Combatant: {}} object.
// It drops some of the fields to make the diff.
//
// phaseStart is optional, if it doesn't exist, it assumes default values
// from the start of the fight.
//
// This may return null (used as a hint not to bother displaying the diff).
diffUpdateInfo(phaseStart, phaseEnd) {
if (!phaseEnd) {
console.error(['diffError: no phase end', phase]);
return;
}
// Sometimes a phase will get entered during a slow wipe when nobody
// attacks, causing a total duration of zero. Just ignore these.
// This happens where ACT stops providing new updates but log entries
// or other triggers indicate that phases have started.
if (phaseStart) {
if (phaseStart.Encounter.DURATION === phaseEnd.Encounter.DURATION)
return;
}
const diffProps = function(start, end, props, out) {
for (let i = 0; i < props.length; ++i) {
const prop = props[i];
out[prop] = end[prop] - start[prop];
}
};
const copyProps = function(start, props, out) {
for (let i = 0; i < props.length; ++i)
out[props[i]] = start[props[i]];
};
const setDPS = function(duration, encDuration, out) {
out.dps = (out.damage / duration).toFixed(2);
out.encdps = (out.damage / encDuration).toFixed(2);
out.DPS = Math.floor(out.dps);
out.ENCDPS = Math.floor(out.encdps);
};
const encounterDiffProps = [
'DURATION',
'damage',
'hits',
'misses',
'crithits',
'swings',
'healed',
'critheals',
'cures',
'damagetaken',
'healstaken',
'deaths',
];
const combatantCopyProps = [
'name',
'Job',
];
const combatantDiffProps = [
'DURATION',
'damage',
'hits',
'crithits',
'misses',
'swings',
'healed',
'critheals',
'heals',
'cures',
'damagetaken',
'healstaken',
'kills',
'deaths',
];
const encounter = {};
if (phaseStart) {
diffProps(phaseStart.Encounter, phaseEnd.Encounter, encounterDiffProps, encounter);
setDPS(encounter.DURATION, encounter.DURATION, encounter);
} else {
copyProps(phaseEnd.Encounter, encounterDiffProps, encounter);
setDPS(encounter.DURATION, encounter.DURATION, encounter);
}
// Deliberately use end, as combatants aren't initially listed before
// they've done any damage right when the fight starts.
const combatant = {};
for (const name in phaseEnd.Combatant) {
const start = phaseStart ? phaseStart.Combatant[name] : null;
const end = phaseEnd.Combatant[name];
if (!end)
continue;
const c = {};
copyProps(end, combatantCopyProps, c);
if (start) {
diffProps(start, end, combatantDiffProps, c);
} else {
// If combatant doesn't exist, assume they start at zero.
copyProps(end, combatantDiffProps, c);
}
setDPS(c.DURATION, encounter.DURATION, c);
combatant[name] = c;
}
return {
Encounter: encounter,
Combatant: combatant,
};
}
}