forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullcounter.ts
435 lines (394 loc) · 12.8 KB
/
pullcounter.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
import { Lang } from '../../resources/languages';
import logDefinitions from '../../resources/netlog_defs';
import NetRegexes, { commonNetRegex } from '../../resources/netregexes';
import { UnreachableCode } from '../../resources/not_reached';
import { addOverlayListener, callOverlayHandler } from '../../resources/overlay_plugin_api';
import { LocaleNetRegex } from '../../resources/translations';
import UserConfig from '../../resources/user_config';
import ZoneId from '../../resources/zone_id';
import { BaseOptions } from '../../types/data';
import { EventResponses, Party, SavedConfig } from '../../types/event';
import { CactbotBaseRegExp, TriggerTypes } from '../../types/net_trigger';
import { LocaleObject, ZoneIdType } from '../../types/trigger';
import '../../resources/defaults.css';
import './pullcounter.css';
interface PullCounterOptions extends BaseOptions {
Language: Lang;
}
const defaultOptions: PullCounterOptions = {
...UserConfig.getDefaultBaseOptions(),
Language: 'en',
};
// NOTE: do not add more fights to this data structure.
// These exist for testing pullcounter and for backwards compatibility
// with pullcounter keys. None of these were translated in the past,
// and so it's also not worth going back and adding these as there is
// no backwards compatibility issue for other languages.
type Boss = {
readonly id: string;
readonly zoneId?: ZoneIdType;
readonly startRegex?: LocaleObject<CactbotBaseRegExp<TriggerTypes>>;
readonly countdownStarts?: boolean;
readonly preventAutoStart?: boolean;
};
const bossFightTriggers: readonly Boss[] = [
{
id: 'test',
zoneId: ZoneId.MiddleLaNoscea,
startRegex: {
en: NetRegexes.gameLog({ line: 'You bow courteously to the striking dummy.*?' }),
de: NetRegexes.gameLog(
{ line: 'Du verbeugst dich hochachtungsvoll vor der Trainingspuppe.*?' },
),
fr: NetRegexes.gameLog({ line: 'Vous vous inclinez devant le mannequin d\'entraînement.*?' }),
ja: NetRegexes.gameLog({ line: '.*は木人にお辞儀した.*?' }),
cn: NetRegexes.gameLog({ line: '.*恭敬地对木人行礼.*?' }),
ko: NetRegexes.gameLog({ line: '.*나무인형에게 공손하게 인사합니다.*?' }),
},
countdownStarts: true,
preventAutoStart: true,
},
{
id: 'o1s',
zoneId: ZoneId.DeltascapeV10Savage,
},
{
id: 'o2s',
zoneId: ZoneId.DeltascapeV20Savage,
},
{
id: 'o3s',
zoneId: ZoneId.DeltascapeV30Savage,
},
{
id: 'o4s-exdeath',
zoneId: ZoneId.DeltascapeV40Savage,
// 23F4 Dualcast
startRegex: {
en: NetRegexes.ability({ source: 'Exdeath', id: '23F4' }),
de: NetRegexes.ability({ source: 'Exdeath', id: '23F4' }),
fr: NetRegexes.ability({ source: 'Exdeath', id: '23F4' }),
ja: NetRegexes.ability({ source: 'エクスデス', id: '23F4' }),
cn: NetRegexes.ability({ source: '艾克斯迪司', id: '23F4' }),
ko: NetRegexes.ability({ source: '엑스데스', id: '23F4' }),
},
preventAutoStart: true,
},
{
id: 'o4s-neo',
zoneId: ZoneId.DeltascapeV40Savage,
// 2417 Almagest
startRegex: {
en: NetRegexes.ability({ source: 'Neo Exdeath', id: '2417' }),
de: NetRegexes.ability({ source: 'Neo Exdeath', id: '2417' }),
fr: NetRegexes.ability({ source: 'Neo Exdeath', id: '2417' }),
ja: NetRegexes.ability({ source: 'ネオエクスデス', id: '2417' }),
cn: NetRegexes.ability({ source: '新生艾克斯迪司', id: '2417' }),
ko: NetRegexes.ability({ source: '네오 엑스데스', id: '2417' }),
},
preventAutoStart: true,
},
{
id: 'Unending Coil',
zoneId: ZoneId.TheUnendingCoilOfBahamutUltimate,
},
{
id: 'Shinryu Ex',
zoneId: ZoneId.TheMinstrelsBalladShinryusDomain,
},
{
id: 'o5s',
zoneId: ZoneId.SigmascapeV10Savage,
},
{
id: 'o6s',
zoneId: ZoneId.SigmascapeV20Savage,
},
{
id: 'o7s',
zoneId: ZoneId.SigmascapeV30Savage,
},
{
id: 'o8s-kefka',
zoneId: ZoneId.SigmascapeV40Savage,
startRegex: {
en: NetRegexes.startsUsing({ source: 'Kefka', id: '28C2' }),
de: NetRegexes.startsUsing({ source: 'Kefka', id: '28C2' }),
fr: NetRegexes.startsUsing({ source: 'Kefka', id: '28C2' }),
ja: NetRegexes.startsUsing({ source: 'ケフカ', id: '28C2' }),
cn: NetRegexes.startsUsing({ source: '凯夫卡', id: '28C2' }),
ko: NetRegexes.startsUsing({ source: '케프카', id: '28C2' }),
},
preventAutoStart: true,
},
{
id: 'o8s-god kefka',
zoneId: ZoneId.SigmascapeV40Savage,
startRegex: {
en: NetRegexes.startsUsing({ source: 'Kefka', id: '28EC' }),
de: NetRegexes.startsUsing({ source: 'Kefka', id: '28EC' }),
fr: NetRegexes.startsUsing({ source: 'Kefka', id: '28EC' }),
ja: NetRegexes.startsUsing({ source: 'ケフカ', id: '28EC' }),
cn: NetRegexes.startsUsing({ source: '凯夫卡', id: '28EC' }),
ko: NetRegexes.startsUsing({ source: '케프카', id: '28EC' }),
},
preventAutoStart: true,
},
{
id: 'Byakko Ex',
zoneId: ZoneId.TheJadeStoaExtreme,
},
{
id: 'Tsukuyomi Ex',
zoneId: ZoneId.TheMinstrelsBalladTsukuyomisPain,
},
{
id: 'UwU',
zoneId: ZoneId.TheWeaponsRefrainUltimate,
},
{
id: 'Suzaku Ex',
zoneId: ZoneId.HellsKierExtreme,
},
{
id: 'Seiryu Ex',
zoneId: ZoneId.TheWreathOfSnakesExtreme,
},
{
id: 'o9s',
zoneId: ZoneId.AlphascapeV10Savage,
},
{
id: 'o10s',
zoneId: ZoneId.AlphascapeV20Savage,
},
{
id: 'o11s',
zoneId: ZoneId.AlphascapeV30Savage,
},
{
id: 'o12s-door',
zoneId: ZoneId.AlphascapeV40Savage,
startRegex: {
en: NetRegexes.ability({ source: 'Omega-M', id: '337D' }),
de: NetRegexes.ability({ source: 'Omega-M', id: '337D' }),
fr: NetRegexes.ability({ source: 'Oméga-M', id: '337D' }),
ja: NetRegexes.ability({ source: 'オメガM', id: '337D' }),
cn: NetRegexes.ability({ source: '欧米茄M', id: '337D' }),
ko: NetRegexes.ability({ source: '오메가 M', id: '337D' }),
},
preventAutoStart: true,
},
{
id: 'o12s-final',
zoneId: ZoneId.AlphascapeV40Savage,
startRegex: {
en: NetRegexes.ability({ source: 'Omega', id: '336C' }),
de: NetRegexes.ability({ source: 'Omega', id: '336C' }),
fr: NetRegexes.ability({ source: 'Oméga', id: '336C' }),
ja: NetRegexes.ability({ source: 'オメガ', id: '336C' }),
cn: NetRegexes.ability({ source: '欧米茄', id: '336C' }),
ko: NetRegexes.ability({ source: '오메가', id: '336C' }),
},
preventAutoStart: true,
},
{
id: 'The Southern Bozja Front',
zoneId: ZoneId.TheBozjanSouthernFront,
countdownStarts: false,
preventAutoStart: true,
},
{
id: 'Zadnor',
zoneId: ZoneId.Zadnor,
countdownStarts: false,
preventAutoStart: true,
},
] as const;
const getLocaleObject = <T>(object: LocaleObject<T>, lang: Lang): T => {
return object[lang] ?? object.en;
};
class PullCounter {
private zoneId?: ZoneIdType;
private zoneName = '(unknown)';
private party: Party[] = [];
private bosses: Boss[] = [];
private resetRegex = NetRegexes.echo({ line: '.*pullcounter reset.*?' });
private wipeEndRegex = commonNetRegex.userWipeEcho;
private wipeFadeInRegex = commonNetRegex.wipe;
private countdownEngageRegex: RegExp;
private pullCounts: { [bossId: string]: number } = {};
private bossStarted = false;
private countdownBoss?: Boss;
constructor(private options: PullCounterOptions, private element: HTMLElement) {
this.party = [];
this.countdownEngageRegex = LocaleNetRegex.countdownEngage[this.options.ParserLanguage];
void callOverlayHandler({
call: 'cactbotLoadData',
overlay: 'pullcounter',
}).then((data) => this.SetSaveData(data));
this.ReloadTriggers();
}
OnFightStart(boss: Boss) {
this.pullCounts[boss.id] = (this.pullCounts[boss.id] ?? 0) + 1;
this.bossStarted = true;
this.ShowElementFor(boss.id);
this.SaveData();
}
ShowElementFor(id: string) {
this.element.innerText = (this.pullCounts[id] ?? 0).toString();
this.element.classList.remove('wipe');
}
SaveData() {
void callOverlayHandler({
call: 'cactbotSaveData',
overlay: 'pullcounter',
data: JSON.stringify(this.pullCounts),
});
}
OnNetLog(e: EventResponses['LogLine']): void {
const log = e.rawLine;
if (this.bossStarted)
return;
for (const boss of this.bosses) {
if (
boss.startRegex && getLocaleObject(boss.startRegex, this.options.ParserLanguage).test(log)
) {
this.OnFightStart(boss);
return;
}
}
if (e.line[0] !== logDefinitions.GameLog.type)
return;
if (this.resetRegex.test(log))
this.ResetPullCounter();
if (this.wipeEndRegex.test(log) || this.wipeFadeInRegex.test(log))
this.Wipe();
if (this.countdownEngageRegex.test(log)) {
if (this.countdownBoss)
this.OnFightStart(this.countdownBoss);
else
this.AutoStartBossIfNeeded();
return;
}
}
OnChangeZone(e: EventResponses['ChangeZone']) {
this.element.innerText = '';
this.zoneName = e.zoneName;
this.zoneId = e.zoneID;
// Network log zone names that start with "the" are lowercase.
// Adjust this here to match saved pull counts for zones which
// do not have this property and originally used zone names
// coming from the ffxiv parser plugin.
// TODO: add some backwards compatible way to turn zone names into
// zone ids when we load that zone and a pull count exists?
// Proper-case zone names to match ACT.
this.zoneName = this.zoneName.split(' ').map((word) => {
const firstChar = word[0];
if (firstChar === undefined)
return '';
return firstChar.toUpperCase() + word.substr(1);
}).join(' ');
this.ReloadTriggers();
}
ResetPullCounter() {
if (this.bosses.length > 0) {
for (const boss of this.bosses) {
const id = boss.id;
this.pullCounts[id] = 0;
console.log(`resetting pull count of: ${id}`);
this.ShowElementFor(id);
}
} else {
const id = this.zoneName;
console.log(`resetting pull count of: ${id}`);
this.ShowElementFor(id);
}
this.SaveData();
}
ReloadTriggers() {
this.bosses = [];
this.countdownBoss = undefined;
if (!this.zoneId)
return;
for (const boss of bossFightTriggers) {
if (this.zoneId !== boss.zoneId)
continue;
this.bosses.push(boss);
if (boss.countdownStarts) {
// Only one boss can be started with countdown in a zone.
if (this.countdownBoss)
console.error(`Countdown boss conflict: ${boss.id}, ${this.countdownBoss.id}`);
this.countdownBoss = boss;
}
}
}
OnInCombatChange(e: EventResponses['onInCombatChangedEvent']) {
if (!e.detail.inGameCombat) {
this.bossStarted = false;
return;
}
this.AutoStartBossIfNeeded();
}
AutoStartBossIfNeeded() {
// Start an implicit boss fight for this zone in parties of 8 people
// unless there's a door fight that specifies otherwise.
if (this.bosses.length > 1)
return;
if (this.bossStarted)
return;
if (this.party.length !== 8)
return;
const firstBoss = this.bosses[0];
if (firstBoss) {
if (firstBoss.preventAutoStart)
return;
this.OnFightStart(firstBoss);
return;
}
this.OnFightStart({
id: this.zoneName,
countdownStarts: true,
});
}
Wipe() {
this.element.classList.add('wipe');
}
OnPartyChange(e: EventResponses['PartyChanged']) {
this.party = e.party;
}
SetSaveData(e?: SavedConfig) {
if (e?.data === undefined) {
this.pullCounts = {};
this.ReloadTriggers();
return;
}
try {
if (typeof e.data !== 'string')
throw new Error(e.data.toString());
const parsed: unknown = JSON.parse(e.data);
if (parsed === null || typeof parsed !== 'object')
throw new Error(e.data);
for (const [id, count] of Object.entries(parsed ?? {})) {
if (typeof count !== 'number')
throw new Error(e.data);
this.pullCounts[id] = count;
}
} catch (err) {
console.error(`onSendSaveData parse error`);
console.error(err);
}
this.ReloadTriggers();
}
}
UserConfig.getUserConfigLocation('pullcounter', defaultOptions, () => {
const options = { ...defaultOptions };
const element = document.getElementById('pullcounttext');
if (!element)
throw new UnreachableCode();
const pullcounter = new PullCounter(options, element);
addOverlayListener('LogLine', (e) => pullcounter.OnNetLog(e));
addOverlayListener('ChangeZone', (e) => pullcounter.OnChangeZone(e));
addOverlayListener('onInCombatChangedEvent', (e) => pullcounter.OnInCombatChange(e));
addOverlayListener('PartyChanged', (e) => pullcounter.OnPartyChange(e));
});