pf2e-sage-stats 0.2.2 → 0.2.4

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.
Files changed (3) hide show
  1. package/dist/index.js +42 -11
  2. package/package.json +1 -1
  3. package/src/index.ts +49 -12
package/dist/index.js CHANGED
@@ -222,19 +222,50 @@ program.command('track')
222
222
  });
223
223
  while (true) {
224
224
  yield new Promise((resolve) => setTimeout(resolve, 500));
225
- const input = yield new Promise((resolve) => (rl.question('>', resolve)));
226
- const comm = input.match(/(\S+)\s*([+-]\d+)/);
227
- if (!comm) {
228
- continue;
225
+ const input = yield new Promise((resolve) => (rl.question('> ', resolve)));
226
+ const hpmod = input.match(/([^\d\s+-]+)\s*([+-]?\d+)/);
227
+ if (hpmod) {
228
+ const file = yield promises_1.default.readFile(argument, { encoding: 'utf-8' });
229
+ const tracker = (0, app_1.parseTracker)(file);
230
+ const index = tracker.findIndex((c) => c.alias === hpmod[1]);
231
+ if (index >= 0) {
232
+ let hp = tracker[index].hp;
233
+ let thp = tracker[index].temphp;
234
+ if (hpmod[2].match(/^[+-]/)) {
235
+ const delta = parseInt(hpmod[2]);
236
+ if (delta < 0) {
237
+ thp += delta;
238
+ hp += Math.min(thp, 0);
239
+ }
240
+ else {
241
+ hp += delta;
242
+ }
243
+ }
244
+ else {
245
+ hp = parseInt(hpmod[2]);
246
+ }
247
+ tracker[index].temphp = Math.max(0, thp);
248
+ tracker[index].hp = Math.min(tracker[index].maxhp, Math.max(0, hp));
249
+ yield promises_1.default.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' });
250
+ }
229
251
  }
230
- const file = yield promises_1.default.readFile(argument, { encoding: 'utf-8' });
231
- const tracker = (0, app_1.parseTracker)(file);
232
- const index = tracker.findIndex((c) => c.alias === comm[1]);
233
- if (index < 0) {
234
- continue;
252
+ const thpmod = input.match(/(\S+)\s+[tT]\s*([+-]?\d+)/);
253
+ if (thpmod) {
254
+ const file = yield promises_1.default.readFile(argument, { encoding: 'utf-8' });
255
+ const tracker = (0, app_1.parseTracker)(file);
256
+ const index = tracker.findIndex((c) => c.alias === thpmod[1]);
257
+ if (index >= 0) {
258
+ let thp = tracker[index].temphp;
259
+ if (thpmod[2].match(/^[+-]/)) {
260
+ thp += parseInt(thpmod[2]);
261
+ }
262
+ else {
263
+ thp = parseInt(thpmod[2]);
264
+ }
265
+ tracker[index].temphp = Math.max(0, thp);
266
+ yield promises_1.default.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' });
267
+ }
235
268
  }
236
- tracker[index].hp += parseInt(comm[2]);
237
- yield promises_1.default.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' });
238
269
  }
239
270
  }))(),
240
271
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pf2e-sage-stats",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "An RPG Sage's .tsv stat generation tool",
5
5
  "main": "dist/index.js",
6
6
  "author": "ikariott@gmail.com",
package/src/index.ts CHANGED
@@ -215,28 +215,65 @@ program.command('track')
215
215
  await new Promise((resolve) => setTimeout(resolve, 500));
216
216
 
217
217
  const input = await new Promise<string>((resolve) => (
218
- rl.question('>', resolve)
218
+ rl.question('> ', resolve)
219
219
  ));
220
220
 
221
- const comm = input.match(/(\S+)\s*([+-]\d+)/);
221
+ const hpmod = input.match(/([^\d\s+-]+)\s*([+-]?\d+)/);
222
222
 
223
- if (!comm) {
224
- continue;
225
- }
223
+ if (hpmod) {
224
+ const file = await fs.readFile(argument, { encoding: 'utf-8' });
225
+
226
+ const tracker = parseTracker(file);
227
+
228
+ const index = tracker.findIndex((c) => c.alias === hpmod[1])
226
229
 
227
- const file = await fs.readFile(argument, { encoding: 'utf-8' });
230
+ if (index >= 0) {
231
+ let hp = tracker[index].hp;
232
+ let thp = tracker[index].temphp;
228
233
 
229
- const tracker = parseTracker(file);
234
+ if (hpmod[2].match(/^[+-]/)) {
235
+ const delta = parseInt(hpmod[2])
230
236
 
231
- const index = tracker.findIndex((c) => c.alias === comm[1])
237
+ if (delta < 0) {
238
+ thp += delta;
239
+ hp += Math.min(thp, 0)
240
+ } else {
241
+ hp += delta;
242
+ }
243
+ } else {
244
+ hp = parseInt(hpmod[2]);
245
+ }
232
246
 
233
- if (index < 0) {
234
- continue;
247
+ tracker[index].temphp = Math.max(0, thp)
248
+ tracker[index].hp = Math.min(tracker[index].maxhp, Math.max(0, hp))
249
+
250
+ await fs.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' })
251
+ }
235
252
  }
236
253
 
237
- tracker[index].hp += parseInt(comm[2]);
254
+ const thpmod = input.match(/(\S+)\s+[tT]\s*([+-]?\d+)/);
255
+
256
+ if (thpmod) {
257
+ const file = await fs.readFile(argument, { encoding: 'utf-8' });
258
+
259
+ const tracker = parseTracker(file);
238
260
 
239
- await fs.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' })
261
+ const index = tracker.findIndex((c) => c.alias === thpmod[1])
262
+
263
+ if (index >= 0) {
264
+ let thp = tracker[index].temphp;
265
+
266
+ if (thpmod[2].match(/^[+-]/)) {
267
+ thp += parseInt(thpmod[2]);
268
+ } else {
269
+ thp = parseInt(thpmod[2]);
270
+ }
271
+
272
+ tracker[index].temphp = Math.max(0, thp)
273
+
274
+ await fs.writeFile(argument, JSON.stringify(tracker, undefined, 2), { encoding: 'utf-8' })
275
+ }
276
+ }
240
277
  }
241
278
  })(),
242
279
  ])