pf2e-sage-stats 0.3.1 → 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/app.d.ts CHANGED
@@ -600,6 +600,93 @@ export declare const newtracker: (status: string) => {
600
600
  }[];
601
601
  }[];
602
602
  export declare const formatTracker: (tracker: CharSchema[]) => string;
603
+ export type TrackerCommand = {
604
+ command: 'block';
605
+ index: number;
606
+ } | {
607
+ command: 'reset';
608
+ } | {
609
+ command: 'purge';
610
+ } | {
611
+ command: 'blank';
612
+ } | {
613
+ command: 'foe';
614
+ name?: string;
615
+ alias?: string;
616
+ hp?: number;
617
+ } | {
618
+ command: 'heal';
619
+ } | {
620
+ command: 'exit';
621
+ } | {
622
+ command: 'mod-hp';
623
+ tag: string;
624
+ value: number;
625
+ } | {
626
+ command: 'set-hp';
627
+ tag: string;
628
+ value: number;
629
+ } | {
630
+ command: 'mod-thp';
631
+ tag: string;
632
+ value: number;
633
+ } | {
634
+ command: 'set-thp';
635
+ tag: string;
636
+ value: number;
637
+ } | {
638
+ command: 'mod-init';
639
+ tag: string;
640
+ value: number;
641
+ } | {
642
+ command: 'set-init';
643
+ tag: string;
644
+ value: number;
645
+ } | {
646
+ command: 'set-conditions';
647
+ tag: string;
648
+ value: string;
649
+ } | {
650
+ command: 'add-conditions';
651
+ tag: string;
652
+ value: string;
653
+ } | {
654
+ command: 'rem-conditions';
655
+ tag: string;
656
+ value: string;
657
+ } | {
658
+ command: 'inc-conditions';
659
+ tag: string;
660
+ value: string;
661
+ } | {
662
+ command: 'dec-conditions';
663
+ tag: string;
664
+ value: string;
665
+ } | {
666
+ command: 'check';
667
+ tag: string;
668
+ } | {
669
+ command: 'arrow';
670
+ tag: string;
671
+ } | {
672
+ command: 'empty';
673
+ tag: string;
674
+ } | {
675
+ command: 'cross';
676
+ tag: string;
677
+ } | {
678
+ command: 'delete';
679
+ tag: string;
680
+ } | {
681
+ command: 'wait';
682
+ tag: string;
683
+ };
684
+ export type TrackerUpdate = {
685
+ tracker: CharSchema[];
686
+ exit: boolean;
687
+ };
688
+ export declare const parseTrackerCommands: (line: string) => TrackerCommand[];
689
+ export declare const applyTrackerCommands: (tracker: CharSchema[], actions: TrackerCommand[]) => TrackerUpdate;
603
690
  export declare const sortFolder: () => Promise<{
604
691
  oldname: string;
605
692
  newname: string;
package/dist/app.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sortFolder = exports.formatTracker = exports.newtracker = exports.formatHP = exports.formatTable = exports.formatCommand = exports.formatTSV = exports.flatten = exports.parseStatblock = exports.parseTracker = exports.parseJSON = exports.formatJSON = exports.fromatMap = exports.diceMap = exports.flatMap = exports.charSchema = exports.stub = void 0;
6
+ exports.sortFolder = exports.applyTrackerCommands = exports.parseTrackerCommands = exports.formatTracker = exports.newtracker = exports.formatHP = exports.formatTable = exports.formatCommand = exports.formatTSV = exports.flatten = exports.parseStatblock = exports.parseTracker = exports.parseJSON = exports.formatJSON = exports.fromatMap = exports.diceMap = exports.flatMap = exports.charSchema = exports.stub = void 0;
7
7
  const zod_1 = __importDefault(require("zod"));
8
8
  const tsv_1 = require("tsv");
9
9
  const promises_1 = require("fs/promises");
@@ -693,6 +693,316 @@ const formatTracker = (tracker) => {
693
693
  return lines.join('\n');
694
694
  };
695
695
  exports.formatTracker = formatTracker;
696
+ const trackerCommands = [{
697
+ regex: /^\s*[bB]\s*(\d+)/,
698
+ map: (match) => { var _a; return ({ command: 'block', index: parseInt((_a = match[1]) !== null && _a !== void 0 ? _a : '', 10) }); },
699
+ }, {
700
+ regex: /^\s*reset/i,
701
+ map: () => ({ command: 'reset' }),
702
+ }, {
703
+ regex: /^\s*purge/i,
704
+ map: () => ({ command: 'purge' }),
705
+ }, {
706
+ regex: /^\s*blank/i,
707
+ map: () => ({ command: 'blank' }),
708
+ }, {
709
+ regex: /^\s*foe(\s+(\S+))?(\s+(\S+))?(\s+(\d+))?/i,
710
+ map: (match) => ({ command: 'foe', name: match[2], alias: match[4], hp: match[6] ? parseInt(match[6], 10) : undefined }),
711
+ }, {
712
+ regex: /^\s*heal/i,
713
+ map: () => ({ command: 'heal' }),
714
+ }, {
715
+ regex: /^\s*exit/i,
716
+ map: () => ({ command: 'exit' }),
717
+ }, {
718
+ regex: /^\s*([^\d\s+-]+)\s*([+-]?\d+)/,
719
+ map: (match) => {
720
+ var _a, _b;
721
+ const tag = (_a = match[1]) !== null && _a !== void 0 ? _a : '';
722
+ const raw = (_b = match[2]) !== null && _b !== void 0 ? _b : '';
723
+ if (raw.match(/^[+-]/)) {
724
+ return { command: 'mod-hp', tag, value: parseInt(raw, 10) };
725
+ }
726
+ return { command: 'set-hp', tag, value: parseInt(raw, 10) };
727
+ },
728
+ }, {
729
+ regex: /^\s*(\S+)\s+[tT]\s*([+-]?\d+)/,
730
+ map: (match) => {
731
+ var _a, _b;
732
+ const tag = (_a = match[1]) !== null && _a !== void 0 ? _a : '';
733
+ const raw = (_b = match[2]) !== null && _b !== void 0 ? _b : '';
734
+ if (raw.match(/^[+-]/)) {
735
+ return { command: 'mod-thp', tag, value: parseInt(raw, 10) };
736
+ }
737
+ return { command: 'set-thp', tag, value: parseInt(raw, 10) };
738
+ },
739
+ }, {
740
+ regex: /^\s*(\S+)\s+[iI]\s*([+-]?\d+)/,
741
+ map: (match) => {
742
+ var _a, _b;
743
+ const tag = (_a = match[1]) !== null && _a !== void 0 ? _a : '';
744
+ const raw = (_b = match[2]) !== null && _b !== void 0 ? _b : '';
745
+ if (raw.match(/^[+-]/)) {
746
+ return { command: 'mod-init', tag, value: parseInt(raw, 10) };
747
+ }
748
+ return { command: 'set-init', tag, value: parseInt(raw, 10) };
749
+ },
750
+ }, {
751
+ regex: /^\s*([^\s="+-]+)\s*=?"([^"]*)"/,
752
+ map: (match) => { var _a, _b; return ({ command: 'set-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
753
+ }, {
754
+ regex: /^\s*([^\s="+-]+)\s*\+"([^"]*)"/,
755
+ map: (match) => { var _a, _b; return ({ command: 'add-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
756
+ }, {
757
+ regex: /^\s*([^\s="+-]+)\s*-"([^"]*)"/,
758
+ map: (match) => { var _a, _b; return ({ command: 'rem-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
759
+ }, {
760
+ regex: /^\s*([^\s="+-]+)\s*\+\+"([^"]*)"/,
761
+ map: (match) => { var _a, _b; return ({ command: 'inc-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
762
+ }, {
763
+ regex: /^\s*([^\s="+-]+)\s*--"([^"]*)"/,
764
+ map: (match) => { var _a, _b; return ({ command: 'dec-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
765
+ }, {
766
+ regex: /^\s*(\S+)\s+[sS]?\s*=?"([^"]*)"/,
767
+ map: (match) => { var _a, _b; return ({ command: 'set-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
768
+ }, {
769
+ regex: /^\s*(\S+)\s+[sS]?\s*\+"([^"]*)"/,
770
+ map: (match) => { var _a, _b; return ({ command: 'add-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
771
+ }, {
772
+ regex: /^\s*(\S+)\s+[sS]?\s*-"([^"]*)"/,
773
+ map: (match) => { var _a, _b; return ({ command: 'rem-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
774
+ }, {
775
+ regex: /^\s*(\S+)\s+[sS]?\s*\+\+"([^"]*)"/,
776
+ map: (match) => { var _a, _b; return ({ command: 'inc-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
777
+ }, {
778
+ regex: /^\s*(\S+)\s+[sS]?\s*--"([^"]*)"/,
779
+ map: (match) => { var _a, _b; return ({ command: 'dec-conditions', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '', value: (_b = match[2]) !== null && _b !== void 0 ? _b : '' }); },
780
+ }, {
781
+ regex: /^\s*(\S+)\s+[cC]/,
782
+ map: (match) => { var _a; return ({ command: 'check', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
783
+ }, {
784
+ regex: /^\s*(\S+)\s+[aA]/,
785
+ map: (match) => { var _a; return ({ command: 'arrow', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
786
+ }, {
787
+ regex: /^\s*(\S+)\s+[eE]/,
788
+ map: (match) => { var _a; return ({ command: 'empty', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
789
+ }, {
790
+ regex: /^\s*(\S+)\s+[xX]/,
791
+ map: (match) => { var _a; return ({ command: 'cross', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
792
+ }, {
793
+ regex: /^\s*(\S+)\s+[dD]/,
794
+ map: (match) => { var _a; return ({ command: 'delete', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
795
+ }, {
796
+ regex: /^\s*(\S+)\s+[wW]/,
797
+ map: (match) => { var _a; return ({ command: 'wait', tag: (_a = match[1]) !== null && _a !== void 0 ? _a : '' }); },
798
+ }];
799
+ const parseTrackerCommands = (line) => {
800
+ const actions = [];
801
+ let input = line;
802
+ while (input.length > 0) {
803
+ const command = trackerCommands.find((entry) => input.match(entry.regex));
804
+ if (!command) {
805
+ break;
806
+ }
807
+ const match = input.match(command.regex);
808
+ if (!match) {
809
+ break;
810
+ }
811
+ actions.push(command.map(match));
812
+ input = input.slice(match[0].length);
813
+ }
814
+ return actions;
815
+ };
816
+ exports.parseTrackerCommands = parseTrackerCommands;
817
+ const sameTag = (entry, tag) => (entry.id === tag || entry.alias === tag || entry.name === tag);
818
+ const findCharacter = (tracker, tag) => (tracker.find((entry) => sameTag(entry, tag)));
819
+ const findCharacterOrChild = (tracker, tag) => {
820
+ var _a;
821
+ return ((_a = findCharacter(tracker, tag)) !== null && _a !== void 0 ? _a : tracker.flatMap((entry) => entry.children).find((entry) => sameTag(entry, tag)));
822
+ };
823
+ const initiativeBlocks = (tracker) => {
824
+ const sorted = [...tracker].sort((a, b) => (a.init === b.init) ? (b.foe ? 1 : 0) - (a.foe ? 1 : 0) : b.init - a.init);
825
+ const blocks = [[sorted[0]]];
826
+ for (let i = 1; i < sorted.length; ++i) {
827
+ if (sorted[i].foe === blocks[blocks.length - 1][0].foe) {
828
+ blocks[blocks.length - 1].push(sorted[i]);
829
+ }
830
+ else {
831
+ blocks.push([sorted[i]]);
832
+ }
833
+ }
834
+ return blocks;
835
+ };
836
+ const updateConditions = (tracker, tag, update) => {
837
+ const item = findCharacterOrChild(tracker, tag);
838
+ if (item) {
839
+ const conditions = item.conditions.split(',').map((condition) => condition.toLowerCase().trim()).filter((condition) => condition.length);
840
+ item.conditions = update(conditions).join(', ');
841
+ }
842
+ };
843
+ const numberedCondition = (condition, name, next) => {
844
+ if (!condition.includes(name)) {
845
+ return condition;
846
+ }
847
+ const match = condition.match(/(.*)\s+(\d+)/);
848
+ if (match && match[1] && match[2]) {
849
+ return next(match[1].trim(), parseInt(match[2], 10));
850
+ }
851
+ return condition;
852
+ };
853
+ const unexpectedTrackerCommand = (action) => {
854
+ throw new Error(`Unexpected tracker command: ${JSON.stringify(action)}`);
855
+ };
856
+ const applyTrackerCommand = (tracker, action) => {
857
+ var _a, _b;
858
+ switch (action.command) {
859
+ case 'block': {
860
+ const blocks = initiativeBlocks(tracker);
861
+ for (let i = 0; i < blocks.length; ++i) {
862
+ const state = i < action.index ? 'check' : i === action.index ? 'arrow' : 'empty';
863
+ blocks[i].forEach((entry) => {
864
+ entry.state = state;
865
+ });
866
+ }
867
+ return tracker;
868
+ }
869
+ case 'wait': {
870
+ const blocks = initiativeBlocks(tracker);
871
+ const blockIndex = blocks.findIndex((block) => block.find((entry) => sameTag(entry, action.tag)));
872
+ const block = blocks[blockIndex + 1];
873
+ const item = findCharacter(tracker, action.tag);
874
+ if (block && item) {
875
+ item.init = ((_b = (_a = block[block.length - 1]) === null || _a === void 0 ? void 0 : _a.init) !== null && _b !== void 0 ? _b : 0) - 1;
876
+ }
877
+ return tracker;
878
+ }
879
+ case 'check':
880
+ case 'arrow':
881
+ case 'empty':
882
+ case 'cross': {
883
+ const item = findCharacter(tracker, action.tag);
884
+ if (item) {
885
+ item.state = action.command;
886
+ }
887
+ return tracker;
888
+ }
889
+ case 'delete':
890
+ return tracker.filter((entry) => !sameTag(entry, action.tag)).map((entry) => ({
891
+ ...entry,
892
+ children: entry.children.filter((child) => !sameTag(child, action.tag)),
893
+ }));
894
+ case 'purge':
895
+ return tracker.filter((entry) => !entry.foe);
896
+ case 'heal':
897
+ tracker.forEach((char) => {
898
+ char.hp = char.maxhp;
899
+ char.children.forEach((child) => {
900
+ child.hp = child.maxhp;
901
+ });
902
+ });
903
+ return tracker;
904
+ case 'reset':
905
+ tracker.forEach((char) => {
906
+ char.state = 'empty';
907
+ });
908
+ return tracker;
909
+ case 'set-thp': {
910
+ const item = findCharacterOrChild(tracker, action.tag);
911
+ if (item) {
912
+ item.temphp = Math.max(0, action.value);
913
+ }
914
+ return tracker;
915
+ }
916
+ case 'set-hp': {
917
+ const item = findCharacterOrChild(tracker, action.tag);
918
+ if (item) {
919
+ item.hp = Math.min(item.maxhp, Math.max(0, action.value));
920
+ }
921
+ return tracker;
922
+ }
923
+ case 'set-init': {
924
+ const item = findCharacter(tracker, action.tag);
925
+ if (item) {
926
+ item.init = Math.max(0, action.value);
927
+ }
928
+ return tracker;
929
+ }
930
+ case 'blank':
931
+ tracker.forEach((item) => {
932
+ item.conditions = '';
933
+ item.children.forEach((child) => {
934
+ child.conditions = '';
935
+ });
936
+ });
937
+ return tracker;
938
+ case 'set-conditions': {
939
+ const item = findCharacterOrChild(tracker, action.tag);
940
+ if (item) {
941
+ item.conditions = action.value.trim();
942
+ }
943
+ return tracker;
944
+ }
945
+ case 'add-conditions':
946
+ updateConditions(tracker, action.tag, (conditions) => [...conditions, action.value.toLowerCase().trim()]);
947
+ return tracker;
948
+ case 'rem-conditions':
949
+ updateConditions(tracker, action.tag, (conditions) => conditions.filter((condition) => !condition.includes(action.value.toLowerCase().trim())));
950
+ return tracker;
951
+ case 'inc-conditions':
952
+ updateConditions(tracker, action.tag, (conditions) => conditions.map((condition) => (numberedCondition(condition, action.value.toLowerCase().trim(), (label, value) => `${label} ${value + 1}`))));
953
+ return tracker;
954
+ case 'dec-conditions':
955
+ updateConditions(tracker, action.tag, (conditions) => conditions.map((condition) => (numberedCondition(condition, action.value.toLowerCase().trim(), (label, value) => (value > 1 ? `${label} ${value - 1}` : label)))));
956
+ return tracker;
957
+ case 'mod-thp': {
958
+ const item = findCharacterOrChild(tracker, action.tag);
959
+ if (item) {
960
+ item.temphp = Math.max(0, item.temphp + action.value);
961
+ }
962
+ return tracker;
963
+ }
964
+ case 'mod-hp': {
965
+ const item = findCharacterOrChild(tracker, action.tag);
966
+ if (item) {
967
+ let hp = item.hp;
968
+ let thp = item.temphp;
969
+ if (action.value < 0) {
970
+ thp += action.value;
971
+ hp += Math.min(thp, 0);
972
+ }
973
+ else {
974
+ hp += action.value;
975
+ }
976
+ item.temphp = Math.max(0, thp);
977
+ item.hp = Math.min(item.maxhp, Math.max(0, hp));
978
+ }
979
+ return tracker;
980
+ }
981
+ case 'mod-init': {
982
+ const item = findCharacter(tracker, action.tag);
983
+ if (item) {
984
+ item.init = Math.max(0, item.init + action.value);
985
+ }
986
+ return tracker;
987
+ }
988
+ case 'foe':
989
+ tracker.push(exports.charSchema.parse({ foe: true, name: action.name, alias: action.alias, maxhp: action.hp, hp: action.hp }));
990
+ return tracker;
991
+ default:
992
+ return unexpectedTrackerCommand(action);
993
+ }
994
+ };
995
+ const applyTrackerCommands = (tracker, actions) => {
996
+ let next = tracker;
997
+ for (const action of actions) {
998
+ if (action.command === 'exit') {
999
+ return { tracker: next, exit: true };
1000
+ }
1001
+ next = applyTrackerCommand(next, action);
1002
+ }
1003
+ return { tracker: next, exit: false };
1004
+ };
1005
+ exports.applyTrackerCommands = applyTrackerCommands;
696
1006
  const sortFolder = async () => {
697
1007
  const dir = await (0, promises_1.readdir)('.');
698
1008
  const summary = await Promise.all(dir.map(async (file) => {
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
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pf2e-sage-stats",
3
- "version": "0.3.1",
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",