forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetregexes.ts
525 lines (468 loc) · 18.3 KB
/
netregexes.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
import { BaseRegExp } from 'types/trigger';
import Regexes, { Params } from './regexes';
interface Fields {
field: string;
value?: string;
}
export type NetRegex<T extends string> = BaseRegExp<Exclude<T, 'capture'>>;
// Differences from Regexes:
// * may have more fields
// * AddedCombatant npc id is broken up into npcNameId and npcBaseId
// * gameLog always splits name into its own field (but previously wouldn't)
const separator = '\\|';
const matchDefault = '[^|]*';
const startsUsingParams = ['timestamp', 'sourceId', 'source', 'id', 'ability', 'targetId', 'target', 'castTime'] as const;
const abilityParams = ['sourceId', 'source', 'id', 'ability', 'targetId', 'target'] as const;
const abilityFullParams = ['sourceId', 'source', 'id', 'ability', 'targetId', 'target', 'flags', 'damage', 'targetCurrentHp', 'targetMaxHp', 'x', 'y', 'z', 'heading'] as const;
const headMarkerParams = ['targetId', 'target', 'id'] as const;
const addedCombatantParams = ['id', 'name'] as const;
const addedCombatantFullParams = ['id', 'name', 'job', 'level', 'ownerId', 'world', 'npcNameId', 'npcBaseId', 'currentHp', 'hp', 'x', 'y', 'z', 'heading'] as const;
const removingCombatantParams = ['id', 'name', 'hp'] as const;
const gainsEffectParams = ['effectId', 'effect', 'duration', 'sourceId', 'source', 'targetId', 'target', 'count'] as const;
const statusEffectExplicitParams = ['targetId', 'target', 'hp', 'maxHp', 'x', 'y', 'z', 'heading', 'data0', 'data1', 'data2', 'data3', 'data4'] as const;
const losesEffectParams = ['effectId', 'effect', 'sourceId', 'source', 'targetId', 'target', 'count'] as const;
const tetherParams = ['sourceId', 'source', 'targetId', 'target', 'id'] as const;
const wasDefeatedParams = ['targetId', 'target', 'sourceId', 'source'] as const;
const echoParams = ['code', 'name', 'line'] as const;
const dialogParams = ['code', 'name', 'line'] as const;
const messageParams = ['code', 'name', 'line'] as const;
const gameLogParams = ['code', 'name', 'line'] as const;
const gameNameLogParams = ['code', 'name', 'line'] as const;
const statChangeParams = ['job', 'strength', 'dexterity', 'vitality', 'intelligence', 'mind', 'piety', 'attackPower', 'directHit', 'criticalHit', 'attackMagicPotency', 'healMagicPotency', 'determination', 'skillSpeed', 'spellSpeed', 'tenacity'] as const;
const changeZoneParams = ['id', 'name'] as const;
const network6dParams = ['instance', 'command', 'data0', 'data1', 'data2', 'data3'] as const;
const nameToggleParams = ['id', 'name', 'toggle'] as const;
export type StartsUsingParams = typeof startsUsingParams[number];
export type AbilityParams = typeof abilityParams[number];
export type AbilityFullParams = typeof abilityFullParams[number];
export type HeadMarkerParams = typeof headMarkerParams[number];
export type AddedCombatantParams = typeof addedCombatantParams[number];
export type AddedCombatantFullParams = typeof addedCombatantFullParams[number];
export type RemovingCombatantParams = typeof removingCombatantParams[number];
export type GainsEffectParams = typeof gainsEffectParams[number];
export type StatusEffectExplicitParams = typeof statusEffectExplicitParams[number];
export type LosesEffectParams = typeof losesEffectParams[number];
export type TetherParams = typeof tetherParams[number];
export type WasDefeatedParams = typeof wasDefeatedParams[number];
export type EchoParams = typeof echoParams[number];
export type DialogParams = typeof dialogParams[number];
export type MessageParams = typeof messageParams[number];
export type GameLogParams = typeof gameLogParams[number];
export type GameNameLogParams = typeof gameNameLogParams[number];
export type StatChangeParams = typeof statChangeParams[number];
export type ChangeZoneParams = typeof changeZoneParams[number];
export type Network6dParams = typeof network6dParams[number];
export type NameToggleParams = typeof nameToggleParams[number];
// If NetRegexes.setFlagTranslationsNeeded is set to true, then any
// regex created that requires a translation will begin with this string
// and match the magicStringRegex. This is maybe a bit goofy, but is
// a pretty straightforward way to mark regexes for translations.
// If issue #1306 is ever resolved, we can remove this.
const magicTranslationString = `^^`;
const magicStringRegex = new RegExp('^\\^\\^');
const keysThatRequireTranslation = [
'ability',
'name',
'source',
'target',
'line',
];
const parseHelper = (
params: { timestamp?: string; capture?: boolean } | undefined,
funcName: string,
fields: { [s: string]: Fields },
): RegExp => {
params = params ?? {};
const validFields: string[] = [];
for (const value of Object.values(fields)) {
if (typeof value !== 'object')
continue;
validFields.push(value.field);
}
Regexes.validateParams(params, funcName, ['capture', ...validFields]);
// Find the last key we care about, so we can shorten the regex if needed.
const capture = Regexes.trueIfUndefined(params.capture);
const fieldKeys = Object.keys(fields);
let maxKey;
if (capture) {
maxKey = fieldKeys[fieldKeys.length - 1];
} else {
maxKey = 0;
for (const key of fieldKeys) {
const value = fields[key] ?? {};
if (typeof value !== 'object')
continue;
const fieldName = fields[key]?.field;
if (fieldName && fieldName in params)
maxKey = key;
}
}
// For testing, it's useful to know if this is a regex that requires
// translation. We test this by seeing if there are any specified
// fields, and if so, inserting a magic string that we can detect.
// This lets us differentiate between "regex that should be translated"
// e.g. a regex with `target` specified, and "regex that shouldn't"
// e.g. a gains effect with just effectId specified.
const transParams = Object.keys(params).filter((k) => keysThatRequireTranslation.includes(k));
const needsTranslations = NetRegexes.flagTranslationsNeeded && transParams.length > 0;
// Build the regex from the fields.
let str = needsTranslations ? magicTranslationString : '^';
let lastKey = -1;
for (const _key in fields) {
const key = parseInt(_key);
// Fill in blanks.
const missingFields = key - lastKey - 1;
if (missingFields === 1)
str += '\\y{NetField}';
else if (missingFields > 1)
str += `\\y{NetField}{${missingFields}}`;
lastKey = key;
const value = fields[key];
if (typeof value !== 'object')
throw new Error(`${funcName}: invalid value: ${JSON.stringify(value)}`);
const fieldName = fields[key]?.field;
const fieldValue = fields[key]?.value?.toString() ?? matchDefault;
if (fieldName) {
str += Regexes.maybeCapture(
// more accurate type instead of `as` cast
// maybe this function needs a refactoring
capture, fieldName, (params as { [s: string]: string })[fieldName], fieldValue) +
separator;
} else {
str += fieldValue + separator;
}
// Stop if we're not capturing and don't care about future fields.
if (key >= (maxKey ?? 0 as number))
break;
}
return Regexes.parse(str);
};
export default class NetRegexes {
static flagTranslationsNeeded = false;
static setFlagTranslationsNeeded(value: boolean): void {
NetRegexes.flagTranslationsNeeded = value;
}
static doesNetRegexNeedTranslation(regex: RegExp | string): boolean {
// Need to `setFlagTranslationsNeeded` before calling this function.
console.assert(NetRegexes.flagTranslationsNeeded);
const str = typeof regex === 'string' ? regex : regex.source;
return !!magicStringRegex.exec(str);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#14-networkstartscasting
*/
static startsUsing(params?: Params<StartsUsingParams>): NetRegex<StartsUsingParams> {
return parseHelper(params, 'startsUsing', {
0: { field: 'type', value: '20' },
1: { field: 'timestamp' },
2: { field: 'sourceId' },
3: { field: 'source' },
4: { field: 'id' },
5: { field: 'ability' },
6: { field: 'targetId' },
7: { field: 'target' },
8: { field: 'castTime' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#15-networkability
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#16-networkaoeability
*/
static ability(params?: Params<AbilityParams>): NetRegex<AbilityParams> {
return parseHelper(params, 'ability', {
0: { field: 'type', value: '2[12]' },
1: { field: 'timestamp' },
2: { field: 'sourceId' },
3: { field: 'source' },
4: { field: 'id' },
5: { field: 'ability' },
6: { field: 'targetId' },
7: { field: 'target' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#15-networkability
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#16-networkaoeability
*/
static abilityFull(params?: Params<AbilityFullParams>): NetRegex<AbilityFullParams> {
return parseHelper(params, 'abilityFull', {
0: { field: 'type', value: '2[12]' },
1: { field: 'timestamp' },
2: { field: 'sourceId' },
3: { field: 'source' },
4: { field: 'id' },
5: { field: 'ability' },
6: { field: 'targetId' },
7: { field: 'target' },
8: { field: 'flags' },
9: { field: 'damage' },
24: { field: 'targetCurrentHp' },
25: { field: 'targetMaxHp' },
40: { field: 'x' },
41: { field: 'y' },
42: { field: 'z' },
43: { field: 'heading' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#1b-networktargeticon-head-markers
*/
static headMarker(params?: Params<HeadMarkerParams>): NetRegex<HeadMarkerParams> {
return parseHelper(params, 'headMarker', {
0: { field: 'type', value: '27' },
1: { field: 'timestamp' },
2: { field: 'targetId' },
3: { field: 'target' },
6: { field: 'id' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#03-addcombatant
*/
static addedCombatant(params?: Params<AddedCombatantParams>): NetRegex<AddedCombatantParams> {
return parseHelper(params, 'addedCombatant', {
0: { field: 'type', value: '03' },
1: { field: 'timestamp' },
2: { field: 'id' },
3: { field: 'name' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#03-addcombatant
*/
static addedCombatantFull(
params?: Params<AddedCombatantFullParams>,
): NetRegex<AddedCombatantFullParams> {
return parseHelper(params, 'addedCombatantFull', {
0: { field: 'type', value: '03' },
1: { field: 'timestamp' },
2: { field: 'id' },
3: { field: 'name' },
4: { field: 'job' },
5: { field: 'level' },
6: { field: 'ownerId' },
8: { field: 'world' },
9: { field: 'npcNameId' },
10: { field: 'npcBaseId' },
11: { field: 'currentHp' },
12: { field: 'hp' },
17: { field: 'x' },
18: { field: 'y' },
19: { field: 'z' },
20: { field: 'heading' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#04-removecombatant
*/
static removingCombatant(
params?: Params<RemovingCombatantParams>,
): NetRegex<RemovingCombatantParams> {
return parseHelper(params, 'removingCombatant', {
0: { field: 'type', value: '04' },
1: { field: 'timestamp' },
2: { field: 'id' },
3: { field: 'name' },
12: { field: 'hp' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#1a-networkbuff
*/
static gainsEffect(params?: Params<GainsEffectParams>): NetRegex<GainsEffectParams> {
return parseHelper(params, 'gainsEffect', {
0: { field: 'type', value: '26' },
1: { field: 'timestamp' },
2: { field: 'effectId' },
3: { field: 'effect' },
4: { field: 'duration' },
5: { field: 'sourceId' },
6: { field: 'source' },
7: { field: 'targetId' },
8: { field: 'target' },
9: { field: 'count' },
});
}
/**
* Prefer gainsEffect over this function unless you really need extra data.
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#26-networkstatuseffects
*/
static statusEffectExplicit(
params?: Params<StatusEffectExplicitParams>,
): NetRegex<StatusEffectExplicitParams> {
return parseHelper(params, 'statusEffectExplicit', {
0: { field: 'type', value: '38' },
1: { field: 'timestamp' },
2: { field: 'targetId' },
3: { field: 'target' },
5: { field: 'hp' },
6: { field: 'maxHp' },
11: { field: 'x' },
12: { field: 'y' },
13: { field: 'z' },
14: { field: 'heading' },
15: { field: 'data0' },
16: { field: 'data1' },
17: { field: 'data2' },
18: { field: 'data3' },
19: { field: 'data4' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#1e-networkbuffremove
*/
static losesEffect(params?: Params<LosesEffectParams>): NetRegex<LosesEffectParams> {
return parseHelper(params, 'losesEffect', {
0: { field: 'type', value: '30' },
1: { field: 'timestamp' },
2: { field: 'effectId' },
3: { field: 'effect' },
5: { field: 'sourceId' },
6: { field: 'source' },
7: { field: 'targetId' },
8: { field: 'target' },
9: { field: 'count' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#23-networktether
*/
static tether(params?: Params<TetherParams>): NetRegex<TetherParams> {
return parseHelper(params, 'tether', {
0: { field: 'type', value: '35' },
1: { field: 'timestamp' },
2: { field: 'sourceId' },
3: { field: 'source' },
4: { field: 'targetId' },
5: { field: 'target' },
8: { field: 'id' },
});
}
/**
* 'target' was defeated by 'source'
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#19-networkdeath
*/
static wasDefeated(params?: Params<WasDefeatedParams>): NetRegex<WasDefeatedParams> {
return parseHelper(params, 'wasDefeated', {
0: { field: 'type', value: '25' },
1: { field: 'timestamp' },
2: { field: 'targetId' },
3: { field: 'target' },
4: { field: 'sourceId' },
5: { field: 'source' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#00-logline
*/
static echo(params?: Params<EchoParams>): NetRegex<EchoParams> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(params, 'echo', ['type', 'timestamp', 'code', 'name', 'line', 'capture']);
params.code = '0038';
return NetRegexes.gameLog(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#00-logline
*/
static dialog(params?: Params<DialogParams>): NetRegex<DialogParams> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(params, 'dialog', ['type', 'timestamp', 'code', 'name', 'line', 'capture']);
params.code = '0044';
return NetRegexes.gameLog(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#00-logline
*/
static message(params?: Params<MessageParams>): NetRegex<MessageParams> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(params, 'message', ['type', 'timestamp', 'code', 'name', 'line', 'capture']);
params.code = '0839';
return NetRegexes.gameLog(params);
}
/**
* fields: code, name, line, capture
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#00-logline
*/
static gameLog(params?: Params<GameLogParams>): NetRegex<GameLogParams> {
return parseHelper(params, 'gameLog', {
0: { field: 'type', value: '00' },
1: { field: 'timestamp' },
2: { field: 'code' },
3: { field: 'name' },
4: { field: 'line' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#00-logline
*/
static gameNameLog(params?: Params<GameNameLogParams>): NetRegex<GameNameLogParams> {
// for compat with Regexes.
return NetRegexes.gameLog(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#0c-playerstats
*/
static statChange(params?: Params<StatChangeParams>): NetRegex<StatChangeParams> {
return parseHelper(params, 'statChange', {
0: { field: 'type', value: '12' },
1: { field: 'timestamp' },
2: { field: 'job' },
3: { field: 'strength' },
4: { field: 'dexterity' },
5: { field: 'vitality' },
6: { field: 'intelligence' },
7: { field: 'mind' },
8: { field: 'piety' },
9: { field: 'attackPower' },
10: { field: 'directHit' },
11: { field: 'criticalHit' },
12: { field: 'attackMagicPotency' },
13: { field: 'healMagicPotency' },
14: { field: 'determination' },
15: { field: 'skillSpeed' },
16: { field: 'spellSpeed' },
18: { field: 'tenacity' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#01-changezone
*/
static changeZone(params?: Params<ChangeZoneParams>): NetRegex<ChangeZoneParams> {
return parseHelper(params, 'changeZone', {
0: { field: 'type', value: '01' },
1: { field: 'timestamp' },
2: { field: 'id' },
3: { field: 'name' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#21-network6d-actor-control-lines
*/
static network6d(params?: Params<Network6dParams>): NetRegex<Network6dParams> {
return parseHelper(params, 'network6d', {
0: { field: 'type', value: '33' },
1: { field: 'timestamp' },
2: { field: 'instance' },
3: { field: 'command' },
4: { field: 'data0' },
5: { field: 'data1' },
6: { field: 'data2' },
7: { field: 'data3' },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#22-networknametoggle
*/
static nameToggle(params?: Params<NameToggleParams>): NetRegex<NameToggleParams> {
return parseHelper(params, 'nameToggle', {
0: { field: 'type', value: '34' },
1: { field: 'timestamp' },
2: { field: 'id' },
3: { field: 'name' },
6: { field: 'toggle' },
});
}
}