Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Coin support in _simplifyDiceTerms #2493

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions module/dice/simplify-roll-formula.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,18 @@ function _simplifyDiceTerms(terms) {
// Split the unannotated terms into different die sizes and signs
const diceQuantities = unannotated.reduce((obj, curr, i) => {
if ( curr instanceof OperatorTerm ) return obj;
const key = `${unannotated[i - 1].operator}${curr.faces}`;
const face = curr.constructor?.name === "Coin" ? "c" : curr.faces;
const key = `${unannotated[i - 1].operator}${face}`;
obj[key] = (obj[key] ?? 0) + curr.number;
return obj;
}, {});

// Add new die and operator terms to simplified for each die size and sign
const simplified = Object.entries(diceQuantities).flatMap(([key, number]) => ([
new OperatorTerm({ operator: key.charAt(0) }),
new Die({ number, faces: parseInt(key.slice(1)) })
key.slice(1) === "c"
? new Coin({ number: number })
: new Die({ number, faces: parseInt(key.slice(1)) })
]));
return [...simplified, ...annotated];
}
Expand Down