Skip to content

Commit

Permalink
Fix min/max setting. (#306)
Browse files Browse the repository at this point in the history
* #304 fix

* Revert arbitary range, add defined range with '-' seperator.

* remove unused import I accidently brought in

* fix: Remove Array prototype pollution

* fix: Fixes lexing of new dice range syntax

---------

Co-authored-by: Jeremy Valentine <38669521+valentine195@users.noreply.github.com>
  • Loading branch information
AustinYQM and valentine195 committed Apr 15, 2024
1 parent e46afb2 commit fc7ee80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/parser/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export const LINE_REGEX =
/(?:\d+[Dd])?(?:\[.*\]\(|\[\[)(?:.+)(?:\)|\]\])\|line/u;
export const MATH_REGEX = /[\(\^\+\-\*\/\)]/u;
export const OMITTED_REGEX =
/(?:\d+|\b)[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*,[ \t]*\d+)+\]|\b)/u;
/(?:\d+|\b)[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*[,-][ \t]*\d+)+\]|\b)/u;

export const CONDITIONAL_REGEX =
/(?:=|=!|<|>|<=|>=|=<|=>|-=|=-)(?:\d+(?:[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*,[ \t]*\d+)+\]|\b))?)/u;
/(?:=|=!|<|>|<=|>=|=<|=>|-=|=-)(?:\d+(?:[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*[,-][ \t]*\d+)+\]|\b))?)/u;

export interface LexicalToken extends Partial<moo.Token> {
conditions?: Conditional[];
Expand Down Expand Up @@ -71,8 +71,9 @@ export default class Lexer {
roll = this.defaultRoll,
faces = this.defaultFace
} = match.match(
/(?<roll>\d+)?[Dd](?<faces>%|F|-?\d+|\[\d+(?:[ \t]*,[ \t]*\d+)+\])?/
/(?<roll>\d+)?[Dd](?<faces>%|F|-?\d+|\[\d+(?:[ \t]*[,-][ \t]*\d+)+\])?/
).groups;
console.log("🚀 ~ file: lexer.ts:73 ~ faces:", faces);
return `${roll}d${faces}`;
}
},
Expand Down Expand Up @@ -180,7 +181,7 @@ export default class Lexer {
if (!previous.conditions) previous.conditions = [];
const [_, operator, comparer] =
token.value.match(
/(?<operator>=|=!|<|>|<=|>=|=<|=>|-=|=-)(?<comparer>\d+(?:[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*,[ \t]*\d+)+\]|\b))?)/
/(?<operator>=|=!|<|>|<=|>=|=<|=>|-=|=-)(?<comparer>\d+(?:[Dd](?:%|F|-?\d+|\[\d+(?:[ \t]*[,-][ \t]*\d+)+\]|\b))?)/
) ?? [];
const lexemes = this.parse(comparer);
previous.conditions.push({
Expand Down
13 changes: 12 additions & 1 deletion src/roller/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ export class DiceRoller {
this.modifiersAllowed = false;
}
let [, rolls, maxStr = "1"] = this.dice.match(
/(\-?\d+)[dD](%|F|-?\d+|\[\d+(?:[ \t]*,[ \t]*\d+)+\])/
/(\-?\d+)[dD](%|F|-?\d+|\[\d+(?:[ \t]*[,-][ \t]*\d+)+\])/
) || [, 1, "1"];

rolls = Number(rolls);

this.multiplier = rolls < 0 ? -1 : 1;
let min = 1;
let max = isNaN(Number(maxStr)) ? 1 : Number(maxStr);

this.rolls = Math.abs(Number(rolls)) || 1;

//ugly af
Expand All @@ -57,6 +58,15 @@ export class DiceRoller {
.replace(/[\[\]\s]/g, "")
.split(",")
.map((v) => Number(v));
} else if (/\[\d+(?:[ \t]*-[ \t]*\d+)+\]/.test(maxStr)) {
[min, max] = maxStr
.replace(/[\[\]\s]/g, "")
.split("-")
.map((v) => Number(v));
this.possibilities = Array.from(
{ length: max - min },
(_, i) => i + min
);
} else if (maxStr === "F") {
this.possibilities = [-1, 0, 1];
this.fudge = true;
Expand Down Expand Up @@ -1359,6 +1369,7 @@ export class StackRoller extends GenericRoller<number> {
dice
);
}

index++;
break;
}
Expand Down

0 comments on commit fc7ee80

Please sign in to comment.