-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for tables with dice rolls
- Loading branch information
Showing
7 changed files
with
176 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export class Die { | ||
private static EMPTY_STR_TO_UNDEFINED = (str: string) => | ||
str === '' ? undefined : str | ||
|
||
private notation: string | ||
readonly number: number | ||
readonly dice: number | ||
readonly modifier: number | ||
|
||
constructor(notation: string) { | ||
this.notation = notation | ||
|
||
const diceNotation: RegExp = /^(\d*)d(\d+)(\s*(\+|-)\s*(\d+))?$/g | ||
|
||
const [, number = '1', dice = '1', , plusMinus = '+', modifier = '0'] = | ||
diceNotation.exec(this.notation)!.map(Die.EMPTY_STR_TO_UNDEFINED) | ||
|
||
this.number = parseInt(number) | ||
this.dice = parseInt(dice) | ||
this.modifier = parseInt(plusMinus + modifier) | ||
} | ||
|
||
roll(): number { | ||
const rolls = Array.from({ length: this.number }, () => | ||
Math.floor(Math.random() * this.dice + 1), | ||
) | ||
return rolls.reduce((a, b) => a + b, 0) + this.modifier | ||
} | ||
|
||
toString(): string { | ||
return this.notation | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type Range = number[] | ||
|
||
const EMPTY_STR_TO_UNDEFINED = (str: string) => (str === '' ? undefined : str) | ||
|
||
export function parseRange(notation: string): Range | undefined { | ||
const rangeNotation: RegExp = /^(\d*)(\W?-(\W?\d*))?$/g | ||
const [, start, , end] = rangeNotation | ||
.exec(notation)! | ||
.map(EMPTY_STR_TO_UNDEFINED) | ||
|
||
if (start && !end) { | ||
return [parseInt(start)] | ||
} else if (start && end) { | ||
const size = parseInt(end) - parseInt(start) + 1 | ||
return [...Array(size).keys()].map((i) => i + parseInt(start)) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters