Skip to content

Commit

Permalink
Add Coin support in _simplifyDiceTerms (#2493)
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Mantas <kim.mantas@gmail.com>
Closes #2281
  • Loading branch information
Hoppyhob committed Oct 19, 2023
1 parent 9596c7c commit b75c657
Showing 1 changed file with 5 additions and 2 deletions.
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

0 comments on commit b75c657

Please sign in to comment.