Added more loop protection, started on exploding once/on range options

This commit is contained in:
Ean Milligan (Bastion) 2022-05-17 01:41:53 -04:00
parent febd735c05
commit 26b9309aa3
2 changed files with 22 additions and 6 deletions

View File

@ -221,8 +221,7 @@ export const constantCmds = {
The Artificer supports most of the [Roll20 formatting](https://artificer.eanm.dev/roll20). More details and examples can be found [here](https://artificer.eanm.dev/roll20).` The Artificer supports most of the [Roll20 formatting](https://artificer.eanm.dev/roll20). More details and examples can be found [here](https://artificer.eanm.dev/roll20).`
}, { }, {
name: `\`${config.prefix}xdydzracsq!${config.postfix}\` ...`, name: `\`${config.prefix}xdydzracsq!${config.postfix}\` ...`,
value: `Rolls all configs requested, you may repeat the command multiple times in the same message (just ensure you close each roll with \`${config.postfix}\`)`, value: `Rolls all configs requested, you may repeat the command multiple times in the same message (just ensure you close each roll with \`${config.postfix}\`)`
inline: true
}, { }, {
name: "`x` [Optional]", name: "`x` [Optional]",
value: "Number of dice to roll, if omitted, 1 is used", value: "Number of dice to roll, if omitted, 1 is used",

View File

@ -54,7 +54,11 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
on: false, on: false,
range: <number[]>[] range: <number[]>[]
}, },
exploding: false exploding: {
on: false,
once: false,
nums: <number[]>[]
}
}; };
// If the dpts is not long enough, throw error // If the dpts is not long enough, throw error
@ -209,7 +213,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
break; break;
case "!": case "!":
// Configure Exploding // Configure Exploding
rollConf.exploding = true; rollConf.exploding.on = true;
afterNumIdx = 1; afterNumIdx = 1;
break; break;
default: default:
@ -328,7 +332,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
} }
// If needed, handle rerolling and exploding dice now // If needed, handle rerolling and exploding dice now
if (rollConf.reroll.on || rollConf.exploding) { if (rollConf.reroll.on || rollConf.exploding.on) {
for (let i = 0; i < rollSet.length; i++) { for (let i = 0; i < rollSet.length; i++) {
log(LT.LOG, `Handling roll ${rollStr} | Handling rerolling and exploding ${JSON.stringify(rollSet[i])}`); log(LT.LOG, `Handling roll ${rollStr} | Handling rerolling and exploding ${JSON.stringify(rollSet[i])}`);
// If loopCount gets too high, stop trying to calculate infinity // If loopCount gets too high, stop trying to calculate infinity
@ -361,7 +365,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
// Slot this new roll in after the current iteration so it can be processed in the next loop // Slot this new roll in after the current iteration so it can be processed in the next loop
rollSet.splice(i + 1, 0, newRoll); rollSet.splice(i + 1, 0, newRoll);
} else if (rollConf.exploding && !rollSet[i].rerolled && rollSet[i].critHit) { } else if (rollConf.exploding.on && !rollSet[i].rerolled && rollSet[i].critHit) {
//If it exploded, we keep both, so no flags need to be set //If it exploded, we keep both, so no flags need to be set
// Copy the template to fill out for this iteration // Copy the template to fill out for this iteration
@ -398,12 +402,19 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
let rerollCount = 0; let rerollCount = 0;
if (rollConf.reroll.on) { if (rollConf.reroll.on) {
for (let i = 0; i < rollSet.length; i++) { for (let i = 0; i < rollSet.length; i++) {
// If loopCount gets too high, stop trying to calculate infinity
if (loopCount > MAXLOOPS) {
throw new Error("MaxLoopsExceeded");
}
log(LT.LOG, `Handling roll ${rollStr} | Setting originalIdx on ${JSON.stringify(rollSet[i])}`); log(LT.LOG, `Handling roll ${rollStr} | Setting originalIdx on ${JSON.stringify(rollSet[i])}`);
rollSet[i].origidx = i; rollSet[i].origidx = i;
if (rollSet[i].rerolled) { if (rollSet[i].rerolled) {
rerollCount++; rerollCount++;
} }
loopCount++;
} }
} }
@ -447,6 +458,11 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
// Now its time to drop all dice needed // Now its time to drop all dice needed
let i = 0; let i = 0;
while (dropCount > 0 && i < rollSet.length) { while (dropCount > 0 && i < rollSet.length) {
// If loopCount gets too high, stop trying to calculate infinity
if (loopCount > MAXLOOPS) {
throw new Error("MaxLoopsExceeded");
}
log(LT.LOG, `Handling roll ${rollStr} | Dropping dice ${dropCount} ${JSON.stringify(rollSet[i])}`); log(LT.LOG, `Handling roll ${rollStr} | Dropping dice ${dropCount} ${JSON.stringify(rollSet[i])}`);
// Skip all rolls that were rerolled // Skip all rolls that were rerolled
if (!rollSet[i].rerolled) { if (!rollSet[i].rerolled) {
@ -454,6 +470,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
dropCount--; dropCount--;
} }
i++; i++;
loopCount++;
} }
// Finally, return the rollSet to its original order // Finally, return the rollSet to its original order