pf2e-sage-stats 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -188,355 +188,20 @@ program.command('track')
188
188
  }
189
189
  })(),
190
190
  (async () => {
191
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
192
191
  const rl = readline_1.default.createInterface({
193
192
  input: process.stdin,
194
193
  output: process.stdout
195
194
  });
196
195
  while (true) {
197
196
  await new Promise((resolve) => setTimeout(resolve, 500));
198
- let input = await new Promise((resolve) => (rl.question('> ', resolve)));
199
- const commands = [{
200
- regex: /^\s*[bB]\s*(\d+)/,
201
- map: (match) => ({ command: 'block', index: parseInt(match[1], 10) })
202
- }, {
203
- regex: /^\s*reset/i,
204
- map: () => ({ command: 'reset' })
205
- }, {
206
- regex: /^\s*purge/i,
207
- map: () => ({ command: 'purge' })
208
- }, {
209
- regex: /^\s*blank/i,
210
- map: () => ({ command: 'blank' })
211
- }, {
212
- regex: /^\s*foe(\s+(\S+))?(\s+(\S+))?(\s+(\d+))?/i,
213
- map: (match) => ({ command: 'foe', name: match[2], alias: match[4], hp: match[6] && parseInt(match[6], 10) })
214
- }, {
215
- regex: /^\s*heal/i,
216
- map: () => ({ command: 'heal' })
217
- }, {
218
- regex: /^\s*exit/i,
219
- map: () => ({ command: 'exit' })
220
- }, {
221
- regex: /^\s*([^\d\s+-]+)\s*([+-]?\d+)/,
222
- map: (match) => {
223
- if (match[2].match(/^[+-]/)) {
224
- return { command: 'mod-hp', tag: match[1], value: parseInt(match[2]) };
225
- }
226
- else {
227
- return { command: 'set-hp', tag: match[1], value: parseInt(match[2]) };
228
- }
229
- }
230
- }, {
231
- regex: /^\s*(\S+)\s+[tT]\s*([+-]?\d+)/,
232
- map: (match) => {
233
- if (match[2].match(/^[+-]/)) {
234
- return { command: 'mod-thp', tag: match[1], value: parseInt(match[2]) };
235
- }
236
- else {
237
- return { command: 'set-thp', tag: match[1], value: parseInt(match[2]) };
238
- }
239
- }
240
- }, {
241
- regex: /^\s*(\S+)\s+[iI]\s*([+-]?\d+)/,
242
- map: (match) => {
243
- if (match[2].match(/^[+-]/)) {
244
- return { command: 'mod-init', tag: match[1], value: parseInt(match[2]) };
245
- }
246
- else {
247
- return { command: 'set-init', tag: match[1], value: parseInt(match[2]) };
248
- }
249
- },
250
- }, {
251
- regex: /^\s*([^\s="+-]+)\s*=?"([^"]*)"/,
252
- map: (match) => {
253
- return { command: 'set-conditions', tag: match[1], value: match[2] };
254
- },
255
- }, {
256
- regex: /^\s*([^\s="+-]+)\s*\+"([^"]*)"/,
257
- map: (match) => {
258
- return { command: 'add-conditions', tag: match[1], value: match[2] };
259
- },
260
- }, {
261
- regex: /^\s*([^\s="+-]+)\s*-"([^"]*)"/,
262
- map: (match) => {
263
- return { command: 'rem-conditions', tag: match[1], value: match[2] };
264
- },
265
- }, {
266
- regex: /^\s*([^\s="+-]+)\s*\+\+"([^"]*)"/,
267
- map: (match) => {
268
- return { command: 'inc-conditions', tag: match[1], value: match[2] };
269
- },
270
- }, {
271
- regex: /^\s*([^\s="+-]+)\s*--"([^"]*)"/,
272
- map: (match) => {
273
- return { command: 'dec-conditions', tag: match[1], value: match[2] };
274
- },
275
- }, {
276
- regex: /^\s*(\S+)\s+[sS]?\s*=?"([^"]*)"/,
277
- map: (match) => {
278
- return { command: 'set-conditions', tag: match[1], value: match[2] };
279
- },
280
- }, {
281
- regex: /^\s*(\S+)\s+[sS]?\s*\+"([^"]*)"/,
282
- map: (match) => {
283
- return { command: 'add-conditions', tag: match[1], value: match[2] };
284
- },
285
- }, {
286
- regex: /^\s*(\S+)\s+[sS]?\s*-"([^"]*)"/,
287
- map: (match) => {
288
- return { command: 'rem-conditions', tag: match[1], value: match[2] };
289
- },
290
- }, {
291
- regex: /^\s*(\S+)\s+[sS]?\s*\+\+"([^"]*)"/,
292
- map: (match) => {
293
- return { command: 'inc-conditions', tag: match[1], value: match[2] };
294
- },
295
- }, {
296
- regex: /^\s*(\S+)\s+[sS]?\s*--"([^"]*)"/,
297
- map: (match) => {
298
- return { command: 'dec-conditions', tag: match[1], value: match[2] };
299
- },
300
- }, {
301
- regex: /^\s*(\S+)\s+[cC]/,
302
- map: (match) => ({ command: 'check', tag: match[1] }),
303
- }, {
304
- regex: /^\s*(\S+)\s+[aA]/,
305
- map: (match) => ({ command: 'arrow', tag: match[1] }),
306
- }, {
307
- regex: /^\s*(\S+)\s+[eE]/,
308
- map: (match) => ({ command: 'empty', tag: match[1] }),
309
- }, {
310
- regex: /^\s*(\S+)\s+[xX]/,
311
- map: (match) => ({ command: 'cross', tag: match[1] }),
312
- }, {
313
- regex: /^\s*(\S+)\s+[dD]/,
314
- map: (match) => ({ command: 'delete', tag: match[1] }),
315
- }, {
316
- regex: /^\s*(\S+)\s+[wW]/,
317
- map: (match) => ({ command: 'wait', tag: match[1] }),
318
- }];
319
- const actions = [];
320
- while (true) {
321
- const command = commands.find((c) => input.match(c.regex));
322
- if (!command) {
323
- break;
324
- }
325
- const match = input.match(command.regex);
326
- if (!match) {
327
- break;
328
- }
329
- const action = command.map(match);
330
- input = input.slice(match[0].length);
331
- actions.push(action);
332
- }
197
+ const input = await new Promise((resolve) => (rl.question('> ', resolve)));
333
198
  const file = await promises_1.default.readFile(argument, { encoding: 'utf-8' });
334
- let tracker = (0, app_1.parseTracker)(file);
335
- for (const action of actions) {
336
- if (action.command === 'block') {
337
- const sorted = [...tracker].sort((a, b) => (a.init === b.init) ? (b.foe ? 1 : 0) - (a.foe ? 1 : 0) : b.init - a.init);
338
- const blocks = [[sorted[0]]];
339
- for (let i = 1; i < sorted.length; ++i) {
340
- if (sorted[i].foe === blocks[blocks.length - 1][0].foe) {
341
- blocks[blocks.length - 1].push(sorted[i]);
342
- }
343
- else {
344
- blocks.push([sorted[i]]);
345
- }
346
- }
347
- for (let i = 0; i < blocks.length; ++i) {
348
- if (i < action.index) {
349
- blocks[i].forEach((b) => b.state = 'check');
350
- }
351
- else if (i === action.index) {
352
- blocks[i].forEach((b) => b.state = 'arrow');
353
- }
354
- else {
355
- blocks[i].forEach((b) => b.state = 'empty');
356
- }
357
- }
358
- }
359
- else if (action.command === 'wait') {
360
- const sorted = [...tracker].sort((a, b) => (a.init === b.init) ? (b.foe ? 1 : 0) - (a.foe ? 1 : 0) : b.init - a.init);
361
- const blocks = [[sorted[0]]];
362
- for (let i = 1; i < sorted.length; ++i) {
363
- if (sorted[i].foe === blocks[blocks.length - 1][0].foe) {
364
- blocks[blocks.length - 1].push(sorted[i]);
365
- }
366
- else {
367
- blocks.push([sorted[i]]);
368
- }
369
- }
370
- const blockIndex = blocks.findIndex((b) => b.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
371
- const block = blocks[blockIndex + 1];
372
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
373
- if (block && item) {
374
- item.init = ((_b = (_a = block[block.length - 1]) === null || _a === void 0 ? void 0 : _a.init) !== null && _b !== void 0 ? _b : 0) - 1;
375
- }
376
- }
377
- else if (action.command === 'check') {
378
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
379
- if (item) {
380
- item.state = "check";
381
- }
382
- }
383
- else if (action.command === 'arrow') {
384
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
385
- if (item) {
386
- item.state = "arrow";
387
- }
388
- }
389
- else if (action.command === 'empty') {
390
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
391
- if (item) {
392
- item.state = "empty";
393
- }
394
- }
395
- else if (action.command === 'cross') {
396
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
397
- if (item) {
398
- item.state = "cross";
399
- }
400
- }
401
- else if (action.command === 'delete') {
402
- tracker = tracker.filter((c) => !(c.id === action.tag || c.alias === action.tag || c.name == action.tag)).map((p) => ({
403
- ...p,
404
- children: p.children.filter((c) => !(c.id === action.tag || c.alias === action.tag || c.name == action.tag)),
405
- }));
406
- }
407
- else if (action.command === 'purge') {
408
- tracker = tracker.filter((c) => !c.foe);
409
- }
410
- else if (action.command === 'exit') {
411
- process.exit(0);
412
- }
413
- else if (action.command === 'heal') {
414
- tracker.forEach((char) => {
415
- char.hp = char.maxhp;
416
- char.children.forEach((child) => {
417
- child.hp = child.maxhp;
418
- });
419
- });
420
- }
421
- else if (action.command === 'reset') {
422
- tracker.forEach((char) => {
423
- char.state = 'empty';
424
- });
425
- }
426
- else if (action.command === 'set-thp') {
427
- const item = (_c = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _c !== void 0 ? _c : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
428
- if (item) {
429
- item.temphp = Math.max(0, action.value);
430
- }
431
- }
432
- else if (action.command === 'set-hp') {
433
- const item = (_d = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _d !== void 0 ? _d : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
434
- if (item) {
435
- item.hp = Math.min(item.maxhp, Math.max(0, action.value));
436
- }
437
- }
438
- else if (action.command === 'set-init') {
439
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
440
- if (item) {
441
- item.init = Math.max(0, action.value);
442
- }
443
- }
444
- else if (action.command === 'blank') {
445
- tracker.forEach((item) => {
446
- item.conditions = "";
447
- item.children.forEach((child) => {
448
- child.conditions = "";
449
- });
450
- });
451
- }
452
- else if (action.command === 'set-conditions') {
453
- const item = (_e = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _e !== void 0 ? _e : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
454
- if (item) {
455
- item.conditions = action.value.trim();
456
- }
457
- }
458
- else if (action.command === 'add-conditions') {
459
- const item = (_f = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _f !== void 0 ? _f : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
460
- if (item) {
461
- const conditions = [...item.conditions.split(',').map((c) => c.toLowerCase().trim()).filter((c) => c.length), action.value.toLowerCase().trim()];
462
- item.conditions = conditions.join(', ');
463
- }
464
- }
465
- else if (action.command === 'rem-conditions') {
466
- const item = (_g = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _g !== void 0 ? _g : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
467
- if (item) {
468
- const conditions = item.conditions.split(',').map((c) => c.toLowerCase().trim()).filter((c) => c.length).filter((c) => !c.includes(action.value.toLowerCase().trim()));
469
- item.conditions = conditions.join(', ');
470
- }
471
- }
472
- else if (action.command === 'inc-conditions') {
473
- const item = (_h = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _h !== void 0 ? _h : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
474
- if (item) {
475
- const conditions = item.conditions.split(',').map((c) => c.toLowerCase().trim()).filter((c) => c.length).map((c) => {
476
- if (c.includes(action.value.toLowerCase().trim())) {
477
- const match = c.match(/(.*)\s+(\d+)/);
478
- if (match && match[1] && match[2]) {
479
- return `${match[1].trim()} ${parseInt(match[2]) + 1}`;
480
- }
481
- }
482
- return c;
483
- });
484
- item.conditions = conditions.join(', ');
485
- }
486
- }
487
- else if (action.command === 'dec-conditions') {
488
- const item = (_j = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _j !== void 0 ? _j : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
489
- if (item) {
490
- const conditions = item.conditions.split(',').map((c) => c.toLowerCase().trim()).filter((c) => c.length).map((c) => {
491
- if (c.includes(action.value.toLowerCase().trim())) {
492
- const match = c.match(/(.*)\s+(\d+)/);
493
- if (match && match[1] && match[2]) {
494
- if (parseInt(match[2]) > 1) {
495
- return `${match[1].trim()} ${parseInt(match[2]) - 1}`;
496
- }
497
- else {
498
- return match[1].trim();
499
- }
500
- }
501
- }
502
- return c;
503
- });
504
- item.conditions = conditions.join(', ');
505
- }
506
- }
507
- else if (action.command === 'mod-thp') {
508
- const item = (_k = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _k !== void 0 ? _k : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
509
- if (item) {
510
- item.temphp = Math.max(0, item.temphp + action.value);
511
- }
512
- }
513
- else if (action.command === 'mod-hp') {
514
- const item = (_l = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag)) !== null && _l !== void 0 ? _l : (tracker.flatMap((c) => c.children).find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag));
515
- if (item) {
516
- let hp = item.hp;
517
- let thp = item.temphp;
518
- if (action.value < 0) {
519
- thp += action.value;
520
- hp += Math.min(thp, 0);
521
- }
522
- else {
523
- hp += action.value;
524
- }
525
- item.temphp = Math.max(0, thp);
526
- item.hp = Math.min(item.maxhp, Math.max(0, hp));
527
- }
528
- }
529
- else if (action.command === 'mod-init') {
530
- const item = tracker.find((c) => c.id === action.tag || c.alias === action.tag || c.name == action.tag);
531
- if (item) {
532
- item.init = Math.max(0, item.init + action.value);
533
- }
534
- }
535
- else if (action.command === 'foe') {
536
- tracker.push(app_1.charSchema.parse({ foe: true, name: action.name, alias: action.alias, maxhp: action.hp, hp: action.hp }));
537
- }
199
+ const tracker = (0, app_1.parseTracker)(file);
200
+ const { tracker: next, exit } = (0, app_1.applyTrackerCommands)(tracker, (0, app_1.parseTrackerCommands)(input));
201
+ if (exit) {
202
+ process.exit(0);
538
203
  }
539
- await promises_1.default.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' });
204
+ await promises_1.default.writeFile(argument, JSON.stringify(next, undefined, 2), { encoding: 'utf-8' });
540
205
  }
541
206
  })(),
542
207
  ]);
@@ -0,0 +1,13 @@
1
+ export interface NormalisedLine {
2
+ text: string;
3
+ }
4
+ export interface ClassifiedLine {
5
+ text: string;
6
+ kind: string;
7
+ lineFrom: number;
8
+ lineTo: number;
9
+ }
10
+ export declare function normaliseStatblockLines(source: string): NormalisedLine[];
11
+ export declare function classifyStatblockLines(source: string): ClassifiedLine[];
12
+ export declare function getLine(lines: ClassifiedLine[], kind: string): string;
13
+ export declare function listLines(lines: ClassifiedLine[], kind: string): string;
package/dist/lines.js ADDED
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normaliseStatblockLines = normaliseStatblockLines;
4
+ exports.classifyStatblockLines = classifyStatblockLines;
5
+ exports.getLine = getLine;
6
+ exports.listLines = listLines;
7
+ function isCapitalised(token) {
8
+ const first = token.charAt(0);
9
+ return /^\p{L}/u.test(first) && first.toUpperCase() === first;
10
+ }
11
+ function isUnbalanced(str) {
12
+ let count = 0;
13
+ for (const char of str) {
14
+ if (char === '(')
15
+ count++;
16
+ if (char === ')')
17
+ count--;
18
+ if (count < 0)
19
+ return false;
20
+ }
21
+ return count > 0;
22
+ }
23
+ function normaliseLine(line) {
24
+ return line
25
+ .replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, " ")
26
+ .replace(/[\u00AD\b]/g, "")
27
+ .replace(/[\u2010\u2011\u2012\u2013\u2014\u2212]/g, "-")
28
+ .replace(/[\u2018\u2019\u2032]/g, "'")
29
+ .replace(/[\u201C\u201D]/g, '"')
30
+ .replace(/[ \t\f\v]+/g, " ")
31
+ .trim();
32
+ }
33
+ const CONTINUATIONS = [
34
+ "plus", "and", "but", "or", 'a', "•",
35
+ ];
36
+ const POINTS = [
37
+ "•",
38
+ ];
39
+ function normaliseStatblockLines(source) {
40
+ var _a, _b;
41
+ const pieces = source.replace(/^\uFEFF/, "").split(/\r\n|\n|\r|\u2028|\u2029/u);
42
+ const lines = [];
43
+ for (const piece of pieces) {
44
+ const text = normaliseLine(piece);
45
+ if (text.length === 0) {
46
+ continue;
47
+ }
48
+ const lastLine = lines[lines.length - 1];
49
+ // Pathbuilder exception
50
+ if (/^Languages\s+/.test(text) && /^Perception\s+.*;$/.test((_a = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text) !== null && _a !== void 0 ? _a : '')) {
51
+ lastLine.text = lastLine.text.slice(0, -1);
52
+ }
53
+ const lastText = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text.split(' ');
54
+ const lastWord = lastText === null || lastText === void 0 ? void 0 : lastText[lastText.length - 1];
55
+ if ((isCapitalised(text) || POINTS.includes(text.charAt(0)))
56
+ &&
57
+ !(lastWord && (CONTINUATIONS.includes(lastWord) || /[,:;]$/.test(lastWord)))
58
+ &&
59
+ !isUnbalanced((_b = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text) !== null && _b !== void 0 ? _b : '')) {
60
+ lines.push({ text });
61
+ }
62
+ else {
63
+ lines[lines.length - 1].text = [lines[lines.length - 1].text, text].join(' ');
64
+ }
65
+ }
66
+ return lines;
67
+ }
68
+ function findIndexFrom(array, predicate, index) {
69
+ return array.slice(index).findIndex(predicate) + index;
70
+ }
71
+ function classifyStatblockLines(source) {
72
+ const normalised = normaliseStatblockLines(source);
73
+ const lines = [];
74
+ let lastLine = -1;
75
+ const titleLine = findIndexFrom(normalised, ({ text }) => /\S+\s+-?\d+$/.test(text), lastLine + 1);
76
+ const title = { text: normalised[titleLine].text, kind: 'title', lineFrom: titleLine, lineTo: titleLine };
77
+ lastLine = titleLine;
78
+ lines.push(title);
79
+ const perceptionLine = findIndexFrom(normalised, ({ text }) => /^Perception\s+[+-]\d+/.test(text), lastLine + 1);
80
+ if (perceptionLine > lastLine) {
81
+ const perception = { text: normalised[perceptionLine].text, kind: 'perception', lineFrom: perceptionLine, lineTo: perceptionLine };
82
+ lastLine = perceptionLine;
83
+ lines.push(perception);
84
+ }
85
+ const stealthLine = findIndexFrom(normalised, ({ text }) => /^Stealth\s+(?:DC)\s?[+-]?\d+/.test(text), lastLine + 1);
86
+ if (stealthLine > lastLine) {
87
+ const stealth = { text: normalised[stealthLine].text, kind: 'stealth', lineFrom: stealthLine, lineTo: stealthLine };
88
+ lastLine = stealthLine;
89
+ lines.push(stealth);
90
+ }
91
+ const languagesLine = findIndexFrom(normalised, ({ text }) => /^Languages\s+/.test(text), lastLine + 1);
92
+ if (languagesLine > lastLine) {
93
+ const languages = { text: normalised[languagesLine].text, kind: 'languages', lineFrom: languagesLine, lineTo: languagesLine };
94
+ lastLine = languagesLine;
95
+ lines.push(languages);
96
+ }
97
+ const skillsLine = findIndexFrom(normalised, ({ text }) => /^Skills\s+/.test(text), lastLine + 1);
98
+ if (skillsLine > lastLine) {
99
+ const skills = { text: normalised[skillsLine].text, kind: 'skills', lineFrom: skillsLine, lineTo: skillsLine };
100
+ lastLine = skillsLine;
101
+ lines.push(skills);
102
+ }
103
+ const descriptionLine = findIndexFrom(normalised, ({ text }) => /^Description\s+/.test(text), lastLine + 1);
104
+ if (descriptionLine > lastLine) {
105
+ const description = { text: normalised[descriptionLine].text, kind: 'description', lineFrom: descriptionLine, lineTo: descriptionLine };
106
+ lastLine = descriptionLine;
107
+ lines.push(description);
108
+ }
109
+ const disableLine = findIndexFrom(normalised, ({ text }) => /^Disable\s+/.test(text), lastLine + 1);
110
+ if (disableLine > lastLine) {
111
+ const disable = { text: normalised[disableLine].text, kind: 'disable', lineFrom: disableLine, lineTo: disableLine };
112
+ lastLine = disableLine;
113
+ lines.push(disable);
114
+ }
115
+ const attributeLine = findIndexFrom(normalised, ({ text }) => /^Str\s+[+-]\d+,/.test(text), lastLine + 1);
116
+ if (attributeLine > lastLine) {
117
+ const attributes = { text: normalised[attributeLine].text, kind: 'attributes', lineFrom: attributeLine, lineTo: attributeLine };
118
+ lastLine = attributeLine;
119
+ lines.push(attributes);
120
+ }
121
+ const itemsLine = findIndexFrom(normalised, ({ text }) => /^Items\s+/.test(text), lastLine + 1);
122
+ if (itemsLine > lastLine) {
123
+ const items = { text: normalised[itemsLine].text, kind: 'items', lineFrom: itemsLine, lineTo: itemsLine };
124
+ lastLine = itemsLine;
125
+ lines.push(items);
126
+ }
127
+ const defenceLine = findIndexFrom(normalised, ({ text }) => /^AC\s+\d+/.test(text), lastLine + 1);
128
+ if (defenceLine > lastLine) {
129
+ const defence = { text: normalised[defenceLine].text, kind: 'defence', lineFrom: defenceLine, lineTo: defenceLine };
130
+ lastLine = defenceLine;
131
+ lines.push(defence);
132
+ }
133
+ const hpLine = findIndexFrom(normalised, ({ text }) => /^(?:Hardness\s+\d+;\s+)?HP\s+\d+/.test(text), lastLine + 1);
134
+ if (hpLine > lastLine) {
135
+ const hp = { text: normalised[hpLine].text, kind: 'hp', lineFrom: hpLine, lineTo: hpLine };
136
+ lastLine = hpLine;
137
+ lines.push(hp);
138
+ }
139
+ const speedLine = findIndexFrom(normalised, ({ text }) => /^Speed\s+(?:(?:fly|climb|swim|burrow)\s+)?\d+/.test(text), lastLine + 1);
140
+ if (speedLine > lastLine) {
141
+ const speed = { text: normalised[speedLine].text, kind: 'speed', lineFrom: speedLine, lineTo: speedLine };
142
+ lastLine = speedLine;
143
+ lines.push(speed);
144
+ }
145
+ let strikeLine = findIndexFrom(normalised, ({ text }) => /^(?:Melee|Ranged)\s+/.test(text), lastLine + 1);
146
+ while (strikeLine > lastLine) {
147
+ const strike = { text: normalised[strikeLine].text, kind: 'strike', lineFrom: strikeLine, lineTo: strikeLine };
148
+ lastLine = strikeLine;
149
+ lines.push(strike);
150
+ strikeLine = findIndexFrom(normalised, ({ text }) => /^(?:Melee|Ranged)\s+/.test(text), lastLine + 1);
151
+ }
152
+ let spellsLine = findIndexFrom(normalised, ({ text }) => /^(?:Arcane|Divine|Occult|Primal)\s+(?:(?:Prepared|Spontaneous|Innate)\s+)?Spells/.test(text), lastLine + 1);
153
+ while (spellsLine > lastLine) {
154
+ const spells = { text: normalised[spellsLine].text, kind: 'spells', lineFrom: spellsLine, lineTo: spellsLine };
155
+ lastLine = spellsLine;
156
+ lines.push(spells);
157
+ spellsLine = findIndexFrom(normalised, ({ text }) => /^(?:Arcane|Divine|Occult|Primal)\s+(?:(?:Prepared|Spontaneous|Innate)\s+)?Spells/.test(text), lastLine + 1);
158
+ }
159
+ const ritualsLine = findIndexFrom(normalised, ({ text }) => /^Rituals\s+/.test(text), lastLine + 1);
160
+ if (ritualsLine > lastLine) {
161
+ const rituals = { text: normalised[ritualsLine].text, kind: 'rituals', lineFrom: ritualsLine, lineTo: ritualsLine };
162
+ lastLine = ritualsLine;
163
+ lines.push(rituals);
164
+ }
165
+ const featsLine = findIndexFrom(normalised, ({ text }) => /^Additional\s+Feats\s+/.test(text), lastLine + 1);
166
+ if (featsLine > lastLine) {
167
+ const feats = { text: normalised[featsLine].text, kind: 'feats', lineFrom: featsLine, lineTo: featsLine };
168
+ lastLine = featsLine;
169
+ lines.push(feats);
170
+ }
171
+ const specialsLine = findIndexFrom(normalised, ({ text }) => /^Additional\s+Specials\s+/.test(text), lastLine + 1);
172
+ if (specialsLine > lastLine) {
173
+ const specials = { text: normalised[specialsLine].text, kind: 'specials', lineFrom: specialsLine, lineTo: specialsLine };
174
+ lastLine = specialsLine;
175
+ lines.push(specials);
176
+ }
177
+ // Recovery
178
+ const skills = lines.find(({ kind }) => kind === 'skills');
179
+ while (skills && skills.lineTo + 1 < attributeLine) { // There's more skill lines
180
+ if (/\S+\s+[+-]\d+/.test(normalised[skills.lineTo + 1].text)) {
181
+ skills.text = [skills.text, normalised[skills.lineTo + 1].text].join(' ');
182
+ skills.lineTo = skills.lineTo + 1;
183
+ }
184
+ else {
185
+ break;
186
+ }
187
+ }
188
+ if (title.lineTo + 1 < lines[1].lineFrom) {
189
+ const traits = { text: normalised[title.lineTo + 1].text, kind: 'traits', lineFrom: title.lineTo + 1, lineTo: title.lineTo + 1 };
190
+ lines.splice(1, 0, traits);
191
+ }
192
+ const traitsIndex = lines.findIndex(({ kind }) => kind === 'traits');
193
+ if (traitsIndex > 0 && lines[traitsIndex].lineTo + 1 < lines[traitsIndex + 1].lineFrom) {
194
+ const source = { text: normalised[lines[traitsIndex].lineTo + 1].text, kind: 'source', lineFrom: lines[traitsIndex].lineTo + 1, lineTo: lines[traitsIndex].lineTo + 1 };
195
+ lines.splice(traitsIndex + 1, 0, source);
196
+ }
197
+ const abilityBlocks = [...lines, { text: 'stub', kind: 'stub', lineFrom: normalised.length, lineTo: normalised.length }].flatMap((item, i) => {
198
+ const prevTo = i === 0 ? 0 : lines[i - 1].lineTo;
199
+ const start = prevTo + 1;
200
+ return start <= item.lineFrom - 1 ? [
201
+ { lineFrom: start, lineTo: item.lineFrom - 1 }
202
+ ] : [];
203
+ }).map((s) => (normalised.slice(s.lineFrom, s.lineTo + 1).map((l, i) => ({ text: l.text, kind: 'ability', lineFrom: s.lineFrom + i, lineTo: s.lineFrom + i }))));
204
+ for (const abilityBlock of abilityBlocks) {
205
+ let ability = abilityBlock[0];
206
+ let line = 1;
207
+ while (line < abilityBlock.length) {
208
+ const text = abilityBlock[line].text.replace(/(\[.+\]|\(.+\))\s+/g, '');
209
+ if (!/^\p{Lu}[\p{L}\p{P}]*(?:\s+[\p{Ll}\p{N}][\p{L}\p{N}\p{P}]*){2}/u.test(text)
210
+ &&
211
+ !/^\p{Lu}\p{L}*\./u.test(text)
212
+ &&
213
+ !/^\p{Lu}[\p{L}\p{P}]*\s+[\p{L}\p{N}\p{P}]+\./u.test(text)
214
+ &&
215
+ !/^(Success|Critical Success|Failure|Critical Failure|Maximum Duration|Recall Knowledge|Hit Points|Saving Throw|Effect|Requirements)\p{P}?\s+/u.test(abilityBlock[line].text)
216
+ &&
217
+ !/^\p{L}+;\s+/u.test(text)) {
218
+ lines.push(ability);
219
+ ability = abilityBlock[line];
220
+ }
221
+ else {
222
+ ability.text = [ability.text, abilityBlock[line].text].join(' ');
223
+ ability.lineTo = abilityBlock[line].lineTo;
224
+ }
225
+ line++;
226
+ }
227
+ lines.push(ability);
228
+ }
229
+ return lines.sort((a, b) => a.lineFrom - b.lineFrom);
230
+ }
231
+ function getLine(lines, kind) {
232
+ var _a, _b;
233
+ return (_b = (_a = lines.find((l) => kind === l.kind)) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : '';
234
+ }
235
+ function listLines(lines, kind) {
236
+ return lines.filter((l) => kind === l.kind).map(({ text }) => text).join('\n');
237
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pf2e-sage-stats",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "An RPG Sage's .tsv stat generation tool",
5
5
  "author": "ikariott@gmail.com",
6
6
  "license": "MIT",
@@ -48,6 +48,5 @@
48
48
  "pluralize": "^8.0.0",
49
49
  "tsv": "^0.2.0",
50
50
  "zod": "^3.24.1"
51
- },
52
- "packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
51
+ }
53
52
  }