forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oopsy_summary_list.ts
334 lines (281 loc) · 10.4 KB
/
oopsy_summary_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
import { OopsyMistake } from '../../types/oopsy';
import { DeathReport } from './death_report';
import { MistakeObserver, ViewEvent } from './mistake_observer';
import { GetFormattedTime, ShortNamify, Translate } from './oopsy_common';
import { OopsyOptions } from './oopsy_options';
type TableEntry = {
count: number;
elem: HTMLElement;
};
type TableRow = {
nameElem: HTMLElement;
entries: {
[type: string]: TableEntry;
};
};
export class OopsySummaryTable implements MistakeObserver {
private mistakes?: {
[name: string]: TableRow;
};
// TODO: should this come from options?
private types: readonly string[] = ['death', 'fail', 'warn', 'pull'];
private sortCol = 'death';
private sortAsc = false;
constructor(private options: OopsyOptions, private table: HTMLElement) {
// this.table has one column for name, and then one for each of the types.
document.documentElement.style.setProperty('--table-cols', (this.types.length + 1).toString());
}
Reset(): void {
this.mistakes = undefined;
while (this.table?.lastChild)
this.table.removeChild(this.table.lastChild);
}
BuildHeaderRow(parent: HTMLElement): void {
const dummyFirstDiv = document.createElement('div');
dummyFirstDiv.classList.add('header', 'name');
parent.appendChild(dummyFirstDiv);
for (const type of this.types) {
const typeElem = document.createElement('div');
typeElem.classList.add('header', 'mistake-icon', type);
parent.appendChild(typeElem);
typeElem.addEventListener('click', () => {
if (this.sortCol === type) {
this.sortAsc = !this.sortAsc;
this.SortTable();
return;
}
this.sortAsc = false;
this.sortCol = type;
this.SortTable();
});
}
}
BuildPlayerRow(parent: HTMLElement, name: string): TableRow {
const nameElem = document.createElement('div');
nameElem.classList.add('name');
nameElem.innerText = name;
parent.appendChild(nameElem);
const row: TableRow = { nameElem: nameElem, entries: {} };
for (const type of this.types) {
const elem = document.createElement('div');
elem.classList.add('number');
parent.appendChild(elem);
row.entries[type] = {
count: 0,
elem: elem,
};
}
return row;
}
OnMistakeObj(m: OopsyMistake): void {
const longName = m.name ?? m.blame;
if (!longName)
return;
const name = ShortNamify(longName, this.options.PlayerNicks);
// Don't create a player row if the summary doesn't care about this type of mistake.
if (!this.types.includes(m.type))
return;
if (!this.mistakes) {
// Wait until we've seen any mistakes to start the table.
this.BuildHeaderRow(this.table);
this.mistakes = {};
}
const row = this.mistakes[name] ??= this.BuildPlayerRow(this.table, name);
const entry = row.entries[m.type];
if (!entry)
return;
entry.count++;
entry.elem.innerText = entry.count.toString();
if (m.type === this.sortCol)
this.SortTable();
}
SortTable(): void {
if (!this.mistakes)
return;
// Generate counts.
const counts: { [name: string]: number } = {};
for (const [name, row] of Object.entries(this.mistakes)) {
const entry = row.entries[this.sortCol];
counts[name] = entry?.count ?? 0;
}
// Sort names by counts.
const names = Object.keys(counts);
if (this.sortAsc)
names.sort((a, b) => (counts[a] ?? -1) - (counts[b] ?? -1));
else
names.sort((a, b) => (counts[b] ?? -1) - (counts[a] ?? -1));
// Apply style to sort by ordering.
for (const [name, row] of Object.entries(this.mistakes)) {
const idx = names.indexOf(name).toString();
row.nameElem.style.setProperty('order', idx);
for (const entry of Object.values(row.entries))
entry.elem.style.setProperty('order', idx);
}
}
OnEvent(event: ViewEvent): void {
if (event.type === 'Mistake')
this.OnMistakeObj(event.mistake);
}
OnSyncEvents(events: ViewEvent[]): void {
this.Reset();
for (const event of events)
this.OnEvent(event);
}
}
export class OopsySummaryList implements MistakeObserver {
private pullIdx = 0;
private zoneName?: string;
private currentDiv: HTMLElement | null = null;
private baseTime?: number;
constructor(private options: OopsyOptions, private container: HTMLElement) {
this.container.classList.remove('hide');
}
Reset(): void {
this.pullIdx = 0;
this.baseTime = undefined;
this.currentDiv = null;
while (this.container?.lastChild)
this.container.removeChild(this.container.lastChild);
}
private GetTimeStr(d: Date): string {
// ISO-8601 or death.
const month = `0${d.getMonth() + 1}`.slice(-2);
const day = `0${d.getDate()}`.slice(-2);
const hours = `00${d.getHours()}`.slice(-2);
const minutes = `00${d.getMinutes()}`.slice(-2);
return `${d.getFullYear()}-${month}-${day} ${hours}:${minutes}`;
}
private StartNewSectionIfNeeded(timestamp: number): HTMLElement {
if (this.currentDiv)
return this.currentDiv;
const section = document.createElement('div');
section.classList.add('section');
this.container.appendChild(section);
const headerDiv = document.createElement('div');
headerDiv.classList.add('section-header');
section.appendChild(headerDiv);
// TODO: It would kind of be nice to sync this with pullcounter,
// but it's not clear how to connect these two.
this.pullIdx++;
const pullDiv = document.createElement('div');
pullDiv.innerText = `Pull ${this.pullIdx}`;
headerDiv.appendChild(pullDiv);
const zoneDiv = document.createElement('div');
if (this.zoneName)
zoneDiv.innerText = `(${this.zoneName})`;
headerDiv.appendChild(zoneDiv);
const timeDiv = document.createElement('div');
timeDiv.innerText = this.GetTimeStr(new Date(timestamp));
headerDiv.appendChild(timeDiv);
const rowContainer = document.createElement('div');
rowContainer.classList.add('section-rows');
section.appendChild(rowContainer);
this.currentDiv = rowContainer;
return this.currentDiv;
}
private EndSection(): void {
this.currentDiv = null;
}
OnMistakeObj(timestamp: number, m: OopsyMistake): void {
const iconClass = m.type;
const blame = m.name ?? m.blame;
const blameText = blame ? `${ShortNamify(blame, this.options.PlayerNicks)}: ` : '';
const text = Translate(this.options.DisplayLanguage, m.text);
if (!text)
return;
this.AddLine(m, iconClass, `${blameText} ${text}`, GetFormattedTime(this.baseTime, timestamp));
}
AddLine(m: OopsyMistake, iconClass: string, text: string, time: string): void {
const currentSection = this.StartNewSectionIfNeeded(this.baseTime ?? Date.now());
const rowDiv = document.createElement('div');
rowDiv.classList.add('mistake-row');
currentSection.appendChild(rowDiv);
// TODO: maybe combine this with OopsyLiveList.
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);
if (!m.report)
return;
const collapserDiv = document.createElement('div');
collapserDiv.classList.add('mistake-collapser');
rowDiv.appendChild(collapserDiv);
const detailsDiv = document.createElement('div');
detailsDiv.classList.add('death-details');
currentSection.appendChild(detailsDiv);
let expanded = false;
rowDiv.addEventListener('click', () => {
expanded = !expanded;
if (expanded) {
collapserDiv.classList.add('expanded');
detailsDiv.classList.add('expanded');
} else {
collapserDiv.classList.remove('expanded');
detailsDiv.classList.remove('expanded');
}
});
const report = new DeathReport(m.report);
for (const event of report.parseReportLines()) {
const hpElem = document.createElement('div');
hpElem.classList.add('death-row-hp');
if (event.currentHp !== undefined)
hpElem.innerText = event.currentHp.toString();
detailsDiv.appendChild(hpElem);
const damageElem = document.createElement('div');
damageElem.classList.add('death-row-amount');
if (event.amountClass)
damageElem.classList.add(event.amountClass);
if (event.amountStr !== undefined)
damageElem.innerText = event.amountStr;
detailsDiv.appendChild(damageElem);
const iconElem = document.createElement('div');
iconElem.classList.add('death-row-icon');
if (event.icon !== undefined)
iconElem.classList.add('mistake-icon', event.icon);
detailsDiv.appendChild(iconElem);
const textElem = document.createElement('div');
textElem.classList.add('death-row-text');
if (event.text !== undefined)
textElem.innerHTML = event.text;
detailsDiv.appendChild(textElem);
const timeElem = document.createElement('div');
timeElem.classList.add('death-row-time');
timeElem.innerText = event.timestampStr;
detailsDiv.appendChild(timeElem);
}
}
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(event.zoneName);
}
OnSyncEvents(events: ViewEvent[]): void {
this.Reset();
for (const event of events)
this.OnEvent(event);
}
StartEncounter(timestamp: number): void {
// TODO: If you reload the summary while in combat, then the OnInCombatChangedEvent
// for the current combat will send a new StartEncounter (creating a new section)
// even though the current combat is still ongoing. We could try to handle this
// by explicitly having StartEncounter/StopEncounter however this requires a bit
// of wrangling to get right. For now, don't reload the summary while in combat. ;)
this.EndSection();
this.baseTime = timestamp;
this.StartNewSectionIfNeeded(timestamp);
}
OnChangeZone(zoneName: string): void {
this.zoneName = zoneName;
}
}