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

Create Dynamic Dice #629

Merged
merged 4 commits into from
Sep 20, 2024
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ import { D20 } from 'randsum'
D20.roll()

// Make a new 120 sided die and roll it
import { dieFactory } from 'randsum'
import { D } from 'randsum'

const D120 = dieFactory(120)
const D120 = new D(120)
D120.roll()

//'heads' or 'tails'?
const Coin = dieFactory(['heads', 'tails'])
const Coin = new D(['heads', 'tails'])
Coin.roll()
```

Expand Down
41 changes: 41 additions & 0 deletions src/D.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isCustomSides } from '~guards'

type Faces<T> = T extends string[] ? T : number[]
type Result<T> = T extends number[] ? number : string

class D<Sides extends string[] | number> {
sides: number
faces: Faces<Sides>

constructor(sides: Sides) {
if (isCustomSides(sides)) {
this.sides = sides.length
this.faces = sides as Faces<Sides>
return
}
this.sides = sides
this.faces = Array.from(
{ length: Number(sides) },
(_, index) => index + 1
) as Faces<Sides>
}

roll(): Result<Faces<Sides>> {
return this.faces[this.rawRoll()] as Result<Faces<Sides>>
}

protected rawRoll(): number {
return Math.floor(Math.random() * Number(this.sides))
}
}

const D4 = new D(4)
const D6 = new D(6)
const D8 = new D(8)
const D10 = new D(10)
const D12 = new D(12)
const D20 = new D(20)
const D100 = new D(100)
const FudgeDice = new D(['+', '+', '+', '-', '0', '0'])

export { D, D4, D6, D8, D10, D12, D20, D100, FudgeDice }
14 changes: 0 additions & 14 deletions src/Dice/constants.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/Dice/die.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/Dice/factories.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/Dice/index.ts

This file was deleted.

19 changes: 1 addition & 18 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import {
capPattern,
coreNotationPattern,
dropConstraintsPattern,
dropHighestPattern,
dropLowestPattern,
explodePattern,
minusPattern,
plusPattern,
replacePattern,
rerollPattern,
uniquePattern
} from '~patterns'
import { completeRollPattern, coreNotationPattern } from '~patterns'
import {
RandsumNotation,
RandsumRollOptions,
Expand All @@ -19,11 +7,6 @@ import {
RandsumRollArgument
} from '~types'

const completeRollPattern = new RegExp(
`${coreNotationPattern.source}|${dropHighestPattern.source}|${dropLowestPattern.source}|${dropConstraintsPattern.source}|${explodePattern.source}|${uniquePattern.source}|${replacePattern.source}|${rerollPattern.source}|${capPattern.source}|${plusPattern.source}|${minusPattern.source}`,
'g'
)

export function isDiceNotation(argument: unknown): argument is RandsumNotation {
const notAString = typeof argument !== 'string'
const basicTest = !!coreNotationPattern.test(String(argument))
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './Dice'
export * from './D'
export { roll } from './roll'
export { validateDiceNotation } from './validateDiceNotation'
export { parameterizeRollArgument } from './parameterizeRollArgument'
Expand Down
4 changes: 2 additions & 2 deletions src/parameterizeRollArgument/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CoreRollArgument, RandsumRollParameters } from '~types'
import { dieFactory } from '~src/Dice'
import { D } from '~src/D'
import { formatDescription } from '~utils/formatDescription'
import { formatNotation } from '~utils/formatNotation'
import { parseDiceOptions } from './parseDiceOptions'
Expand All @@ -8,7 +8,7 @@ function parameterizeRollArgument(
argument: CoreRollArgument | undefined
): RandsumRollParameters {
const options = parseDiceOptions(argument)
const die = dieFactory(options.sides)
const die = new D(options.sides)
return {
options,
argument,
Expand Down
5 changes: 5 additions & 0 deletions src/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export const capPattern = new RegExp(
/[Cc]/.source + greaterThanLessThanMatcher.source,
'g'
)

export const completeRollPattern = new RegExp(
`${coreNotationPattern.source}|${dropHighestPattern.source}|${dropLowestPattern.source}|${dropConstraintsPattern.source}|${explodePattern.source}|${uniquePattern.source}|${replacePattern.source}|${rerollPattern.source}|${capPattern.source}|${plusPattern.source}|${minusPattern.source}`,
'g'
)
30 changes: 16 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Die } from './Dice/die'
import { D } from './D'

// Primitives
type DiceNotationWithNumericSides = `${number}${'d' | 'D'}${number}${string}`
type DiceNotationWithCustomSides = `${number}${'d' | 'D'}{${string}}`

export type RandsumNotation<D extends string | number = string | number> =
D extends number ? DiceNotationWithNumericSides : DiceNotationWithCustomSides
export type RandsumNotation<Sides extends string | number = string | number> =
Sides extends number
? DiceNotationWithNumericSides
: DiceNotationWithCustomSides

export enum DicePoolType {
standard = 'standard',
Expand All @@ -15,11 +17,11 @@ export enum DicePoolType {

// Options
export interface RandsumRollOptions<
D extends string | number = string | number
Sides extends string | number = string | number
> {
quantity?: number
sides: D extends number ? number : string[]
modifiers?: D extends number ? Modifiers : Record<string, never>
sides: Sides extends number ? number : string[]
modifiers?: Sides extends number ? Modifiers : Record<string, never>
}

export type Modifiers = {
Expand Down Expand Up @@ -57,15 +59,15 @@ export interface UniqueOptions {
notUnique: number[]
}

type CoreRollOptions<D extends string | number = string | number> = Omit<
RandsumRollOptions<D>,
type CoreRollOptions<Sides extends string | number = string | number> = Omit<
RandsumRollOptions<Sides>,
'modifiers'
>

export type RequiredCoreDiceParameters<
D extends string | number = string | number
Sides extends string | number = string | number
> = {
[Property in keyof CoreRollOptions<D>]-?: CoreRollOptions<D>[Property]
[Property in keyof CoreRollOptions<Sides>]-?: CoreRollOptions<Sides>[Property]
}

// Arguments
Expand All @@ -85,12 +87,12 @@ export type RandsumRollArgument =
// Parameters

export interface RandsumRollParameters<
D extends string | number = string | number
Sides extends string | number = string | number
> {
argument: RandsumRollArgument
options: RandsumRollOptions<D>
die: Die<D>
notation: RandsumNotation<D>
options: RandsumRollOptions<Sides>
die: D<Sides extends string ? string[] : number>
notation: RandsumNotation<Sides>
description: string[]
}
export interface DicePools {
Expand Down
38 changes: 38 additions & 0 deletions tests/D.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, test } from 'bun:test'
import { D } from '~src/D'

describe('D', () => {
describe('Creating a Numberical Die', () => {
const sides = 6
const die = new D(sides)

test('.sides returns the number given as sides', () => {
expect(die.sides).toEqual(sides)
})

test('.faces returns the number given as sides', () => {
expect(die.faces).toEqual([1, 2, 3, 4, 5, 6])
})

test('.roll() returns a number included in the constructor', () => {
expect([1, 2, 3, 4, 5, 6]).toContain(die.roll())
})
})

describe('Creating a Die with Custom Sides', () => {
const sides = ['+', '+', '-', '-']
const die = new D(sides)

test('.sides returns the number of sides given in the contructor', () => {
expect(die.sides).toEqual(sides.length)
})

test('.faces returns the sides given in the contructor', () => {
expect(die.faces).toEqual(sides)
})

test('.roll() returns a string included in the constructor', () => {
expect(sides).toContain(die.roll())
})
})
})
92 changes: 0 additions & 92 deletions tests/Die.test.ts

This file was deleted.

Loading
Loading