-
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.
- Loading branch information
Showing
22 changed files
with
489 additions
and
471 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export * from './D' | ||
export { roll } from './roll' | ||
export { validateDiceNotation } from './validateDiceNotation' | ||
export { parameterizeRollArgument } from './parameterizeRollArgument' | ||
export * from './models' | ||
export * from './types' |
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,96 @@ | ||
import { | ||
RandsumRollArgument, | ||
DicePools, | ||
RandsumRollOptions, | ||
RandsumRollParameters | ||
} from '~types' | ||
import { uuidv7 as uuid } from 'uuidv7' | ||
import { | ||
isD, | ||
isCustomSidesD, | ||
isDicePoolOptions, | ||
isDiceNotationArg | ||
} from '~guards' | ||
import { NotationModel, OptionsModel } from '~models' | ||
import { D } from '~src/D' | ||
|
||
function formDicePools(args: RandsumRollArgument[]): DicePools { | ||
return { | ||
dicePools: args.reduce( | ||
(acc, arg) => ({ ...acc, [uuid()]: parameterize(arg) }), | ||
{} | ||
) | ||
} | ||
} | ||
|
||
function normalizeArgument(argument: RandsumRollArgument): RandsumRollOptions { | ||
if (isD(argument)) { | ||
return { | ||
quantity: 1, | ||
sides: isCustomSidesD(argument) ? argument.faces : argument.sides | ||
} | ||
} | ||
|
||
if (isDicePoolOptions(argument)) { | ||
return argument | ||
} | ||
|
||
if (isDiceNotationArg(argument)) { | ||
return NotationModel.toOptions(argument) | ||
} | ||
|
||
if (Array.isArray(argument)) { | ||
return { | ||
quantity: 1, | ||
sides: argument.map(String) | ||
} | ||
} | ||
|
||
return { | ||
quantity: 1, | ||
sides: Number(argument || 20) | ||
} | ||
} | ||
|
||
function parameterize(argument: RandsumRollArgument): RandsumRollParameters { | ||
const options = normalizeArgument(argument) | ||
const die = new D(options.sides) | ||
return { | ||
options, | ||
argument, | ||
die, | ||
notation: OptionsModel.toNotation(options), | ||
description: OptionsModel.toDescription(options) | ||
} | ||
} | ||
|
||
function normalize(argument: RandsumRollArgument): RandsumRollOptions { | ||
if (isD(argument)) { | ||
return { | ||
quantity: 1, | ||
sides: isCustomSidesD(argument) ? argument.faces : argument.sides | ||
} | ||
} | ||
|
||
if (isDicePoolOptions(argument)) { | ||
return argument | ||
} | ||
|
||
if (isDiceNotationArg(argument)) { | ||
return NotationModel.toOptions(argument) | ||
} | ||
|
||
if (Array.isArray(argument)) { | ||
return { | ||
quantity: 1, | ||
sides: argument.map(String) | ||
} | ||
} | ||
|
||
return { | ||
quantity: 1, | ||
sides: Number(argument || 20) | ||
} | ||
} | ||
|
||
export default { formDicePools, normalize, parameterize } |
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
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,117 @@ | ||
import { isCustomParameters } from '~guards' | ||
import { RandsumRollParameters } from '~types' | ||
import { | ||
applyReroll, | ||
applyUnique, | ||
applyReplace, | ||
applyDrop, | ||
applyExplode, | ||
applySingleCap | ||
} from './modifierApplicators' | ||
|
||
type RollBonuses = { | ||
rolls: number[] | ||
simpleMathModifier: number | ||
} | ||
|
||
type ModifiedRollBonuses = { | ||
rolls: string[] | ||
simpleMathModifier: 0 | ||
} | ||
function applyModifiers( | ||
poolParameters: RandsumRollParameters<string> | RandsumRollParameters<number>, | ||
initialRolls: number[] | string[] | ||
): RollBonuses | ModifiedRollBonuses { | ||
if (isCustomParameters(poolParameters)) { | ||
return { | ||
simpleMathModifier: 0, | ||
rolls: initialRolls as string[] | ||
} | ||
} | ||
|
||
const rollBonuses: RollBonuses = { | ||
simpleMathModifier: 0, | ||
rolls: initialRolls as number[] | ||
} | ||
|
||
const { | ||
options: { sides, quantity, modifiers = {} } | ||
} = poolParameters | ||
|
||
const rollOne: () => number = () => poolParameters.die.roll() | ||
|
||
return Object.keys(modifiers).reduce((bonuses, key) => { | ||
switch (key) { | ||
case 'reroll': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.reroll | ||
? applyReroll(bonuses.rolls, modifiers.reroll, rollOne) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'unique': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.unique | ||
? applyUnique( | ||
bonuses.rolls, | ||
{ sides, quantity: quantity || 1, unique: modifiers.unique }, | ||
rollOne | ||
) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'replace': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.replace | ||
? applyReplace(bonuses.rolls, modifiers.replace) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'cap': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.cap | ||
? bonuses.rolls.map(applySingleCap(modifiers.cap)) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'drop': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.drop | ||
? applyDrop(bonuses.rolls, modifiers.drop) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'explode': | ||
return { | ||
...bonuses, | ||
rolls: modifiers.explode | ||
? applyExplode(bonuses.rolls, { sides }, rollOne) | ||
: bonuses.rolls | ||
} | ||
|
||
case 'plus': | ||
return { | ||
...bonuses, | ||
simpleMathModifier: | ||
bonuses.simpleMathModifier + Number(modifiers.plus) | ||
} | ||
|
||
case 'minus': | ||
return { | ||
...bonuses, | ||
simpleMathModifier: | ||
bonuses.simpleMathModifier - Number(modifiers.minus) | ||
} | ||
|
||
default: | ||
throw new Error(`Unknown modifier: ${key}`) | ||
} | ||
}, rollBonuses) | ||
} | ||
|
||
export default { applyModifiers } |
Oops, something went wrong.