forked from OverlayPlugin/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oopsy_live_list.ts
420 lines (362 loc) · 13.4 KB
/
oopsy_live_list.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
import { UnreachableCode } from '../../resources/not_reached';
import { callOverlayHandler } from '../../resources/overlay_plugin_api';
import PartyTracker from '../../resources/party';
import { OopsyMistake } from '../../types/oopsy';
import { DeathReport } from './death_report';
import { MistakeObserver, ViewEvent } from './mistake_observer';
import { GetFormattedTime, Translate } from './oopsy_common';
import { OopsyOptions } from './oopsy_options';
const kCopiedMessage = {
en: 'Copied!',
de: 'Kopiert!',
fr: 'Copié !',
ja: 'コピーした!',
cn: '已复制!',
ko: '복사 완료!',
};
const errorMessageEnableACTWS = {
en: 'Plugins -> OverlayPlugin WSServer -> Stream/Local Overlay -> Start',
de: 'Plugins -> OverlayPlugin WSServer -> Stream/Local Overlay -> Start',
fr: 'Plugins -> OverlayPlugin WSServer -> Stream/Local Overlay -> Start',
ja: 'Plugins -> OverlayPlugin WSServer -> Stream/Local Overlay -> Start',
cn: 'Plugins -> OverlayPlugin WSServer -> 直播/本地悬浮窗 -> 启用',
ko: 'Plugins -> OverlayPlugin WSServer -> Stream/Local 오버레이 -> 시작',
};
export class DeathReportLive {
private reportQueue: DeathReport[] = [];
private queueTimeoutHandle = 0;
constructor(private options: OopsyOptions, private reportElem: HTMLElement) {}
// Briefly shows a death report on screen for a few seconds while in combat.
// If one is already showing, queues it up to display after.
// TODO: add some CSS animation here to fade it in/out?
// TODO: should we show the player's death report with no timer while they are dead?
public queue(report: DeathReport): void {
const timeoutMs = this.options.TimeToShowDeathReportMs;
if (timeoutMs <= 0)
return;
const isFirstReport = this.reportQueue.length === 0;
this.reportQueue.push(report);
if (isFirstReport) {
this.setDeathReport(report);
this.queueTimeoutHandle = window.setTimeout(() => this.handleQueue(), timeoutMs);
}
}
private handleQueue(): void {
const r = this.reportQueue.shift();
if (!r) {
this.cancelQueue();
this.hide();
return;
}
this.setDeathReport(r);
this.queueTimeoutHandle = window.setTimeout(
() => this.handleQueue(),
this.options.TimeToShowDeathReportMs,
);
}
// Cancels the queue of death reports and shows this one immediately.
public show(report: DeathReport): void {
this.cancelQueue();
this.setDeathReport(report);
}
public mouseOver(report: DeathReport, inCombat: boolean): void {
// While in combat, mouseovers interrupt the queue and temporarily show
// TODO: should there be no timer and we just show while mouseovering?
if (inCombat) {
this.cancelQueue();
this.hide();
this.queue(report);
} else {
this.show(report);
}
}
public hide(): void {
while (this.reportElem.lastChild)
this.reportElem.removeChild(this.reportElem.lastChild);
this.cancelQueue();
}
private cancelQueue(): void {
this.reportQueue = [];
window.clearTimeout(this.queueTimeoutHandle);
this.queueTimeoutHandle = 0;
}
private setDeathReport(report: DeathReport) {
this.hide();
const container = document.createElement('div');
container.classList.add('livelist-shadow');
this.reportElem.appendChild(container);
const titleDiv = document.createElement('div');
titleDiv.classList.add('death-title');
container.appendChild(titleDiv);
const titleIcon = document.createElement('div');
titleIcon.classList.add('death-title-icon', 'mistake-icon', 'death');
titleDiv.appendChild(titleIcon);
const titleText = document.createElement('div');
titleText.classList.add('death-title-text');
titleText.innerHTML = report.targetName;
titleDiv.appendChild(titleText);
const closeButton = document.createElement('div');
closeButton.classList.add('death-title-close', 'mistake-icon', 'icon-entry', 'icon-close');
closeButton.addEventListener('click', () => {
// Clicking the close button also cancels the queue. Otherwise, you
// close one and then another appears seconds later, which seems incorrect.
this.cancelQueue();
this.hide();
});
titleDiv.appendChild(closeButton);
const detailsDiv = document.createElement('div');
detailsDiv.classList.add('death-details');
container.appendChild(detailsDiv);
for (const event of report.parseReportLines()) {
this.AppendDetails(
detailsDiv,
event.timestampStr,
event.currentHp?.toString(),
event.amountStr,
event.amountClass,
event.icon,
event.text,
);
}
}
private AppendDetails(
detailsDiv: HTMLElement,
timestampStr: string,
currentHp?: string,
amount?: string,
amountClass?: string,
icon?: string,
text?: string,
): void {
const hpElem = document.createElement('div');
hpElem.classList.add('death-row-hp');
if (currentHp !== undefined)
hpElem.innerText = currentHp;
detailsDiv.appendChild(hpElem);
const damageElem = document.createElement('div');
damageElem.classList.add('death-row-amount');
if (amountClass !== undefined)
damageElem.classList.add(amountClass);
if (amount !== undefined)
damageElem.innerText = amount;
detailsDiv.appendChild(damageElem);
const iconElem = document.createElement('div');
iconElem.classList.add('death-row-icon');
if (icon !== undefined)
iconElem.classList.add('mistake-icon', icon);
detailsDiv.appendChild(iconElem);
const textElem = document.createElement('div');
textElem.classList.add('death-row-text');
if (text !== undefined)
textElem.innerHTML = text;
detailsDiv.appendChild(textElem);
const timeElem = document.createElement('div');
timeElem.classList.add('death-row-time');
timeElem.innerText = timestampStr;
detailsDiv.appendChild(timeElem);
}
}
export class OopsyLiveList implements MistakeObserver {
private container: Element;
private iconContainer: HTMLElement;
private inCombat = false;
private numItems = 0;
private items: HTMLElement[] = [];
private baseTime?: number;
private deathReport?: DeathReportLive;
private itemIdxToListener: { [itemIdx: number]: () => void } = {};
constructor(
private options: OopsyOptions,
private scroller: HTMLElement,
private partyTracker: PartyTracker,
) {
const container = this.scroller.children[0];
if (!container)
throw new UnreachableCode();
this.container = container;
const reportDiv = document.getElementById('death-report');
if (!reportDiv)
throw new UnreachableCode();
if (this.options.DeathReportSide !== 'disabled')
this.deathReport = new DeathReportLive(options, reportDiv);
document.body.classList.add(`report-side-${this.options.DeathReportSide}`);
const iconContainer = document.getElementById('icon-container');
if (!iconContainer)
throw new UnreachableCode();
this.iconContainer = iconContainer;
const closeDiv = document.getElementById('icon-close');
if (!closeDiv)
throw new UnreachableCode();
closeDiv.addEventListener('click', () => {
this.Reset();
});
const summaryDiv = document.getElementById('icon-summary');
if (!summaryDiv)
throw new UnreachableCode();
summaryDiv.addEventListener('click', () => {
const regex = /\w*.html$/;
if (!regex.exec(window.location.href)) {
console.error(`Unable to parse location for summary: ${window.location.href}`);
return;
}
const url = window.location.href.replace(regex, 'oopsy_summary.html');
callOverlayHandler({ call: 'openWebsiteWithWS', url: url }).catch(() => {
console.error(`Failed to open summary`);
this.OnMistakeObj(Date.now(), {
type: 'fail',
text: errorMessageEnableACTWS,
});
});
});
this.Reset();
this.SetInCombat(false);
}
SetInCombat(inCombat: boolean): void {
// For usability sake:
// - to avoid dungeon trash starting stopping combat and resetting the
// list repeatedly, only reset when ACT starts a new encounter.
// - for consistency with DPS meters, fflogs, etc, use ACT's encounter
// time as the start time, not when game combat becomes true.
// - to make it more readable, show/hide old mistakes out of game
// combat, and consider early pulls starting game combat early. This
// allows for one long dungeon ACT encounter to have multiple early
// or late pulls.
if (this.inCombat === inCombat)
return;
this.inCombat = inCombat;
if (inCombat) {
document.body.classList.remove('out-of-combat');
this.HideOldItems();
} else {
// TODO: Add an X button to hide/clear the list.
document.body.classList.add('out-of-combat');
this.ShowAllItems();
}
}
OnMistakeObj(timestamp: number, m: OopsyMistake): void {
const report = m.report ? new DeathReport(m.report) : undefined;
if (report)
this.deathReport?.queue(report);
const iconClass = m.type;
const blame = m.name ?? m.blame;
const blameText = blame !== undefined
? `${this.partyTracker.member(blame).toString()}: `
: '';
const translatedText = Translate(this.options.DisplayLanguage, m.text);
if (translatedText === undefined)
return;
const time = GetFormattedTime(this.baseTime, timestamp);
const text = `${blameText}${translatedText}`;
const maxItems = this.options.NumLiveListItemsInCombat;
// Get an existing row or create a new one.
let rowDiv;
const itemIdx = this.numItems;
if (itemIdx < this.items.length)
rowDiv = this.items[itemIdx];
if (!rowDiv)
rowDiv = this.MakeRow();
// Clean up / add any event listeners.
const listener = this.itemIdxToListener[itemIdx];
if (listener) {
rowDiv.removeEventListener('mousemove', listener);
delete this.itemIdxToListener[itemIdx];
}
if (report) {
const func = () => this.deathReport?.mouseOver(report, this.inCombat);
rowDiv.addEventListener('mousemove', func);
this.itemIdxToListener[itemIdx] = func;
}
this.numItems++;
const iconDiv = document.createElement('div');
iconDiv.classList.add('mistake-icon');
iconDiv.classList.add(iconClass);
rowDiv.appendChild(iconDiv);
const textDiv = document.createElement('div');
textDiv.classList.add('mistake-text');
textDiv.innerHTML = text;
rowDiv.appendChild(textDiv);
const timeDiv = document.createElement('div');
timeDiv.classList.add('mistake-time');
timeDiv.innerHTML = time;
rowDiv.appendChild(timeDiv);
// Hide anything over the limit from the past.
if (this.inCombat) {
if (this.numItems > maxItems)
this.items[this.numItems - maxItems - 1]?.classList.add('hide');
}
// Show and scroll to bottom.
this.container.classList.remove('hide');
this.iconContainer.classList.remove('hide');
this.scroller.scrollTop = this.scroller.scrollHeight;
}
private MakeRow(): HTMLElement {
const div = document.createElement('div');
div.classList.add('mistake-row');
// click-to-copy function
div.addEventListener('click', () => {
const mistakeText = div.childNodes[1]?.textContent ?? '';
const mistakeTime = div.childNodes[2]?.textContent;
const str = typeof mistakeTime === 'string' ? `[${mistakeTime}] ${mistakeText}` : mistakeText;
const el = document.createElement('textarea');
el.value = str;
document.body.appendChild(el);
el.select();
// TODO: fix me
/* eslint-disable-next-line deprecation/deprecation */
document.execCommand('copy');
document.body.removeChild(el);
// copied message
const msg = document.createElement('div');
msg.classList.add('copied-msg');
msg.innerText = kCopiedMessage[this.options.DisplayLanguage] || kCopiedMessage['en'];
msg.style.width = `${div.clientWidth}px`;
msg.style.height = `${div.clientHeight}px`;
div.appendChild(msg);
window.setTimeout(() => {
// oopsy live list may have been hidden/destroyed before the timeout happens.
if (msg.parentNode)
div.removeChild(msg);
}, 1000);
});
this.items.push(div);
this.container.appendChild(div);
return div;
}
private ShowAllItems(): void {
for (const item of this.items)
item.classList.remove('hide');
this.scroller.scrollTop = this.scroller.scrollHeight;
}
private HideOldItems(): void {
const maxItems = this.options.NumLiveListItemsInCombat;
for (let i = 0; i < this.items.length - maxItems; ++i)
this.items[i]?.classList.add('hide');
}
Reset(): void {
this.container.classList.add('hide');
this.iconContainer.classList.add('hide');
this.items = [];
this.numItems = 0;
this.container.innerHTML = '';
this.itemIdxToListener = {};
this.deathReport?.hide();
}
OnEvent(event: ViewEvent): void {
if (event.type === 'Mistake')
this.OnMistakeObj(event.timestamp, event.mistake);
else if (event.type === 'StartEncounter')
this.StartEncounter(event.timestamp);
else if (event.type === 'ChangeZone')
this.OnChangeZone();
}
OnSyncEvents(_events: ViewEvent[]): void {
// don't bother syncing for the live list
}
StartEncounter(timestamp: number): void {
this.Reset();
this.baseTime = timestamp;
}
OnChangeZone(): void {
this.Reset();
}
}