1
1
mirror of https://github.com/Burn-E99/TheArtificer.git synced 2026-06-04 09:03:50 -04:00

move getModifiers into artigen, simplify its error handling a lot

This commit is contained in:
Ean Milligan
2025-05-03 09:54:25 -04:00
parent 9bd757741b
commit 829ec0ecea
5 changed files with 28 additions and 51 deletions

View File

@@ -44,8 +44,9 @@ export interface RollModifiers {
order: string;
count: boolean;
commaTotals: boolean;
valid: boolean;
apiWarn: string;
valid: boolean;
error: Error;
}
// Basic conf interfaces

View File

@@ -0,0 +1,104 @@
import { log, LogTypes as LT } from '@Log4Deno';
import { RollModifiers } from 'artigen/dice/dice.d.ts';
export const getModifiers = (args: string[]): RollModifiers => {
const modifiers: RollModifiers = {
noDetails: false,
superNoDetails: false,
spoiler: '',
maxRoll: false,
minRoll: false,
nominalRoll: false,
gmRoll: false,
gms: [],
order: '',
count: false,
commaTotals: false,
apiWarn: '',
valid: false,
error: new Error(),
};
// Check if any of the args are command flags and pull those out into the modifiers object
for (let i = 0; i < args.length; i++) {
log(LT.LOG, `Checking ${args.join(' ')} for command modifiers ${i}`);
let defaultCase = false;
switch (args[i].toLowerCase()) {
case '-c':
modifiers.count = true;
break;
case '-nd':
modifiers.noDetails = true;
break;
case '-snd':
modifiers.superNoDetails = true;
break;
case '-s':
modifiers.spoiler = '||';
break;
case '-max':
case '-m':
modifiers.maxRoll = true;
break;
case '-min':
modifiers.minRoll = true;
break;
case '-n':
modifiers.nominalRoll = true;
break;
case '-gm':
modifiers.gmRoll = true;
// -gm is a little more complex, as we must get all of the GMs that need to be DMd
while (i + 1 < args.length && args[i + 1].startsWith('<@')) {
log(LT.LOG, `Finding all GMs, checking args ${JSON.stringify(args)}`);
// Keep looping thru the rest of the args until one does not start with the discord mention code
modifiers.gms.push(args[i + 1].replace(/!/g, ''));
args.splice(i + 1, 1);
}
if (modifiers.gms.length < 1) {
// If -gm is on and none were found, throw an error
modifiers.error.name = 'NoGMsFound';
modifiers.error.message = 'Must specify at least one GM by @mentioning them';
return modifiers;
}
break;
case '-o':
// Shift the -o out of the array so the next item is the direction
args.splice(i, 1);
if (!args[i] || (args[i].toLowerCase()[0] !== 'd' && args[i].toLowerCase()[0] !== 'a')) {
// If -o is on and asc or desc was not specified, error out
modifiers.error.name = 'NoOrderFound';
modifiers.error.message = 'Must specify `a` or `d` to order the rolls ascending or descending';
return modifiers;
}
modifiers.order = args[i].toLowerCase()[0];
break;
case '-ct':
modifiers.commaTotals = true;
break;
default:
// Default case should not mess with the array
defaultCase = true;
break;
}
if (!defaultCase) {
args.splice(i, 1);
i--;
}
}
// maxRoll, minRoll, and nominalRoll cannot be on at same time, throw an error
if ([modifiers.maxRoll, modifiers.minRoll, modifiers.nominalRoll].filter((b) => b).length > 1) {
modifiers.error.name = 'MaxAndNominal';
modifiers.error.message = 'Can only use one of the following at a time:\n`maximize`, `minimize`, `nominal`';
return modifiers;
}
modifiers.valid = true;
return modifiers;
};