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

Die operation description #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions src/diceRoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ export class DiceRoller {
while (i < rolls.length && dropped < toDrop) {
if (rolls[i].valid) {
rolls[i].valid = false;
rolls[i].drop = true
dropped++;
}

Expand All @@ -539,6 +540,7 @@ export class DiceRoller {
while (i < rolls.length && dropped < toDrop) {
if (rolls[i].valid) {
rolls[i].valid = false;
rolls[i].drop = true
dropped++;
}

Expand Down Expand Up @@ -573,6 +575,7 @@ export class DiceRoller {
let explodeCount = 0;

while (targetMethod(roll) && explodeCount++ < 1000) {
roll.explode = true
const newRoll = this.reRoll(roll, ++i);
rolls.splice(i, 0, newRoll);
roll = newRoll;
Expand Down Expand Up @@ -607,13 +610,14 @@ export class DiceRoller {
let explodeCount = 0;

while (targetMethod(roll) && explodeCount++ < 1000) {
const newRoll = this.reRoll(rolls[i], ++i);
roll.explode = true
const newRoll = this.reRoll(roll,i+1);
rollValue += newRoll.roll;
roll = newRoll;
}

roll.value = rollValue;
roll.roll = rollValue;
rolls[i].value = rollValue;
rolls[i].roll = rollValue;
}

return rolls;
Expand Down Expand Up @@ -644,6 +648,7 @@ export class DiceRoller {
let explodeCount = 0;

while (targetMethod(roll) && explodeCount++ < 1000) {
roll.explode = true
const newRoll = this.reRoll(roll, ++i);
newRoll.value -= 1;
newRoll.roll -= 1;
Expand All @@ -668,6 +673,7 @@ export class DiceRoller {

for (let i = 0; i < rolls.length; i++) {
while (targetMethod(rolls[i].roll)) {
rolls[i].reroll = true
rolls[i].valid = false;
const newRoll = this.reRoll(rolls[i], i + 1);
rolls.splice(++i, 0, newRoll);
Expand All @@ -690,6 +696,7 @@ export class DiceRoller {

for (let i = 0; i < rolls.length; i++) {
if (targetMethod(rolls[i].roll)) {
rolls[i].reroll = true
rolls[i].valid = false;
const newRoll = this.reRoll(rolls[i], i + 1);
rolls.splice(++i, 0, newRoll);
Expand Down
6 changes: 6 additions & 0 deletions src/rollTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface RollBase {
label?: string;
/** A property used to maintain ordering of dice rolls within groups */
order: number;
/** Has this die been dropped */
drop?: boolean;
/** Has this die exploded */
explode?: boolean;
/** Has this die been rerolled */
reroll?: boolean;
}

/** An intermediate interface extended for groups of dice */
Expand Down
25 changes: 25 additions & 0 deletions test/rollerTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,29 @@ testRolls.forEach(([roll, expectedValue]) => {
test(roll, () => {
expect(roller.rollValue(roll)).toBe(expectedValue)
});
});

const testFixedRolls: [string, number, number[]][] = [
['1d6!!', 14, [.84, .84, .17]], // value = [6,6,2]
['4d6!!', 24, [.84, .67, .5, .17, .84, 0]], // value = [6,5,4,2,6,1]
['4d6dl1', 15, [.84, .67, .5, .17]] // value = [6,5,4,2]
]

let externalCount: number = 0
let rollsAsFloats: Array<number> = []
const fixedRoller = new dist.DiceRoller((rolls: Array<number> = rollsAsFloats)=>{
if(rolls.length > 0) {
return rolls[externalCount++]
} else {
console.warn("No results passed to the dice-roller-parser. Using fallback Math.random")
return Math.random()
}
})

testFixedRolls.forEach(([roll, expectedValue, values]) => {
test(roll, () => {
externalCount = 0
rollsAsFloats = values
expect(fixedRoller.rollValue(roll)).toBe(expectedValue)
});
});